001
014
015 package com.liferay.portal.service.persistence.impl;
016
017 import com.liferay.portal.kernel.dao.orm.QueryPos;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.dao.orm.SQLQuery;
020 import com.liferay.portal.kernel.dao.orm.Session;
021 import com.liferay.portal.kernel.dao.orm.Type;
022 import com.liferay.portal.kernel.exception.SystemException;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.Team;
029 import com.liferay.portal.model.impl.TeamImpl;
030 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
031 import com.liferay.portal.service.persistence.TeamFinder;
032 import com.liferay.util.dao.orm.CustomSQLUtil;
033
034 import java.util.Iterator;
035 import java.util.LinkedHashMap;
036 import java.util.List;
037 import java.util.Map;
038
039
042 public class TeamFinderImpl extends TeamFinderBaseImpl implements TeamFinder {
043
044 public static final String COUNT_BY_G_N_D =
045 TeamFinder.class.getName() + ".countByG_N_D";
046
047 public static final String FIND_BY_G_U =
048 TeamFinder.class.getName() + ".findByG_U";
049
050 public static final String FIND_BY_G_N_D =
051 TeamFinder.class.getName() + ".findByG_N_D";
052
053 public static final String JOIN_BY_USERS_TEAMS =
054 TeamFinder.class.getName() + ".joinByUsersTeams";
055
056 public static final String JOIN_BY_USERS_USER_GROUPS =
057 TeamFinder.class.getName() + ".joinByUsersUserGroups";
058
059 @Override
060 public int countByG_N_D(
061 long groupId, String name, String description,
062 LinkedHashMap<String, Object> params) {
063
064 return doCountByG_N_D(groupId, name, description, params, false);
065 }
066
067 @Override
068 public int filterCountByG_N_D(
069 long groupId, String name, String description,
070 LinkedHashMap<String, Object> params) {
071
072 return doCountByG_N_D(groupId, name, description, params, true);
073 }
074
075 @Override
076 public List<Team> filterFindByG_N_D(
077 long groupId, String name, String description,
078 LinkedHashMap<String, Object> params, int start, int end,
079 OrderByComparator<Team> obc) {
080
081 return doFindByG_N_D(
082 groupId, name, description, params, start, end, obc, true);
083 }
084
085 @Override
086 public List<Team> findByG_U(
087 long groupId, long userId, int start, int end,
088 OrderByComparator<Team> obc) {
089
090 return doFindByG_U(groupId, userId, start, end, obc);
091 }
092
093 @Override
094 public List<Team> findByG_N_D(
095 long groupId, String name, String description,
096 LinkedHashMap<String, Object> params, int start, int end,
097 OrderByComparator<Team> obc) {
098
099 return doFindByG_N_D(
100 groupId, name, description, params, start, end, obc, false);
101 }
102
103 protected int doCountByG_N_D(
104 long groupId, String name, String description,
105 LinkedHashMap<String, Object> params, boolean inlineSQLHelper) {
106
107 name = CustomSQLUtil.keywords(name)[0];
108 description = CustomSQLUtil.keywords(description)[0];
109
110 Session session = null;
111
112 try {
113 session = openSession();
114
115 String sql = CustomSQLUtil.get(COUNT_BY_G_N_D);
116
117 if (inlineSQLHelper) {
118 sql = InlineSQLHelperUtil.replacePermissionCheck(
119 sql, Team.class.getName(), "Team.teamId", groupId);
120 }
121
122 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
123 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
124
125 SQLQuery q = session.createSynchronizedSQLQuery(sql);
126
127 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
128
129 QueryPos qPos = QueryPos.getInstance(q);
130
131 setJoin(qPos, params);
132
133 qPos.add(groupId);
134 qPos.add(name);
135 qPos.add(name);
136 qPos.add(description);
137 qPos.add(description);
138
139 Iterator<Long> itr = q.iterate();
140
141 if (itr.hasNext()) {
142 Long count = itr.next();
143
144 if (count != null) {
145 return count.intValue();
146 }
147 }
148
149 return 0;
150 }
151 catch (Exception e) {
152 throw new SystemException(e);
153 }
154 finally {
155 closeSession(session);
156 }
157 }
158
159 protected List<Team> doFindByG_U(
160 long groupId, long userId, int start, int end,
161 OrderByComparator<Team> obc) {
162
163 Session session = null;
164
165 try {
166 session = openSession();
167
168 String sql = CustomSQLUtil.get(FIND_BY_G_U);
169
170 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
171
172 SQLQuery q = session.createSynchronizedSQLQuery(sql);
173
174 q.addEntity("Team", TeamImpl.class);
175
176 QueryPos qPos = QueryPos.getInstance(q);
177
178 qPos.add(groupId);
179 qPos.add(userId);
180 qPos.add(userId);
181
182 return (List<Team>)QueryUtil.list(q, getDialect(), start, end);
183 }
184 catch (Exception e) {
185 throw new SystemException(e);
186 }
187 finally {
188 closeSession(session);
189 }
190 }
191
192 protected List<Team> doFindByG_N_D(
193 long groupId, String name, String description,
194 LinkedHashMap<String, Object> params, int start, int end,
195 OrderByComparator<Team> obc, boolean inlineSQLHelper) {
196
197 name = CustomSQLUtil.keywords(name)[0];
198 description = CustomSQLUtil.keywords(description)[0];
199
200 Session session = null;
201
202 try {
203 session = openSession();
204
205 String sql = CustomSQLUtil.get(FIND_BY_G_N_D);
206
207 if (inlineSQLHelper) {
208 sql = InlineSQLHelperUtil.replacePermissionCheck(
209 sql, Team.class.getName(), "Team.teamId", groupId);
210 }
211
212 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
213 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
214 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
215
216 SQLQuery q = session.createSynchronizedSQLQuery(sql);
217
218 q.addEntity("Team", TeamImpl.class);
219
220 QueryPos qPos = QueryPos.getInstance(q);
221
222 setJoin(qPos, params);
223
224 qPos.add(groupId);
225 qPos.add(name);
226 qPos.add(name);
227 qPos.add(description);
228 qPos.add(description);
229
230 return (List<Team>)QueryUtil.list(q, getDialect(), start, end);
231 }
232 catch (Exception e) {
233 throw new SystemException(e);
234 }
235 finally {
236 closeSession(session);
237 }
238 }
239
240 protected String getJoin(LinkedHashMap<String, Object> params) {
241 if ((params == null) || params.isEmpty()) {
242 return StringPool.BLANK;
243 }
244
245 StringBundler sb = new StringBundler(params.size());
246
247 for (Map.Entry<String, Object> entry : params.entrySet()) {
248 String key = entry.getKey();
249 Object value = entry.getValue();
250
251 if (Validator.isNotNull(value)) {
252 sb.append(getJoin(key));
253 }
254 }
255
256 return sb.toString();
257 }
258
259 protected String getJoin(String key) {
260 String join = StringPool.BLANK;
261
262 if (key.equals("usersUserGroups")) {
263 join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
264 }
265 else if (key.equals("usersTeams")) {
266 join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
267 }
268
269 if (Validator.isNotNull(join)) {
270 int pos = join.indexOf("WHERE");
271
272 if (pos != -1) {
273 join = join.substring(0, pos);
274 }
275 }
276
277 return join;
278 }
279
280 protected String getWhere(LinkedHashMap<String, Object> params) {
281 if ((params == null) || params.isEmpty()) {
282 return StringPool.BLANK;
283 }
284
285 StringBundler sb = new StringBundler(params.size());
286
287 for (Map.Entry<String, Object> entry : params.entrySet()) {
288 String key = entry.getKey();
289 Object value = entry.getValue();
290
291 if (Validator.isNotNull(value)) {
292 sb.append(getWhere(key));
293 }
294 }
295
296 return sb.toString();
297 }
298
299 protected String getWhere(String key) {
300 String join = StringPool.BLANK;
301
302 if (key.equals("usersUserGroups")) {
303 join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
304 }
305 else if (key.equals("usersTeams")) {
306 join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
307 }
308
309 if (Validator.isNotNull(join)) {
310 int pos = join.indexOf("WHERE");
311
312 if (pos != -1) {
313 join = join.substring(pos + 5, join.length()).concat(" AND ");
314 }
315 else {
316 join = StringPool.BLANK;
317 }
318 }
319
320 return join;
321 }
322
323 protected void setJoin(
324 QueryPos qPos, LinkedHashMap<String, Object> params) {
325
326 if (params == null) {
327 return;
328 }
329
330 for (Map.Entry<String, Object> entry : params.entrySet()) {
331 Object value = entry.getValue();
332
333 if (value instanceof Long) {
334 Long valueLong = (Long)value;
335
336 if (Validator.isNotNull(valueLong)) {
337 qPos.add(valueLong);
338 }
339 }
340 }
341 }
342
343 }