1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
31  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
32  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
33  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
34  import com.liferay.util.dao.orm.CustomSQLUtil;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="DLFileEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class DLFileEntryFinderImpl
46      extends BasePersistenceImpl implements DLFileEntryFinder {
47  
48      public static String COUNT_BY_FOLDER_IDS =
49          DLFileEntryFinder.class.getName() + ".countByFolderIds";
50  
51      public static String COUNT_BY_GROUP_ID =
52          DLFileEntryFinder.class.getName() + ".countByGroupId";
53  
54      public static String COUNT_BY_G_U =
55          DLFileEntryFinder.class.getName() + ".countByG_U";
56  
57      public static String FIND_BY_GROUP_ID =
58          DLFileEntryFinder.class.getName() + ".findByGroupId";
59  
60      public static String FIND_BY_NO_ASSETS =
61          DLFileEntryFinder.class.getName() + ".findByNoAssets";
62  
63      public static String FIND_BY_UUID_G =
64          DLFileEntryFinder.class.getName() + ".findByUuid_G";
65  
66      public static String FIND_BY_G_U =
67          DLFileEntryFinder.class.getName() + ".findByG_U";
68  
69      public int countByFolderIds(List<Long> folderIds) throws SystemException {
70          Session session = null;
71  
72          try {
73              session = openSession();
74  
75              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
76  
77              sql = StringUtil.replace(
78                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
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              for (int i = 0; i < folderIds.size(); i++) {
87                  Long folderId = folderIds.get(i);
88  
89                  qPos.add(folderId);
90              }
91  
92              Iterator<Long> itr = q.list().iterator();
93  
94              if (itr.hasNext()) {
95                  Long count = itr.next();
96  
97                  if (count != null) {
98                      return count.intValue();
99                  }
100             }
101 
102             return 0;
103         }
104         catch (Exception e) {
105             throw new SystemException(e);
106         }
107         finally {
108             closeSession(session);
109         }
110     }
111 
112     public int countByGroupId(long groupId) throws SystemException {
113         Session session = null;
114 
115         try {
116             session = openSession();
117 
118             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
119 
120             SQLQuery q = session.createSQLQuery(sql);
121 
122             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
123 
124             QueryPos qPos = QueryPos.getInstance(q);
125 
126             qPos.add(groupId);
127 
128             Iterator<Long> itr = q.list().iterator();
129 
130             if (itr.hasNext()) {
131                 Long count = itr.next();
132 
133                 if (count != null) {
134                     return count.intValue();
135                 }
136             }
137 
138             return 0;
139         }
140         catch (Exception e) {
141             throw new SystemException(e);
142         }
143         finally {
144             closeSession(session);
145         }
146     }
147 
148     public int countByG_U(long groupId, long userId) throws SystemException {
149         Session session = null;
150 
151         try {
152             session = openSession();
153 
154             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
155 
156             SQLQuery q = session.createSQLQuery(sql);
157 
158             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
159 
160             QueryPos qPos = QueryPos.getInstance(q);
161 
162             qPos.add(groupId);
163             qPos.add(userId);
164 
165             Iterator<Long> itr = q.list().iterator();
166 
167             if (itr.hasNext()) {
168                 Long count = itr.next();
169 
170                 if (count != null) {
171                     return count.intValue();
172                 }
173             }
174 
175             return 0;
176         }
177         catch (Exception e) {
178             throw new SystemException(e);
179         }
180         finally {
181             closeSession(session);
182         }
183     }
184 
185     public List<DLFileEntry> findByGroupId(long groupId, int start, int end)
186         throws SystemException {
187 
188         return findByGroupId(groupId, start, end, null);
189     }
190 
191     public List<DLFileEntry> findByGroupId(
192             long groupId, int start, int end, OrderByComparator obc)
193         throws SystemException {
194 
195         Session session = null;
196 
197         try {
198             session = openSession();
199 
200             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
201 
202             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
203 
204             SQLQuery q = session.createSQLQuery(sql);
205 
206             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
207 
208             QueryPos qPos = QueryPos.getInstance(q);
209 
210             qPos.add(groupId);
211 
212             return (List<DLFileEntry>)QueryUtil.list(
213                 q, getDialect(), start, end);
214         }
215         catch (Exception e) {
216             throw new SystemException(e);
217         }
218         finally {
219             closeSession(session);
220         }
221     }
222 
223     public List<DLFileEntry> findByNoAssets() throws SystemException {
224         Session session = null;
225 
226         try {
227             session = openSession();
228 
229             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
230 
231             SQLQuery q = session.createSQLQuery(sql);
232 
233             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
234 
235             return q.list();
236         }
237         catch (Exception e) {
238             throw new SystemException(e);
239         }
240         finally {
241             closeSession(session);
242         }
243     }
244 
245     public List<DLFileEntry> findByG_U(
246             long groupId, long userId, int start, int end)
247         throws SystemException {
248 
249         return findByG_U(groupId, userId, start, end, null);
250     }
251 
252     public List<DLFileEntry> findByG_U(
253             long groupId, long userId, int start, int end,
254             OrderByComparator obc)
255         throws SystemException {
256 
257         Session session = null;
258 
259         try {
260             session = openSession();
261 
262             String sql = CustomSQLUtil.get(FIND_BY_G_U);
263 
264             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
265 
266             SQLQuery q = session.createSQLQuery(sql);
267 
268             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
269 
270             QueryPos qPos = QueryPos.getInstance(q);
271 
272             qPos.add(groupId);
273             qPos.add(userId);
274 
275             return (List<DLFileEntry>)QueryUtil.list(
276                 q, getDialect(), start, end);
277         }
278         catch (Exception e) {
279             throw new SystemException(e);
280         }
281         finally {
282             closeSession(session);
283         }
284     }
285 
286     public DLFileEntry findByUuid_G(String uuid, long groupId)
287         throws NoSuchFileEntryException, SystemException {
288 
289         Session session = null;
290 
291         try {
292             session = openSession();
293 
294             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
295 
296             SQLQuery q = session.createSQLQuery(sql);
297 
298             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
299 
300             QueryPos qPos = QueryPos.getInstance(q);
301 
302             qPos.add(uuid);
303             qPos.add(groupId);
304 
305             List<DLFileEntry> list = q.list();
306 
307             if (list.size() == 0) {
308                 StringBuilder sb = new StringBuilder();
309 
310                 sb.append("No DLFileEntry exists with the key {uuid=");
311                 sb.append(uuid);
312                 sb.append(", groupId=");
313                 sb.append(groupId);
314                 sb.append("}");
315 
316                 throw new NoSuchFileEntryException(sb.toString());
317             }
318             else {
319                 return list.get(0);
320             }
321         }
322         catch (NoSuchFileEntryException nsfee) {
323             throw nsfee;
324         }
325         catch (Exception e) {
326             throw new SystemException(e);
327         }
328         finally {
329             closeSession(session);
330         }
331     }
332 
333     protected String getFolderIds(List<Long> folderIds) {
334         StringBuilder sb = new StringBuilder();
335 
336         for (int i = 0; i < folderIds.size(); i++) {
337             sb.append("folderId = ? ");
338 
339             if ((i + 1) != folderIds.size()) {
340                 sb.append("OR ");
341             }
342         }
343 
344         return sb.toString();
345     }
346 
347 }