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