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.BlogsEntry;
31  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
32  import com.liferay.util.dao.hibernate.QueryPos;
33  import com.liferay.util.dao.hibernate.QueryUtil;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  import org.hibernate.Hibernate;
40  import org.hibernate.SQLQuery;
41  import org.hibernate.Session;
42  
43  /**
44   * <a href="BlogsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class BlogsEntryFinderImpl implements BlogsEntryFinder {
50  
51      public static String COUNT_BY_ORGANIZATION_IDS =
52          BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
53  
54      public static String FIND_BY_ORGANIZATION_IDS =
55          BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
56  
57      public static String FIND_BY_NO_ASSETS =
58          BlogsEntryFinder.class.getName() + ".findByNoAssets";
59  
60      public int countByOrganizationId(long organizationId)
61          throws SystemException {
62  
63          List<Long> organizationIds = new ArrayList<Long>();
64  
65          organizationIds.add(organizationId);
66  
67          return countByOrganizationIds(organizationIds);
68      }
69  
70      public int countByOrganizationIds(List<Long> organizationIds)
71          throws SystemException {
72  
73          Session session = null;
74  
75          try {
76              session = HibernateUtil.openSession();
77  
78              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
79  
80              sql = StringUtil.replace(
81                  sql, "[$ORGANIZATION_ID$]",
82                  getOrganizationIds(organizationIds));
83  
84              SQLQuery q = session.createSQLQuery(sql);
85  
86              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
87  
88              QueryPos qPos = QueryPos.getInstance(q);
89  
90              for (int i = 0; i < organizationIds.size(); i++) {
91                  Long organizationId = organizationIds.get(i);
92  
93                  qPos.add(organizationId);
94              }
95  
96              Iterator<Long> itr = q.list().iterator();
97  
98              if (itr.hasNext()) {
99                  Long count = itr.next();
100 
101                 if (count != null) {
102                     return count.intValue();
103                 }
104             }
105 
106             return 0;
107         }
108         catch (Exception e) {
109             throw new SystemException(e);
110         }
111         finally {
112             HibernateUtil.closeSession(session);
113         }
114     }
115 
116     public List<BlogsEntry> findByOrganizationId(
117             long organizationId, int begin, int end)
118         throws SystemException {
119 
120         List<Long> organizationIds = new ArrayList<Long>();
121 
122         organizationIds.add(organizationId);
123 
124         return findByOrganizationIds(organizationIds, begin, end);
125     }
126 
127     public List<BlogsEntry> findByOrganizationIds(
128             List<Long> organizationIds, int begin, int end)
129         throws SystemException {
130 
131         Session session = null;
132 
133         try {
134             session = HibernateUtil.openSession();
135 
136             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
137 
138             sql = StringUtil.replace(
139                 sql, "[$ORGANIZATION_ID$]",
140                 getOrganizationIds(organizationIds));
141 
142             SQLQuery q = session.createSQLQuery(sql);
143 
144             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
145 
146             QueryPos qPos = QueryPos.getInstance(q);
147 
148             for (int i = 0; i < organizationIds.size(); i++) {
149                 Long organizationId = organizationIds.get(i);
150 
151                 qPos.add(organizationId);
152             }
153 
154             return (List<BlogsEntry>)QueryUtil.list(
155                 q, HibernateUtil.getDialect(), begin, end);
156         }
157         catch (Exception e) {
158             throw new SystemException(e);
159         }
160         finally {
161             HibernateUtil.closeSession(session);
162         }
163     }
164 
165     public List<BlogsEntry> findByNoAssets() throws SystemException {
166         Session session = null;
167 
168         try {
169             session = HibernateUtil.openSession();
170 
171             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
172 
173             SQLQuery q = session.createSQLQuery(sql);
174 
175             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
176 
177             return q.list();
178         }
179         catch (Exception e) {
180             throw new SystemException(e);
181         }
182         finally {
183             HibernateUtil.closeSession(session);
184         }
185     }
186 
187     protected String getOrganizationIds(List<Long> organizationIds) {
188         StringMaker sm = new StringMaker();
189 
190         for (int i = 0; i < organizationIds.size(); i++) {
191             sm.append("Users_Orgs.organizationId = ? ");
192 
193             if ((i + 1) != organizationIds.size()) {
194                 sm.append("OR ");
195             }
196         }
197 
198         return sm.toString();
199     }
200 
201 }