001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.NoSuchUserException;
018    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.service.UserLocalServiceUtil;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.jsp.JspException;
025    import javax.servlet.jsp.tagext.TagSupport;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class UserDisplayTag extends TagSupport {
031    
032            @Override
033            public int doEndTag() throws JspException {
034                    try {
035                            PortalIncludeUtil.include(pageContext, getEndPage());
036    
037                            return EVAL_PAGE;
038                    }
039                    catch (Exception e) {
040                            throw new JspException(e);
041                    }
042            }
043    
044            @Override
045            public int doStartTag() throws JspException {
046                    try {
047                            HttpServletRequest request =
048                                    (HttpServletRequest)pageContext.getRequest();
049    
050                            request.setAttribute(
051                                    "liferay-ui:user-display:user-id", String.valueOf(_userId));
052                            request.setAttribute(
053                                    "liferay-ui:user-display:user-name", _userName);
054    
055                            User user = null;
056    
057                            try {
058                                    user = UserLocalServiceUtil.getUserById(_userId);
059    
060                                    if (user.isDefaultUser()) {
061                                            user = null;
062                                    }
063    
064                                    request.setAttribute("liferay-ui:user-display:user", user);
065    
066                                    pageContext.setAttribute("userDisplay", user);
067                            }
068                            catch (NoSuchUserException nsue) {
069                                    request.removeAttribute("liferay-ui:user-display:user");
070    
071                                    pageContext.removeAttribute("userDisplay");
072                            }
073    
074                            request.setAttribute("liferay-ui:user-display:url", _url);
075                            request.setAttribute(
076                                    "liferay-ui:user-display:displayStyle",
077                                    String.valueOf(_displayStyle));
078    
079                            PortalIncludeUtil.include(pageContext, getStartPage());
080    
081                            if (user != null) {
082                                    return EVAL_BODY_INCLUDE;
083                            }
084                            else {
085                                    return SKIP_BODY;
086                            }
087                    }
088                    catch (Exception e) {
089                            throw new JspException(e);
090                    }
091            }
092    
093            public void setDisplayStyle(int displayStyle) {
094                    _displayStyle = displayStyle;
095            }
096    
097            public void setEndPage(String endPage) {
098                    _endPage = endPage;
099            }
100    
101            public void setStartPage(String startPage) {
102                    _startPage = startPage;
103            }
104    
105            public void setUrl(String url) {
106                    _url = url;
107            }
108    
109            public void setUserId(long userId) {
110                    _userId = userId;
111            }
112    
113            public void setUserName(String userName) {
114                    _userName = userName;
115            }
116    
117            protected String getEndPage() {
118                    if (Validator.isNull(_endPage)) {
119                            return _END_PAGE;
120                    }
121                    else {
122                            return _endPage;
123                    }
124            }
125    
126            protected String getStartPage() {
127                    if (Validator.isNull(_startPage)) {
128                            return _START_PAGE;
129                    }
130                    else {
131                            return _startPage;
132                    }
133            }
134    
135            private static final String _END_PAGE =
136                    "/html/taglib/ui/user_display/end.jsp";
137    
138            private static final String _START_PAGE =
139                    "/html/taglib/ui/user_display/start.jsp";
140    
141            private int _displayStyle = 1;
142            private String _endPage;
143            private String _startPage;
144            private String _url;
145            private long _userId;
146            private String _userName;
147    
148    }