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