1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
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.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
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.service.persistence.impl.BasePersistenceImpl;
27  import com.liferay.portlet.documentlibrary.model.DLFolder;
28  import com.liferay.util.dao.orm.CustomSQLUtil;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * <a href="DLFolderFinderImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class DLFolderFinderImpl
40      extends BasePersistenceImpl<DLFolder> implements DLFolderFinder {
41  
42      public static String COUNT_FE_FS_BY_FOLDER_IDS =
43          DLFolderFinder.class.getName() + ".countFE_FS_ByFolderIds";
44  
45      public static String COUNT_F_FE_FS_BY_FOLDER_IDS =
46          DLFolderFinder.class.getName() + ".countF_FE_FS_ByFolderIds";
47  
48      public static String FIND_FE_FS_BY_FOLDER_IDS =
49          DLFolderFinder.class.getName() + ".findFE_FS_ByFolderIds";
50  
51      public static String FIND_F_FE_FS_BY_FOLDER_IDS =
52          DLFolderFinder.class.getName() + ".findF_FE_FS_ByFolderIds";
53  
54      public int countFE_FS_ByFolderIds(List<Long> folderIds)
55          throws SystemException {
56  
57          Session session = null;
58  
59          try {
60              session = openSession();
61  
62              String sql = CustomSQLUtil.get(COUNT_FE_FS_BY_FOLDER_IDS);
63  
64              sql = StringUtil.replace(
65                  sql, "[$FILE_ENTRY_FOLDER_ID$]",
66                  getFolderIds(folderIds, "DLFileEntry"));
67              sql = StringUtil.replace(
68                  sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
69                  getFolderIds(folderIds, "DLFileShortcut"));
70  
71              SQLQuery q = session.createSQLQuery(sql);
72  
73              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
74  
75              QueryPos qPos = QueryPos.getInstance(q);
76  
77              for (Long folderId : folderIds) {
78                  qPos.add(folderId);
79              }
80  
81              for (Long folderId : folderIds) {
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 int countF_FE_FS_ByFolderIds(List<Long> folderIds)
108         throws SystemException {
109 
110         Session session = null;
111 
112         try {
113             session = openSession();
114 
115             String sql = CustomSQLUtil.get(COUNT_F_FE_FS_BY_FOLDER_IDS);
116 
117             sql = StringUtil.replace(
118                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
119                 getFolderIds(folderIds, "DLFolder"));
120             sql = StringUtil.replace(
121                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
122                 getFolderIds(folderIds, "DLFileEntry"));
123             sql = StringUtil.replace(
124                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
125                 getFolderIds(folderIds, "DLFileShortcut"));
126 
127             SQLQuery q = session.createSQLQuery(sql);
128 
129             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
130 
131             QueryPos qPos = QueryPos.getInstance(q);
132 
133             for (Long folderId : folderIds) {
134                 qPos.add(folderId);
135             }
136 
137             for (Long folderId : folderIds) {
138                 qPos.add(folderId);
139             }
140 
141             for (Long folderId : folderIds) {
142                 qPos.add(folderId);
143             }
144 
145             int count = 0;
146 
147             Iterator<Long> itr = q.list().iterator();
148 
149             while (itr.hasNext()) {
150                 Long l = itr.next();
151 
152                 if (l != null) {
153                     count += l.intValue();
154                 }
155             }
156 
157             return count;
158         }
159         catch (Exception e) {
160             throw new SystemException(e);
161         }
162         finally {
163             closeSession(session);
164         }
165     }
166 
167     public List<Object> findFE_FS_ByFolderIds(
168             List<Long> folderIds, int start, int end)
169         throws SystemException {
170 
171         Session session = null;
172 
173         try {
174             session = openSession();
175 
176             String sql = CustomSQLUtil.get(FIND_FE_FS_BY_FOLDER_IDS);
177 
178             sql = StringUtil.replace(
179                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
180                 getFolderIds(folderIds, "DLFileEntry"));
181             sql = StringUtil.replace(
182                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
183                 getFolderIds(folderIds, "DLFileShortcut"));
184 
185             SQLQuery q = session.createSQLQuery(sql);
186 
187             q.addScalar("modelFolderId", Type.LONG);
188             q.addScalar("name", Type.STRING);
189             q.addScalar("title", Type.STRING);
190             q.addScalar("fileShortcutId", Type.LONG);
191 
192             QueryPos qPos = QueryPos.getInstance(q);
193 
194             for (Long folderId : folderIds) {
195                 qPos.add(folderId);
196             }
197 
198             for (Long folderId : folderIds) {
199                 qPos.add(folderId);
200             }
201 
202             List<Object> models = new ArrayList<Object>();
203 
204             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
205                 q, getDialect(), start, end);
206 
207             while (itr.hasNext()) {
208                 Object[] array = itr.next();
209 
210                 long folderId = (Long)array[0];
211                 String name = (String)array[1];
212                 //String title = (String)array[2];
213                 long fileShortcutId = (Long)array[3];
214 
215                 Object obj = null;
216 
217                 if (fileShortcutId > 0) {
218                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
219                 }
220                 else {
221                     obj = DLFileEntryUtil.findByF_N(folderId, name);
222                 }
223 
224                 models.add(obj);
225             }
226 
227             return models;
228         }
229         catch (Exception e) {
230             throw new SystemException(e);
231         }
232         finally {
233             closeSession(session);
234         }
235     }
236 
237     public List<Object> findF_FE_FS_ByFolderIds(
238             List<Long> folderIds, int start, int end)
239         throws SystemException {
240 
241         Session session = null;
242 
243         try {
244             session = openSession();
245 
246             String sql = CustomSQLUtil.get(FIND_F_FE_FS_BY_FOLDER_IDS);
247 
248             sql = StringUtil.replace(
249                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
250                 getFolderIds(folderIds, "DLFolder"));
251             sql = StringUtil.replace(
252                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
253                 getFolderIds(folderIds, "DLFileEntry"));
254             sql = StringUtil.replace(
255                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
256                 getFolderIds(folderIds, "DLFileShortcut"));
257 
258             SQLQuery q = session.createSQLQuery(sql);
259 
260             q.addScalar("modelFolderId", Type.LONG);
261             q.addScalar("name", Type.STRING);
262             q.addScalar("title", Type.STRING);
263             q.addScalar("fileShortcutId", Type.LONG);
264             q.addScalar("modelFolder", Type.LONG);
265 
266             QueryPos qPos = QueryPos.getInstance(q);
267 
268             for (Long folderId : folderIds) {
269                 qPos.add(folderId);
270             }
271 
272             for (Long folderId : folderIds) {
273                 qPos.add(folderId);
274             }
275 
276             for (Long folderId : folderIds) {
277                 qPos.add(folderId);
278             }
279 
280             List<Object> models = new ArrayList<Object>();
281 
282             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
283                 q, getDialect(), start, end);
284 
285             while (itr.hasNext()) {
286                 Object[] array = itr.next();
287 
288                 long folderId = (Long)array[0];
289                 String name = (String)array[1];
290                 //String title = (String)array[2];
291                 long fileShortcutId = (Long)array[3];
292                 long modelFolder = (Long)array[4];
293 
294                 Object obj = null;
295 
296                 if (modelFolder == 1) {
297                     obj = DLFolderUtil.findByPrimaryKey(folderId);
298                 }
299                 else if (fileShortcutId > 0) {
300                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
301                 }
302                 else {
303                     obj = DLFileEntryUtil.findByF_N(folderId, name);
304                 }
305 
306                 models.add(obj);
307             }
308 
309             return models;
310         }
311         catch (Exception e) {
312             throw new SystemException(e);
313         }
314         finally {
315             closeSession(session);
316         }
317     }
318 
319     protected String getFolderIds(List<Long> folderIds, String table) {
320         if (folderIds.isEmpty()) {
321             return StringPool.BLANK;
322         }
323 
324         StringBundler sb = new StringBundler(folderIds.size() * 5 - 1);
325 
326         for (int i = 0; i < folderIds.size(); i++) {
327             sb.append(table);
328             sb.append(".");
329 
330             if (table.equals("DLFolder")) {
331                 sb.append("parentFolderId");
332             }
333             else {
334                 sb.append("folderId");
335             }
336 
337             sb.append("= ? ");
338 
339             if ((i + 1) != folderIds.size()) {
340                 sb.append("OR ");
341             }
342         }
343 
344         return sb.toString();
345     }
346 
347 }