1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
45 |
|
|
46 |
|
@Component |
47 |
|
@Named("org.xwiki.rest.internal.resources.comments.CommentsResourceImpl") |
|
|
| 76.7% |
Uncovered Elements: 10 (43) |
Complexity: 8 |
Complexity Density: 0.24 |
|
48 |
|
public class CommentsResourceImpl extends XWikiResource implements CommentsResource |
49 |
|
{ |
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
|
50 |
7 |
@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 |
|
|
|
|
| 71% |
Uncovered Elements: 9 (31) |
Complexity: 6 |
Complexity Density: 0.26 |
|
78 |
3 |
@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 |
|
} |