1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.NoSuchSubscriptionException;
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.exception.SystemException;
24  import com.liferay.portal.kernel.util.ListUtil;
25  import com.liferay.portal.kernel.util.UnmodifiableList;
26  import com.liferay.portal.kernel.workflow.WorkflowConstants;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.security.permission.InlineSQLHelperUtil;
29  import com.liferay.portal.service.GroupLocalServiceUtil;
30  import com.liferay.portal.service.SubscriptionLocalServiceUtil;
31  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portlet.messageboards.model.MBCategory;
34  import com.liferay.portlet.messageboards.model.MBCategoryConstants;
35  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
36  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
37  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
38  import com.liferay.util.dao.orm.CustomSQLUtil;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="MBCategoryFinderImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Raymond Augé
47   */
48  public class MBCategoryFinderImpl
49      extends BasePersistenceImpl<MBCategory> implements MBCategoryFinder {
50  
51      public static String COUNT_BY_S_G_U =
52          MBCategoryFinder.class.getName() + ".countByS_G_U";
53  
54      public static String FIND_BY_S_G_U =
55          MBCategoryFinder.class.getName() + ".findByS_G_U";
56  
57      public int countByS_G_U(long groupId, long userId) throws SystemException {
58          return doCountByS_G_U(groupId, userId, false);
59      }
60  
61      public int filterCountByS_G_U(long groupId, long userId)
62          throws SystemException {
63  
64          return doCountByS_G_U(groupId, userId, true);
65      }
66  
67      public List<MBCategory> filterFindByS_G_U(
68              long groupId, long userId, int start, int end)
69          throws SystemException {
70  
71          return doFindByS_G_U(groupId, userId, start, end, true);
72      }
73  
74      public List<MBCategory> findByS_G_U(
75              long groupId, long userId, int start, int end)
76          throws SystemException {
77  
78          return doFindByS_G_U(groupId, userId, start, end, false);
79      }
80  
81      protected int doCountByS_G_U(
82              long groupId, long userId, boolean inlineSQLHelper)
83          throws SystemException {
84  
85          Session session = null;
86  
87          try {
88              session = openSession();
89  
90              String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
91  
92              if (inlineSQLHelper) {
93                  sql = InlineSQLHelperUtil.replacePermissionCheck(
94                      sql, MBCategory.class.getName(), "MBCategory.categoryId",
95                      "MBCategory.userId", groupId);
96              }
97  
98              SQLQuery q = session.createSQLQuery(sql);
99  
100             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
101 
102             QueryPos qPos = QueryPos.getInstance(q);
103 
104             qPos.add(PortalUtil.getClassNameId(MBCategory.class.getName()));
105             qPos.add(groupId);
106             qPos.add(userId);
107 
108             int count = 0;
109 
110             Iterator<Long> itr = q.list().iterator();
111 
112             if (itr.hasNext()) {
113                 Long l = itr.next();
114 
115                 if (l != null) {
116                     count = l.intValue();
117                 }
118             }
119 
120             try {
121                 Group group = GroupLocalServiceUtil.getGroup(groupId);
122 
123                 SubscriptionLocalServiceUtil.getSubscription(
124                     group.getCompanyId(), userId, MBCategory.class.getName(),
125                     groupId);
126 
127                 count++;
128             }
129             catch (NoSuchSubscriptionException nsse) {
130             }
131 
132             return count;
133         }
134         catch (Exception e) {
135             throw new SystemException(e);
136         }
137         finally {
138             closeSession(session);
139         }
140     }
141 
142     protected List<MBCategory> doFindByS_G_U(
143             long groupId, long userId, int start, int end,
144             boolean inlineSQLHelper)
145         throws SystemException {
146 
147         Session session = null;
148 
149         try {
150             session = openSession();
151 
152             String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
153 
154             if (inlineSQLHelper) {
155                 sql = InlineSQLHelperUtil.replacePermissionCheck(
156                     sql, MBCategory.class.getName(), "MBCategory.categoryId",
157                     "MBCategory.userId", groupId);
158             }
159 
160             SQLQuery q = session.createSQLQuery(sql);
161 
162             q.addEntity("MBCategory", MBCategoryImpl.class);
163 
164             QueryPos qPos = QueryPos.getInstance(q);
165 
166             qPos.add(PortalUtil.getClassNameId(MBCategory.class.getName()));
167             qPos.add(groupId);
168             qPos.add(userId);
169 
170             List<MBCategory> list = (List<MBCategory>)QueryUtil.list(
171                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, false);
172 
173             try {
174                 Group group = GroupLocalServiceUtil.getGroup(groupId);
175 
176                 SubscriptionLocalServiceUtil.getSubscription(
177                     group.getCompanyId(), userId, MBCategory.class.getName(),
178                     groupId);
179 
180                 int threadCount =
181                     MBThreadLocalServiceUtil.getCategoryThreadsCount(
182                         groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
183                         WorkflowConstants.STATUS_APPROVED);
184                 int messageCount =
185                     MBMessageLocalServiceUtil.getCategoryMessagesCount(
186                         groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
187                         WorkflowConstants.STATUS_APPROVED);
188 
189                 MBCategory category = new MBCategoryImpl();
190 
191                 category.setCompanyId(group.getCompanyId());
192                 category.setName(group.getName());
193                 category.setDescription(group.getDescription());
194                 category.setThreadCount(threadCount);
195                 category.setMessageCount(messageCount);
196 
197                 list.add(category);
198             }
199             catch (NoSuchSubscriptionException nsse) {
200             }
201 
202             return new UnmodifiableList<MBCategory>(
203                 ListUtil.subList(list, start, end));
204         }
205         catch (Exception e) {
206             throw new SystemException(e);
207         }
208         finally {
209             closeSession(session);
210         }
211     }
212 
213 }