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