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.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.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.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                            try {
156                                    Group group = GroupLocalServiceUtil.getGroup(groupId);
157    
158                                    SubscriptionLocalServiceUtil.getSubscription(
159                                            group.getCompanyId(), userId, MBCategory.class.getName(),
160                                            groupId);
161    
162                                    count++;
163                            }
164                            catch (NoSuchSubscriptionException nsse) {
165                            }
166    
167                            return count;
168                    }
169                    catch (Exception e) {
170                            throw new SystemException(e);
171                    }
172                    finally {
173                            closeSession(session);
174                    }
175            }
176    
177            protected List<MBCategory> doFindByS_G_U_P(
178                    long groupId, long userId, long[] parentCategoryIds,
179                    QueryDefinition<MBCategory> queryDefinition, boolean inlineSQLHelper) {
180    
181                    Session session = null;
182    
183                    try {
184                            session = openSession();
185    
186                            String sql = CustomSQLUtil.get(FIND_BY_S_G_U_P);
187    
188                            if (ArrayUtil.isEmpty(parentCategoryIds)) {
189                                    sql = StringUtil.replace(
190                                            sql, "(MBCategory.parentCategoryId = ?) AND",
191                                            StringPool.BLANK);
192                            }
193                            else {
194                                    sql = StringUtil.replace(
195                                            sql, "MBCategory.parentCategoryId = ?",
196                                            "MBCategory.parentCategoryId = " +
197                                                    StringUtil.merge(
198                                                            parentCategoryIds,
199                                                            " OR MBCategory.parentCategoryId = "));
200                            }
201    
202                            sql = updateSQL(sql, queryDefinition);
203    
204                            if (inlineSQLHelper) {
205                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
206                                            sql, MBCategory.class.getName(), "MBCategory.categoryId",
207                                            groupId);
208                            }
209    
210                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
211    
212                            q.addEntity("MBCategory", MBCategoryImpl.class);
213    
214                            QueryPos qPos = QueryPos.getInstance(q);
215    
216                            qPos.add(PortalUtil.getClassNameId(MBCategory.class.getName()));
217                            qPos.add(groupId);
218                            qPos.add(userId);
219    
220                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
221                                    qPos.add(queryDefinition.getStatus());
222                            }
223    
224                            List<MBCategory> list = (List<MBCategory>)QueryUtil.list(
225                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, false);
226    
227                            try {
228                                    Group group = GroupLocalServiceUtil.getGroup(groupId);
229    
230                                    SubscriptionLocalServiceUtil.getSubscription(
231                                            group.getCompanyId(), userId, MBCategory.class.getName(),
232                                            groupId);
233    
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                            catch (NoSuchSubscriptionException nsse) {
255                            }
256    
257                            return Collections.unmodifiableList(
258                                    ListUtil.subList(
259                                            list, queryDefinition.getStart(),
260                                            queryDefinition.getEnd()));
261                    }
262                    catch (Exception e) {
263                            throw new SystemException(e);
264                    }
265                    finally {
266                            closeSession(session);
267                    }
268            }
269    
270            protected String updateSQL(
271                    String sql, QueryDefinition<MBCategory> queryDefinition) {
272    
273                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
274                            return sql;
275                    }
276    
277                    if (queryDefinition.isExcludeStatus()) {
278                            return CustomSQLUtil.appendCriteria(
279                                    sql, "AND (MBCategory.status != ?)");
280                    }
281    
282                    return CustomSQLUtil.appendCriteria(sql, "AND (MBCategory.status = ?)");
283            }
284    
285    }