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.imagegallery.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.SQLQuery;
25  import com.liferay.portal.kernel.dao.orm.Session;
26  import com.liferay.portal.kernel.dao.orm.Type;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
29  import com.liferay.portlet.imagegallery.model.IGImage;
30  import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
31  import com.liferay.util.dao.orm.CustomSQLUtil;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="IGImageFinderImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class IGImageFinderImpl
43      extends BasePersistenceImpl implements IGImageFinder {
44  
45      public static String COUNT_BY_FOLDER_IDS =
46          IGImageFinder.class.getName() + ".countByFolderIds";
47  
48      public static String FIND_BY_NO_ASSETS =
49          IGImageFinder.class.getName() + ".findByNoAssets";
50  
51      public int countByFolderIds(List<Long> folderIds) throws SystemException {
52          Session session = null;
53  
54          try {
55              session = openSession();
56  
57              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
58  
59              sql = StringUtil.replace(
60                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
61  
62              SQLQuery q = session.createSQLQuery(sql);
63  
64              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
65  
66              QueryPos qPos = QueryPos.getInstance(q);
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         StringBuilder sb = new StringBuilder();
118 
119         for (int i = 0; i < folderIds.size(); i++) {
120             sb.append("folderId = ? ");
121 
122             if ((i + 1) != folderIds.size()) {
123                 sb.append("OR ");
124             }
125         }
126 
127         return sb.toString();
128     }
129 
130 }