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