001
014
015 package com.liferay.portlet.shopping.service.persistence;
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.StringBundler;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
027 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028 import com.liferay.portlet.shopping.model.ShoppingItem;
029 import com.liferay.portlet.shopping.model.impl.ShoppingItemImpl;
030 import com.liferay.util.dao.orm.CustomSQLUtil;
031
032 import java.util.Iterator;
033 import java.util.List;
034
035
038 public class ShoppingItemFinderImpl
039 extends BasePersistenceImpl<ShoppingItem> implements ShoppingItemFinder {
040
041 public static final String COUNT_BY_G_C =
042 ShoppingItemFinder.class.getName() + ".countByG_C";
043
044 @Override
045 public int countByG_C(long groupId, List<Long> categoryIds)
046 throws SystemException {
047
048 return doCountByG_C(groupId, categoryIds, false);
049 }
050
051 @Override
052 public int countByFeatured(long groupId, long[] categoryIds)
053 throws SystemException {
054
055 Session session = null;
056
057 try {
058 session = openSession();
059
060 StringBundler query = new StringBundler();
061
062 query.append("SELECT COUNT(*) AS COUNT_VALUE FROM ShoppingItem ");
063 query.append("WHERE ");
064 query.append("ShoppingItem.groupId = ? AND (");
065
066 if ((categoryIds != null) && (categoryIds.length > 0)) {
067 query.append("(");
068
069 for (int i = 0; i < categoryIds.length; i++) {
070 query.append("ShoppingItem.categoryId = ? ");
071
072 if ((i + 1) < categoryIds.length) {
073 query.append("OR ");
074 }
075 }
076
077 query.append(") AND ");
078 }
079
080 query.append("ShoppingItem.featured = ? AND ");
081 query.append("ShoppingItem.smallImage = ?");
082
083 SQLQuery q = session.createSQLQuery(query.toString());
084
085 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
086
087 QueryPos qPos = QueryPos.getInstance(q);
088
089 qPos.add(groupId);
090
091 for (long categoryId : categoryIds) {
092 qPos.add(categoryId);
093 }
094
095 qPos.add(true);
096 qPos.add(true);
097
098 Iterator<Long> itr = q.iterate();
099
100 if (itr.hasNext()) {
101 Long count = itr.next();
102
103 if (count != null) {
104 return count.intValue();
105 }
106 }
107
108 return 0;
109 }
110 catch (Exception e) {
111 throw new SystemException(e);
112 }
113 finally {
114 closeSession(session);
115 }
116 }
117
118 @Override
119 public int countByKeywords(
120 long groupId, long[] categoryIds, String keywords)
121 throws SystemException {
122
123 Session session = null;
124
125 try {
126 session = openSession();
127
128 StringBundler query = new StringBundler();
129
130 query.append("SELECT COUNT(*) AS COUNT_VALUE FROM ShoppingItem ");
131 query.append("WHERE ");
132 query.append("ShoppingItem.groupId = ? AND (");
133
134 if ((categoryIds != null) && (categoryIds.length > 0)) {
135 query.append("(");
136
137 for (int i = 0; i < categoryIds.length; i++) {
138 query.append("ShoppingItem.categoryId = ? ");
139
140 if ((i + 1) < categoryIds.length) {
141 query.append("OR ");
142 }
143 }
144
145 query.append(") AND ");
146 }
147
148 query.append("(ShoppingItem.name LIKE ? OR ");
149 query.append("ShoppingItem.description LIKE ? OR ");
150 query.append("ShoppingItem.properties LIKE ?))");
151
152 keywords = '%' + keywords + '%';
153
154 SQLQuery q = session.createSQLQuery(query.toString());
155
156 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
157
158 QueryPos qPos = QueryPos.getInstance(q);
159
160 qPos.add(groupId);
161
162 for (long categoryId : categoryIds) {
163 qPos.add(categoryId);
164 }
165
166 qPos.add(keywords);
167 qPos.add(keywords);
168 qPos.add(keywords);
169
170 Iterator<Long> itr = q.iterate();
171
172 if (itr.hasNext()) {
173 Long count = itr.next();
174
175 if (count != null) {
176 return count.intValue();
177 }
178 }
179
180 return 0;
181 }
182 catch (Exception e) {
183 throw new SystemException(e);
184 }
185 finally {
186 closeSession(session);
187 }
188 }
189
190 @Override
191 public int countBySale(long groupId, long[] categoryIds)
192 throws SystemException {
193
194 Session session = null;
195
196 try {
197 session = openSession();
198
199 StringBundler query = new StringBundler();
200
201 query.append("SELECT COUNT(*) AS COUNT_VALUE FROM ShoppingItem ");
202 query.append("WHERE ");
203 query.append("ShoppingItem.groupId = ? AND (");
204
205 if ((categoryIds != null) && (categoryIds.length > 0)) {
206 query.append("(");
207
208 for (int i = 0; i < categoryIds.length; i++) {
209 query.append("ShoppingItem.categoryId = ? ");
210
211 if ((i + 1) < categoryIds.length) {
212 query.append("OR ");
213 }
214 }
215
216 query.append(") AND ");
217 }
218
219 query.append("ShoppingItem.sale = ? AND ");
220 query.append("ShoppingItem.smallImage = ?");
221
222 SQLQuery q = session.createSQLQuery(query.toString());
223
224 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
225
226 QueryPos qPos = QueryPos.getInstance(q);
227
228 qPos.add(groupId);
229
230 for (long categoryId : categoryIds) {
231 qPos.add(categoryId);
232 }
233
234 qPos.add(true);
235 qPos.add(true);
236
237 Iterator<Long> itr = q.iterate();
238
239 if (itr.hasNext()) {
240 Long count = itr.next();
241
242 if (count != null) {
243 return count.intValue();
244 }
245 }
246
247 return 0;
248 }
249 catch (Exception e) {
250 throw new SystemException(e);
251 }
252 finally {
253 closeSession(session);
254 }
255 }
256
257 @Override
258 public int filterCountByG_C(long groupId, List<Long> categoryIds)
259 throws SystemException {
260
261 return doCountByG_C(groupId, categoryIds, true);
262 }
263
264 @Override
265 public List<ShoppingItem> findByFeatured(
266 long groupId, long[] categoryIds, int numOfItems)
267 throws SystemException {
268
269 int countByFeatured = countByFeatured(groupId, categoryIds);
270
271 Session session = null;
272
273 try {
274 session = openSession();
275
276 StringBundler query = new StringBundler();
277
278 query.append("SELECT {ShoppingItem.*} FROM ShoppingItem ");
279 query.append("WHERE ");
280 query.append("ShoppingItem.groupId = ? AND (");
281
282 if ((categoryIds != null) && (categoryIds.length > 0)) {
283 query.append("(");
284
285 for (int i = 0; i < categoryIds.length; i++) {
286 query.append("ShoppingItem.categoryId = ? ");
287
288 if ((i + 1) < categoryIds.length) {
289 query.append("OR ");
290 }
291 }
292
293 query.append(") AND ");
294 }
295
296 query.append("ShoppingItem.featured = ? AND ");
297 query.append("ShoppingItem.smallImage = ?");
298
299 SQLQuery q = session.createSQLQuery(query.toString());
300
301 q.addEntity("ShoppingItem", ShoppingItemImpl.class);
302
303 QueryPos qPos = QueryPos.getInstance(q);
304
305 qPos.add(groupId);
306
307 for (long categoryId : categoryIds) {
308 qPos.add(categoryId);
309 }
310
311 qPos.add(true);
312 qPos.add(true);
313
314 return (List<ShoppingItem>)QueryUtil.randomList(
315 q, getDialect(), countByFeatured, numOfItems);
316 }
317 catch (Exception e) {
318 throw new SystemException(e);
319 }
320 finally {
321 closeSession(session);
322 }
323 }
324
325 @Override
326 public List<ShoppingItem> findByKeywords(
327 long groupId, long[] categoryIds, String keywords, int start,
328 int end)
329 throws SystemException {
330
331 Session session = null;
332
333 try {
334 session = openSession();
335
336 StringBundler query = new StringBundler();
337
338 query.append("SELECT {ShoppingItem.*} FROM ShoppingItem ");
339 query.append("WHERE ");
340 query.append("ShoppingItem.groupId = ? AND (");
341
342 if ((categoryIds != null) && (categoryIds.length > 0)) {
343 query.append("(");
344
345 for (int i = 0; i < categoryIds.length; i++) {
346 query.append("ShoppingItem.categoryId = ? ");
347
348 if ((i + 1) < categoryIds.length) {
349 query.append("OR ");
350 }
351 }
352
353 query.append(") AND ");
354 }
355
356 query.append("(ShoppingItem.name LIKE ? OR ");
357 query.append("ShoppingItem.description LIKE ? OR ");
358 query.append("ShoppingItem.properties LIKE ?))");
359
360 keywords = '%' + keywords + '%';
361
362 SQLQuery q = session.createSQLQuery(query.toString());
363
364 q.addEntity("ShoppingItem", ShoppingItemImpl.class);
365
366 QueryPos qPos = QueryPos.getInstance(q);
367
368 qPos.add(groupId);
369
370 for (long categoryId : categoryIds) {
371 qPos.add(categoryId);
372 }
373
374 qPos.add(keywords);
375 qPos.add(keywords);
376 qPos.add(keywords);
377
378 return (List<ShoppingItem>)QueryUtil.list(
379 q, getDialect(), start, end);
380 }
381 catch (Exception e) {
382 throw new SystemException(e);
383 }
384 finally {
385 closeSession(session);
386 }
387 }
388
389 @Override
390 public List<ShoppingItem> findBySale(
391 long groupId, long[] categoryIds, int numOfItems)
392 throws SystemException {
393
394 int countBySale = countBySale(groupId, categoryIds);
395
396 Session session = null;
397
398 try {
399 session = openSession();
400
401 StringBundler query = new StringBundler();
402
403 query.append("SELECT {ShoppingItem.*} FROM ShoppingItem ");
404 query.append("WHERE ");
405 query.append("ShoppingItem.groupId = ? AND (");
406
407 if ((categoryIds != null) && (categoryIds.length > 0)) {
408 query.append("(");
409
410 for (int i = 0; i < categoryIds.length; i++) {
411 query.append("ShoppingItem.categoryId = ? ");
412
413 if ((i + 1) < categoryIds.length) {
414 query.append("OR ");
415 }
416 }
417
418 query.append(") AND ");
419 }
420
421 query.append("ShoppingItem.sale = ? AND ");
422 query.append("ShoppingItem.smallImage = ?");
423
424 SQLQuery q = session.createSQLQuery(query.toString());
425
426 q.addEntity("ShoppingItem", ShoppingItemImpl.class);
427
428 QueryPos qPos = QueryPos.getInstance(q);
429
430 qPos.add(groupId);
431
432 for (long categoryId : categoryIds) {
433 qPos.add(categoryId);
434 }
435
436 qPos.add(true);
437 qPos.add(true);
438
439 return (List<ShoppingItem>)QueryUtil.randomList(
440 q, getDialect(), countBySale, numOfItems);
441 }
442 catch (Exception e) {
443 throw new SystemException(e);
444 }
445 finally {
446 closeSession(session);
447 }
448 }
449
450 protected int doCountByG_C(
451 long groupId, List<Long> categoryIds, boolean inlineSQLHelper)
452 throws SystemException {
453
454 Session session = null;
455
456 try {
457 session = openSession();
458
459 String sql = CustomSQLUtil.get(COUNT_BY_G_C);
460
461 if (inlineSQLHelper) {
462 sql = InlineSQLHelperUtil.replacePermissionCheck(
463 sql, ShoppingItem.class.getName(), "ShoppingItem.itemId",
464 groupId);
465 }
466
467 sql = StringUtil.replace(
468 sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
469
470 SQLQuery q = session.createSQLQuery(sql);
471
472 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
473
474 QueryPos qPos = QueryPos.getInstance(q);
475
476 qPos.add(groupId);
477
478 for (int i = 0; i < categoryIds.size(); i++) {
479 Long categoryId = categoryIds.get(i);
480
481 qPos.add(categoryId);
482 }
483
484 Iterator<Long> itr = q.iterate();
485
486 if (itr.hasNext()) {
487 Long count = itr.next();
488
489 if (count != null) {
490 return count.intValue();
491 }
492 }
493
494 return 0;
495 }
496 catch (Exception e) {
497 throw new SystemException(e);
498 }
499 finally {
500 closeSession(session);
501 }
502 }
503
504 protected String getCategoryIds(List<Long> categoryIds) {
505 if (categoryIds.isEmpty()) {
506 return StringPool.BLANK;
507 }
508
509 StringBundler sb = new StringBundler(categoryIds.size() * 2 - 1);
510
511 for (int i = 0; i < categoryIds.size(); i++) {
512 sb.append("categoryId = ? ");
513
514 if ((i + 1) != categoryIds.size()) {
515 sb.append("OR ");
516 }
517 }
518
519 return sb.toString();
520 }
521
522 }