1
19
20 package com.liferay.portlet.documentlibrary.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.QueryUtil;
25 import com.liferay.portal.kernel.dao.orm.SQLQuery;
26 import com.liferay.portal.kernel.dao.orm.Session;
27 import com.liferay.portal.kernel.dao.orm.Type;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
30 import com.liferay.util.dao.orm.CustomSQLUtil;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36
43 public class DLFileEntryAndShortcutFinderImpl
44 extends BasePersistenceImpl implements DLFileEntryAndShortcutFinder {
45
46 public static String COUNT_BY_FOLDER_IDS =
47 DLFileEntryAndShortcutFinder.class.getName() + ".countByFolderIds";
48
49 public static String FIND_BY_FOLDER_IDS =
50 DLFileEntryAndShortcutFinder.class.getName() + ".findByFolderIds";
51
52 public int countByFolderIds(List<Long> folderIds) throws SystemException {
53 Session session = null;
54
55 try {
56 session = openSession();
57
58 String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
59
60 sql = StringUtil.replace(
61 sql, "[$FILE_ENTRY_FOLDER_ID$]",
62 getFolderIds(folderIds, "DLFileEntry"));
63 sql = StringUtil.replace(
64 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
65 getFolderIds(folderIds, "DLFileShortcut"));
66
67 SQLQuery q = session.createSQLQuery(sql);
68
69 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
70
71 QueryPos qPos = QueryPos.getInstance(q);
72
73 for (int i = 0; i < folderIds.size(); i++) {
74 Long folderId = folderIds.get(i);
75
76 qPos.add(folderId);
77 }
78
79 for (int i = 0; i < folderIds.size(); i++) {
80 Long folderId = folderIds.get(i);
81
82 qPos.add(folderId);
83 }
84
85 int count = 0;
86
87 Iterator<Long> itr = q.list().iterator();
88
89 while (itr.hasNext()) {
90 Long l = itr.next();
91
92 if (l != null) {
93 count += l.intValue();
94 }
95 }
96
97 return count;
98 }
99 catch (Exception e) {
100 throw new SystemException(e);
101 }
102 finally {
103 closeSession(session);
104 }
105 }
106
107 public List<Object> findByFolderIds(
108 List<Long> folderIds, int start, int end)
109 throws SystemException {
110
111 Session session = null;
112
113 try {
114 session = openSession();
115
116 String sql = CustomSQLUtil.get(FIND_BY_FOLDER_IDS);
117
118 sql = StringUtil.replace(
119 sql, "[$FILE_ENTRY_FOLDER_ID$]",
120 getFolderIds(folderIds, "DLFileEntry"));
121 sql = StringUtil.replace(
122 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
123 getFolderIds(folderIds, "DLFileShortcut"));
124
125 SQLQuery q = session.createSQLQuery(sql);
126
127 q.addScalar("modelFolderId", Type.LONG);
128 q.addScalar("name", Type.STRING);
129 q.addScalar("title", Type.STRING);
130 q.addScalar("fileShortcutId", Type.LONG);
131
132 QueryPos qPos = QueryPos.getInstance(q);
133
134 for (int i = 0; i < folderIds.size(); i++) {
135 Long folderId = folderIds.get(i);
136
137 qPos.add(folderId);
138 }
139
140 for (int i = 0; i < folderIds.size(); i++) {
141 Long folderId = folderIds.get(i);
142
143 qPos.add(folderId);
144 }
145
146 List<Object> fileEntriesAndShortcuts = new ArrayList<Object>();
147
148 Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
149 q, getDialect(), start, end);
150
151 while (itr.hasNext()) {
152 Object[] array = itr.next();
153
154 Long folderId = (Long)array[0];
155 String name = (String)array[1];
156 long fileShortcutId = ((Long)array[3]).longValue();
158
159 Object obj = null;
160
161 if (fileShortcutId > 0) {
162 obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
163 }
164 else {
165 obj = DLFileEntryUtil.findByF_N(folderId.longValue(), name);
166 }
167
168 fileEntriesAndShortcuts.add(obj);
169 }
170
171 return fileEntriesAndShortcuts;
172 }
173 catch (Exception e) {
174 throw new SystemException(e);
175 }
176 finally {
177 closeSession(session);
178 }
179 }
180
181 protected String getFolderIds(List<Long> folderIds, String table) {
182 StringBuilder sb = new StringBuilder();
183
184 for (int i = 0; i < folderIds.size(); i++) {
185 sb.append(table);
186 sb.append(".folderId = ? ");
187
188 if ((i + 1) != folderIds.size()) {
189 sb.append("OR ");
190 }
191 }
192
193 return sb.toString();
194 }
195
196 }