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                            HttpServletRequest request =
038                                    (HttpServletRequest)pageContext.getRequest();
039    
040                            request.removeAttribute("liferay-ui:user-display:url");
041    
042                            return EVAL_PAGE;
043                    }
044                    catch (Exception e) {
045                            throw new JspException(e);
046                    }
047            }
048    
049            @Override
050            public int doStartTag() throws JspException {
051                    try {
052                            HttpServletRequest request =
053                                    (HttpServletRequest)pageContext.getRequest();
054    
055                            request.setAttribute(
056                                    "liferay-ui:user-display:user-id", String.valueOf(_userId));
057                            request.setAttribute(
058                                    "liferay-ui:user-display:user-name", _userName);
059    
060                            User user = null;
061    
062                            try {
063                                    user = UserLocalServiceUtil.getUserById(_userId);
064    
065                                    if (user.isDefaultUser()) {
066                                            user = null;
067                                    }
068    
069                                    request.setAttribute("liferay-ui:user-display:user", user);
070    
071                                    pageContext.setAttribute("userDisplay", user);
072                            }
073                            catch (NoSuchUserException nsue) {
074                                    request.removeAttribute("liferay-ui:user-display:user");
075    
076                                    pageContext.removeAttribute("userDisplay");
077                            }
078    
079                            request.setAttribute("liferay-ui:user-display:url", _url);
080                            request.setAttribute(
081                                    "liferay-ui:user-display:displayStyle",
082                                    String.valueOf(_displayStyle));
083    
084                            PortalIncludeUtil.include(pageContext, getStartPage());
085    
086                            if (user != null) {
087                                    return EVAL_BODY_INCLUDE;
088                            }
089                            else {
090                                    return SKIP_BODY;
091                            }
092                    }
093                    catch (Exception e) {
094                            throw new JspException(e);
095                    }
096            }
097    
098            public void setDisplayStyle(int displayStyle) {
099                    _displayStyle = displayStyle;
100            }
101    
102            public void setEndPage(String endPage) {
103                    _endPage = endPage;
104            }
105    
106            public void setStartPage(String startPage) {
107                    _startPage = startPage;
108            }
109    
110            public void setUrl(String url) {
111                    _url = url;
112            }
113    
114            public void setUserId(long userId) {
115                    _userId = userId;
116            }
117    
118            public void setUserName(String userName) {
119                    _userName = userName;
120            }
121    
122            protected String getEndPage() {
123                    if (Validator.isNull(_endPage)) {
124                            return _END_PAGE;
125                    }
126                    else {
127                            return _endPage;
128                    }
129            }
130    
131            protected String getStartPage() {
132                    if (Validator.isNull(_startPage)) {
133                            return _START_PAGE;
134                    }
135                    else {
136                            return _startPage;
137                    }
138            }
139    
140            private static final String _END_PAGE =
141                    "/html/taglib/ui/user_display/end.jsp";
142    
143            private static final String _START_PAGE =
144                    "/html/taglib/ui/user_display/start.jsp";
145    
146            private int _displayStyle = 1;
147            private String _endPage;
148            private String _startPage;
149            private String _url;
150            private long _userId;
151            private String _userName;
152    
153    }