KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > clientimpl > RemoteCollectionStrategy


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;
17
18 import org.outerj.daisy.repository.commonimpl.DocumentCollectionImpl;
19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
20 import org.outerj.daisy.repository.commonimpl.CollectionStrategy;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerj.daisy.repository.RepositoryEventType;
23 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
24 import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy;
25 import org.apache.commons.httpclient.methods.PostMethod;
26 import org.apache.commons.httpclient.methods.GetMethod;
27 import org.apache.commons.httpclient.methods.DeleteMethod;
28 import org.apache.commons.httpclient.HttpMethod;
29 import org.outerx.daisy.x10.CollectionDocument;
30 import org.outerx.daisy.x10.CollectionsDocument;
31
32 import java.util.Collection JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 public class RemoteCollectionStrategy extends AbstractRemoteStrategy implements CollectionStrategy {
36
37     public RemoteCollectionStrategy(RemoteRepositoryManager.Context context) {
38         super(context);
39     }
40
41     public void store(DocumentCollectionImpl collection) throws RepositoryException {
42         if (collection==null) throw new RuntimeException JavaDoc("DocumentCollectionImpl expected - instead received null!");
43         DocumentCollectionImpl.IntimateAccess collInt = collection.getIntimateAccess(this);
44
45         DaisyHttpClient httpClient = getClient(collInt.getCurrentUser());
46
47         String JavaDoc url = "/repository";
48         boolean isNew = collection.getId() == -1;
49         if (isNew)
50             url += "/collection";
51         else
52             url += "/collection/" + collection.getId();
53
54         PostMethod method = new PostMethod(url);
55
56         CollectionDocument collectionDocument = collection.getXml();
57         method.setRequestBody(collectionDocument.newInputStream());
58
59         CollectionDocument responseCollectionDocument = (CollectionDocument)httpClient.executeMethod(method, CollectionDocument.class, true);
60         CollectionDocument.Collection collectionXml = responseCollectionDocument.getCollection();
61         DocumentCollectionImpl.IntimateAccess collectionInt = collection.getIntimateAccess(this);
62         collectionInt.saved(collectionXml.getId(), collectionXml.getName(), collectionXml.getLastModified().getTime(), collectionXml.getLastModifier(), collectionXml.getUpdatecount());
63
64         if (isNew)
65             context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.COLLECTION_CREATED, collection.getId(), collection.getUpdateCount());
66         else
67             context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.COLLECTION_UPDATED, collection.getId(), collection.getUpdateCount());
68     }
69
70     public DocumentCollectionImpl loadCollection(long collectionId, AuthenticatedUser user) throws RepositoryException {
71         DaisyHttpClient httpClient = getClient(user);
72         HttpMethod method = new GetMethod("/repository/collection/" + collectionId);
73
74         CollectionDocument collectionDocument = (CollectionDocument)httpClient.executeMethod(method, CollectionDocument.class, true);
75         CollectionDocument.Collection documentXml = collectionDocument.getCollection();
76         DocumentCollectionImpl document = instantiateCollectionFromXml(documentXml, user);
77         return document;
78     }
79
80     public DocumentCollectionImpl loadCollectionByName(String JavaDoc name, AuthenticatedUser user) throws RepositoryException {
81         DaisyHttpClient httpClient = getClient(user);
82         String JavaDoc encodedName = encodeNameForUseInPath("collection", name);
83         HttpMethod method = new GetMethod("/repository/collectionByName/" + encodedName);
84
85         CollectionDocument collectionDocument = (CollectionDocument)httpClient.executeMethod(method, CollectionDocument.class, true);
86         CollectionDocument.Collection documentXml = collectionDocument.getCollection();
87         DocumentCollectionImpl document = instantiateCollectionFromXml(documentXml, user);
88         return document;
89     }
90
91     private DocumentCollectionImpl instantiateCollectionFromXml(CollectionDocument.Collection collectionXml, AuthenticatedUser user) {
92         DocumentCollectionImpl docColl = new DocumentCollectionImpl(this, collectionXml.getName(), user);
93
94         DocumentCollectionImpl.IntimateAccess collInt = docColl.getIntimateAccess(this);
95         collInt.setId(collectionXml.getId());
96         collInt.setLastModified(collectionXml.getLastModified().getTime());
97         collInt.setLastModifier(collectionXml.getLastModifier());
98         collInt.setUpdateCount(collectionXml.getUpdatecount());
99
100         return docColl;
101     }
102
103     public Collection JavaDoc loadCollections(AuthenticatedUser user) throws RepositoryException {
104         DaisyHttpClient httpClient = getClient(user);
105         HttpMethod method = new GetMethod("/repository/collection");
106
107         CollectionsDocument documentCollectionsDocument = (CollectionsDocument)httpClient.executeMethod(method, CollectionsDocument.class, true);
108         CollectionsDocument.Collections collections = documentCollectionsDocument.getCollections();
109         CollectionDocument.Collection[] collectionsXml = collections.getCollectionArray();
110         ArrayList JavaDoc documentCollections = new ArrayList JavaDoc(collectionsXml.length);
111         for (int i = 0; i < collectionsXml.length; i++) {
112             DocumentCollectionImpl documentCollection = instantiateCollectionFromXml(collectionsXml[i], user);
113             documentCollections.add(documentCollection);
114         }
115         return documentCollections;
116     }
117
118     public void deleteCollection(long collectionId, AuthenticatedUser user) throws RepositoryException {
119         DaisyHttpClient httpClient = getClient(user);
120         DeleteMethod method = new DeleteMethod("/repository/collection/" + collectionId);
121         httpClient.executeMethod(method, null, true);
122         context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.COLLECTION_DELETED, collectionId, -1);
123     }
124 }
125
Popular Tags