1
19
20 package com.liferay.portlet.imagegallery.service.persistence;
21
22 import com.liferay.portal.SystemException;
23 import com.liferay.portal.kernel.dao.orm.QueryPos;
24 import com.liferay.portal.kernel.dao.orm.SQLQuery;
25 import com.liferay.portal.kernel.dao.orm.Session;
26 import com.liferay.portal.kernel.dao.orm.Type;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
29 import com.liferay.portlet.imagegallery.model.IGImage;
30 import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
31 import com.liferay.util.dao.orm.CustomSQLUtil;
32
33 import java.util.Iterator;
34 import java.util.List;
35
36
42 public class IGImageFinderImpl
43 extends BasePersistenceImpl implements IGImageFinder {
44
45 public static String COUNT_BY_FOLDER_IDS =
46 IGImageFinder.class.getName() + ".countByFolderIds";
47
48 public static String FIND_BY_NO_ASSETS =
49 IGImageFinder.class.getName() + ".findByNoAssets";
50
51 public int countByFolderIds(List<Long> folderIds) throws SystemException {
52 Session session = null;
53
54 try {
55 session = openSession();
56
57 String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
58
59 sql = StringUtil.replace(
60 sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
61
62 SQLQuery q = session.createSQLQuery(sql);
63
64 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
65
66 QueryPos qPos = QueryPos.getInstance(q);
67
68 for (int i = 0; i < folderIds.size(); i++) {
69 Long folderId = folderIds.get(i);
70
71 qPos.add(folderId);
72 }
73
74 Iterator<Long> itr = q.list().iterator();
75
76 if (itr.hasNext()) {
77 Long count = itr.next();
78
79 if (count != null) {
80 return count.intValue();
81 }
82 }
83
84 return 0;
85 }
86 catch (Exception e) {
87 throw new SystemException(e);
88 }
89 finally {
90 closeSession(session);
91 }
92 }
93
94 public List<IGImage> findByNoAssets() throws SystemException {
95 Session session = null;
96
97 try {
98 session = openSession();
99
100 String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
101
102 SQLQuery q = session.createSQLQuery(sql);
103
104 q.addEntity("IGImage", IGImageImpl.class);
105
106 return q.list();
107 }
108 catch (Exception e) {
109 throw new SystemException(e);
110 }
111 finally {
112 closeSession(session);
113 }
114 }
115
116 protected String getFolderIds(List<Long> folderIds) {
117 StringBuilder sb = new StringBuilder();
118
119 for (int i = 0; i < folderIds.size(); i++) {
120 sb.append("folderId = ? ");
121
122 if ((i + 1) != folderIds.size()) {
123 sb.append("OR ");
124 }
125 }
126
127 return sb.toString();
128 }
129
130 }