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