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