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