1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
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.exception.SystemException;
23  import com.liferay.portal.kernel.util.StringBundler;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.workflow.StatusConstants;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.documentlibrary.model.DLFolder;
29  import com.liferay.util.dao.orm.CustomSQLUtil;
30  
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="DLFolderFinderImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class DLFolderFinderImpl
41      extends BasePersistenceImpl<DLFolder> implements DLFolderFinder {
42  
43      public static String COUNT_FE_FS_BY_G_F_S =
44          DLFolderFinder.class.getName() + ".countFE_FS_ByG_F_S";
45  
46      public static String COUNT_F_FE_FS_BY_G_F_S =
47          DLFolderFinder.class.getName() + ".countF_FE_FS_ByG_F_S";
48  
49      public static String FIND_FE_FS_BY_G_F_S =
50          DLFolderFinder.class.getName() + ".findFE_FS_ByG_F_S";
51  
52      public static String FIND_F_FE_FS_BY_G_F_S =
53          DLFolderFinder.class.getName() + ".findF_FE_FS_ByG_F_S";
54  
55      public int countFE_FS_ByG_F_S(
56              long groupId, List<Long> folderIds, int status)
57          throws SystemException {
58  
59          Session session = null;
60  
61          try {
62              session = openSession();
63  
64              String sql = CustomSQLUtil.get(COUNT_FE_FS_BY_G_F_S);
65  
66              sql = StringUtil.replace(
67                  sql, "[$FILE_ENTRY_FOLDER_ID$]",
68                  getFolderIds(folderIds, "DLFileEntry"));
69              sql = StringUtil.replace(
70                  sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
71                  getFolderIds(folderIds, "DLFileShortcut"));
72  
73              if (status != StatusConstants.APPROVED) {
74                  sql = StringUtil.replace(
75                      sql, "(DLFileEntry.version > 0) AND", "");
76                  sql = StringUtil.replace(
77                      sql, "(DLFileShortcut.status = 0) AND", "");
78              }
79  
80              SQLQuery q = session.createSQLQuery(sql);
81  
82              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
83  
84              QueryPos qPos = QueryPos.getInstance(q);
85  
86              qPos.add(groupId);
87  
88              for (Long folderId : folderIds) {
89                  qPos.add(folderId);
90              }
91  
92              qPos.add(groupId);
93  
94              for (Long folderId : folderIds) {
95                  qPos.add(folderId);
96              }
97  
98              int count = 0;
99  
100             Iterator<Long> itr = q.list().iterator();
101 
102             while (itr.hasNext()) {
103                 Long l = itr.next();
104 
105                 if (l != null) {
106                     count += l.intValue();
107                 }
108             }
109 
110             return count;
111         }
112         catch (Exception e) {
113             throw new SystemException(e);
114         }
115         finally {
116             closeSession(session);
117         }
118     }
119 
120     public int countF_FE_FS_ByG_F_S(
121             long groupId, List<Long> folderIds, int status)
122         throws SystemException {
123 
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             String sql = CustomSQLUtil.get(COUNT_F_FE_FS_BY_G_F_S);
130 
131             sql = StringUtil.replace(
132                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
133                 getFolderIds(folderIds, "DLFolder"));
134             sql = StringUtil.replace(
135                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
136                 getFolderIds(folderIds, "DLFileEntry"));
137             sql = StringUtil.replace(
138                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
139                 getFolderIds(folderIds, "DLFileShortcut"));
140 
141             if (status != StatusConstants.APPROVED) {
142                 sql = StringUtil.replace(
143                     sql, "(DLFileEntry.version > 0) AND", "");
144                 sql = StringUtil.replace(
145                     sql, "(DLFileShortcut.status = 0) AND", "");
146             }
147 
148             SQLQuery q = session.createSQLQuery(sql);
149 
150             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
151 
152             QueryPos qPos = QueryPos.getInstance(q);
153 
154             qPos.add(groupId);
155 
156             for (Long folderId : folderIds) {
157                 qPos.add(folderId);
158             }
159 
160             qPos.add(groupId);
161 
162             for (Long folderId : folderIds) {
163                 qPos.add(folderId);
164             }
165 
166             qPos.add(groupId);
167 
168             for (Long folderId : folderIds) {
169                 qPos.add(folderId);
170             }
171 
172             int count = 0;
173 
174             Iterator<Long> itr = q.list().iterator();
175 
176             while (itr.hasNext()) {
177                 Long l = itr.next();
178 
179                 if (l != null) {
180                     count += l.intValue();
181                 }
182             }
183 
184             return count;
185         }
186         catch (Exception e) {
187             throw new SystemException(e);
188         }
189         finally {
190             closeSession(session);
191         }
192     }
193 
194     public List<Object> findFE_FS_ByG_F_S(
195             long groupId, List<Long> folderIds, int status, int start, int end)
196         throws SystemException {
197 
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             String sql = CustomSQLUtil.get(FIND_FE_FS_BY_G_F_S);
204 
205             sql = StringUtil.replace(
206                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
207                 getFolderIds(folderIds, "DLFileEntry"));
208             sql = StringUtil.replace(
209                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
210                 getFolderIds(folderIds, "DLFileShortcut"));
211 
212             if (status != StatusConstants.APPROVED) {
213                 sql = StringUtil.replace(
214                     sql, "(DLFileEntry.version > 0) AND", "");
215                 sql = StringUtil.replace(
216                     sql, "(DLFileShortcut.status = 0) AND", "");
217             }
218 
219             SQLQuery q = session.createSQLQuery(sql);
220 
221             q.addScalar("modelFolderId", Type.LONG);
222             q.addScalar("name", Type.STRING);
223             q.addScalar("title", Type.STRING);
224             q.addScalar("fileShortcutId", Type.LONG);
225 
226             QueryPos qPos = QueryPos.getInstance(q);
227 
228             qPos.add(groupId);
229 
230             for (Long folderId : folderIds) {
231                 qPos.add(folderId);
232             }
233 
234             qPos.add(groupId);
235 
236             for (Long folderId : folderIds) {
237                 qPos.add(folderId);
238             }
239 
240             List<Object> models = new ArrayList<Object>();
241 
242             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
243                 q, getDialect(), start, end);
244 
245             while (itr.hasNext()) {
246                 Object[] array = itr.next();
247 
248                 long folderId = (Long)array[0];
249                 String name = (String)array[1];
250                 //String title = (String)array[2];
251                 long fileShortcutId = (Long)array[3];
252 
253                 Object obj = null;
254 
255                 if (fileShortcutId > 0) {
256                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
257                 }
258                 else {
259                     obj = DLFileEntryUtil.findByG_F_N(groupId, folderId, name);
260                 }
261 
262                 models.add(obj);
263             }
264 
265             return models;
266         }
267         catch (Exception e) {
268             throw new SystemException(e);
269         }
270         finally {
271             closeSession(session);
272         }
273     }
274 
275     public List<Object> findF_FE_FS_ByG_F_S(
276             long groupId, List<Long> folderIds, int status, int start, int end)
277         throws SystemException {
278 
279         Session session = null;
280 
281         try {
282             session = openSession();
283 
284             String sql = CustomSQLUtil.get(FIND_F_FE_FS_BY_G_F_S);
285 
286             sql = StringUtil.replace(
287                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
288                 getFolderIds(folderIds, "DLFolder"));
289             sql = StringUtil.replace(
290                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
291                 getFolderIds(folderIds, "DLFileEntry"));
292             sql = StringUtil.replace(
293                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
294                 getFolderIds(folderIds, "DLFileShortcut"));
295 
296             if (status != StatusConstants.APPROVED) {
297                 sql = StringUtil.replace(
298                     sql, "(DLFileEntry.version > 0) AND", "");
299                 sql = StringUtil.replace(
300                     sql, "(DLFileShortcut.status = 0) AND", "");
301             }
302 
303             SQLQuery q = session.createSQLQuery(sql);
304 
305             q.addScalar("modelFolderId", Type.LONG);
306             q.addScalar("name", Type.STRING);
307             q.addScalar("title", Type.STRING);
308             q.addScalar("fileShortcutId", Type.LONG);
309             q.addScalar("modelFolder", Type.LONG);
310 
311             QueryPos qPos = QueryPos.getInstance(q);
312 
313             qPos.add(groupId);
314 
315             for (Long folderId : folderIds) {
316                 qPos.add(folderId);
317             }
318 
319             qPos.add(groupId);
320 
321             for (Long folderId : folderIds) {
322                 qPos.add(folderId);
323             }
324 
325             qPos.add(groupId);
326 
327             for (Long folderId : folderIds) {
328                 qPos.add(folderId);
329             }
330 
331             List<Object> models = new ArrayList<Object>();
332 
333             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
334                 q, getDialect(), start, end);
335 
336             while (itr.hasNext()) {
337                 Object[] array = itr.next();
338 
339                 long folderId = (Long)array[0];
340                 String name = (String)array[1];
341                 //String title = (String)array[2];
342                 long fileShortcutId = (Long)array[3];
343                 long modelFolder = (Long)array[4];
344 
345                 Object obj = null;
346 
347                 if (modelFolder == 1) {
348                     obj = DLFolderUtil.findByPrimaryKey(folderId);
349                 }
350                 else if (fileShortcutId > 0) {
351                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
352                 }
353                 else {
354                     obj = DLFileEntryUtil.findByG_F_N(groupId, folderId, name);
355                 }
356 
357                 models.add(obj);
358             }
359 
360             return models;
361         }
362         catch (Exception e) {
363             throw new SystemException(e);
364         }
365         finally {
366             closeSession(session);
367         }
368     }
369 
370     protected String getFolderIds(List<Long> folderIds, String table) {
371         if (folderIds.isEmpty()) {
372             return StringPool.BLANK;
373         }
374 
375         StringBundler sb = new StringBundler(folderIds.size() * 5 - 1);
376 
377         for (int i = 0; i < folderIds.size(); i++) {
378             sb.append(table);
379             sb.append(".");
380 
381             if (table.equals("DLFolder")) {
382                 sb.append("parentFolderId");
383             }
384             else {
385                 sb.append("folderId");
386             }
387 
388             sb.append("= ? ");
389 
390             if ((i + 1) != folderIds.size()) {
391                 sb.append("OR ");
392             }
393         }
394 
395         return sb.toString();
396     }
397 
398 }