KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > clientimpl > comment > RemoteCommentStrategy


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.clientimpl.comment;
17
18 import org.outerj.daisy.repository.commonimpl.comment.CommentImpl;
19 import org.outerj.daisy.repository.commonimpl.comment.CommentStrategy;
20 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
21 import org.outerj.daisy.repository.comment.CommentVisibility;
22 import org.outerj.daisy.repository.comment.Comment;
23 import org.outerj.daisy.repository.RepositoryException;
24 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
25 import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy;
26 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager;
27 import org.apache.commons.httpclient.methods.PostMethod;
28 import org.apache.commons.httpclient.methods.GetMethod;
29 import org.apache.commons.httpclient.methods.DeleteMethod;
30 import org.apache.commons.httpclient.NameValuePair;
31 import org.outerx.daisy.x10.CommentDocument;
32 import org.outerx.daisy.x10.CommentsDocument;
33
34 public class RemoteCommentStrategy extends AbstractRemoteStrategy implements CommentStrategy {
35
36     public RemoteCommentStrategy(RemoteRepositoryManager.Context context) {
37         super(context);
38     }
39
40     public CommentImpl storeComment(long documentId, long branchId, long languageId, CommentVisibility visibility, String JavaDoc text, AuthenticatedUser user) throws RepositoryException {
41         DaisyHttpClient httpClient = getClient(user);
42         PostMethod method = new PostMethod("/repository/document/" + documentId + "/comment");
43
44         CommentDocument commentDocument = CommentDocument.Factory.newInstance();
45         CommentDocument.Comment commentXml = commentDocument.addNewComment();
46         commentXml.setBranchId(branchId);
47         commentXml.setLanguageId(languageId);
48         commentXml.setContent(text);
49         commentXml.setVisibility(CommentDocument.Comment.Visibility.Enum.forString(visibility.toString()));
50
51         method.setRequestBody(commentDocument.newInputStream());
52
53         CommentDocument responseCommentDocument = (CommentDocument)httpClient.executeMethod(method, CommentDocument.class, true);
54         CommentDocument.Comment responseCommentXml = responseCommentXml = responseCommentDocument.getComment();
55         return instantiateCommentFromXml(responseCommentXml);
56     }
57
58     public Comment[] loadComments(long documentId, long branchId, long languageId, AuthenticatedUser user) throws RepositoryException {
59         DaisyHttpClient httpClient = getClient(user);
60         GetMethod method = new GetMethod("/repository/document/" + documentId + "/comment");
61         method.setQueryString(getBranchLangParams(branchId, languageId));
62
63         CommentsDocument commentsDocument = (CommentsDocument)httpClient.executeMethod(method, CommentsDocument.class, true);
64         return instantiateCommentsFromXml(commentsDocument);
65     }
66
67     public Comment[] loadComments(CommentVisibility visibility, AuthenticatedUser user) throws RepositoryException {
68         DaisyHttpClient httpClient = getClient(user);
69         GetMethod method = new GetMethod("/repository/comments");
70
71         if (visibility != null) {
72             NameValuePair[] queryString = new NameValuePair[1];
73             queryString[0] = new NameValuePair("visibility", visibility.toString());
74             method.setQueryString(queryString);
75         }
76
77         CommentsDocument commentsDocument = (CommentsDocument)httpClient.executeMethod(method, CommentsDocument.class, true);
78         return instantiateCommentsFromXml(commentsDocument);
79     }
80
81     public Comment[] loadComments(AuthenticatedUser user) throws RepositoryException {
82         return loadComments(null, user);
83     }
84
85     private Comment[] instantiateCommentsFromXml(CommentsDocument commentsDocument) {
86         CommentDocument.Comment[] commentsXml = commentsDocument.getComments().getCommentArray();
87         Comment[] comments = new Comment[commentsXml.length];
88         for (int i = 0; i < comments.length; i++) {
89             comments[i] = instantiateCommentFromXml(commentsXml[i]);
90         }
91         return comments;
92     }
93
94     private CommentImpl instantiateCommentFromXml(CommentDocument.Comment commentXml) {
95         return new CommentImpl(commentXml.getDocumentId(), commentXml.getBranchId(), commentXml.getLanguageId(),
96                 commentXml.getId(), commentXml.getContent(),
97                 CommentVisibility.fromString(commentXml.getVisibility().toString()),
98                 commentXml.getCreatedOn().getTime(), commentXml.getCreatedBy());
99     }
100
101     public void deleteComment(long documentId, long branchId, long languageId, long id, AuthenticatedUser user) throws RepositoryException {
102         DaisyHttpClient httpClient = getClient(user);
103         DeleteMethod method = new DeleteMethod("/repository/document/" + documentId + "/comment/" + id);
104         method.setQueryString(getBranchLangParams(branchId, languageId));
105         httpClient.executeMethod(method, null, true);
106     }
107 }
108
Popular Tags