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