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