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