1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.NoSuchUserException;
26  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.jsp.JspException;
33  import javax.servlet.jsp.tagext.TagSupport;
34  
35  /**
36   * <a href="UserDisplayTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class UserDisplayTag extends TagSupport {
42  
43      public int doStartTag() throws JspException {
44          try {
45              HttpServletRequest request =
46                  (HttpServletRequest)pageContext.getRequest();
47  
48              request.setAttribute(
49                  "liferay-ui:user-display:user-id", String.valueOf(_userId));
50              request.setAttribute(
51                  "liferay-ui:user-display:user-name", _userName);
52  
53              User user = null;
54  
55              try {
56                  user = UserLocalServiceUtil.getUserById(_userId);
57  
58                  if (user.isDefaultUser()) {
59                      user = null;
60                  }
61  
62                  request.setAttribute("liferay-ui:user-display:user", user);
63  
64                  pageContext.setAttribute("userDisplay", user);
65              }
66              catch (NoSuchUserException usue) {
67                  request.removeAttribute("liferay-ui:user-display:user");
68  
69                  pageContext.removeAttribute("userDisplay");
70              }
71  
72              request.setAttribute("liferay-ui:user-display:url", _url);
73              request.setAttribute(
74                  "liferay-ui:user-display:displayStyle",
75                  String.valueOf(_displayStyle));
76  
77              PortalIncludeUtil.include(pageContext, getStartPage());
78  
79              if (user != null) {
80                  return EVAL_BODY_INCLUDE;
81              }
82              else {
83                  return SKIP_BODY;
84              }
85          }
86          catch (Exception e) {
87              throw new JspException(e);
88          }
89      }
90  
91      public int doEndTag() throws JspException {
92          try {
93              PortalIncludeUtil.include(pageContext, getEndPage());
94  
95              return EVAL_PAGE;
96          }
97          catch (Exception e) {
98              throw new JspException(e);
99          }
100     }
101 
102     public String getStartPage() {
103         if (Validator.isNull(_startPage)) {
104             return _START_PAGE;
105         }
106         else {
107             return _startPage;
108         }
109     }
110 
111     public void setStartPage(String startPage) {
112         _startPage = startPage;
113     }
114 
115     public String getEndPage() {
116         if (Validator.isNull(_endPage)) {
117             return _END_PAGE;
118         }
119         else {
120             return _endPage;
121         }
122     }
123 
124     public void setEndPage(String endPage) {
125         _endPage = endPage;
126     }
127 
128     public void setUserId(long userId) {
129         _userId = userId;
130     }
131 
132     public void setUserName(String userName) {
133         _userName = userName;
134     }
135 
136     public void setUrl(String url) {
137         _url = url;
138     }
139 
140     public void setDisplayStyle(int displayStyle) {
141         _displayStyle = displayStyle;
142     }
143 
144     private static final String _START_PAGE =
145         "/html/taglib/ui/user_display/start.jsp";
146 
147     private static final String _END_PAGE =
148         "/html/taglib/ui/user_display/end.jsp";
149 
150     private String _startPage;
151     private String _endPage;
152     private long _userId;
153     private String _userName;
154     private String _url;
155     private int _displayStyle = 1;
156 
157 }