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.calendar.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.exception.SystemException;
022    import com.liferay.portal.kernel.util.CalendarUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.calendar.model.CalEvent;
029    import com.liferay.portlet.calendar.model.CalEventConstants;
030    import com.liferay.portlet.calendar.model.impl.CalEventImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.sql.Timestamp;
034    
035    import java.util.Calendar;
036    import java.util.Date;
037    import java.util.Iterator;
038    import java.util.List;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Zsolt Balogh
043     */
044    public class CalEventFinderImpl
045            extends BasePersistenceImpl<CalEvent> implements CalEventFinder {
046    
047            public static final String COUNT_BY_G_SD_T =
048                    CalEventFinder.class.getName() + ".countByG_SD_T";
049    
050            public static final String FIND_BY_FUTURE_REMINDERS =
051                    CalEventFinder.class.getName() + ".findByFutureReminders";
052    
053            public static final String FIND_BY_NO_ASSETS =
054                    CalEventFinder.class.getName() + ".findByNoAssets";
055    
056            public static final String FIND_BY_G_SD_T =
057                    CalEventFinder.class.getName() + ".findByG_SD_T";
058    
059            public int countByG_SD_T(
060                            long groupId, Date startDateGT, Date startDateLT,
061                            boolean timeZoneSensitive, String[] types)
062                    throws SystemException {
063    
064                    Timestamp startDateGT_TS = CalendarUtil.getTimestamp(startDateGT);
065                    Timestamp startDateLT_TS = CalendarUtil.getTimestamp(startDateLT);
066    
067                    Session session = null;
068    
069                    try {
070                            session = openSession();
071    
072                            String sql = CustomSQLUtil.get(COUNT_BY_G_SD_T);
073    
074                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
075    
076                            SQLQuery q = session.createSQLQuery(sql);
077    
078                            q.addEntity("CalEvent", CalEventImpl.class);
079    
080                            QueryPos qPos = QueryPos.getInstance(q);
081    
082                            qPos.add(groupId);
083                            qPos.add(startDateGT_TS);
084                            qPos.add(startDateLT_TS);
085                            qPos.add(timeZoneSensitive);
086                            qPos.add(false);
087    
088                            if ((types != null) && (types.length > 0) &&
089                                    ((types.length > 1) || Validator.isNotNull(types[0]))) {
090    
091                                    for (String type : types) {
092                                            qPos.add(type);
093                                    }
094                            }
095    
096                            Iterator<Long> itr = q.iterate();
097    
098                            if (itr.hasNext()) {
099                                    Long count = itr.next();
100    
101                                    if (count != null) {
102                                            return count.intValue();
103                                    }
104                            }
105    
106                            return 0;
107                    }
108                    catch (Exception e) {
109                            throw new SystemException(e);
110                    }
111                    finally {
112                            closeSession(session);
113                    }
114            }
115    
116            public List<CalEvent> findByFutureReminders() throws SystemException {
117                    Calendar calendar = Calendar.getInstance();
118    
119                    calendar.add(Calendar.HOUR, -24);
120    
121                    Timestamp calendar_TS = CalendarUtil.getTimestamp(calendar.getTime());
122    
123                    Session session = null;
124    
125                    try {
126                            session = openSession();
127    
128                            String sql = CustomSQLUtil.get(FIND_BY_FUTURE_REMINDERS);
129    
130                            SQLQuery q = session.createSQLQuery(sql);
131    
132                            q.addEntity("CalEvent", CalEventImpl.class);
133    
134                            QueryPos qPos = QueryPos.getInstance(q);
135    
136                            qPos.add(CalEventConstants.REMIND_BY_NONE);
137                            qPos.add(calendar_TS);
138                            qPos.add(calendar_TS);
139    
140                            return q.list(true);
141                    }
142                    catch (Exception e) {
143                            throw new SystemException(e);
144                    }
145                    finally {
146                            closeSession(session);
147                    }
148            }
149    
150            public List<CalEvent> findByNoAssets() throws SystemException {
151                    Session session = null;
152    
153                    try {
154                            session = openSession();
155    
156                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
157    
158                            SQLQuery q = session.createSQLQuery(sql);
159    
160                            q.addEntity("CalEvent", CalEventImpl.class);
161    
162                            return q.list(true);
163                    }
164                    catch (Exception e) {
165                            throw new SystemException(e);
166                    }
167                    finally {
168                            closeSession(session);
169                    }
170            }
171    
172            public List<CalEvent> findByG_SD_T(
173                            long groupId, Date startDateGT, Date startDateLT,
174                            boolean timeZoneSensitive, String[] types)
175                    throws SystemException {
176    
177                    return findByG_SD_T(
178                            groupId, startDateGT, startDateLT, timeZoneSensitive, types,
179                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
180            }
181    
182            public List<CalEvent> findByG_SD_T(
183                            long groupId, Date startDateGT, Date startDateLT,
184                            boolean timeZoneSensitive, String[] types, int start, int end)
185                    throws SystemException {
186    
187                    Timestamp startDateGT_TS = CalendarUtil.getTimestamp(startDateGT);
188                    Timestamp startDateLT_TS = CalendarUtil.getTimestamp(startDateLT);
189    
190                    Session session = null;
191    
192                    try {
193                            session = openSession();
194    
195                            String sql = CustomSQLUtil.get(FIND_BY_G_SD_T);
196    
197                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
198    
199                            SQLQuery q = session.createSQLQuery(sql);
200    
201                            q.addEntity("CalEvent", CalEventImpl.class);
202    
203                            QueryPos qPos = QueryPos.getInstance(q);
204    
205                            qPos.add(groupId);
206                            qPos.add(startDateGT_TS);
207                            qPos.add(startDateLT_TS);
208                            qPos.add(timeZoneSensitive);
209                            qPos.add(false);
210    
211                            if ((types != null) && (types.length > 0) &&
212                                    ((types.length > 1) || Validator.isNotNull(types[0]))) {
213    
214                                    for (String type : types) {
215                                            qPos.add(type);
216                                    }
217                            }
218    
219                            return (List<CalEvent>)QueryUtil.list(q, getDialect(), start, end);
220                    }
221                    catch (Exception e) {
222                            throw new SystemException(e);
223                    }
224                    finally {
225                            closeSession(session);
226                    }
227            }
228    
229            protected String getTypes(String[] types) {
230                    if ((types != null) && (types.length > 0) &&
231                            ((types.length > 1) || Validator.isNotNull(types[0]))) {
232    
233                            StringBundler sb = new StringBundler(types.length * 2 + 1);
234    
235                            sb.append(" AND (");
236    
237                            for (int i = 0; i < types.length; i++) {
238                                    sb.append("type_ = ? ");
239    
240                                    if ((i + 1) != types.length) {
241                                            sb.append("OR ");
242                                    }
243                            }
244    
245                            sb.append(")");
246    
247                            return sb.toString();
248                    }
249    
250                    return StringPool.BLANK;
251            }
252    
253    }