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.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
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.exception.SystemException;
23  import com.liferay.portal.kernel.util.CalendarUtil;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.workflow.WorkflowConstants;
28  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
29  import com.liferay.portlet.blogs.model.BlogsEntry;
30  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
31  import com.liferay.util.dao.orm.CustomSQLUtil;
32  
33  import java.sql.Timestamp;
34  
35  import java.util.ArrayList;
36  import java.util.Date;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /**
41   * <a href="BlogsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class BlogsEntryFinderImpl
46      extends BasePersistenceImpl<BlogsEntry> implements BlogsEntryFinder {
47  
48      public static String COUNT_BY_ORGANIZATION_IDS =
49          BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
50  
51      public static String FIND_BY_GROUP_IDS =
52          BlogsEntryFinder.class.getName() + ".findByGroupIds";
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(
61              long organizationId, Date displayDate, int status)
62          throws SystemException {
63  
64          List<Long> organizationIds = new ArrayList<Long>();
65  
66          organizationIds.add(organizationId);
67  
68          return countByOrganizationIds(organizationIds, displayDate, status);
69      }
70  
71      public int countByOrganizationIds(
72              List<Long> organizationIds, Date displayDate, int status)
73          throws SystemException {
74  
75          Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
76  
77          Session session = null;
78  
79          try {
80              session = openSession();
81  
82              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
83  
84              if (status != WorkflowConstants.STATUS_ANY) {
85                  sql = StringUtil.replace(
86                      sql, "[$STATUS$]", "AND (BlogsEntry.status = ?)");
87              }
88              else {
89                  sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
90              }
91  
92              sql = StringUtil.replace(
93                  sql, "[$ORGANIZATION_ID$]",
94                  getOrganizationIds(organizationIds));
95  
96              SQLQuery q = session.createSQLQuery(sql);
97  
98              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
99  
100             QueryPos qPos = QueryPos.getInstance(q);
101 
102             for (int i = 0; i < organizationIds.size(); i++) {
103                 Long organizationId = organizationIds.get(i);
104 
105                 qPos.add(organizationId);
106             }
107 
108             qPos.add(displayDate_TS);
109 
110             if (status != WorkflowConstants.STATUS_ANY) {
111                 qPos.add(status);
112             }
113 
114             Iterator<Long> itr = q.list().iterator();
115 
116             if (itr.hasNext()) {
117                 Long count = itr.next();
118 
119                 if (count != null) {
120                     return count.intValue();
121                 }
122             }
123 
124             return 0;
125         }
126         catch (Exception e) {
127             throw new SystemException(e);
128         }
129         finally {
130             closeSession(session);
131         }
132     }
133 
134     public List<BlogsEntry> findByGroupIds(
135             long companyId, long groupId, int status, int start, int end)
136         throws SystemException {
137 
138         Session session = null;
139 
140         try {
141             session = openSession();
142 
143             String sql = CustomSQLUtil.get(FIND_BY_GROUP_IDS);
144 
145             if (status != WorkflowConstants.STATUS_ANY) {
146                 sql = StringUtil.replace(
147                     sql, "[$STATUS$]", "AND (BlogsEntry.status = ?)");
148             }
149             else {
150                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
151             }
152 
153             SQLQuery q = session.createSQLQuery(sql);
154 
155             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
156 
157             QueryPos qPos = QueryPos.getInstance(q);
158 
159             qPos.add(companyId);
160             qPos.add(groupId);
161             qPos.add(groupId);
162 
163             if (status != WorkflowConstants.STATUS_ANY) {
164                 qPos.add(status);
165             }
166 
167             return (List<BlogsEntry>)QueryUtil.list(
168                 q, getDialect(), start, end);
169         }
170         catch (Exception e) {
171             throw new SystemException(e);
172         }
173         finally {
174             closeSession(session);
175         }
176     }
177 
178     public List<BlogsEntry> findByOrganizationId(
179             long organizationId, Date displayDate, int status, int start,
180             int end)
181         throws SystemException {
182 
183         List<Long> organizationIds = new ArrayList<Long>();
184 
185         organizationIds.add(organizationId);
186 
187         return findByOrganizationIds(
188             organizationIds, displayDate, status, start, end);
189     }
190 
191     public List<BlogsEntry> findByOrganizationIds(
192             List<Long> organizationIds, Date displayDate, int status,
193             int start, int end)
194         throws SystemException {
195 
196         Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
197 
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
204 
205             if (status != WorkflowConstants.STATUS_ANY) {
206                 sql = StringUtil.replace(
207                     sql, "[$STATUS$]", "AND (BlogsEntry.status = ?)");
208             }
209             else {
210                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
211             }
212 
213             sql = StringUtil.replace(
214                 sql, "[$ORGANIZATION_ID$]",
215                 getOrganizationIds(organizationIds));
216 
217             SQLQuery q = session.createSQLQuery(sql);
218 
219             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
220 
221             QueryPos qPos = QueryPos.getInstance(q);
222 
223             for (int i = 0; i < organizationIds.size(); i++) {
224                 Long organizationId = organizationIds.get(i);
225 
226                 qPos.add(organizationId);
227             }
228 
229             qPos.add(displayDate_TS);
230 
231             if (status != WorkflowConstants.STATUS_ANY) {
232                 qPos.add(status);
233             }
234 
235             return (List<BlogsEntry>)QueryUtil.list(
236                 q, getDialect(), start, end);
237         }
238         catch (Exception e) {
239             throw new SystemException(e);
240         }
241         finally {
242             closeSession(session);
243         }
244     }
245 
246     public List<BlogsEntry> findByNoAssets() throws SystemException {
247         Session session = null;
248 
249         try {
250             session = openSession();
251 
252             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
253 
254             SQLQuery q = session.createSQLQuery(sql);
255 
256             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
257 
258             return q.list();
259         }
260         catch (Exception e) {
261             throw new SystemException(e);
262         }
263         finally {
264             closeSession(session);
265         }
266     }
267 
268     protected String getOrganizationIds(List<Long> organizationIds) {
269         if (organizationIds.isEmpty()) {
270             return StringPool.BLANK;
271         }
272 
273         StringBundler sb = new StringBundler(organizationIds.size() * 2 - 1);
274 
275         for (int i = 0; i < organizationIds.size(); i++) {
276             sb.append("Users_Orgs.organizationId = ? ");
277 
278             if ((i + 1) != organizationIds.size()) {
279                 sb.append("OR ");
280             }
281         }
282 
283         return sb.toString();
284     }
285 
286 }