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