1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32 StringBuilder sql = new StringBuilder("SELECT " + Utils.EXO_ID + " FROM ");
33 sql.append(Utils.EXO_CALENDAR_EVENT);
34 sql.append(" WHERE");
35
36
37
38
39
40
41 if (getCalendarId() != null || getParticipants() != null) {
42 sql.append(" AND (");
43
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
50 if (getParticipants() != null) {
51 for (String participant : getParticipants()) {
52
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
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
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
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 }