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.blogs.service.persistence;
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.CalendarUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
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.service.persistence.impl.BasePersistenceImpl;
030    import com.liferay.portlet.blogs.model.BlogsEntry;
031    import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.sql.Timestamp;
035    
036    import java.util.ArrayList;
037    import java.util.Date;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class BlogsEntryFinderImpl
045            extends BasePersistenceImpl<BlogsEntry> implements BlogsEntryFinder {
046    
047            public static final String COUNT_BY_ORGANIZATION_IDS =
048                    BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
049    
050            public static final String FIND_BY_GROUP_IDS =
051                    BlogsEntryFinder.class.getName() + ".findByGroupIds";
052    
053            public static final String FIND_BY_ORGANIZATION_IDS =
054                    BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
055    
056            public static final String FIND_BY_NO_ASSETS =
057                    BlogsEntryFinder.class.getName() + ".findByNoAssets";
058    
059            @Override
060            public int countByOrganizationId(
061                            long organizationId, Date displayDate,
062                            QueryDefinition queryDefinition)
063                    throws SystemException {
064    
065                    List<Long> organizationIds = new ArrayList<Long>();
066    
067                    organizationIds.add(organizationId);
068    
069                    return countByOrganizationIds(
070                            organizationIds, displayDate, queryDefinition);
071            }
072    
073            @Override
074            public int countByOrganizationIds(
075                            List<Long> organizationIds, Date displayDate,
076                            QueryDefinition queryDefinition)
077                    throws SystemException {
078    
079                    Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
080    
081                    Session session = null;
082    
083                    try {
084                            session = openSession();
085    
086                            String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
087    
088                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
089                                    if (queryDefinition.isExcludeStatus()) {
090                                            sql = CustomSQLUtil.appendCriteria(
091                                                    sql, "AND (BlogsEntry.status != ?)");
092                                    }
093                                    else {
094                                            sql = CustomSQLUtil.appendCriteria(
095                                                    sql, "AND (BlogsEntry.status = ?)");
096                                    }
097                            }
098    
099                            sql = StringUtil.replace(
100                                    sql, "[$ORGANIZATION_ID$]",
101                                    getOrganizationIds(organizationIds));
102    
103                            SQLQuery q = session.createSQLQuery(sql);
104    
105                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
106    
107                            QueryPos qPos = QueryPos.getInstance(q);
108    
109                            for (int i = 0; i < organizationIds.size(); i++) {
110                                    Long organizationId = organizationIds.get(i);
111    
112                                    qPos.add(organizationId);
113                            }
114    
115                            qPos.add(displayDate_TS);
116    
117                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
118                                    qPos.add(queryDefinition.getStatus());
119                            }
120    
121                            Iterator<Long> itr = q.iterate();
122    
123                            if (itr.hasNext()) {
124                                    Long count = itr.next();
125    
126                                    if (count != null) {
127                                            return count.intValue();
128                                    }
129                            }
130    
131                            return 0;
132                    }
133                    catch (Exception e) {
134                            throw new SystemException(e);
135                    }
136                    finally {
137                            closeSession(session);
138                    }
139            }
140    
141            @Override
142            public List<BlogsEntry> findByGroupIds(
143                            long companyId, long groupId, Date displayDate,
144                            QueryDefinition queryDefinition)
145                    throws SystemException {
146    
147                    Session session = null;
148    
149                    try {
150                            session = openSession();
151    
152                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_IDS);
153    
154                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
155                                    if (queryDefinition.isExcludeStatus()) {
156                                            sql = CustomSQLUtil.appendCriteria(
157                                                    sql, "AND (BlogsEntry.status != ?)");
158                                    }
159                                    else {
160                                            sql = CustomSQLUtil.appendCriteria(
161                                                    sql, "AND (BlogsEntry.status = ?)");
162                                    }
163                            }
164    
165                            SQLQuery q = session.createSQLQuery(sql);
166    
167                            q.addEntity("BlogsEntry", BlogsEntryImpl.class);
168    
169                            QueryPos qPos = QueryPos.getInstance(q);
170    
171                            qPos.add(companyId);
172                            qPos.add(groupId);
173                            qPos.add(groupId);
174                            qPos.add(groupId);
175                            qPos.add(displayDate);
176    
177                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
178                                    qPos.add(queryDefinition.getStatus());
179                            }
180    
181                            return (List<BlogsEntry>)QueryUtil.list(
182                                    q, getDialect(), queryDefinition.getStart(),
183                                    queryDefinition.getEnd());
184                    }
185                    catch (Exception e) {
186                            throw new SystemException(e);
187                    }
188                    finally {
189                            closeSession(session);
190                    }
191            }
192    
193            @Override
194            public List<BlogsEntry> findByOrganizationId(
195                            long organizationId, Date displayDate,
196                            QueryDefinition queryDefinition)
197                    throws SystemException {
198    
199                    List<Long> organizationIds = new ArrayList<Long>();
200    
201                    organizationIds.add(organizationId);
202    
203                    return findByOrganizationIds(
204                            organizationIds, displayDate, queryDefinition);
205            }
206    
207            @Override
208            public List<BlogsEntry> findByOrganizationIds(
209                            List<Long> organizationIds, Date displayDate,
210                            QueryDefinition queryDefinition)
211                    throws SystemException {
212    
213                    Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
214    
215                    Session session = null;
216    
217                    try {
218                            session = openSession();
219    
220                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
221    
222                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
223                                    if (queryDefinition.isExcludeStatus()) {
224                                            sql = CustomSQLUtil.appendCriteria(
225                                                    sql, "AND (BlogsEntry.status != ?)");
226                                    }
227                                    else {
228                                            sql = CustomSQLUtil.appendCriteria(
229                                                    sql, "AND (BlogsEntry.status = ?)");
230                                    }
231                            }
232    
233                            sql = StringUtil.replace(
234                                    sql, "[$ORGANIZATION_ID$]",
235                                    getOrganizationIds(organizationIds));
236                            sql = CustomSQLUtil.replaceOrderBy(
237                                    sql, queryDefinition.getOrderByComparator());
238    
239                            SQLQuery q = session.createSQLQuery(sql);
240    
241                            q.addEntity("BlogsEntry", BlogsEntryImpl.class);
242    
243                            QueryPos qPos = QueryPos.getInstance(q);
244    
245                            for (int i = 0; i < organizationIds.size(); i++) {
246                                    Long organizationId = organizationIds.get(i);
247    
248                                    qPos.add(organizationId);
249                            }
250    
251                            qPos.add(displayDate_TS);
252    
253                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
254                                    qPos.add(queryDefinition.getStatus());
255                            }
256    
257                            return (List<BlogsEntry>)QueryUtil.list(
258                                    q, getDialect(), queryDefinition.getStart(),
259                                    queryDefinition.getEnd());
260                    }
261                    catch (Exception e) {
262                            throw new SystemException(e);
263                    }
264                    finally {
265                            closeSession(session);
266                    }
267            }
268    
269            @Override
270            public List<BlogsEntry> findByNoAssets() throws SystemException {
271                    Session session = null;
272    
273                    try {
274                            session = openSession();
275    
276                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
277    
278                            SQLQuery q = session.createSQLQuery(sql);
279    
280                            q.addEntity("BlogsEntry", BlogsEntryImpl.class);
281    
282                            return q.list(true);
283                    }
284                    catch (Exception e) {
285                            throw new SystemException(e);
286                    }
287                    finally {
288                            closeSession(session);
289                    }
290            }
291    
292            protected String getOrganizationIds(List<Long> organizationIds) {
293                    if (organizationIds.isEmpty()) {
294                            return StringPool.BLANK;
295                    }
296    
297                    StringBundler sb = new StringBundler(organizationIds.size() * 2 - 1);
298    
299                    for (int i = 0; i < organizationIds.size(); i++) {
300                            sb.append("Users_Orgs.organizationId = ? ");
301    
302                            if ((i + 1) != organizationIds.size()) {
303                                    sb.append("OR ");
304                            }
305                    }
306    
307                    return sb.toString();
308            }
309    
310    }