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