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