001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public int doStartTag() throws JspException {
033                    try {
034                            HttpServletRequest request =
035                                    (HttpServletRequest)pageContext.getRequest();
036    
037                            request.setAttribute(
038                                    "liferay-ui:user-display:user-id", String.valueOf(_userId));
039                            request.setAttribute(
040                                    "liferay-ui:user-display:user-name", _userName);
041    
042                            User user = null;
043    
044                            try {
045                                    user = UserLocalServiceUtil.getUserById(_userId);
046    
047                                    if (user.isDefaultUser()) {
048                                            user = null;
049                                    }
050    
051                                    request.setAttribute("liferay-ui:user-display:user", user);
052    
053                                    pageContext.setAttribute("userDisplay", user);
054                            }
055                            catch (NoSuchUserException usue) {
056                                    request.removeAttribute("liferay-ui:user-display:user");
057    
058                                    pageContext.removeAttribute("userDisplay");
059                            }
060    
061                            request.setAttribute("liferay-ui:user-display:url", _url);
062                            request.setAttribute(
063                                    "liferay-ui:user-display:displayStyle",
064                                    String.valueOf(_displayStyle));
065    
066                            PortalIncludeUtil.include(pageContext, getStartPage());
067    
068                            if (user != null) {
069                                    return EVAL_BODY_INCLUDE;
070                            }
071                            else {
072                                    return SKIP_BODY;
073                            }
074                    }
075                    catch (Exception e) {
076                            throw new JspException(e);
077                    }
078            }
079    
080            public int doEndTag() throws JspException {
081                    try {
082                            PortalIncludeUtil.include(pageContext, getEndPage());
083    
084                            return EVAL_PAGE;
085                    }
086                    catch (Exception e) {
087                            throw new JspException(e);
088                    }
089            }
090    
091            protected String getStartPage() {
092                    if (Validator.isNull(_startPage)) {
093                            return _START_PAGE;
094                    }
095                    else {
096                            return _startPage;
097                    }
098            }
099    
100            public void setStartPage(String startPage) {
101                    _startPage = startPage;
102            }
103    
104            protected String getEndPage() {
105                    if (Validator.isNull(_endPage)) {
106                            return _END_PAGE;
107                    }
108                    else {
109                            return _endPage;
110                    }
111            }
112    
113            public void setEndPage(String endPage) {
114                    _endPage = endPage;
115            }
116    
117            public void setUserId(long userId) {
118                    _userId = userId;
119            }
120    
121            public void setUserName(String userName) {
122                    _userName = userName;
123            }
124    
125            public void setUrl(String url) {
126                    _url = url;
127            }
128    
129            public void setDisplayStyle(int displayStyle) {
130                    _displayStyle = displayStyle;
131            }
132    
133            private static final String _START_PAGE =
134                    "/html/taglib/ui/user_display/start.jsp";
135    
136            private static final String _END_PAGE =
137                    "/html/taglib/ui/user_display/end.jsp";
138    
139            private String _startPage;
140            private String _endPage;
141            private long _userId;
142            private String _userName;
143            private String _url;
144            private int _displayStyle = 1;
145    
146    }