1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.blogs.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
33  import com.liferay.portlet.blogs.model.BlogsEntry;
34  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
35  import com.liferay.util.dao.orm.CustomSQLUtil;
36  
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="BlogsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class BlogsEntryFinderImpl
48      extends BasePersistenceImpl implements BlogsEntryFinder {
49  
50      public static String COUNT_BY_ORGANIZATION_IDS =
51          BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
52  
53      public static String FIND_BY_ORGANIZATION_IDS =
54          BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
55  
56      public static String FIND_BY_NO_ASSETS =
57          BlogsEntryFinder.class.getName() + ".findByNoAssets";
58  
59      public int countByOrganizationId(long organizationId, boolean draft)
60          throws SystemException {
61  
62          List<Long> organizationIds = new ArrayList<Long>();
63  
64          organizationIds.add(organizationId);
65  
66          return countByOrganizationIds(organizationIds, draft);
67      }
68  
69      public int countByOrganizationIds(
70              List<Long> organizationIds, boolean draft)
71          throws SystemException {
72  
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
79  
80              sql = StringUtil.replace(
81                  sql, "[$ORGANIZATION_ID$]",
82                  getOrganizationIds(organizationIds));
83  
84              SQLQuery q = session.createSQLQuery(sql);
85  
86              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
87  
88              QueryPos qPos = QueryPos.getInstance(q);
89  
90              for (int i = 0; i < organizationIds.size(); i++) {
91                  Long organizationId = organizationIds.get(i);
92  
93                  qPos.add(organizationId);
94              }
95  
96              qPos.add(draft);
97  
98              Iterator<Long> itr = q.list().iterator();
99  
100             if (itr.hasNext()) {
101                 Long count = itr.next();
102 
103                 if (count != null) {
104                     return count.intValue();
105                 }
106             }
107 
108             return 0;
109         }
110         catch (Exception e) {
111             throw new SystemException(e);
112         }
113         finally {
114             closeSession(session);
115         }
116     }
117 
118     public List<BlogsEntry> findByOrganizationId(
119             long organizationId, boolean draft, int start, int end)
120         throws SystemException {
121 
122         List<Long> organizationIds = new ArrayList<Long>();
123 
124         organizationIds.add(organizationId);
125 
126         return findByOrganizationIds(organizationIds, draft, start, end);
127     }
128 
129     public List<BlogsEntry> findByOrganizationIds(
130             List<Long> organizationIds, boolean draft, int start, int end)
131         throws SystemException {
132 
133         Session session = null;
134 
135         try {
136             session = openSession();
137 
138             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
139 
140             sql = StringUtil.replace(
141                 sql, "[$ORGANIZATION_ID$]",
142                 getOrganizationIds(organizationIds));
143 
144             SQLQuery q = session.createSQLQuery(sql);
145 
146             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
147 
148             QueryPos qPos = QueryPos.getInstance(q);
149 
150             for (int i = 0; i < organizationIds.size(); i++) {
151                 Long organizationId = organizationIds.get(i);
152 
153                 qPos.add(organizationId);
154             }
155 
156             qPos.add(draft);
157 
158             return (List<BlogsEntry>)QueryUtil.list(
159                 q, getDialect(), start, end);
160         }
161         catch (Exception e) {
162             throw new SystemException(e);
163         }
164         finally {
165             closeSession(session);
166         }
167     }
168 
169     public List<BlogsEntry> findByNoAssets() throws SystemException {
170         Session session = null;
171 
172         try {
173             session = openSession();
174 
175             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
176 
177             SQLQuery q = session.createSQLQuery(sql);
178 
179             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
180 
181             return q.list();
182         }
183         catch (Exception e) {
184             throw new SystemException(e);
185         }
186         finally {
187             closeSession(session);
188         }
189     }
190 
191     protected String getOrganizationIds(List<Long> organizationIds) {
192         StringBuilder sb = new StringBuilder();
193 
194         for (int i = 0; i < organizationIds.size(); i++) {
195             sb.append("Users_Orgs.organizationId = ? ");
196 
197             if ((i + 1) != organizationIds.size()) {
198                 sb.append("OR ");
199             }
200         }
201 
202         return sb.toString();
203     }
204 
205 }