1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.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.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
33  import com.liferay.util.dao.orm.CustomSQLUtil;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="DLFolderFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class DLFolderFinderImpl
46      extends BasePersistenceImpl implements DLFolderFinder {
47  
48      public static String COUNT_FE_FS_BY_FOLDER_IDS =
49          DLFolderFinder.class.getName() + ".countFE_FS_ByFolderIds";
50  
51      public static String COUNT_F_FE_FS_BY_FOLDER_IDS =
52          DLFolderFinder.class.getName() + ".countF_FE_FS_ByFolderIds";
53  
54      public static String FIND_FE_FS_BY_FOLDER_IDS =
55          DLFolderFinder.class.getName() + ".findFE_FS_ByFolderIds";
56  
57      public static String FIND_F_FE_FS_BY_FOLDER_IDS =
58          DLFolderFinder.class.getName() + ".findF_FE_FS_ByFolderIds";
59  
60      public int countFE_FS_ByFolderIds(List<Long> folderIds)
61          throws SystemException {
62  
63          Session session = null;
64  
65          try {
66              session = openSession();
67  
68              String sql = CustomSQLUtil.get(COUNT_FE_FS_BY_FOLDER_IDS);
69  
70              sql = StringUtil.replace(
71                  sql, "[$FILE_ENTRY_FOLDER_ID$]",
72                  getFolderIds(folderIds, "DLFileEntry"));
73              sql = StringUtil.replace(
74                  sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
75                  getFolderIds(folderIds, "DLFileShortcut"));
76  
77              SQLQuery q = session.createSQLQuery(sql);
78  
79              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
80  
81              QueryPos qPos = QueryPos.getInstance(q);
82  
83              for (Long folderId : folderIds) {
84                  qPos.add(folderId);
85              }
86  
87              for (Long folderId : folderIds) {
88                  qPos.add(folderId);
89              }
90  
91              int count = 0;
92  
93              Iterator<Long> itr = q.list().iterator();
94  
95              while (itr.hasNext()) {
96                  Long l = itr.next();
97  
98                  if (l != null) {
99                      count += l.intValue();
100                 }
101             }
102 
103             return count;
104         }
105         catch (Exception e) {
106             throw new SystemException(e);
107         }
108         finally {
109             closeSession(session);
110         }
111     }
112 
113     public int countF_FE_FS_ByFolderIds(List<Long> folderIds)
114         throws SystemException {
115 
116         Session session = null;
117 
118         try {
119             session = openSession();
120 
121             String sql = CustomSQLUtil.get(COUNT_F_FE_FS_BY_FOLDER_IDS);
122 
123             sql = StringUtil.replace(
124                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
125                 getFolderIds(folderIds, "DLFolder"));
126             sql = StringUtil.replace(
127                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
128                 getFolderIds(folderIds, "DLFileEntry"));
129             sql = StringUtil.replace(
130                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
131                 getFolderIds(folderIds, "DLFileShortcut"));
132 
133             SQLQuery q = session.createSQLQuery(sql);
134 
135             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
136 
137             QueryPos qPos = QueryPos.getInstance(q);
138 
139             for (Long folderId : folderIds) {
140                 qPos.add(folderId);
141             }
142 
143             for (Long folderId : folderIds) {
144                 qPos.add(folderId);
145             }
146 
147             for (Long folderId : folderIds) {
148                 qPos.add(folderId);
149             }
150 
151             int count = 0;
152 
153             Iterator<Long> itr = q.list().iterator();
154 
155             while (itr.hasNext()) {
156                 Long l = itr.next();
157 
158                 if (l != null) {
159                     count += l.intValue();
160                 }
161             }
162 
163             return count;
164         }
165         catch (Exception e) {
166             throw new SystemException(e);
167         }
168         finally {
169             closeSession(session);
170         }
171     }
172 
173     public List<Object> findFE_FS_ByFolderIds(
174             List<Long> folderIds, int start, int end)
175         throws SystemException {
176 
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             String sql = CustomSQLUtil.get(FIND_FE_FS_BY_FOLDER_IDS);
183 
184             sql = StringUtil.replace(
185                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
186                 getFolderIds(folderIds, "DLFileEntry"));
187             sql = StringUtil.replace(
188                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
189                 getFolderIds(folderIds, "DLFileShortcut"));
190 
191             SQLQuery q = session.createSQLQuery(sql);
192 
193             q.addScalar("folderId", Type.LONG);
194             q.addScalar("name", Type.STRING);
195             q.addScalar("title", Type.STRING);
196             q.addScalar("fileShortcutId", Type.LONG);
197 
198             QueryPos qPos = QueryPos.getInstance(q);
199 
200             for (Long folderId : folderIds) {
201                 qPos.add(folderId);
202             }
203 
204             for (Long folderId : folderIds) {
205                 qPos.add(folderId);
206             }
207 
208             List<Object> models = new ArrayList<Object>();
209 
210             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
211                 q, getDialect(), start, end);
212 
213             while (itr.hasNext()) {
214                 Object[] array = itr.next();
215 
216                 long folderId = (Long)array[0];
217                 String name = (String)array[1];
218                 //String title = (String)array[2];
219                 long fileShortcutId = (Long)array[3];
220 
221                 Object obj = null;
222 
223                 if (fileShortcutId > 0) {
224                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
225                 }
226                 else {
227                     obj = DLFileEntryUtil.findByF_N(folderId, name);
228                 }
229 
230                 models.add(obj);
231             }
232 
233             return models;
234         }
235         catch (Exception e) {
236             throw new SystemException(e);
237         }
238         finally {
239             closeSession(session);
240         }
241     }
242 
243     public List<Object> findF_FE_FS_ByFolderIds(
244             List<Long> folderIds, int start, int end)
245         throws SystemException {
246 
247         Session session = null;
248 
249         try {
250             session = openSession();
251 
252             String sql = CustomSQLUtil.get(FIND_F_FE_FS_BY_FOLDER_IDS);
253 
254             sql = StringUtil.replace(
255                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
256                 getFolderIds(folderIds, "DLFolder"));
257             sql = StringUtil.replace(
258                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
259                 getFolderIds(folderIds, "DLFileEntry"));
260             sql = StringUtil.replace(
261                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
262                 getFolderIds(folderIds, "DLFileShortcut"));
263 
264             SQLQuery q = session.createSQLQuery(sql);
265 
266             q.addScalar("folderId", Type.LONG);
267             q.addScalar("name", Type.STRING);
268             q.addScalar("title", Type.STRING);
269             q.addScalar("fileShortcutId", Type.LONG);
270             q.addScalar("modelFolder", Type.LONG);
271 
272             QueryPos qPos = QueryPos.getInstance(q);
273 
274             for (Long folderId : folderIds) {
275                 qPos.add(folderId);
276             }
277 
278             for (Long folderId : folderIds) {
279                 qPos.add(folderId);
280             }
281 
282             for (Long folderId : folderIds) {
283                 qPos.add(folderId);
284             }
285 
286             List<Object> models = new ArrayList<Object>();
287 
288             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
289                 q, getDialect(), start, end);
290 
291             while (itr.hasNext()) {
292                 Object[] array = itr.next();
293 
294                 long folderId = (Long)array[0];
295                 String name = (String)array[1];
296                 //String title = (String)array[2];
297                 long fileShortcutId = (Long)array[3];
298                 long modelFolder = (Long)array[4];
299 
300                 Object obj = null;
301 
302                 if (modelFolder == 1) {
303                     obj = DLFolderUtil.findByPrimaryKey(folderId);
304                 }
305                 else if (fileShortcutId > 0) {
306                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
307                 }
308                 else {
309                     obj = DLFileEntryUtil.findByF_N(folderId, name);
310                 }
311 
312                 models.add(obj);
313             }
314 
315             return models;
316         }
317         catch (Exception e) {
318             throw new SystemException(e);
319         }
320         finally {
321             closeSession(session);
322         }
323     }
324 
325     protected String getFolderIds(List<Long> folderIds, String table) {
326         StringBuilder sb = new StringBuilder();
327 
328         for (int i = 0; i < folderIds.size(); i++) {
329             sb.append(table);
330             sb.append(".");
331 
332             if (table.equals("DLFolder")) {
333                 sb.append("parentFolderId");
334             }
335             else {
336                 sb.append("folderId");
337             }
338 
339             sb.append("= ? ");
340 
341             if ((i + 1) != folderIds.size()) {
342                 sb.append("OR ");
343             }
344         }
345 
346         return sb.toString();
347     }
348 
349 }