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.SQLQuery;
19  import com.liferay.portal.kernel.dao.orm.Session;
20  import com.liferay.portal.kernel.dao.orm.Type;
21  import com.liferay.portal.kernel.exception.SystemException;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.workflow.StatusConstants;
26  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
27  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
28  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
29  import com.liferay.util.dao.orm.CustomSQLUtil;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * <a href="DLFileEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class DLFileEntryFinderImpl
40      extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
41  
42      public static String COUNT_BY_G_F_S =
43          DLFileEntryFinder.class.getName() + ".countByG_F_S";
44  
45      public static String FIND_BY_NO_ASSETS =
46          DLFileEntryFinder.class.getName() + ".findByNoAssets";
47  
48      public int countByG_F_S(long groupId, List<Long> folderIds, int status)
49          throws SystemException {
50  
51          Session session = null;
52  
53          try {
54              session = openSession();
55  
56              String sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
57  
58              sql = StringUtil.replace(
59                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
60  
61              if (status == StatusConstants.ANY) {
62                  sql = StringUtil.replace(
63                      sql, "(DLFileVersion.status = ?) AND", "");
64              }
65  
66              SQLQuery q = session.createSQLQuery(sql);
67  
68              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
69  
70              QueryPos qPos = QueryPos.getInstance(q);
71  
72              qPos.add(groupId);
73  
74              if (status != StatusConstants.ANY) {
75                  qPos.add(status);
76              }
77  
78              for (int i = 0; i < folderIds.size(); i++) {
79                  Long folderId = folderIds.get(i);
80  
81                  qPos.add(folderId);
82              }
83  
84              Iterator<Long> itr = q.list().iterator();
85  
86              if (itr.hasNext()) {
87                  Long count = itr.next();
88  
89                  if (count != null) {
90                      return count.intValue();
91                  }
92              }
93  
94              return 0;
95          }
96          catch (Exception e) {
97              throw new SystemException(e);
98          }
99          finally {
100             closeSession(session);
101         }
102     }
103 
104     public List<DLFileEntry> findByNoAssets() throws SystemException {
105         Session session = null;
106 
107         try {
108             session = openSession();
109 
110             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
111 
112             SQLQuery q = session.createSQLQuery(sql);
113 
114             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
115 
116             return q.list();
117         }
118         catch (Exception e) {
119             throw new SystemException(e);
120         }
121         finally {
122             closeSession(session);
123         }
124     }
125 
126     protected String getFolderIds(List<Long> folderIds) {
127         if (folderIds.isEmpty()) {
128             return StringPool.BLANK;
129         }
130 
131         StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
132 
133         for (int i = 0; i < folderIds.size(); i++) {
134             sb.append("DLFileEntry.folderId = ? ");
135 
136             if ((i + 1) != folderIds.size()) {
137                 sb.append("OR ");
138             }
139         }
140 
141         return sb.toString();
142     }
143 
144 }