1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.blogs.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
29  import com.liferay.portal.spring.hibernate.HibernateUtil;
30  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
31  import com.liferay.util.dao.hibernate.QueryPos;
32  import com.liferay.util.dao.hibernate.QueryUtil;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  import org.hibernate.Hibernate;
39  import org.hibernate.SQLQuery;
40  import org.hibernate.Session;
41  
42  /**
43   * <a href="BlogsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class BlogsEntryFinderImpl implements BlogsEntryFinder {
49  
50      public static String COUNT_BY_ORGANIZATION_IDS =
51          BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
52  
53      public static String FIND_BY_ORGANIZATION_IDS =
54          BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
55  
56      public static String FIND_BY_NO_ASSETS =
57          BlogsEntryFinder.class.getName() + ".findByNoAssets";
58  
59      public int countByOrganizationId(long organizationId)
60          throws SystemException {
61  
62          List organizationIds = new ArrayList();
63  
64          organizationIds.add(new Long(organizationId));
65  
66          return countByOrganizationIds(organizationIds);
67      }
68  
69      public int countByOrganizationIds(List organizationIds)
70          throws SystemException {
71  
72          Session session = null;
73  
74          try {
75              session = HibernateUtil.openSession();
76  
77              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
78  
79              sql = StringUtil.replace(
80                  sql, "[$ORGANIZATION_ID$]",
81                  getOrganizationIds(organizationIds));
82  
83              SQLQuery q = session.createSQLQuery(sql);
84  
85              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
86  
87              QueryPos qPos = QueryPos.getInstance(q);
88  
89              for (int i = 0; i < organizationIds.size(); i++) {
90                  Long organizationId = (Long)organizationIds.get(i);
91  
92                  qPos.add(organizationId);
93              }
94  
95              Iterator itr = q.list().iterator();
96  
97              if (itr.hasNext()) {
98                  Long count = (Long)itr.next();
99  
100                 if (count != null) {
101                     return count.intValue();
102                 }
103             }
104 
105             return 0;
106         }
107         catch (Exception e) {
108             throw new SystemException(e);
109         }
110         finally {
111             HibernateUtil.closeSession(session);
112         }
113     }
114 
115     public List findByOrganizationId(long organizationId, int begin, int end)
116         throws SystemException {
117 
118         List organizationIds = new ArrayList();
119 
120         organizationIds.add(new Long(organizationId));
121 
122         return findByOrganizationIds(organizationIds, begin, end);
123     }
124 
125     public List findByOrganizationIds(List organizationIds, int begin, int end)
126         throws SystemException {
127 
128         Session session = null;
129 
130         try {
131             session = HibernateUtil.openSession();
132 
133             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
134 
135             sql = StringUtil.replace(
136                 sql, "[$ORGANIZATION_ID$]",
137                 getOrganizationIds(organizationIds));
138 
139             SQLQuery q = session.createSQLQuery(sql);
140 
141             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
142 
143             QueryPos qPos = QueryPos.getInstance(q);
144 
145             for (int i = 0; i < organizationIds.size(); i++) {
146                 Long organizationId = (Long)organizationIds.get(i);
147 
148                 qPos.add(organizationId);
149             }
150 
151             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
152         }
153         catch (Exception e) {
154             throw new SystemException(e);
155         }
156         finally {
157             HibernateUtil.closeSession(session);
158         }
159     }
160 
161     public List findByNoAssets() throws SystemException {
162         Session session = null;
163 
164         try {
165             session = HibernateUtil.openSession();
166 
167             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
168 
169             SQLQuery q = session.createSQLQuery(sql);
170 
171             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
172 
173             return q.list();
174         }
175         catch (Exception e) {
176             throw new SystemException(e);
177         }
178         finally {
179             HibernateUtil.closeSession(session);
180         }
181     }
182 
183     protected String getOrganizationIds(List organizationIds) {
184         StringMaker sm = new StringMaker();
185 
186         for (int i = 0; i < organizationIds.size(); i++) {
187             sm.append("Users_Orgs.organizationId = ? ");
188 
189             if ((i + 1) != organizationIds.size()) {
190                 sm.append("OR ");
191             }
192         }
193 
194         return sm.toString();
195     }
196 
197 }