1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.blogs.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.kernel.util.CalendarUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
31  import com.liferay.portlet.blogs.model.BlogsEntry;
32  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
33  import com.liferay.util.dao.orm.CustomSQLUtil;
34  
35  import java.sql.Timestamp;
36  
37  import java.util.ArrayList;
38  import java.util.Date;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="BlogsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class BlogsEntryFinderImpl
49      extends BasePersistenceImpl implements BlogsEntryFinder {
50  
51      public static String COUNT_BY_ORGANIZATION_IDS =
52          BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
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, boolean draft)
62          throws SystemException {
63  
64          List<Long> organizationIds = new ArrayList<Long>();
65  
66          organizationIds.add(organizationId);
67  
68          return countByOrganizationIds(organizationIds, displayDate, draft);
69      }
70  
71      public int countByOrganizationIds(
72              List<Long> organizationIds, Date displayDate, boolean draft)
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              sql = StringUtil.replace(
85                  sql, "[$ORGANIZATION_ID$]",
86                  getOrganizationIds(organizationIds));
87  
88              SQLQuery q = session.createSQLQuery(sql);
89  
90              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
91  
92              QueryPos qPos = QueryPos.getInstance(q);
93  
94              for (int i = 0; i < organizationIds.size(); i++) {
95                  Long organizationId = organizationIds.get(i);
96  
97                  qPos.add(organizationId);
98              }
99  
100             qPos.add(displayDate_TS);
101             qPos.add(draft);
102 
103             Iterator<Long> itr = q.list().iterator();
104 
105             if (itr.hasNext()) {
106                 Long count = itr.next();
107 
108                 if (count != null) {
109                     return count.intValue();
110                 }
111             }
112 
113             return 0;
114         }
115         catch (Exception e) {
116             throw new SystemException(e);
117         }
118         finally {
119             closeSession(session);
120         }
121     }
122 
123     public List<BlogsEntry> findByOrganizationId(
124             long organizationId, Date displayDate, boolean draft, int start,
125             int end)
126         throws SystemException {
127 
128         List<Long> organizationIds = new ArrayList<Long>();
129 
130         organizationIds.add(organizationId);
131 
132         return findByOrganizationIds(
133             organizationIds, displayDate, draft, start, end);
134     }
135 
136     public List<BlogsEntry> findByOrganizationIds(
137             List<Long> organizationIds, Date displayDate, boolean draft,
138             int start, int end)
139         throws SystemException {
140 
141         Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
142 
143         Session session = null;
144 
145         try {
146             session = openSession();
147 
148             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
149 
150             sql = StringUtil.replace(
151                 sql, "[$ORGANIZATION_ID$]",
152                 getOrganizationIds(organizationIds));
153 
154             SQLQuery q = session.createSQLQuery(sql);
155 
156             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
157 
158             QueryPos qPos = QueryPos.getInstance(q);
159 
160             for (int i = 0; i < organizationIds.size(); i++) {
161                 Long organizationId = organizationIds.get(i);
162 
163                 qPos.add(organizationId);
164             }
165 
166             qPos.add(displayDate_TS);
167             qPos.add(draft);
168 
169             return (List<BlogsEntry>)QueryUtil.list(
170                 q, getDialect(), start, end);
171         }
172         catch (Exception e) {
173             throw new SystemException(e);
174         }
175         finally {
176             closeSession(session);
177         }
178     }
179 
180     public List<BlogsEntry> findByNoAssets() throws SystemException {
181         Session session = null;
182 
183         try {
184             session = openSession();
185 
186             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
187 
188             SQLQuery q = session.createSQLQuery(sql);
189 
190             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
191 
192             return q.list();
193         }
194         catch (Exception e) {
195             throw new SystemException(e);
196         }
197         finally {
198             closeSession(session);
199         }
200     }
201 
202     protected String getOrganizationIds(List<Long> organizationIds) {
203         StringBuilder sb = new StringBuilder();
204 
205         for (int i = 0; i < organizationIds.size(); i++) {
206             sb.append("Users_Orgs.organizationId = ? ");
207 
208             if ((i + 1) != organizationIds.size()) {
209                 sb.append("OR ");
210             }
211         }
212 
213         return sb.toString();
214     }
215 
216 }