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