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