001
014
015 package com.liferay.portlet.messageboards.service.persistence;
016
017 import com.liferay.portal.NoSuchSubscriptionException;
018 import com.liferay.portal.kernel.dao.orm.QueryPos;
019 import com.liferay.portal.kernel.dao.orm.QueryUtil;
020 import com.liferay.portal.kernel.dao.orm.SQLQuery;
021 import com.liferay.portal.kernel.dao.orm.Session;
022 import com.liferay.portal.kernel.dao.orm.Type;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.util.ListUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.UnmodifiableList;
028 import com.liferay.portal.kernel.workflow.WorkflowConstants;
029 import com.liferay.portal.model.Group;
030 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
031 import com.liferay.portal.service.GroupLocalServiceUtil;
032 import com.liferay.portal.service.SubscriptionLocalServiceUtil;
033 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portlet.messageboards.model.MBCategory;
036 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
037 import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
038 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
039 import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
040 import com.liferay.util.dao.orm.CustomSQLUtil;
041
042 import java.util.Iterator;
043 import java.util.List;
044
045
048 public class MBCategoryFinderImpl
049 extends BasePersistenceImpl<MBCategory> implements MBCategoryFinder {
050
051 public static String COUNT_BY_S_G_U_P =
052 MBCategoryFinder.class.getName() + ".countByS_G_U_P";
053
054 public static String FIND_BY_S_G_U_P =
055 MBCategoryFinder.class.getName() + ".findByS_G_U_P";
056
057 public int countByS_G_U_P(
058 long groupId, long userId, long[] parentCategoryIds)
059 throws SystemException {
060
061 return doCountByS_G_U_P(groupId, userId, parentCategoryIds, false);
062 }
063
064 public int filterCountByS_G_U_P(
065 long groupId, long userId, long[] parentCategoryIds)
066 throws SystemException {
067
068 return doCountByS_G_U_P(groupId, userId, parentCategoryIds, true);
069 }
070
071 public List<MBCategory> filterFindByS_G_U_P(
072 long groupId, long userId, long[] parentCategoryIds, int start,
073 int end)
074 throws SystemException {
075
076 return doFindByS_G_U_P(
077 groupId, userId, parentCategoryIds, start, end, true);
078 }
079
080 public List<MBCategory> findByS_G_U_P(
081 long groupId, long userId, long[] parentCategoryIds, int start,
082 int end)
083 throws SystemException {
084
085 return doFindByS_G_U_P(
086 groupId, userId, parentCategoryIds, start, end, false);
087 }
088
089 protected int doCountByS_G_U_P(
090 long groupId, long userId, long[] parentCategoryIds,
091 boolean inlineSQLHelper)
092 throws SystemException {
093
094 Session session = null;
095
096 try {
097 session = openSession();
098
099 String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_P);
100
101 if ((parentCategoryIds == null) ||
102 (parentCategoryIds.length == 0)) {
103
104 sql = StringUtil.replace(
105 sql, "(MBCategory.parentCategoryId = ?) AND",
106 StringPool.BLANK);
107 }
108 else {
109 sql = StringUtil.replace(
110 sql, "MBCategory.parentCategoryId = ?",
111 "MBCategory.parentCategoryId = " +
112 StringUtil.merge(
113 parentCategoryIds,
114 " OR MBCategory.parentCategoryId = "));
115 }
116
117 if (inlineSQLHelper) {
118 sql = InlineSQLHelperUtil.replacePermissionCheck(
119 sql, MBCategory.class.getName(), "MBCategory.categoryId",
120 "MBCategory.userId", groupId);
121 }
122
123 SQLQuery q = session.createSQLQuery(sql);
124
125 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
126
127 QueryPos qPos = QueryPos.getInstance(q);
128
129 qPos.add(PortalUtil.getClassNameId(MBCategory.class.getName()));
130 qPos.add(groupId);
131 qPos.add(userId);
132
133 int count = 0;
134
135 Iterator<Long> itr = q.list().iterator();
136
137 if (itr.hasNext()) {
138 Long l = itr.next();
139
140 if (l != null) {
141 count = l.intValue();
142 }
143 }
144
145 try {
146 Group group = GroupLocalServiceUtil.getGroup(groupId);
147
148 SubscriptionLocalServiceUtil.getSubscription(
149 group.getCompanyId(), userId, MBCategory.class.getName(),
150 groupId);
151
152 count++;
153 }
154 catch (NoSuchSubscriptionException nsse) {
155 }
156
157 return count;
158 }
159 catch (Exception e) {
160 throw new SystemException(e);
161 }
162 finally {
163 closeSession(session);
164 }
165 }
166
167 protected List<MBCategory> doFindByS_G_U_P(
168 long groupId, long userId, long[] parentCategoryIds, int start,
169 int end, boolean inlineSQLHelper)
170 throws SystemException {
171
172 Session session = null;
173
174 try {
175 session = openSession();
176
177 String sql = CustomSQLUtil.get(FIND_BY_S_G_U_P);
178
179 if ((parentCategoryIds == null) ||
180 (parentCategoryIds.length == 0)) {
181
182 sql = StringUtil.replace(
183 sql, "(MBCategory.parentCategoryId = ?) AND",
184 StringPool.BLANK);
185 }
186 else {
187 sql = StringUtil.replace(
188 sql, "MBCategory.parentCategoryId = ?",
189 "MBCategory.parentCategoryId = " +
190 StringUtil.merge(
191 parentCategoryIds,
192 " OR MBCategory.parentCategoryId = "));
193 }
194
195 if (inlineSQLHelper) {
196 sql = InlineSQLHelperUtil.replacePermissionCheck(
197 sql, MBCategory.class.getName(), "MBCategory.categoryId",
198 "MBCategory.userId", groupId);
199 }
200
201 SQLQuery q = session.createSQLQuery(sql);
202
203 q.addEntity("MBCategory", MBCategoryImpl.class);
204
205 QueryPos qPos = QueryPos.getInstance(q);
206
207 qPos.add(PortalUtil.getClassNameId(MBCategory.class.getName()));
208 qPos.add(groupId);
209 qPos.add(userId);
210
211 List<MBCategory> list = (List<MBCategory>)QueryUtil.list(
212 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, false);
213
214 try {
215 Group group = GroupLocalServiceUtil.getGroup(groupId);
216
217 SubscriptionLocalServiceUtil.getSubscription(
218 group.getCompanyId(), userId, MBCategory.class.getName(),
219 groupId);
220
221 int threadCount =
222 MBThreadLocalServiceUtil.getCategoryThreadsCount(
223 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
224 WorkflowConstants.STATUS_APPROVED);
225 int messageCount =
226 MBMessageLocalServiceUtil.getCategoryMessagesCount(
227 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
228 WorkflowConstants.STATUS_APPROVED);
229
230 MBCategory category = new MBCategoryImpl();
231
232 category.setCompanyId(group.getCompanyId());
233 category.setName(group.getName());
234 category.setDescription(group.getDescription());
235 category.setThreadCount(threadCount);
236 category.setMessageCount(messageCount);
237
238 list.add(category);
239 }
240 catch (NoSuchSubscriptionException nsse) {
241 }
242
243 return new UnmodifiableList<MBCategory>(
244 ListUtil.subList(list, start, end));
245 }
246 catch (Exception e) {
247 throw new SystemException(e);
248 }
249 finally {
250 closeSession(session);
251 }
252 }
253
254 }