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