001    /**
002     * Copyright (c) 2000-present 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.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.dao.search.ResultRow;
019    import com.liferay.portal.kernel.dao.search.SearchEntry;
020    import com.liferay.portal.kernel.servlet.ServletContextPool;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.ServerDetector;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.taglib.search.UserSearchEntry;
026    
027    import java.util.Date;
028    import java.util.List;
029    import java.util.Map;
030    
031    import javax.portlet.PortletURL;
032    
033    import javax.servlet.ServletContext;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    import javax.servlet.jsp.JspException;
037    import javax.servlet.jsp.JspTagException;
038    
039    /**
040     * @author Eudaldo Alonso
041     */
042    public class SearchContainerColumnUserTag<R> extends SearchContainerColumnTag {
043    
044            @Override
045            public int doEndTag() {
046                    try {
047                            SearchContainerRowTag<R> searchContainerRowTag =
048                                    (SearchContainerRowTag<R>)findAncestorWithClass(
049                                            this, SearchContainerRowTag.class);
050    
051                            ResultRow resultRow = searchContainerRowTag.getRow();
052    
053                            if ((_userId == 0) && (resultRow.getObject() != null)) {
054                                    if (Validator.isNull(_property)) {
055                                            _userId = GetterUtil.getLong(
056                                                    BeanPropertiesUtil.getObjectSilent(
057                                                            resultRow.getObject(), "userId"));
058                                    }
059                                    else {
060                                            _userId = GetterUtil.getLong(
061                                                    BeanPropertiesUtil.getObjectSilent(
062                                                            resultRow.getObject(), _property));
063                                    }
064                            }
065    
066                            if (index <= -1) {
067                                    List<SearchEntry> searchEntries = resultRow.getEntries();
068    
069                                    index = searchEntries.size();
070                            }
071    
072                            if (resultRow.isRestricted()) {
073                                    _href = null;
074                            }
075    
076                            UserSearchEntry userSearchEntry = new UserSearchEntry();
077    
078                            userSearchEntry.setAlign(getAlign());
079                            userSearchEntry.setColspan(getColspan());
080                            userSearchEntry.setCssClass(getCssClass());
081                            userSearchEntry.setDate(_date);
082                            userSearchEntry.setRequest(
083                                    (HttpServletRequest)pageContext.getRequest());
084                            userSearchEntry.setResponse(
085                                    (HttpServletResponse)pageContext.getResponse());
086    
087                            ServletContext servletContext = ServletContextPool.get(
088                                    PortalUtil.getServletContextName());
089    
090                            userSearchEntry.setServletContext(servletContext);
091    
092                            userSearchEntry.setShowDetails(isShowDetails());
093                            userSearchEntry.setUserId(_userId);
094                            userSearchEntry.setValign(getValign());
095    
096                            resultRow.addSearchEntry(index, userSearchEntry);
097    
098                            return EVAL_PAGE;
099                    }
100                    finally {
101                            index = -1;
102                            _date = null;
103                            _userId = 0;
104    
105                            if (!ServerDetector.isResin()) {
106                                    align = SearchEntry.DEFAULT_ALIGN;
107                                    colspan = SearchEntry.DEFAULT_COLSPAN;
108                                    cssClass = SearchEntry.DEFAULT_CSS_CLASS;
109                                    _href = null;
110                                    name = null;
111                                    _orderable = false;
112                                    _orderableProperty = null;
113                                    _property = null;
114                                    _showDetails = true;
115                                    valign = SearchEntry.DEFAULT_VALIGN;
116                            }
117                    }
118            }
119    
120            @Override
121            public int doStartTag() throws JspException {
122                    if (_orderable && Validator.isNull(_orderableProperty)) {
123                            _orderableProperty = name;
124                    }
125    
126                    SearchContainerRowTag<R> searchContainerRowTag =
127                            (SearchContainerRowTag<R>)findAncestorWithClass(
128                                    this, SearchContainerRowTag.class);
129    
130                    if (searchContainerRowTag == null) {
131                            throw new JspTagException(
132                                    "Requires liferay-ui:search-container-row");
133                    }
134    
135                    if (!searchContainerRowTag.isHeaderNamesAssigned()) {
136                            List<String> headerNames = searchContainerRowTag.getHeaderNames();
137    
138                            String name = getName();
139    
140                            if (Validator.isNull(name) && Validator.isNotNull(_property)) {
141                                    name = _property;
142                            }
143    
144                            headerNames.add(name);
145    
146                            if (_orderable) {
147                                    Map<String, String> orderableHeaders =
148                                            searchContainerRowTag.getOrderableHeaders();
149    
150                                    if (Validator.isNotNull(_orderableProperty)) {
151                                            orderableHeaders.put(name, _orderableProperty);
152                                    }
153                                    else if (Validator.isNotNull(_property)) {
154                                            orderableHeaders.put(name, _property);
155                                    }
156                                    else if (Validator.isNotNull(name)) {
157                                            orderableHeaders.put(name, name);
158                                    }
159                            }
160                    }
161    
162                    return EVAL_BODY_INCLUDE;
163            }
164    
165            public Date getDate() {
166                    return _date;
167            }
168    
169            public Object getHref() {
170                    if (_href instanceof PortletURL) {
171                            _href = _href.toString();
172                    }
173    
174                    return _href;
175            }
176    
177            public String getOrderableProperty() {
178                    return _orderableProperty;
179            }
180    
181            public String getProperty() {
182                    return _property;
183            }
184    
185            public long getUserId() {
186                    return _userId;
187            }
188    
189            public boolean isOrderable() {
190                    return _orderable;
191            }
192    
193            public boolean isShowDetails() {
194                    return _showDetails;
195            }
196    
197            public void setDate(Date date) {
198                    _date = date;
199            }
200    
201            public void setHref(Object href) {
202                    _href = href;
203            }
204    
205            public void setOrderable(boolean orderable) {
206                    _orderable = orderable;
207            }
208    
209            public void setOrderableProperty(String orderableProperty) {
210                    _orderableProperty = orderableProperty;
211            }
212    
213            public void setProperty(String property) {
214                    _property = property;
215            }
216    
217            public void setShowDetails(boolean showDetails) {
218                    _showDetails = showDetails;
219            }
220    
221            public void setUserId(long userId) {
222                    _userId = userId;
223            }
224    
225            private Date _date;
226            private Object _href;
227            private boolean _orderable;
228            private String _orderableProperty;
229            private String _property;
230            private boolean _showDetails = true;
231            private long _userId;
232    
233    }