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.StatusConstants;
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_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, int status)
59          throws SystemException {
60  
61          List<Long> organizationIds = new ArrayList<Long>();
62  
63          organizationIds.add(organizationId);
64  
65          return countByOrganizationIds(organizationIds, displayDate, status);
66      }
67  
68      public int countByOrganizationIds(
69              List<Long> organizationIds, Date displayDate, int status)
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              if (status != StatusConstants.ANY) {
82                  sql = StringUtil.replace(
83                      sql, "[$STATUS$]", "AND (BlogsEntry.status = ?)");
84              }
85              else {
86                  sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
87              }
88  
89              sql = StringUtil.replace(
90                  sql, "[$ORGANIZATION_ID$]",
91                  getOrganizationIds(organizationIds));
92  
93              SQLQuery q = session.createSQLQuery(sql);
94  
95              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
96  
97              QueryPos qPos = QueryPos.getInstance(q);
98  
99              for (int i = 0; i < organizationIds.size(); i++) {
100                 Long organizationId = organizationIds.get(i);
101 
102                 qPos.add(organizationId);
103             }
104 
105             qPos.add(displayDate_TS);
106 
107             if (status != StatusConstants.ANY) {
108                 qPos.add(status);
109             }
110 
111             Iterator<Long> itr = q.list().iterator();
112 
113             if (itr.hasNext()) {
114                 Long count = itr.next();
115 
116                 if (count != null) {
117                     return count.intValue();
118                 }
119             }
120 
121             return 0;
122         }
123         catch (Exception e) {
124             throw new SystemException(e);
125         }
126         finally {
127             closeSession(session);
128         }
129     }
130 
131     public List<BlogsEntry> findByOrganizationId(
132             long organizationId, Date displayDate, int status, int start,
133             int end)
134         throws SystemException {
135 
136         List<Long> organizationIds = new ArrayList<Long>();
137 
138         organizationIds.add(organizationId);
139 
140         return findByOrganizationIds(
141             organizationIds, displayDate, status, start, end);
142     }
143 
144     public List<BlogsEntry> findByOrganizationIds(
145             List<Long> organizationIds, Date displayDate, int status,
146             int start, int end)
147         throws SystemException {
148 
149         Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
150 
151         Session session = null;
152 
153         try {
154             session = openSession();
155 
156             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
157 
158             if (status != StatusConstants.ANY) {
159                 sql = StringUtil.replace(
160                     sql, "[$STATUS$]", "AND (BlogsEntry.status = ?)");
161             }
162             else {
163                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
164             }
165 
166             sql = StringUtil.replace(
167                 sql, "[$ORGANIZATION_ID$]",
168                 getOrganizationIds(organizationIds));
169 
170             SQLQuery q = session.createSQLQuery(sql);
171 
172             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
173 
174             QueryPos qPos = QueryPos.getInstance(q);
175 
176             for (int i = 0; i < organizationIds.size(); i++) {
177                 Long organizationId = organizationIds.get(i);
178 
179                 qPos.add(organizationId);
180             }
181 
182             qPos.add(displayDate_TS);
183 
184             if (status != StatusConstants.ANY) {
185                 qPos.add(status);
186             }
187 
188             return (List<BlogsEntry>)QueryUtil.list(
189                 q, getDialect(), start, end);
190         }
191         catch (Exception e) {
192             throw new SystemException(e);
193         }
194         finally {
195             closeSession(session);
196         }
197     }
198 
199     public List<BlogsEntry> findByNoAssets() throws SystemException {
200         Session session = null;
201 
202         try {
203             session = openSession();
204 
205             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
206 
207             SQLQuery q = session.createSQLQuery(sql);
208 
209             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
210 
211             return q.list();
212         }
213         catch (Exception e) {
214             throw new SystemException(e);
215         }
216         finally {
217             closeSession(session);
218         }
219     }
220 
221     protected String getOrganizationIds(List<Long> organizationIds) {
222         if (organizationIds.isEmpty()) {
223             return StringPool.BLANK;
224         }
225 
226         StringBundler sb = new StringBundler(organizationIds.size() * 2 - 1);
227 
228         for (int i = 0; i < organizationIds.size(); i++) {
229             sb.append("Users_Orgs.organizationId = ? ");
230 
231             if ((i + 1) != organizationIds.size()) {
232                 sb.append("OR ");
233             }
234         }
235 
236         return sb.toString();
237     }
238 
239 }