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.APPROVED) {
62                  sql = StringUtil.replace(
63                      sql, "(DLFileEntry.version > 0) 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              for (int i = 0; i < folderIds.size(); i++) {
75                  Long folderId = folderIds.get(i);
76  
77                  qPos.add(folderId);
78              }
79  
80              Iterator<Long> itr = q.list().iterator();
81  
82              if (itr.hasNext()) {
83                  Long count = itr.next();
84  
85                  if (count != null) {
86                      return count.intValue();
87                  }
88              }
89  
90              return 0;
91          }
92          catch (Exception e) {
93              throw new SystemException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public List<DLFileEntry> findByNoAssets() throws SystemException {
101         Session session = null;
102 
103         try {
104             session = openSession();
105 
106             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
107 
108             SQLQuery q = session.createSQLQuery(sql);
109 
110             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
111 
112             return q.list();
113         }
114         catch (Exception e) {
115             throw new SystemException(e);
116         }
117         finally {
118             closeSession(session);
119         }
120     }
121 
122     protected String getFolderIds(List<Long> folderIds) {
123         if (folderIds.isEmpty()) {
124             return StringPool.BLANK;
125         }
126 
127         StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
128 
129         for (int i = 0; i < folderIds.size(); i++) {
130             sb.append("folderId = ? ");
131 
132             if ((i + 1) != folderIds.size()) {
133                 sb.append("OR ");
134             }
135         }
136 
137         return sb.toString();
138     }
139 
140 }