1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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             qPos.add(groupId);
141 
142             return (List<BlogsEntry>)QueryUtil.list(
143                 q, getDialect(), start, end);
144         }
145         catch (Exception e) {
146             throw new SystemException(e);
147         }
148         finally {
149             closeSession(session);
150         }
151     }
152 
153     public List<BlogsEntry> findByOrganizationId(
154             long organizationId, Date displayDate, boolean draft, int start,
155             int end)
156         throws SystemException {
157 
158         List<Long> organizationIds = new ArrayList<Long>();
159 
160         organizationIds.add(organizationId);
161 
162         return findByOrganizationIds(
163             organizationIds, displayDate, draft, start, end);
164     }
165 
166     public List<BlogsEntry> findByOrganizationIds(
167             List<Long> organizationIds, Date displayDate, boolean draft,
168             int start, int end)
169         throws SystemException {
170 
171         Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
172 
173         Session session = null;
174 
175         try {
176             session = openSession();
177 
178             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
179 
180             sql = StringUtil.replace(
181                 sql, "[$ORGANIZATION_ID$]",
182                 getOrganizationIds(organizationIds));
183 
184             SQLQuery q = session.createSQLQuery(sql);
185 
186             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
187 
188             QueryPos qPos = QueryPos.getInstance(q);
189 
190             for (int i = 0; i < organizationIds.size(); i++) {
191                 Long organizationId = organizationIds.get(i);
192 
193                 qPos.add(organizationId);
194             }
195 
196             qPos.add(displayDate_TS);
197             qPos.add(draft);
198 
199             return (List<BlogsEntry>)QueryUtil.list(
200                 q, getDialect(), start, end);
201         }
202         catch (Exception e) {
203             throw new SystemException(e);
204         }
205         finally {
206             closeSession(session);
207         }
208     }
209 
210     public List<BlogsEntry> findByNoAssets() throws SystemException {
211         Session session = null;
212 
213         try {
214             session = openSession();
215 
216             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
217 
218             SQLQuery q = session.createSQLQuery(sql);
219 
220             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
221 
222             return q.list();
223         }
224         catch (Exception e) {
225             throw new SystemException(e);
226         }
227         finally {
228             closeSession(session);
229         }
230     }
231 
232     protected String getOrganizationIds(List<Long> organizationIds) {
233         StringBuilder sb = new StringBuilder();
234 
235         for (int i = 0; i < organizationIds.size(); i++) {
236             sb.append("Users_Orgs.organizationId = ? ");
237 
238             if ((i + 1) != organizationIds.size()) {
239                 sb.append("OR ");
240             }
241         }
242 
243         return sb.toString();
244     }
245 
246 }