1   /**
2    * Copyright (c) 2000-2008 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.ServletRequest;
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              ServletRequest req = pageContext.getRequest();
46  
47              req.setAttribute(
48                  "liferay-ui:user-display:user-id", String.valueOf(_userId));
49              req.setAttribute("liferay-ui:user-display:user-name", _userName);
50  
51              User user = null;
52  
53              try {
54                  user = UserLocalServiceUtil.getUserById(_userId);
55  
56                  req.setAttribute("liferay-ui:user-display:user", user);
57  
58                  pageContext.setAttribute("userDisplay", user);
59              }
60              catch (NoSuchUserException usue) {
61                  req.removeAttribute("liferay-ui:user-display:user");
62  
63                  pageContext.removeAttribute("userDisplay");
64              }
65  
66              req.setAttribute("liferay-ui:user-display:url", _url);
67              req.setAttribute(
68                  "liferay-ui:user-display:displayStyle",
69                  String.valueOf(_displayStyle));
70  
71              PortalIncludeUtil.include(pageContext, getStartPage());
72  
73              if (user != null) {
74                  return EVAL_BODY_INCLUDE;
75              }
76              else {
77                  return SKIP_BODY;
78              }
79          }
80          catch (Exception e) {
81              throw new JspException(e);
82          }
83      }
84  
85      public int doEndTag() throws JspException {
86          try {
87              PortalIncludeUtil.include(pageContext, getEndPage());
88  
89              return EVAL_PAGE;
90          }
91          catch (Exception e) {
92              throw new JspException(e);
93          }
94      }
95  
96      public String getStartPage() {
97          if (Validator.isNull(_startPage)) {
98              return _START_PAGE;
99          }
100         else {
101             return _startPage;
102         }
103     }
104 
105     public void setStartPage(String startPage) {
106         _startPage = startPage;
107     }
108 
109     public String getEndPage() {
110         if (Validator.isNull(_endPage)) {
111             return _END_PAGE;
112         }
113         else {
114             return _endPage;
115         }
116     }
117 
118     public void setEndPage(String endPage) {
119         _endPage = endPage;
120     }
121 
122     public void setUserId(long userId) {
123         _userId = userId;
124     }
125 
126     public void setUserName(String userName) {
127         _userName = userName;
128     }
129 
130     public void setUrl(String url) {
131         _url = url;
132     }
133 
134     public void setDisplayStyle(int displayStyle) {
135         _displayStyle = displayStyle;
136     }
137 
138     private static final String _START_PAGE =
139         "/html/taglib/ui/user_display/start.jsp";
140 
141     private static final String _END_PAGE =
142         "/html/taglib/ui/user_display/end.jsp";
143 
144     private String _startPage;
145     private String _endPage;
146     private long _userId;
147     private String _userName;
148     private String _url;
149     private int _displayStyle = 1;
150 
151 }