001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.clientimpl.comment;
017:
018: import org.outerj.daisy.repository.commonimpl.comment.CommentImpl;
019: import org.outerj.daisy.repository.commonimpl.comment.CommentStrategy;
020: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
021: import org.outerj.daisy.repository.commonimpl.DocId;
022: import org.outerj.daisy.repository.comment.CommentVisibility;
023: import org.outerj.daisy.repository.comment.Comment;
024: import org.outerj.daisy.repository.RepositoryException;
025: import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
026: import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy;
027: import org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager;
028: import org.apache.commons.httpclient.methods.PostMethod;
029: import org.apache.commons.httpclient.methods.GetMethod;
030: import org.apache.commons.httpclient.methods.DeleteMethod;
031: import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
032: import org.apache.commons.httpclient.NameValuePair;
033: import org.outerx.daisy.x10.CommentDocument;
034: import org.outerx.daisy.x10.CommentsDocument;
035:
036: import java.util.List;
037:
038: public class RemoteCommentStrategy extends AbstractRemoteStrategy
039: implements CommentStrategy {
040:
041: public RemoteCommentStrategy(RemoteRepositoryManager.Context context) {
042: super (context);
043: }
044:
045: public CommentImpl storeComment(DocId docId, long branchId,
046: long languageId, CommentVisibility visibility, String text,
047: AuthenticatedUser user) throws RepositoryException {
048: DaisyHttpClient httpClient = getClient(user);
049: PostMethod method = new PostMethod("/repository/document/"
050: + docId.toString() + "/comment");
051:
052: CommentDocument commentDocument = CommentDocument.Factory
053: .newInstance();
054: CommentDocument.Comment commentXml = commentDocument
055: .addNewComment();
056: commentXml.setBranchId(branchId);
057: commentXml.setLanguageId(languageId);
058: commentXml.setContent(text);
059: commentXml
060: .setVisibility(CommentDocument.Comment.Visibility.Enum
061: .forString(visibility.toString()));
062:
063: method.setRequestEntity(new InputStreamRequestEntity(
064: commentDocument.newInputStream()));
065:
066: CommentDocument responseCommentDocument = (CommentDocument) httpClient
067: .executeMethod(method, CommentDocument.class, true);
068: CommentDocument.Comment responseCommentXml = responseCommentDocument
069: .getComment();
070: return instantiateCommentFromXml(responseCommentXml);
071: }
072:
073: public Comment[] loadComments(DocId docId, long branchId,
074: long languageId, AuthenticatedUser user)
075: throws RepositoryException {
076: DaisyHttpClient httpClient = getClient(user);
077: GetMethod method = new GetMethod("/repository/document/"
078: + docId.toString() + "/comment");
079: method
080: .setQueryString(getBranchLangParams(branchId,
081: languageId));
082:
083: CommentsDocument commentsDocument = (CommentsDocument) httpClient
084: .executeMethod(method, CommentsDocument.class, true);
085: return instantiateCommentsFromXml(commentsDocument);
086: }
087:
088: public Comment[] loadComments(CommentVisibility visibility,
089: AuthenticatedUser user) throws RepositoryException {
090: DaisyHttpClient httpClient = getClient(user);
091: GetMethod method = new GetMethod("/repository/comments");
092:
093: if (visibility != null) {
094: NameValuePair[] queryString = new NameValuePair[1];
095: queryString[0] = new NameValuePair("visibility", visibility
096: .toString());
097: method.setQueryString(queryString);
098: }
099:
100: CommentsDocument commentsDocument = (CommentsDocument) httpClient
101: .executeMethod(method, CommentsDocument.class, true);
102: return instantiateCommentsFromXml(commentsDocument);
103: }
104:
105: public Comment[] loadComments(AuthenticatedUser user)
106: throws RepositoryException {
107: return loadComments(null, user);
108: }
109:
110: private Comment[] instantiateCommentsFromXml(
111: CommentsDocument commentsDocument) {
112: List<CommentDocument.Comment> commentsXml = commentsDocument
113: .getComments().getCommentList();
114: Comment[] comments = new Comment[commentsXml.size()];
115: for (int i = 0; i < comments.length; i++) {
116: comments[i] = instantiateCommentFromXml(commentsXml.get(i));
117: }
118: return comments;
119: }
120:
121: private CommentImpl instantiateCommentFromXml(
122: CommentDocument.Comment commentXml) {
123: DocId docId = DocId.parseDocId(commentXml.getDocumentId(),
124: context.getCommonRepository());
125: return new CommentImpl(docId, commentXml.getBranchId(),
126: commentXml.getLanguageId(), commentXml.getId(),
127: commentXml.getContent(), CommentVisibility
128: .fromString(commentXml.getVisibility()
129: .toString()), commentXml.getCreatedOn()
130: .getTime(), commentXml.getCreatedBy());
131: }
132:
133: public void deleteComment(DocId docId, long branchId,
134: long languageId, long id, AuthenticatedUser user)
135: throws RepositoryException {
136: DaisyHttpClient httpClient = getClient(user);
137: DeleteMethod method = new DeleteMethod("/repository/document/"
138: + docId.toString() + "/comment/" + id);
139: method
140: .setQueryString(getBranchLangParams(branchId,
141: languageId));
142: httpClient.executeMethod(method, null, true);
143: }
144: }
|