1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.CalendarUtil;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
34  import com.liferay.portlet.blogs.model.BlogsEntry;
35  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
36  import com.liferay.util.dao.orm.CustomSQLUtil;
37  
38  import java.sql.Timestamp;
39  
40  import java.util.ArrayList;
41  import java.util.Date;
42  import java.util.Iterator;
43  import java.util.List;
44  
45  /**
46   * <a href="BlogsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   */
50  public class BlogsEntryFinderImpl
51      extends BasePersistenceImpl implements BlogsEntryFinder {
52  
53      public static String COUNT_BY_ORGANIZATION_IDS =
54          BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
55  
56      public static String FIND_BY_ORGANIZATION_IDS =
57          BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
58  
59      public static String FIND_BY_NO_ASSETS =
60          BlogsEntryFinder.class.getName() + ".findByNoAssets";
61  
62      public int countByOrganizationId(
63              long organizationId, Date displayDate, boolean draft)
64          throws SystemException {
65  
66          List<Long> organizationIds = new ArrayList<Long>();
67  
68          organizationIds.add(organizationId);
69  
70          return countByOrganizationIds(organizationIds, displayDate, draft);
71      }
72  
73      public int countByOrganizationIds(
74              List<Long> organizationIds, Date displayDate, boolean draft)
75          throws SystemException {
76  
77          Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
78  
79          Session session = null;
80  
81          try {
82              session = openSession();
83  
84              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
85  
86              sql = StringUtil.replace(
87                  sql, "[$ORGANIZATION_ID$]",
88                  getOrganizationIds(organizationIds));
89  
90              SQLQuery q = session.createSQLQuery(sql);
91  
92              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
93  
94              QueryPos qPos = QueryPos.getInstance(q);
95  
96              for (int i = 0; i < organizationIds.size(); i++) {
97                  Long organizationId = organizationIds.get(i);
98  
99                  qPos.add(organizationId);
100             }
101 
102             qPos.add(displayDate_TS);
103             qPos.add(draft);
104 
105             Iterator<Long> itr = q.list().iterator();
106 
107             if (itr.hasNext()) {
108                 Long count = itr.next();
109 
110                 if (count != null) {
111                     return count.intValue();
112                 }
113             }
114 
115             return 0;
116         }
117         catch (Exception e) {
118             throw new SystemException(e);
119         }
120         finally {
121             closeSession(session);
122         }
123     }
124 
125     public List<BlogsEntry> findByOrganizationId(
126             long organizationId, Date displayDate, boolean draft, int start,
127             int end)
128         throws SystemException {
129 
130         List<Long> organizationIds = new ArrayList<Long>();
131 
132         organizationIds.add(organizationId);
133 
134         return findByOrganizationIds(
135             organizationIds, displayDate, draft, start, end);
136     }
137 
138     public List<BlogsEntry> findByOrganizationIds(
139             List<Long> organizationIds, Date displayDate, boolean draft,
140             int start, int end)
141         throws SystemException {
142 
143         Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
144 
145         Session session = null;
146 
147         try {
148             session = openSession();
149 
150             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
151 
152             sql = StringUtil.replace(
153                 sql, "[$ORGANIZATION_ID$]",
154                 getOrganizationIds(organizationIds));
155 
156             SQLQuery q = session.createSQLQuery(sql);
157 
158             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
159 
160             QueryPos qPos = QueryPos.getInstance(q);
161 
162             for (int i = 0; i < organizationIds.size(); i++) {
163                 Long organizationId = organizationIds.get(i);
164 
165                 qPos.add(organizationId);
166             }
167 
168             qPos.add(displayDate_TS);
169             qPos.add(draft);
170 
171             return (List<BlogsEntry>)QueryUtil.list(
172                 q, getDialect(), start, end);
173         }
174         catch (Exception e) {
175             throw new SystemException(e);
176         }
177         finally {
178             closeSession(session);
179         }
180     }
181 
182     public List<BlogsEntry> findByNoAssets() throws SystemException {
183         Session session = null;
184 
185         try {
186             session = openSession();
187 
188             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
189 
190             SQLQuery q = session.createSQLQuery(sql);
191 
192             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
193 
194             return q.list();
195         }
196         catch (Exception e) {
197             throw new SystemException(e);
198         }
199         finally {
200             closeSession(session);
201         }
202     }
203 
204     protected String getOrganizationIds(List<Long> organizationIds) {
205         StringBuilder sb = new StringBuilder();
206 
207         for (int i = 0; i < organizationIds.size(); i++) {
208             sb.append("Users_Orgs.organizationId = ? ");
209 
210             if ((i + 1) != organizationIds.size()) {
211                 sb.append("OR ");
212             }
213         }
214 
215         return sb.toString();
216     }
217 
218 }