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.WorkflowConstants;
26  import com.liferay.portal.security.permission.InlineSQLHelperUtil;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
29  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="DLFileEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class DLFileEntryFinderImpl
41      extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
42  
43      public static String COUNT_BY_G_F_S =
44          DLFileEntryFinder.class.getName() + ".countByG_F_S";
45  
46      public static String FIND_BY_NO_ASSETS =
47          DLFileEntryFinder.class.getName() + ".findByNoAssets";
48  
49      public int countByG_F_S(long groupId, List<Long> folderIds, int status)
50          throws SystemException {
51  
52          return doCountByG_F_S(groupId, folderIds, status, false);
53      }
54  
55      public int filterCountByG_F_S(
56              long groupId, List<Long> folderIds, int status)
57          throws SystemException {
58  
59          return doCountByG_F_S(groupId, folderIds, status, true);
60      }
61  
62      public List<DLFileEntry> findByNoAssets() throws SystemException {
63          Session session = null;
64  
65          try {
66              session = openSession();
67  
68              String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
69  
70              SQLQuery q = session.createSQLQuery(sql);
71  
72              q.addEntity("DLFileEntry", DLFileEntryImpl.class);
73  
74              return q.list();
75          }
76          catch (Exception e) {
77              throw new SystemException(e);
78          }
79          finally {
80              closeSession(session);
81          }
82      }
83  
84      protected int doCountByG_F_S(
85              long groupId, List<Long> folderIds, int status,
86              boolean inlineSQLHelper)
87          throws SystemException {
88  
89          Session session = null;
90  
91          try {
92              session = openSession();
93  
94              String sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
95  
96              if (inlineSQLHelper) {
97                  sql = InlineSQLHelperUtil.replacePermissionCheck(
98                      sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
99                      "DLFileEntry.userId", groupId);
100             }
101 
102             sql = StringUtil.replace(
103                 sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
104 
105             if (status == WorkflowConstants.STATUS_ANY) {
106                 sql = StringUtil.replace(
107                     sql, "(DLFileVersion.status = ?) AND", "");
108             }
109 
110             SQLQuery q = session.createSQLQuery(sql);
111 
112             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
113 
114             QueryPos qPos = QueryPos.getInstance(q);
115 
116             qPos.add(groupId);
117 
118             if (status != WorkflowConstants.STATUS_ANY) {
119                 qPos.add(status);
120             }
121 
122             for (int i = 0; i < folderIds.size(); i++) {
123                 Long folderId = folderIds.get(i);
124 
125                 qPos.add(folderId);
126             }
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     protected String getFolderIds(List<Long> folderIds) {
149         if (folderIds.isEmpty()) {
150             return StringPool.BLANK;
151         }
152 
153         StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
154 
155         for (int i = 0; i < folderIds.size(); i++) {
156             sb.append("DLFileEntry.folderId = ? ");
157 
158             if ((i + 1) != folderIds.size()) {
159                 sb.append("OR ");
160             }
161         }
162 
163         return sb.toString();
164     }
165 
166 }