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