1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rest.internal.resources.comments

File CommentsResourceImpl.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

8
33
2
1
125
81
8
0.24
16.5
2
4

Classes

Class Line # Actions
CommentsResourceImpl 48 33 0% 8 10
0.7674418776.7%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rest.internal.resources.comments;
21   
22    import java.util.Date;
23    import java.util.List;
24    import java.util.Vector;
25   
26    import javax.inject.Named;
27    import javax.ws.rs.core.Response;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.rest.XWikiResource;
31    import org.xwiki.rest.XWikiRestException;
32    import org.xwiki.rest.internal.DomainObjectFactory;
33    import org.xwiki.rest.internal.RangeIterable;
34    import org.xwiki.rest.internal.Utils;
35    import org.xwiki.rest.model.jaxb.Comment;
36    import org.xwiki.rest.model.jaxb.Comments;
37    import org.xwiki.rest.resources.comments.CommentResource;
38    import org.xwiki.rest.resources.comments.CommentsResource;
39   
40    import com.xpn.xwiki.XWikiException;
41    import com.xpn.xwiki.api.Document;
42   
43    /**
44    * @version $Id: e03c0722acfc51a16a5646f1925680b5ccb47ca9 $
45    */
46    @Component
47    @Named("org.xwiki.rest.internal.resources.comments.CommentsResourceImpl")
 
48    public class CommentsResourceImpl extends XWikiResource implements CommentsResource
49    {
 
50  7 toggle @Override
51    public Comments getComments(String wikiName, String spaceName, String pageName, Integer start, Integer number,
52    Boolean withPrettyNames) throws XWikiRestException
53    {
54  7 try {
55  7 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
56   
57  7 Document doc = documentInfo.getDocument();
58   
59  7 Comments comments = objectFactory.createComments();
60   
61  7 Vector<com.xpn.xwiki.api.Object> xwikiComments = doc.getComments();
62   
63  7 RangeIterable<com.xpn.xwiki.api.Object> ri =
64    new RangeIterable<com.xpn.xwiki.api.Object>(xwikiComments, start, number);
65   
66  7 for (com.xpn.xwiki.api.Object xwikiComment : ri) {
67  3 comments.getComments().add(
68    DomainObjectFactory.createComment(objectFactory, uriInfo.getBaseUri(), doc, xwikiComment,
69    Utils.getXWikiApi(componentManager), withPrettyNames));
70    }
71   
72  7 return comments;
73    } catch (XWikiException e) {
74  0 throw new XWikiRestException(e);
75    }
76    }
77   
 
78  3 toggle @Override
79    public Response postComment(String wikiName, String spaceName, String pageName, Comment comment)
80    throws XWikiRestException
81    {
82  3 try {
83  3 List<String> spaces = parseSpaceSegments(spaceName);
84  3 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, true, true);
85   
86  3 Document doc = documentInfo.getDocument();
87   
88  3 int id = doc.createNewObject("XWiki.XWikiComments");
89  3 com.xpn.xwiki.api.Object commentObject = doc.getObject("XWiki.XWikiComments", id);
90  3 commentObject.set("author", Utils.getXWikiUser(componentManager));
91  3 commentObject.set("date", new Date());
92   
93  3 boolean save = false;
94   
95  3 if (comment.getHighlight() != null) {
96  0 commentObject.set("highlight", comment.getHighlight());
97  0 save = true;
98    }
99   
100  3 if (comment.getText() != null) {
101  3 commentObject.set("comment", comment.getText());
102  3 save = true;
103    }
104   
105  3 if (comment.getReplyTo() != null) {
106  0 commentObject.set("replyto", comment.getReplyTo());
107    }
108   
109  3 if (save) {
110  3 doc.save();
111   
112  3 Comment createdComment = DomainObjectFactory
113    .createComment(objectFactory, uriInfo.getBaseUri(), doc, commentObject,
114    Utils.getXWikiApi(componentManager), false);
115   
116  3 return Response.created(Utils.createURI(uriInfo.getBaseUri(), CommentResource.class, wikiName,
117    spaces, pageName, id)).entity(createdComment).build();
118    }
119   
120  0 return null;
121    } catch (XWikiException e) {
122  0 throw new XWikiRestException(e);
123    }
124    }
125    }