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