1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.wcm.connector.collaboration;
18
19 import java.net.URLDecoder;
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
24 import javax.jcr.Node;
25 import javax.ws.rs.FormParam;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.QueryParam;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.transform.dom.DOMSource;
34
35 import org.exoplatform.ecm.connector.fckeditor.FCKUtils;
36 import org.exoplatform.services.rest.resource.ResourceContainer;
37 import org.exoplatform.services.wcm.portal.PortalFolderSchemaHandler;
38 import org.exoplatform.services.wcm.webcontent.WebContentSchemaHandler;
39 import org.exoplatform.wcm.connector.BaseConnector;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42
43
44
45
46
47
48
49
50
51
52
53 @Path("/contents/vote/")
54 public class VoteConnector extends BaseConnector implements ResourceContainer {
55
56
57
58
59 public VoteConnector() {}
60
61
62
63
64
65
66
67
68
69
70
71 @POST
72 @Path("/star/")
73
74 public Response postStarVote(
75 @FormParam("jcrPath") String jcrPath,
76 @FormParam("vote") String vote
77 ) throws Exception {
78
79 if (jcrPath.contains("%20")) jcrPath = URLDecoder.decode(jcrPath, "UTF-8");
80
81 return postVote(null, null, jcrPath, vote, "en" );
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95 @GET
96 @Path("/star/")
97
98 public Response getStarVote(
99 @QueryParam("repositoryName") String repositoryName,
100 @QueryParam("workspaceName") String workspaceName,
101 @QueryParam("jcrPath") String jcrPath) throws Exception {
102
103 return getVote(repositoryName, workspaceName, jcrPath);
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 @GET
121 @Path("/postVote/")
122
123 public Response postVote(
124 @QueryParam("repositoryName") String repositoryName,
125 @QueryParam("workspaceName") String workspaceName,
126 @QueryParam("jcrPath") String jcrPath,
127 @QueryParam("vote") String vote,
128 @QueryParam("lang") String lang
129 ) throws Exception {
130 try {
131
132 if (repositoryName==null && workspaceName==null) {
133 String[] path = jcrPath.split("/");
134 repositoryName = path[1];
135 workspaceName = path[2];
136 jcrPath = jcrPath.substring(repositoryName.length()+workspaceName.length()+2);
137 if (jcrPath.charAt(1)=='/') jcrPath.substring(1);
138 }
139 Node content = getContent(workspaceName, jcrPath, null, false);
140 if (content.isNodeType("mix:votable")) {
141 String userName = content.getSession().getUserID();
142 votingService.vote(content, Double.parseDouble(vote), userName, lang);
143 }
144 } catch (Exception e) {
145 Response.serverError().build();
146 }
147
148 DateFormat dateFormat = new SimpleDateFormat(IF_MODIFIED_SINCE_DATE_FORMAT);
149 return Response.ok().header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date())).build();
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163 @GET
164 @Path("/getVote/")
165
166 public Response getVote(
167 @QueryParam("repositoryName") String repositoryName,
168 @QueryParam("workspaceName") String workspaceName,
169 @QueryParam("jcrPath") String jcrPath) throws Exception {
170 try {
171
172 if (repositoryName==null && workspaceName==null) {
173 String[] path = jcrPath.split("/");
174 repositoryName = path[1];
175 workspaceName = path[2];
176 jcrPath = jcrPath.substring(repositoryName.length()+workspaceName.length()+2);
177 if (jcrPath.charAt(1)=='/') jcrPath.substring(1);
178 }
179 Node content = getContent(workspaceName, jcrPath);
180 if (content.isNodeType("mix:votable")) {
181 String votingRate = "";
182 if (content.hasProperty("exo:votingRate"))
183 votingRate = content.getProperty("exo:votingRate").getString();
184 String votingTotal = "";
185 if (content.hasProperty("exo:voteTotalOfLang"))
186 votingTotal = content.getProperty("exo:voteTotalOfLang").getString();
187
188 Document document =
189 DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
190 Element element = document.createElement("vote");
191 Element rate = document.createElement("rate");
192 rate.setTextContent(votingRate);
193 Element total = document.createElement("total");
194 total.setTextContent(votingTotal);
195 element.appendChild(rate);
196 element.appendChild(total);
197 document.appendChild(element);
198
199 DateFormat dateFormat = new SimpleDateFormat(IF_MODIFIED_SINCE_DATE_FORMAT);
200 return Response.ok(new DOMSource(document), MediaType.TEXT_XML)
201 .header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
202 .build();
203 }
204 } catch (Exception e) {
205 Response.serverError().build();
206 }
207
208 DateFormat dateFormat = new SimpleDateFormat(IF_MODIFIED_SINCE_DATE_FORMAT);
209 return Response.ok().header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date())).build();
210 }
211
212
213
214
215
216
217
218 @Override
219 protected Node getRootContentStorage(Node parentNode) throws Exception {
220 try {
221 PortalFolderSchemaHandler folderSchemaHandler =
222 webSchemaConfigService.getWebSchemaHandlerByType(PortalFolderSchemaHandler.class);
223 return folderSchemaHandler.getDocumentStorage(parentNode);
224 } catch (Exception e) {
225 WebContentSchemaHandler webContentSchemaHandler =
226 webSchemaConfigService.getWebSchemaHandlerByType(WebContentSchemaHandler.class);
227 return webContentSchemaHandler.getDocumentFolder(parentNode);
228 }
229 }
230
231
232
233
234
235
236
237 @Override
238 protected String getContentStorageType() throws Exception {
239 return FCKUtils.DOCUMENT_TYPE;
240 }
241 }