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.announcements.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.PortalException;
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.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.announcements.model.AnnouncementsEntry;
031    import com.liferay.portlet.announcements.model.AnnouncementsFlagConstants;
032    import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.sql.Timestamp;
036    
037    import java.util.Date;
038    import java.util.Iterator;
039    import java.util.LinkedHashMap;
040    import java.util.List;
041    import java.util.Map;
042    
043    /**
044     * @author Thiago Moreira
045     * @author Raymond Augé
046     */
047    public class AnnouncementsEntryFinderImpl
048            extends BasePersistenceImpl<AnnouncementsEntry>
049            implements AnnouncementsEntryFinder {
050    
051            public static String COUNT_BY_HIDDEN =
052                    AnnouncementsEntryFinder.class.getName() + ".countByHidden";
053    
054            public static String COUNT_BY_NOT_HIDDEN =
055                    AnnouncementsEntryFinder.class.getName() + ".countByNotHidden";
056    
057            public static String FIND_BY_DISPLAY_DATE =
058                    AnnouncementsEntryFinder.class.getName() + ".findByDisplayDate";
059    
060            public static String FIND_BY_HIDDEN =
061                    AnnouncementsEntryFinder.class.getName() + ".findByHidden";
062    
063            public static String FIND_BY_NOT_HIDDEN =
064                    AnnouncementsEntryFinder.class.getName() + ".findByNotHidden";
065    
066            public int countByScope(
067                            long userId, long classNameId, long[] classPKs,
068                            int displayDateMonth, int displayDateDay, int displayDateYear,
069                            int displayDateHour, int displayDateMinute, int expirationDateMonth,
070                            int expirationDateDay, int expirationDateYear,
071                            int expirationDateHour, int expirationDateMinute, boolean alert,
072                            int flagValue)
073                    throws SystemException {
074    
075                    Session session = null;
076    
077                    try {
078                            session = openSession();
079    
080                            String sql = CustomSQLUtil.get(COUNT_BY_HIDDEN);
081    
082                            if (flagValue == AnnouncementsFlagConstants.NOT_HIDDEN) {
083                                    sql = CustomSQLUtil.get(COUNT_BY_NOT_HIDDEN);
084                            }
085    
086                            sql = StringUtil.replace(
087                                    sql, "[$CLASS_PKS$]", getClassPKs(classNameId, classPKs));
088                            sql = CustomSQLUtil.replaceAndOperator(sql, true);
089    
090                            SQLQuery q = session.createSQLQuery(sql);
091    
092                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
093    
094                            QueryPos qPos = QueryPos.getInstance(q);
095    
096                            setClassPKs(qPos, classNameId, classPKs);
097    
098                            setDates(
099                                    qPos, displayDateMonth, displayDateDay, displayDateYear,
100                                    displayDateHour, displayDateMinute, expirationDateMonth,
101                                    expirationDateDay, expirationDateYear, expirationDateHour,
102                                    expirationDateMinute);
103    
104                            qPos.add(alert);
105                            qPos.add(userId);
106                            qPos.add(AnnouncementsFlagConstants.HIDDEN);
107    
108                            Iterator<Long> itr = q.list().iterator();
109    
110                            if (itr.hasNext()) {
111                                    Long count = itr.next();
112    
113                                    if (count != null) {
114                                            return count.intValue();
115                                    }
116                            }
117    
118                            return 0;
119                    }
120                    catch (Exception e) {
121                            throw new SystemException(e);
122                    }
123                    finally {
124                            closeSession(session);
125                    }
126            }
127    
128            public int countByScopes(
129                            long userId, LinkedHashMap<Long, long[]> scopes,
130                            int displayDateMonth, int displayDateDay, int displayDateYear,
131                            int displayDateHour, int displayDateMinute, int expirationDateMonth,
132                            int expirationDateDay, int expirationDateYear,
133                            int expirationDateHour, int expirationDateMinute, boolean alert,
134                            int flagValue)
135                    throws SystemException {
136    
137                    Session session = null;
138    
139                    try {
140                            session = openSession();
141    
142                            String sql = CustomSQLUtil.get(COUNT_BY_HIDDEN);
143    
144                            if (flagValue == AnnouncementsFlagConstants.NOT_HIDDEN) {
145                                    sql = CustomSQLUtil.get(COUNT_BY_NOT_HIDDEN);
146                            }
147    
148                            sql = StringUtil.replace(sql, "[$CLASS_PKS$]", getClassPKs(scopes));
149                            sql = CustomSQLUtil.replaceAndOperator(sql, true);
150    
151                            SQLQuery q = session.createSQLQuery(sql);
152    
153                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
154    
155                            QueryPos qPos = QueryPos.getInstance(q);
156    
157                            setClassPKs(qPos, scopes);
158    
159                            setDates(
160                                    qPos, displayDateMonth, displayDateDay, displayDateYear,
161                                    displayDateHour, displayDateMinute, expirationDateMonth,
162                                    expirationDateDay, expirationDateYear, expirationDateHour,
163                                    expirationDateMinute);
164    
165                            qPos.add(alert);
166                            qPos.add(userId);
167                            qPos.add(AnnouncementsFlagConstants.HIDDEN);
168    
169                            Iterator<Long> itr = q.list().iterator();
170    
171                            if (itr.hasNext()) {
172                                    Long count = itr.next();
173    
174                                    if (count != null) {
175                                            return count.intValue();
176                                    }
177                            }
178    
179                            return 0;
180                    }
181                    catch (Exception e) {
182                            throw new SystemException(e);
183                    }
184                    finally {
185                            closeSession(session);
186                    }
187            }
188    
189            public List<AnnouncementsEntry> findByDisplayDate(
190                            Date displayDateLT, Date displayDateGT)
191                    throws SystemException {
192    
193                    Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
194                    Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
195    
196                    Session session = null;
197    
198                    try {
199                            session = openSession();
200    
201                            String sql = CustomSQLUtil.get(FIND_BY_DISPLAY_DATE);
202    
203                            SQLQuery q = session.createSQLQuery(sql);
204    
205                            q.addEntity("AnnouncementsEntry", AnnouncementsEntryImpl.class);
206    
207                            QueryPos qPos = QueryPos.getInstance(q);
208    
209                            qPos.add(displayDateGT_TS);
210                            qPos.add(displayDateLT_TS);
211    
212                            return q.list();
213                    }
214                    catch (Exception e) {
215                            throw new SystemException(e);
216                    }
217                    finally {
218                            closeSession(session);
219                    }
220            }
221    
222            public List<AnnouncementsEntry> findByScope(
223                            long userId, long classNameId, long[] classPKs,
224                            int displayDateMonth, int displayDateDay, int displayDateYear,
225                            int displayDateHour, int displayDateMinute, int expirationDateMonth,
226                            int expirationDateDay, int expirationDateYear,
227                            int expirationDateHour, int expirationDateMinute, boolean alert,
228                            int flagValue, int start, int end)
229                    throws SystemException {
230    
231                    Session session = null;
232    
233                    try {
234                            session = openSession();
235    
236                            String sql = CustomSQLUtil.get(FIND_BY_HIDDEN);
237    
238                            if (flagValue == AnnouncementsFlagConstants.NOT_HIDDEN) {
239                                    sql = CustomSQLUtil.get(FIND_BY_NOT_HIDDEN);
240                            }
241    
242                            sql = StringUtil.replace(
243                                    sql, "[$CLASS_PKS$]", getClassPKs(classNameId, classPKs));
244                            sql = CustomSQLUtil.replaceAndOperator(sql, true);
245    
246                            SQLQuery q = session.createSQLQuery(sql);
247    
248                            q.addEntity("AnnouncementsEntry", AnnouncementsEntryImpl.class);
249    
250                            QueryPos qPos = QueryPos.getInstance(q);
251    
252                            setClassPKs(qPos, classNameId, classPKs);
253    
254                            setDates(
255                                    qPos, displayDateMonth, displayDateDay, displayDateYear,
256                                    displayDateHour, displayDateMinute, expirationDateMonth,
257                                    expirationDateDay, expirationDateYear, expirationDateHour,
258                                    expirationDateMinute);
259    
260                            qPos.add(alert);
261                            qPos.add(userId);
262                            qPos.add(AnnouncementsFlagConstants.HIDDEN);
263    
264                            return (List<AnnouncementsEntry>)QueryUtil.list(
265                                    q, getDialect(), start, end);
266                    }
267                    catch (Exception e) {
268                            throw new SystemException(e);
269                    }
270                    finally {
271                            closeSession(session);
272                    }
273            }
274    
275            public List<AnnouncementsEntry> findByScopes(
276                            long userId, LinkedHashMap<Long, long[]> scopes,
277                            int displayDateMonth, int displayDateDay, int displayDateYear,
278                            int displayDateHour, int displayDateMinute, int expirationDateMonth,
279                            int expirationDateDay, int expirationDateYear,
280                            int expirationDateHour, int expirationDateMinute, boolean alert,
281                            int flagValue, int start, int end)
282                    throws SystemException {
283    
284                    Session session = null;
285    
286                    try {
287                            session = openSession();
288    
289                            String sql = CustomSQLUtil.get(FIND_BY_HIDDEN);
290    
291                            if (flagValue == AnnouncementsFlagConstants.NOT_HIDDEN) {
292                                    sql = CustomSQLUtil.get(FIND_BY_NOT_HIDDEN);
293                            }
294    
295                            sql = StringUtil.replace(sql, "[$CLASS_PKS$]", getClassPKs(scopes));
296                            sql = CustomSQLUtil.replaceAndOperator(sql, true);
297    
298                            SQLQuery q = session.createSQLQuery(sql);
299    
300                            q.addEntity("AnnouncementsEntry", AnnouncementsEntryImpl.class);
301    
302                            QueryPos qPos = QueryPos.getInstance(q);
303    
304                            setClassPKs(qPos, scopes);
305    
306                            setDates(
307                                    qPos, displayDateMonth, displayDateDay, displayDateYear,
308                                    displayDateHour, displayDateMinute, expirationDateMonth,
309                                    expirationDateDay, expirationDateYear, expirationDateHour,
310                                    expirationDateMinute);
311    
312                            qPos.add(alert);
313                            qPos.add(userId);
314                            qPos.add(AnnouncementsFlagConstants.HIDDEN);
315    
316                            return (List<AnnouncementsEntry>)QueryUtil.list(
317                                    q, getDialect(), start, end);
318                    }
319                    catch (Exception e) {
320                            throw new SystemException(e);
321                    }
322                    finally {
323                            closeSession(session);
324                    }
325            }
326    
327            protected String getClassPKs(long classNameId, long[] classPKs) {
328                    if (classPKs.length == 0) {
329                            return "(AnnouncementsEntry.classNameId = ?) AND (";
330                    }
331    
332                    StringBundler sb = new StringBundler(classPKs.length * 2 + 1);
333    
334                    sb.append("(AnnouncementsEntry.classNameId = ?) AND (");
335    
336                    for (int i = 0; i < classPKs.length; i++) {
337                            sb.append("(AnnouncementsEntry.classPK = ?)");
338    
339                            if ((i + 1) < classPKs.length) {
340                                    sb.append(" OR ");
341                            }
342                            else {
343                                    sb.append(")");
344                            }
345                    }
346    
347                    return sb.toString();
348            }
349    
350            protected String getClassPKs(LinkedHashMap<Long, long[]> scopes) {
351                    if ((scopes == null) || scopes.isEmpty()) {
352                            return StringPool.BLANK;
353                    }
354    
355                    StringBundler sb = new StringBundler(scopes.size() * 4 - 1);
356    
357                    Iterator<Map.Entry<Long, long[]>> itr = scopes.entrySet().iterator();
358    
359                    while (itr.hasNext()) {
360                            Map.Entry<Long, long[]> entry = itr.next();
361    
362                            Long classNameId = entry.getKey();
363                            long[] classPKs = entry.getValue();
364    
365                            sb.append("(");
366                            sb.append(getClassPKs(classNameId.longValue(), classPKs));
367                            sb.append(")");
368    
369                            if (itr.hasNext()) {
370                                    sb.append(" OR ");
371                            }
372                    }
373    
374                    return sb.toString();
375            }
376    
377            protected void setClassPKs(
378                    QueryPos qPos, long classNameId, long[] classPKs) {
379    
380                    qPos.add(classNameId);
381    
382                    for (int i = 0; i < classPKs.length; i++) {
383                            qPos.add(classPKs[i]);
384                    }
385            }
386    
387            protected void setClassPKs(
388                    QueryPos qPos, LinkedHashMap<Long, long[]> scopes) {
389    
390                    if (scopes == null) {
391                            return;
392                    }
393    
394                    Iterator<Map.Entry<Long, long[]>> itr = scopes.entrySet().iterator();
395    
396                    while (itr.hasNext()) {
397                            Map.Entry<Long, long[]> entry = itr.next();
398    
399                            Long classNameId = entry.getKey();
400                            long[] classPKs = entry.getValue();
401    
402                            setClassPKs(qPos, classNameId.longValue(), classPKs);
403                    }
404            }
405    
406            protected void setDates(
407                    QueryPos qPos, int displayDateMonth, int displayDateDay,
408                    int displayDateYear, int displayDateHour, int displayDateMinute,
409                    int expirationDateMonth, int expirationDateDay, int expirationDateYear,
410                    int expirationDateHour, int expirationDateMinute) {
411    
412                    Date displayDate = null;
413    
414                    try {
415                            displayDate = PortalUtil.getDate(
416                                    displayDateMonth, displayDateDay, displayDateYear,
417                                    displayDateHour, displayDateMinute, new PortalException());
418                    }
419                    catch (PortalException pe) {
420                            displayDate = new Date();
421                    }
422    
423                    Timestamp displayDateTS = CalendarUtil.getTimestamp(displayDate);
424    
425                    Date expirationDate = null;
426    
427                    try {
428                            expirationDate = PortalUtil.getDate(
429                                    expirationDateMonth, expirationDateDay, expirationDateYear,
430                                    expirationDateHour, expirationDateMinute,
431                                    new PortalException());
432                    }
433                    catch (PortalException pe) {
434                            expirationDate = new Date();
435                    }
436    
437                    Timestamp expirationDateTS = CalendarUtil.getTimestamp(expirationDate);
438    
439                    qPos.add(displayDateTS);
440                    qPos.add(displayDateTS);
441                    qPos.add(expirationDateTS);
442                    qPos.add(expirationDateTS);
443            }
444    
445    }