001    /**
002     * Copyright (c) 2000-2013 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.kernel.servlet.PortalIncludeUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.service.UserLocalServiceUtil;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.jsp.JspException;
024    import javax.servlet.jsp.tagext.TagSupport;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class UserDisplayTag extends TagSupport {
030    
031            @Override
032            public int doEndTag() throws JspException {
033                    try {
034                            PortalIncludeUtil.include(pageContext, getEndPage());
035    
036                            HttpServletRequest request =
037                                    (HttpServletRequest)pageContext.getRequest();
038    
039                            request.removeAttribute("liferay-ui:user-display:url");
040    
041                            return EVAL_PAGE;
042                    }
043                    catch (Exception e) {
044                            throw new JspException(e);
045                    }
046            }
047    
048            @Override
049            public int doStartTag() throws JspException {
050                    try {
051                            HttpServletRequest request =
052                                    (HttpServletRequest)pageContext.getRequest();
053    
054                            request.setAttribute(
055                                    "liferay-ui:user-display:user-id", String.valueOf(_userId));
056                            request.setAttribute(
057                                    "liferay-ui:user-display:user-name", _userName);
058    
059                            User user = UserLocalServiceUtil.fetchUserById(_userId);
060    
061                            if (user != null) {
062                                    if (user.isDefaultUser()) {
063                                            user = null;
064                                    }
065    
066                                    request.setAttribute("liferay-ui:user-display:user", user);
067    
068                                    pageContext.setAttribute("userDisplay", user);
069                            }
070                            else {
071                                    request.removeAttribute("liferay-ui:user-display:user");
072    
073                                    pageContext.removeAttribute("userDisplay");
074                            }
075    
076                            request.setAttribute("liferay-ui:user-display:url", _url);
077                            request.setAttribute(
078                                    "liferay-ui:user-display:displayStyle",
079                                    String.valueOf(_displayStyle));
080    
081                            PortalIncludeUtil.include(pageContext, getStartPage());
082    
083                            if (user != null) {
084                                    return EVAL_BODY_INCLUDE;
085                            }
086                            else {
087                                    return SKIP_BODY;
088                            }
089                    }
090                    catch (Exception e) {
091                            throw new JspException(e);
092                    }
093            }
094    
095            public void setDisplayStyle(int displayStyle) {
096                    _displayStyle = displayStyle;
097            }
098    
099            public void setEndPage(String endPage) {
100                    _endPage = endPage;
101            }
102    
103            public void setStartPage(String startPage) {
104                    _startPage = startPage;
105            }
106    
107            public void setUrl(String url) {
108                    _url = url;
109            }
110    
111            public void setUserId(long userId) {
112                    _userId = userId;
113            }
114    
115            public void setUserName(String userName) {
116                    _userName = userName;
117            }
118    
119            protected String getEndPage() {
120                    if (Validator.isNull(_endPage)) {
121                            return _END_PAGE;
122                    }
123                    else {
124                            return _endPage;
125                    }
126            }
127    
128            protected String getStartPage() {
129                    if (Validator.isNull(_startPage)) {
130                            return _START_PAGE;
131                    }
132                    else {
133                            return _startPage;
134                    }
135            }
136    
137            private static final String _END_PAGE =
138                    "/html/taglib/ui/user_display/end.jsp";
139    
140            private static final String _START_PAGE =
141                    "/html/taglib/ui/user_display/start.jsp";
142    
143            private int _displayStyle = 1;
144            private String _endPage;
145            private String _startPage;
146            private String _url;
147            private long _userId;
148            private String _userName;
149    
150    }