1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.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.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.kernel.util.CalendarUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
26  import com.liferay.portlet.blogs.model.BlogsEntry;
27  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
28  import com.liferay.util.dao.orm.CustomSQLUtil;
29  
30  import java.sql.Timestamp;
31  
32  import java.util.ArrayList;
33  import java.util.Date;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * <a href="BlogsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class BlogsEntryFinderImpl
43      extends BasePersistenceImpl<BlogsEntry> implements BlogsEntryFinder {
44  
45      public static String COUNT_BY_ORGANIZATION_IDS =
46          BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
47  
48      public static String FIND_BY_GROUP_IDS =
49          BlogsEntryFinder.class.getName() + ".findByGroupIds";
50  
51      public static String FIND_BY_ORGANIZATION_IDS =
52          BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
53  
54      public static String FIND_BY_NO_ASSETS =
55          BlogsEntryFinder.class.getName() + ".findByNoAssets";
56  
57      public int countByOrganizationId(
58              long organizationId, Date displayDate, boolean draft)
59          throws SystemException {
60  
61          List<Long> organizationIds = new ArrayList<Long>();
62  
63          organizationIds.add(organizationId);
64  
65          return countByOrganizationIds(organizationIds, displayDate, draft);
66      }
67  
68      public int countByOrganizationIds(
69              List<Long> organizationIds, Date displayDate, boolean draft)
70          throws SystemException {
71  
72          Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
73  
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
80  
81              sql = StringUtil.replace(
82                  sql, "[$ORGANIZATION_ID$]",
83                  getOrganizationIds(organizationIds));
84  
85              SQLQuery q = session.createSQLQuery(sql);
86  
87              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
88  
89              QueryPos qPos = QueryPos.getInstance(q);
90  
91              for (int i = 0; i < organizationIds.size(); i++) {
92                  Long organizationId = organizationIds.get(i);
93  
94                  qPos.add(organizationId);
95              }
96  
97              qPos.add(displayDate_TS);
98              qPos.add(draft);
99  
100             Iterator<Long> itr = q.list().iterator();
101 
102             if (itr.hasNext()) {
103                 Long count = itr.next();
104 
105                 if (count != null) {
106                     return count.intValue();
107                 }
108             }
109 
110             return 0;
111         }
112         catch (Exception e) {
113             throw new SystemException(e);
114         }
115         finally {
116             closeSession(session);
117         }
118     }
119 
120     public List<BlogsEntry> findByGroupIds(
121             long companyId, long groupId, int start, int end)
122         throws SystemException {
123 
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             String sql = CustomSQLUtil.get(FIND_BY_GROUP_IDS);
130 
131             SQLQuery q = session.createSQLQuery(sql);
132 
133             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
134 
135             QueryPos qPos = QueryPos.getInstance(q);
136 
137             qPos.add(companyId);
138             qPos.add(groupId);
139             qPos.add(groupId);
140 
141             return (List<BlogsEntry>)QueryUtil.list(
142                 q, getDialect(), start, end);
143         }
144         catch (Exception e) {
145             throw new SystemException(e);
146         }
147         finally {
148             closeSession(session);
149         }
150     }
151 
152     public List<BlogsEntry> findByOrganizationId(
153             long organizationId, Date displayDate, boolean draft, int start,
154             int end)
155         throws SystemException {
156 
157         List<Long> organizationIds = new ArrayList<Long>();
158 
159         organizationIds.add(organizationId);
160 
161         return findByOrganizationIds(
162             organizationIds, displayDate, draft, start, end);
163     }
164 
165     public List<BlogsEntry> findByOrganizationIds(
166             List<Long> organizationIds, Date displayDate, boolean draft,
167             int start, int end)
168         throws SystemException {
169 
170         Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
171 
172         Session session = null;
173 
174         try {
175             session = openSession();
176 
177             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
178 
179             sql = StringUtil.replace(
180                 sql, "[$ORGANIZATION_ID$]",
181                 getOrganizationIds(organizationIds));
182 
183             SQLQuery q = session.createSQLQuery(sql);
184 
185             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
186 
187             QueryPos qPos = QueryPos.getInstance(q);
188 
189             for (int i = 0; i < organizationIds.size(); i++) {
190                 Long organizationId = organizationIds.get(i);
191 
192                 qPos.add(organizationId);
193             }
194 
195             qPos.add(displayDate_TS);
196             qPos.add(draft);
197 
198             return (List<BlogsEntry>)QueryUtil.list(
199                 q, getDialect(), start, end);
200         }
201         catch (Exception e) {
202             throw new SystemException(e);
203         }
204         finally {
205             closeSession(session);
206         }
207     }
208 
209     public List<BlogsEntry> findByNoAssets() throws SystemException {
210         Session session = null;
211 
212         try {
213             session = openSession();
214 
215             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
216 
217             SQLQuery q = session.createSQLQuery(sql);
218 
219             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
220 
221             return q.list();
222         }
223         catch (Exception e) {
224             throw new SystemException(e);
225         }
226         finally {
227             closeSession(session);
228         }
229     }
230 
231     protected String getOrganizationIds(List<Long> organizationIds) {
232         StringBuilder sb = new StringBuilder();
233 
234         for (int i = 0; i < organizationIds.size(); i++) {
235             sb.append("Users_Orgs.organizationId = ? ");
236 
237             if ((i + 1) != organizationIds.size()) {
238                 sb.append("OR ");
239             }
240         }
241 
242         return sb.toString();
243     }
244 
245 }