/
UserLazyDataModel.java
201 lines (156 loc) · 6.37 KB
/
UserLazyDataModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Copyright (c) 2000-2015 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.faces.demos.list;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import com.liferay.faces.util.logging.Logger;
import com.liferay.faces.util.logging.LoggerFactory;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.search.Sort;
import com.liferay.portal.kernel.search.SortFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
/**
* This class extends the PrimeFaces {@link LazyDataModel} in order to provide a lazy-loaded list of {@link User}
* objects to the p:dataTable in the users.xhtml Facelet view.
*
* @author Neil Griffin
* @author Kyle Stiemann
*/
public class UserLazyDataModel extends LazyDataModel<User> implements Serializable {
// Logger
private static final Logger logger = LoggerFactory.getLogger(UserLazyDataModel.class);
// serialVersionUID
private static final long serialVersionUID = 2087063071907939066L;
// Private Constants
private static final String DEFAULT_SORT_CRITERIA = "lastName";
// Private Data Members
private long companyId;
public UserLazyDataModel(long companyId, int pageSize) {
this.companyId = companyId;
setPageSize(pageSize);
setRowCount(countRows());
}
public int countRows() {
int totalCount = 0;
try {
LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();
params.put("expandoAttributes", null);
Sort sort = SortFactoryUtil.getSort(User.class, DEFAULT_SORT_CRITERIA, "asc");
boolean andSearch = true;
int status = WorkflowConstants.STATUS_ANY;
String firstName = null;
String middleName = null;
String lastName = null;
String screenName = null;
String emailAddress = null;
Hits hits = UserLocalServiceUtil.search(companyId, firstName, middleName, lastName, screenName,
emailAddress, status, params, andSearch, QueryUtil.ALL_POS, QueryUtil.ALL_POS, sort);
totalCount = hits.getLength();
}
catch (Exception e) {
logger.error(e.getMessage(), e);
}
return totalCount;
}
/**
* This method is called by the PrimeFaces {@link DataTable} according to the rows specified in the currently
* displayed page of data.
*
* @param first The zero-relative first row index.
* @param pageSize The number of rows to fetch.
* @param sortField The name of the field which the table is sorted by.
* @param sortOrder The sort order, which can be either ascending (default) or descending.
* @param filters The query criteria. Note that in order for the filtering to work with the Liferay API, the
* end-user must specify complete, matching words. Wildcards and partial matches are not
* supported.
*/
@Override
public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder,
Map<String, String> filters) {
List<User> users = null;
Sort sort;
// sort
if (sortField != null) {
if (sortOrder.equals(SortOrder.DESCENDING)) {
sort = SortFactoryUtil.getSort(User.class, sortField, "desc");
}
else {
sort = SortFactoryUtil.getSort(User.class, sortField, "asc");
}
}
else {
sort = SortFactoryUtil.getSort(User.class, DEFAULT_SORT_CRITERIA, "asc");
}
try {
LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();
int liferayOneRelativeFinishRow = first + pageSize + 1;
boolean andSearch = true;
int status = WorkflowConstants.STATUS_ANY;
String firstName = trimExpresssion(filters.get("firstName"));
String middleName = trimExpresssion(filters.get("middleName"));
String lastName = trimExpresssion(filters.get("lastName"));
String screenName = trimExpresssion(filters.get("screenName"));
String emailAddress = trimExpresssion(filters.get("emailAddress"));
// For the sake of speed, search for users in the index rather than
// querying the database directly.
Hits hits = UserLocalServiceUtil.search(companyId, firstName, middleName, lastName, screenName,
emailAddress, status, params, andSearch, first, liferayOneRelativeFinishRow, sort);
List<Document> documentHits = hits.toList();
logger.debug(
("filters firstName=[{0}] middleName=[{1}] lastName=[{2}] screenName=[{3}] emailAddress=[{4}] active=[{5}] andSearch=[{6}] startRow=[{7}] liferayOneRelativeFinishRow=[{8}] sortColumn=[{9}] reverseOrder=[{10}] hitCount=[{11}]"),
firstName, middleName, lastName, screenName, emailAddress, status, andSearch, first,
liferayOneRelativeFinishRow, sortField, sort.isReverse(), documentHits.size());
// Convert the results from the search index into a list of user
// objects.
users = new ArrayList<User>(documentHits.size());
for (Document document : documentHits) {
long userId = GetterUtil.getLong(document.get(Field.USER_ID));
try {
User user = UserLocalServiceUtil.getUserById(userId);
users.add(user);
}
catch (NoSuchUserException nsue) {
logger.error("User with userId=[{0}] does not exist in the search index. Please reindex.");
}
}
}
catch (Exception e) {
logger.error(e.getMessage(), e);
}
return users;
}
protected String trimExpresssion(String value) {
String expression = null;
if (value != null) {
String trimmedValue = value.trim();
if (trimmedValue.length() > 0) {
expression = trimmedValue.toLowerCase();
}
}
return expression;
}
}