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