View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  
18  package org.exoplatform.calendar.ws;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.exoplatform.calendar.service.CalendarEvent;
22  import org.exoplatform.calendar.service.EventQuery;
23  import org.exoplatform.calendar.service.Utils;
24  import org.exoplatform.commons.utils.ISO8601;
25  
26  public class RestEventQuery extends EventQuery {
27  
28    @Override
29    public String getQueryStatement() throws Exception {
30      // events from user, groups, public calendars, and events that contains
31      // specific participant
32      StringBuilder sql = new StringBuilder("SELECT " + Utils.EXO_ID + " FROM ");
33      sql.append(Utils.EXO_CALENDAR_EVENT);
34      sql.append(" WHERE");
35  
36  //    if (getCalendarPath() != null) {
37  //      sql.append(" jcr:path LIKE '").append(getCalendarPath()).append("/%' AND NOT jcr:path LIKE '");
38  //      sql.append(getCalendarPath()).append("/%/%'");
39  //    }
40  
41      if (getCalendarId() != null || getParticipants() != null) {
42        sql.append(" AND (");
43        // calendarIds: public and groups, shared calendars
44        if (getCalendarId() != null) {
45          for (String calId : getCalendarId()) {
46            sql.append(" OR ").append(Utils.EXO_CALENDAR_ID).append(" = '").append(calId).append("'");
47          }
48        }
49        // participant
50        if (getParticipants() != null) {
51          for (String participant : getParticipants()) {
52            //workaround for case calendarRestApi.findEventsByCalendar
53            if (getCalendarPath() != null) {
54              sql.append(" AND ");
55            } else {
56              sql.append(" OR ");
57            }
58            
59            if (CalendarEvent.TYPE_TASK.equals(getEventType())) {
60              sql.append("CONTAINS(").append(Utils.EXO_TASK_DELEGATOR);
61              sql.append(",'").append(participant).append("')");
62            } else {
63              sql.append(Utils.EXO_PARTICIPANT)
64              .append(" = '")
65              .append(participant)
66              .append("'");            
67            }
68          }
69        }
70        sql.append(")");
71      }
72  
73      // date time
74      if (getFromDate() != null) {
75        sql.append(" AND (")
76           .append(Utils.EXO_FROM_DATE_TIME)
77           .append(" >= TIMESTAMP '")
78           .append(ISO8601.format(getFromDate()))
79           .append("')");
80      }
81      if (getToDate() != null) {
82        sql.append(" AND (")
83           .append(Utils.EXO_FROM_DATE_TIME)
84           .append(" <= TIMESTAMP '")
85           .append(ISO8601.format(getToDate()))
86           .append("')");
87      }
88      // category
89      String[] categoryIds = getCategoryId();
90      if (categoryIds != null && categoryIds.length > 0) {
91        sql.append(" AND (");
92        for (int i = 0; i < categoryIds.length; i++) {
93          sql.append(Utils.EXO_EVENT_CATEGORYID);
94          sql.append(" = '").append(categoryIds[i]).append("'");
95          if (i < categoryIds.length - 1) {
96            sql.append(" OR ");
97          }
98        }
99        sql.append(")");
100     }
101     // event or task
102     if (!Utils.isEmpty(getEventType())) {
103       sql.append(" AND ").append(Utils.EXO_EVENT_TYPE).append("='");
104       sql.append(getEventType()).append("'");
105     }
106 
107     int i = sql.indexOf("WHERE AND");
108     if (i != -1) {
109       sql.replace(i, i + 9, "WHERE");
110     }
111     if ((i = sql.indexOf("( OR")) != -1) {
112       sql.replace(i, i + 4, "(");
113     }
114     if ((i = sql.indexOf("( AND")) != -1) {
115       sql.replace(i, i + 5, "(");
116     }
117     
118     String[] orderBy = getOrderBy();
119     String orderType = " " + getOrderType();
120     if (orderBy != null && orderBy.length > 0) {
121       sql.append(" ORDER BY ");
122       
123       for (int j = 0; j < orderBy.length; j++) {
124         orderBy[j] = orderBy[j] + orderType;
125       }
126       sql.append(StringUtils.join(orderBy, ","));
127     }
128     return sql.toString();
129   }
130 }