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_ORGANIZATION_IDS =
49          BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
50  
51      public static String FIND_BY_NO_ASSETS =
52          BlogsEntryFinder.class.getName() + ".findByNoAssets";
53  
54      public int countByOrganizationId(
55              long organizationId, Date displayDate, boolean draft)
56          throws SystemException {
57  
58          List<Long> organizationIds = new ArrayList<Long>();
59  
60          organizationIds.add(organizationId);
61  
62          return countByOrganizationIds(organizationIds, displayDate, draft);
63      }
64  
65      public int countByOrganizationIds(
66              List<Long> organizationIds, Date displayDate, boolean draft)
67          throws SystemException {
68  
69          Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
70  
71          Session session = null;
72  
73          try {
74              session = openSession();
75  
76              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
77  
78              sql = StringUtil.replace(
79                  sql, "[$ORGANIZATION_ID$]",
80                  getOrganizationIds(organizationIds));
81  
82              SQLQuery q = session.createSQLQuery(sql);
83  
84              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
85  
86              QueryPos qPos = QueryPos.getInstance(q);
87  
88              for (int i = 0; i < organizationIds.size(); i++) {
89                  Long organizationId = organizationIds.get(i);
90  
91                  qPos.add(organizationId);
92              }
93  
94              qPos.add(displayDate_TS);
95              qPos.add(draft);
96  
97              Iterator<Long> itr = q.list().iterator();
98  
99              if (itr.hasNext()) {
100                 Long count = itr.next();
101 
102                 if (count != null) {
103                     return count.intValue();
104                 }
105             }
106 
107             return 0;
108         }
109         catch (Exception e) {
110             throw new SystemException(e);
111         }
112         finally {
113             closeSession(session);
114         }
115     }
116 
117     public List<BlogsEntry> findByOrganizationId(
118             long organizationId, Date displayDate, boolean draft, int start,
119             int end)
120         throws SystemException {
121 
122         List<Long> organizationIds = new ArrayList<Long>();
123 
124         organizationIds.add(organizationId);
125 
126         return findByOrganizationIds(
127             organizationIds, displayDate, draft, start, end);
128     }
129 
130     public List<BlogsEntry> findByOrganizationIds(
131             List<Long> organizationIds, Date displayDate, boolean draft,
132             int start, int end)
133         throws SystemException {
134 
135         Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
136 
137         Session session = null;
138 
139         try {
140             session = openSession();
141 
142             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
143 
144             sql = StringUtil.replace(
145                 sql, "[$ORGANIZATION_ID$]",
146                 getOrganizationIds(organizationIds));
147 
148             SQLQuery q = session.createSQLQuery(sql);
149 
150             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
151 
152             QueryPos qPos = QueryPos.getInstance(q);
153 
154             for (int i = 0; i < organizationIds.size(); i++) {
155                 Long organizationId = organizationIds.get(i);
156 
157                 qPos.add(organizationId);
158             }
159 
160             qPos.add(displayDate_TS);
161             qPos.add(draft);
162 
163             return (List<BlogsEntry>)QueryUtil.list(
164                 q, getDialect(), start, end);
165         }
166         catch (Exception e) {
167             throw new SystemException(e);
168         }
169         finally {
170             closeSession(session);
171         }
172     }
173 
174     public List<BlogsEntry> findByNoAssets() throws SystemException {
175         Session session = null;
176 
177         try {
178             session = openSession();
179 
180             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
181 
182             SQLQuery q = session.createSQLQuery(sql);
183 
184             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
185 
186             return q.list();
187         }
188         catch (Exception e) {
189             throw new SystemException(e);
190         }
191         finally {
192             closeSession(session);
193         }
194     }
195 
196     protected String getOrganizationIds(List<Long> organizationIds) {
197         StringBuilder sb = new StringBuilder();
198 
199         for (int i = 0; i < organizationIds.size(); i++) {
200             sb.append("Users_Orgs.organizationId = ? ");
201 
202             if ((i + 1) != organizationIds.size()) {
203                 sb.append("OR ");
204             }
205         }
206 
207         return sb.toString();
208     }
209 
210 }