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