1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.imagegallery.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
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.NoSuchImageException;
27  import com.liferay.portlet.imagegallery.model.IGImage;
28  import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
29  import com.liferay.util.dao.orm.CustomSQLUtil;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * <a href="IGImageFinderImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Julio Camarero
39   */
40  public class IGImageFinderImpl
41      extends BasePersistenceImpl<IGImage> implements IGImageFinder {
42  
43      public static String COUNT_BY_FOLDER_IDS =
44          IGImageFinder.class.getName() + ".countByFolderIds";
45  
46      public static String FIND_BY_ANY_IMAGE_ID =
47          IGImageFinder.class.getName() + ".findByAnyImageId";
48  
49      public static String FIND_BY_NO_ASSETS =
50          IGImageFinder.class.getName() + ".findByNoAssets";
51  
52      public int countByFolderIds(List<Long> folderIds) throws SystemException {
53          Session session = null;
54  
55          try {
56              session = openSession();
57  
58              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
59  
60              sql = StringUtil.replace(
61                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
62  
63              SQLQuery q = session.createSQLQuery(sql);
64  
65              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
66  
67              QueryPos qPos = QueryPos.getInstance(q);
68  
69              for (int i = 0; i < folderIds.size(); i++) {
70                  Long folderId = folderIds.get(i);
71  
72                  qPos.add(folderId);
73              }
74  
75              Iterator<Long> itr = q.list().iterator();
76  
77              if (itr.hasNext()) {
78                  Long count = itr.next();
79  
80                  if (count != null) {
81                      return count.intValue();
82                  }
83              }
84  
85              return 0;
86          }
87          catch (Exception e) {
88              throw new SystemException(e);
89          }
90          finally {
91              closeSession(session);
92          }
93      }
94  
95      public IGImage fetchByAnyImageId(long imageId) throws SystemException {
96          Session session = null;
97  
98          try {
99              session = openSession();
100 
101             String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
102 
103             SQLQuery q = session.createSQLQuery(sql);
104 
105             q.addEntity("IGImage", IGImageImpl.class);
106 
107             QueryPos qPos = QueryPos.getInstance(q);
108 
109             qPos.add(imageId);
110             qPos.add(imageId);
111             qPos.add(imageId);
112             qPos.add(imageId);
113 
114             List<IGImage> list = q.list();
115 
116             if (list.isEmpty()) {
117                 return null;
118             }
119             else {
120                 return list.get(0);
121             }
122         }
123         catch (Exception e) {
124             throw new SystemException(e);
125         }
126         finally {
127             closeSession(session);
128         }
129     }
130 
131     public IGImage findByAnyImageId(long imageId)
132         throws NoSuchImageException, SystemException {
133 
134         IGImage image = fetchByAnyImageId(imageId);
135 
136         if (image == null) {
137             throw new NoSuchImageException(
138                 "No IGImage exists with the imageId " + imageId);
139         }
140         else {
141             return image;
142         }
143     }
144 
145     public List<IGImage> findByNoAssets() throws SystemException {
146         Session session = null;
147 
148         try {
149             session = openSession();
150 
151             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
152 
153             SQLQuery q = session.createSQLQuery(sql);
154 
155             q.addEntity("IGImage", IGImageImpl.class);
156 
157             return q.list();
158         }
159         catch (Exception e) {
160             throw new SystemException(e);
161         }
162         finally {
163             closeSession(session);
164         }
165     }
166 
167     protected String getFolderIds(List<Long> folderIds) {
168         if (folderIds.isEmpty()) {
169             return StringPool.BLANK;
170         }
171 
172         StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
173 
174         for (int i = 0; i < folderIds.size(); i++) {
175             sb.append("folderId = ? ");
176 
177             if ((i + 1) != folderIds.size()) {
178                 sb.append("OR ");
179             }
180         }
181 
182         return sb.toString();
183     }
184 
185 }