001
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.SearchContainer;
020 import com.liferay.portal.kernel.repository.model.RepositoryModel;
021 import com.liferay.portal.kernel.util.CharPool;
022 import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.ServerDetector;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.BaseModel;
027 import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
028
029 import java.util.ArrayList;
030 import java.util.LinkedHashMap;
031 import java.util.List;
032 import java.util.Map;
033
034 import javax.servlet.ServletContext;
035 import javax.servlet.jsp.JspException;
036 import javax.servlet.jsp.tagext.BodyTag;
037
038
041 public class SearchContainerRowTag<R>
042 extends ParamAndPropertyAncestorTagImpl implements BodyTag {
043
044 public static final String DEFAULT_INDEX_VAR = "index";
045
046 public static final String DEFAULT_MODEL_VAR = "model";
047
048 public static final String DEFAULT_ROW_VAR = "row";
049
050 @Override
051 public void addParam(String name, String value) {
052 if (name.equals("className")) {
053 _resultRow.setClassName(value);
054 }
055 else if (name.equals("classHoverName")) {
056 _resultRow.setClassHoverName(value);
057 }
058 else if (name.equals("restricted")) {
059 _resultRow.setRestricted(GetterUtil.getBoolean(value, false));
060 }
061 else {
062 Object obj = pageContext.getAttribute(value);
063
064 if (obj == null) {
065 obj = value;
066 }
067
068 _resultRow.setParameter(name, obj);
069 }
070 }
071
072 @Override
073 public int doAfterBody() {
074 if (!_headerNamesAssigned) {
075 SearchContainerTag<R> searchContainerTag =
076 (SearchContainerTag<R>)findAncestorWithClass(
077 this, SearchContainerTag.class);
078
079 SearchContainer<R> searchContainer =
080 searchContainerTag.getSearchContainer();
081
082 searchContainer.setHeaderNames(_headerNames);
083 searchContainer.setOrderableHeaders(_orderableHeaders);
084
085 _headerNamesAssigned = true;
086 }
087
088 if (!_resultRow.isSkip()) {
089 _resultRows.add(_resultRow);
090 }
091
092 _rowIndex++;
093
094 if (_rowIndex < _results.size()) {
095 processRow();
096
097 return EVAL_BODY_AGAIN;
098 }
099 else {
100 return SKIP_BODY;
101 }
102 }
103
104 @Override
105 public int doEndTag() {
106 _headerNames = null;
107 _headerNamesAssigned = false;
108 _resultRows = null;
109 _rowIndex = 0;
110 _resultRow = null;
111
112 if (!ServerDetector.isResin()) {
113 _bold = false;
114 _className = null;
115 _escapedModel = false;
116 _indexVar = DEFAULT_INDEX_VAR;
117 _keyProperty = null;
118 _modelVar = DEFAULT_MODEL_VAR;
119 _orderableHeaders = null;
120 _rowIdProperty = null;
121 _rowVar = DEFAULT_ROW_VAR;
122 _stringKey = false;
123 }
124
125 return EVAL_PAGE;
126 }
127
128 @Override
129 public int doStartTag() throws JspException {
130 SearchContainerTag<R> searchContainerTag =
131 (SearchContainerTag<R>)findAncestorWithClass(
132 this, SearchContainerTag.class);
133
134 if (searchContainerTag == null) {
135 throw new JspException("Requires liferay-ui:search-container");
136 }
137 else if (!searchContainerTag.isHasResults()) {
138 throw new JspException(
139 "Requires liferay-ui:search-container-results");
140 }
141
142 SearchContainer<R> searchContainer =
143 searchContainerTag.getSearchContainer();
144
145 searchContainer.setClassName(_className);
146
147 _resultRows = searchContainer.getResultRows();
148 _results = searchContainer.getResults();
149
150 if ((_results != null) && !_results.isEmpty()) {
151 processRow();
152
153 return EVAL_BODY_INCLUDE;
154 }
155 else {
156 return SKIP_BODY;
157 }
158 }
159
160 public String getClassName() {
161 return _className;
162 }
163
164 public List<String> getHeaderNames() {
165 if (_headerNames == null) {
166 _headerNames = new ArrayList<String>();
167 }
168
169 return _headerNames;
170 }
171
172 public String getIndexVar() {
173 return _indexVar;
174 }
175
176 public String getKeyProperty() {
177 return _keyProperty;
178 }
179
180 public String getModelVar() {
181 return _modelVar;
182 }
183
184 public Map<String, String> getOrderableHeaders() {
185 if (_orderableHeaders == null) {
186 _orderableHeaders = new LinkedHashMap<String, String>();
187 }
188
189 return _orderableHeaders;
190 }
191
192 public ResultRow getRow() {
193 return _resultRow;
194 }
195
196 public String getRowVar() {
197 return _rowVar;
198 }
199
200 public boolean isBold() {
201 return _bold;
202 }
203
204 public boolean isEscapedModel() {
205 return _escapedModel;
206 }
207
208 public boolean isHeaderNamesAssigned() {
209 return _headerNamesAssigned;
210 }
211
212 public boolean isStringKey() {
213 return _stringKey;
214 }
215
216 public void setBold(boolean bold) {
217 _bold = bold;
218 }
219
220 public void setClassName(String className) {
221 _className = className;
222 }
223
224 public void setEscapedModel(boolean escapedModel) {
225 _escapedModel = escapedModel;
226 }
227
228 public void setHeaderNames(List<String> headerNames) {
229 _headerNames = headerNames;
230 }
231
232 public void setHeaderNamesAssigned(boolean headerNamesAssigned) {
233 _headerNamesAssigned = headerNamesAssigned;
234 }
235
236 public void setIndexVar(String indexVar) {
237 _indexVar = indexVar;
238 }
239
240 public void setKeyProperty(String keyProperty) {
241 _keyProperty = keyProperty;
242 }
243
244 public void setModelVar(String var) {
245 _modelVar = var;
246 }
247
248 public void setOrderableHeaders(Map<String, String> orderableHeaders) {
249 _orderableHeaders = orderableHeaders;
250 }
251
252 public void setRow(ResultRow row) {
253 _resultRow = row;
254 }
255
256 public void setRowIdProperty(String rowIdProperty) {
257 _rowIdProperty = rowIdProperty;
258 }
259
260 public void setRowVar(String rowVar) {
261 _rowVar = rowVar;
262 }
263
264 @Override
265 public void setServletContext(ServletContext servletContext) {
266 }
267
268 public void setStringKey(boolean stringKey) {
269 _stringKey = stringKey;
270 }
271
272 protected void processRow() {
273 Object model = _results.get(_rowIndex);
274
275 if (isEscapedModel()) {
276 if (model instanceof BaseModel) {
277 BaseModel<?> baseModel = (BaseModel<?>)model;
278
279 model = baseModel.toEscapedModel();
280 }
281 else if (model instanceof RepositoryModel) {
282 RepositoryModel<?> repositoryModel = (RepositoryModel<?>)model;
283
284 model = repositoryModel.toEscapedModel();
285 }
286 }
287
288 String primaryKey = null;
289
290 if (Validator.isNull(_keyProperty)) {
291 primaryKey = String.valueOf(model);
292 }
293 else if (isStringKey()) {
294 primaryKey = BeanPropertiesUtil.getString(model, _keyProperty);
295 }
296 else {
297 Object primaryKeyObj = BeanPropertiesUtil.getObject(
298 model, _keyProperty);
299
300 primaryKey = String.valueOf(primaryKeyObj);
301 }
302
303 String rowId = null;
304
305 if (Validator.isNull(_rowIdProperty)) {
306 rowId = String.valueOf(_rowIndex + 1);
307 }
308 else {
309 Object rowIdObj = BeanPropertiesUtil.getObject(
310 model, _rowIdProperty);
311
312 if (Validator.isNull(rowIdObj)) {
313 rowId = String.valueOf(_rowIndex + 1);
314 }
315 else {
316 rowId = FriendlyURLNormalizerUtil.normalize(
317 String.valueOf(rowIdObj),
318 new char[] {CharPool.PERIOD, CharPool.SLASH});
319 }
320 }
321
322 _resultRow = new ResultRow(rowId, model, primaryKey, _rowIndex, _bold);
323
324 pageContext.setAttribute(_indexVar, _rowIndex);
325 pageContext.setAttribute(_modelVar, model);
326 pageContext.setAttribute(_rowVar, _resultRow);
327 }
328
329 private boolean _bold;
330 private String _className;
331 private boolean _escapedModel;
332 private List<String> _headerNames;
333 private boolean _headerNamesAssigned;
334 private String _indexVar = DEFAULT_INDEX_VAR;
335 private String _keyProperty;
336 private String _modelVar = DEFAULT_MODEL_VAR;
337 private Map<String, String> _orderableHeaders;
338 private ResultRow _resultRow;
339 private List<ResultRow> _resultRows;
340 private List<R> _results;
341 private String _rowIdProperty;
342 private int _rowIndex;
343 private String _rowVar = DEFAULT_ROW_VAR;
344 private boolean _stringKey;
345
346 }