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.StatusConstants;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.service.GroupLocalServiceUtil;
29  import com.liferay.portal.service.SubscriptionLocalServiceUtil;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.messageboards.model.MBCategory;
33  import com.liferay.portlet.messageboards.model.MBCategoryConstants;
34  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
35  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
36  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
37  import com.liferay.util.dao.orm.CustomSQLUtil;
38  
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="MBCategoryFinderImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Raymond Augé
46   */
47  public class MBCategoryFinderImpl
48      extends BasePersistenceImpl<MBCategory> implements MBCategoryFinder {
49  
50      public static String COUNT_BY_S_G_U =
51          MBCategoryFinder.class.getName() + ".countByS_G_U";
52  
53      public static String FIND_BY_S_G_U =
54          MBCategoryFinder.class.getName() + ".findByS_G_U";
55  
56      public int countByS_G_U(long groupId, long userId) throws SystemException {
57          Session session = null;
58  
59          try {
60              session = openSession();
61  
62              String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
63  
64              SQLQuery q = session.createSQLQuery(sql);
65  
66              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
67  
68              QueryPos qPos = QueryPos.getInstance(q);
69  
70              qPos.add(PortalUtil.getClassNameId(MBCategory.class.getName()));
71              qPos.add(groupId);
72              qPos.add(userId);
73  
74              int count = 0;
75  
76              Iterator<Long> itr = q.list().iterator();
77  
78              if (itr.hasNext()) {
79                  Long l = itr.next();
80  
81                  if (l != null) {
82                      count = l.intValue();
83                  }
84              }
85  
86              try {
87                  Group group = GroupLocalServiceUtil.getGroup(groupId);
88  
89                  SubscriptionLocalServiceUtil.getSubscription(
90                      group.getCompanyId(), userId, MBCategory.class.getName(),
91                      groupId);
92  
93                  count++;
94              }
95              catch (NoSuchSubscriptionException nsse) {
96              }
97  
98              return count;
99          }
100         catch (Exception e) {
101             throw new SystemException(e);
102         }
103         finally {
104             closeSession(session);
105         }
106     }
107 
108     public List<MBCategory> findByS_G_U(
109             long groupId, long userId, int start, int end)
110         throws SystemException {
111 
112         Session session = null;
113 
114         try {
115             session = openSession();
116 
117             String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
118 
119             SQLQuery q = session.createSQLQuery(sql);
120 
121             q.addEntity("MBCategory", MBCategoryImpl.class);
122 
123             QueryPos qPos = QueryPos.getInstance(q);
124 
125             qPos.add(PortalUtil.getClassNameId(MBCategory.class.getName()));
126             qPos.add(groupId);
127             qPos.add(userId);
128 
129             List<MBCategory> list = (List<MBCategory>)QueryUtil.list(
130                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, false);
131 
132             try {
133                 Group group = GroupLocalServiceUtil.getGroup(groupId);
134 
135                 SubscriptionLocalServiceUtil.getSubscription(
136                     group.getCompanyId(), userId, MBCategory.class.getName(),
137                     groupId);
138 
139                 int threadCount =
140                     MBThreadLocalServiceUtil.getCategoryThreadsCount(
141                         groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
142                         StatusConstants.APPROVED);
143                 int messageCount =
144                     MBMessageLocalServiceUtil.getCategoryMessagesCount(
145                         groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
146                         StatusConstants.APPROVED);
147 
148                 MBCategory category = new MBCategoryImpl();
149 
150                 category.setCompanyId(group.getCompanyId());
151                 category.setName(group.getName());
152                 category.setDescription(group.getDescription());
153                 category.setThreadCount(threadCount);
154                 category.setMessageCount(messageCount);
155 
156                 list.add(category);
157             }
158             catch (NoSuchSubscriptionException nsse) {
159             }
160 
161             return new UnmodifiableList<MBCategory>(
162                 ListUtil.subList(list, start, end));
163         }
164         catch (Exception e) {
165             throw new SystemException(e);
166         }
167         finally {
168             closeSession(session);
169         }
170     }
171 
172 }