1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.imagegallery.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.SQLQuery;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.dao.orm.Type;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
32  import com.liferay.portlet.imagegallery.model.IGImage;
33  import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
34  import com.liferay.util.dao.orm.CustomSQLUtil;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="IGImageFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class IGImageFinderImpl
45      extends BasePersistenceImpl implements IGImageFinder {
46  
47      public static String COUNT_BY_FOLDER_IDS =
48          IGImageFinder.class.getName() + ".countByFolderIds";
49  
50      public static String FIND_BY_NO_ASSETS =
51          IGImageFinder.class.getName() + ".findByNoAssets";
52  
53      public int countByFolderIds(List<Long> folderIds) throws SystemException {
54          Session session = null;
55  
56          try {
57              session = openSession();
58  
59              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
60  
61              sql = StringUtil.replace(
62                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
63  
64              SQLQuery q = session.createSQLQuery(sql);
65  
66              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
67  
68              QueryPos qPos = QueryPos.getInstance(q);
69  
70              for (int i = 0; i < folderIds.size(); i++) {
71                  Long folderId = folderIds.get(i);
72  
73                  qPos.add(folderId);
74              }
75  
76              Iterator<Long> itr = q.list().iterator();
77  
78              if (itr.hasNext()) {
79                  Long count = itr.next();
80  
81                  if (count != null) {
82                      return count.intValue();
83                  }
84              }
85  
86              return 0;
87          }
88          catch (Exception e) {
89              throw new SystemException(e);
90          }
91          finally {
92              closeSession(session);
93          }
94      }
95  
96      public List<IGImage> findByNoAssets() throws SystemException {
97          Session session = null;
98  
99          try {
100             session = openSession();
101 
102             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
103 
104             SQLQuery q = session.createSQLQuery(sql);
105 
106             q.addEntity("IGImage", IGImageImpl.class);
107 
108             return q.list();
109         }
110         catch (Exception e) {
111             throw new SystemException(e);
112         }
113         finally {
114             closeSession(session);
115         }
116     }
117 
118     protected String getFolderIds(List<Long> folderIds) {
119         StringBuilder sb = new StringBuilder();
120 
121         for (int i = 0; i < folderIds.size(); i++) {
122             sb.append("folderId = ? ");
123 
124             if ((i + 1) != folderIds.size()) {
125                 sb.append("OR ");
126             }
127         }
128 
129         return sb.toString();
130     }
131 
132 }