1
14
15 package com.liferay.portlet.documentlibrary.service.persistence;
16
17 import com.liferay.portal.SystemException;
18 import com.liferay.portal.kernel.dao.orm.QueryPos;
19 import com.liferay.portal.kernel.dao.orm.SQLQuery;
20 import com.liferay.portal.kernel.dao.orm.Session;
21 import com.liferay.portal.kernel.dao.orm.Type;
22 import com.liferay.portal.kernel.util.StringBundler;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.kernel.util.StringUtil;
25 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
26 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
27 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
28 import com.liferay.util.dao.orm.CustomSQLUtil;
29
30 import java.util.Iterator;
31 import java.util.List;
32
33
38 public class DLFileEntryFinderImpl
39 extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
40
41 public static String COUNT_BY_FOLDER_IDS =
42 DLFileEntryFinder.class.getName() + ".countByFolderIds";
43
44 public static String FIND_BY_NO_ASSETS =
45 DLFileEntryFinder.class.getName() + ".findByNoAssets";
46
47 public int countByFolderIds(List<Long> folderIds) throws SystemException {
48 Session session = null;
49
50 try {
51 session = openSession();
52
53 String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
54
55 sql = StringUtil.replace(
56 sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
57
58 SQLQuery q = session.createSQLQuery(sql);
59
60 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
61
62 QueryPos qPos = QueryPos.getInstance(q);
63
64 for (int i = 0; i < folderIds.size(); i++) {
65 Long folderId = folderIds.get(i);
66
67 qPos.add(folderId);
68 }
69
70 Iterator<Long> itr = q.list().iterator();
71
72 if (itr.hasNext()) {
73 Long count = itr.next();
74
75 if (count != null) {
76 return count.intValue();
77 }
78 }
79
80 return 0;
81 }
82 catch (Exception e) {
83 throw new SystemException(e);
84 }
85 finally {
86 closeSession(session);
87 }
88 }
89
90 public List<DLFileEntry> findByNoAssets() throws SystemException {
91 Session session = null;
92
93 try {
94 session = openSession();
95
96 String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
97
98 SQLQuery q = session.createSQLQuery(sql);
99
100 q.addEntity("DLFileEntry", DLFileEntryImpl.class);
101
102 return q.list();
103 }
104 catch (Exception e) {
105 throw new SystemException(e);
106 }
107 finally {
108 closeSession(session);
109 }
110 }
111
112 protected String getFolderIds(List<Long> folderIds) {
113 if (folderIds.isEmpty()) {
114 return StringPool.BLANK;
115 }
116
117 StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
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 }