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