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.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.portlet.calendar.service.persistence.CalEventFinder;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.sql.Timestamp;
035    
036    import java.util.Calendar;
037    import java.util.Date;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Zsolt Balogh
044     */
045    public class CalEventFinderImpl
046            extends BasePersistenceImpl<CalEvent> implements CalEventFinder {
047    
048            public static final String COUNT_BY_G_SD_T =
049                    CalEventFinder.class.getName() + ".countByG_SD_T";
050    
051            public static final String FIND_BY_FUTURE_REMINDERS =
052                    CalEventFinder.class.getName() + ".findByFutureReminders";
053    
054            public static final String FIND_BY_NO_ASSETS =
055                    CalEventFinder.class.getName() + ".findByNoAssets";
056    
057            public static final String FIND_BY_G_SD_T =
058                    CalEventFinder.class.getName() + ".findByG_SD_T";
059    
060            @Override
061            public int countByG_SD_T(
062                    long groupId, Date startDateGT, Date startDateLT,
063                    boolean timeZoneSensitive, String[] types) {
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.createSynchronizedSQLQuery(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() {
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.createSynchronizedSQLQuery(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() {
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.createSynchronizedSQLQuery(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    
180                    return findByG_SD_T(
181                            groupId, startDateGT, startDateLT, timeZoneSensitive, types,
182                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
183            }
184    
185            @Override
186            public List<CalEvent> findByG_SD_T(
187                    long groupId, Date startDateGT, Date startDateLT,
188                    boolean timeZoneSensitive, String[] types, int start, int end) {
189    
190                    Timestamp startDateGT_TS = CalendarUtil.getTimestamp(startDateGT);
191                    Timestamp startDateLT_TS = CalendarUtil.getTimestamp(startDateLT);
192    
193                    Session session = null;
194    
195                    try {
196                            session = openSession();
197    
198                            String sql = CustomSQLUtil.get(FIND_BY_G_SD_T);
199    
200                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
201    
202                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
203    
204                            q.addEntity("CalEvent", CalEventImpl.class);
205    
206                            QueryPos qPos = QueryPos.getInstance(q);
207    
208                            qPos.add(groupId);
209                            qPos.add(startDateGT_TS);
210                            qPos.add(startDateLT_TS);
211                            qPos.add(timeZoneSensitive);
212                            qPos.add(false);
213    
214                            if ((types != null) && (types.length > 0) &&
215                                    ((types.length > 1) || Validator.isNotNull(types[0]))) {
216    
217                                    for (String type : types) {
218                                            qPos.add(type);
219                                    }
220                            }
221    
222                            return (List<CalEvent>)QueryUtil.list(q, getDialect(), start, end);
223                    }
224                    catch (Exception e) {
225                            throw new SystemException(e);
226                    }
227                    finally {
228                            closeSession(session);
229                    }
230            }
231    
232            protected String getTypes(String[] types) {
233                    if ((types != null) && (types.length > 0) &&
234                            ((types.length > 1) || Validator.isNotNull(types[0]))) {
235    
236                            StringBundler sb = new StringBundler(types.length * 2 + 1);
237    
238                            sb.append(" AND (");
239    
240                            for (int i = 0; i < types.length; i++) {
241                                    sb.append("type_ = ? ");
242    
243                                    if ((i + 1) != types.length) {
244                                            sb.append("OR ");
245                                    }
246                            }
247    
248                            sb.append(StringPool.CLOSE_PARENTHESIS);
249    
250                            return sb.toString();
251                    }
252    
253                    return StringPool.BLANK;
254            }
255    
256    }