View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (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 General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
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   * Returns and sets a vote value of a given node in the sent parameter.
45   *
46   * {{{{portalname}}}}: The name of portal.
47   * {{{{restcontextname}}}}: The context name of the REST web application which is deployed to the "{{{{portalname}}}}" portal.
48   *
49   * @LevelAPI Provisional
50   * 
51   * @anchor VoteConnector
52   */
53  @Path("/contents/vote/")
54  public class VoteConnector extends BaseConnector implements ResourceContainer {
55  
56    /**
57     * Instantiates a new vote connector.
58     */
59    public VoteConnector() {}
60  
61    /**
62     * Sets a vote value for a given content.
63     *
64     * @param jcrPath The path of the content.
65     * @param vote The vote value.
66     * @return http The code.
67     * @throws Exception The exception
68     * 
69     * @anchor VoteConnector.postStarVote
70     */
71    @POST
72    @Path("/star/")
73  //  @InputTransformer(PassthroughInputTransformer.class)
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     * Returns a vote value for a given content.
86     *
87     * @param repositoryName The repository name.
88     * @param workspaceName The workspace name.
89     * @param jcrPath The path of the content.
90     * @return http The code.
91     * @throws Exception The exception
92     * 
93     * @anchor VoteConnector.getStarVote
94     */
95    @GET
96    @Path("/star/")
97  //  @OutputTransformer(XMLOutputTransformer.class)
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    * Sets a vote value for a given content.
109    *
110    * @param repositoryName The repository name.
111    * @param workspaceName The workspace name.
112    * @param jcrPath The path of the content.
113    * @param vote The vote value.
114    * @param lang The language of the content.
115    * @return http The code.
116    * @throws Exception The exception
117    * 
118    * @anchor VoteConnector.postVote
119    */
120   @GET
121   @Path("/postVote/")
122 //  @InputTransformer(PassthroughInputTransformer.class)
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    * Returns a vote value for a given content.
154    *
155    * @param repositoryName The repository name.
156    * @param workspaceName The workspace name.
157    * @param jcrPath The path of the content.
158    * @return http The code
159    * @throws Exception The exception
160    * 
161    * @anchor VoteConnector.getVote
162    */
163   @GET
164   @Path("/getVote/")
165 //  @OutputTransformer(XMLOutputTransformer.class)
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    * (non-Javadoc)
214    * @see
215    * org.exoplatform.wcm.connector.fckeditor.BaseConnector#getRootContentStorage
216    * (javax.jcr.Node)
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    * (non-Javadoc)
233    * @see
234    * org.exoplatform.wcm.connector.fckeditor.BaseConnector#getContentStorageType
235    * ()
236    */
237   @Override
238   protected String getContentStorageType() throws Exception {
239     return FCKUtils.DOCUMENT_TYPE;
240   }
241 }