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