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;
017:
018: import org.outerj.daisy.repository.commonimpl.DocumentCollectionImpl;
019: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
020: import org.outerj.daisy.repository.commonimpl.CollectionStrategy;
021: import org.outerj.daisy.repository.RepositoryException;
022: import org.outerj.daisy.repository.RepositoryEventType;
023: import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
024: import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy;
025: import org.apache.commons.httpclient.methods.PostMethod;
026: import org.apache.commons.httpclient.methods.GetMethod;
027: import org.apache.commons.httpclient.methods.DeleteMethod;
028: import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
029: import org.apache.commons.httpclient.HttpMethod;
030: import org.outerx.daisy.x10.CollectionDocument;
031: import org.outerx.daisy.x10.CollectionsDocument;
032:
033: import java.util.Collection;
034: import java.util.ArrayList;
035: import java.util.List;
036:
037: public class RemoteCollectionStrategy extends AbstractRemoteStrategy
038: implements CollectionStrategy {
039:
040: public RemoteCollectionStrategy(
041: RemoteRepositoryManager.Context context) {
042: super (context);
043: }
044:
045: public void store(DocumentCollectionImpl collection)
046: throws RepositoryException {
047: if (collection == null)
048: throw new RuntimeException(
049: "DocumentCollectionImpl expected - instead received null!");
050: DocumentCollectionImpl.IntimateAccess collInt = collection
051: .getIntimateAccess(this );
052:
053: DaisyHttpClient httpClient = getClient(collInt.getCurrentUser());
054:
055: String url = "/repository";
056: boolean isNew = collection.getId() == -1;
057: if (isNew)
058: url += "/collection";
059: else
060: url += "/collection/" + collection.getId();
061:
062: PostMethod method = new PostMethod(url);
063:
064: CollectionDocument collectionDocument = collection.getXml();
065: method.setRequestEntity(new InputStreamRequestEntity(
066: collectionDocument.newInputStream()));
067:
068: CollectionDocument responseCollectionDocument = (CollectionDocument) httpClient
069: .executeMethod(method, CollectionDocument.class, true);
070: CollectionDocument.Collection collectionXml = responseCollectionDocument
071: .getCollection();
072: DocumentCollectionImpl.IntimateAccess collectionInt = collection
073: .getIntimateAccess(this );
074: collectionInt.saved(collectionXml.getId(), collectionXml
075: .getName(), collectionXml.getLastModified().getTime(),
076: collectionXml.getLastModifier(), collectionXml
077: .getUpdatecount());
078:
079: if (isNew)
080: context.getCommonRepository().fireRepositoryEvent(
081: RepositoryEventType.COLLECTION_CREATED,
082: new Long(collection.getId()),
083: collection.getUpdateCount());
084: else
085: context.getCommonRepository().fireRepositoryEvent(
086: RepositoryEventType.COLLECTION_UPDATED,
087: new Long(collection.getId()),
088: collection.getUpdateCount());
089: }
090:
091: public DocumentCollectionImpl loadCollection(long collectionId,
092: AuthenticatedUser user) throws RepositoryException {
093: DaisyHttpClient httpClient = getClient(user);
094: HttpMethod method = new GetMethod("/repository/collection/"
095: + collectionId);
096:
097: CollectionDocument collectionDocument = (CollectionDocument) httpClient
098: .executeMethod(method, CollectionDocument.class, true);
099: CollectionDocument.Collection documentXml = collectionDocument
100: .getCollection();
101: DocumentCollectionImpl document = instantiateCollectionFromXml(
102: documentXml, user);
103: return document;
104: }
105:
106: public DocumentCollectionImpl loadCollectionByName(String name,
107: AuthenticatedUser user) throws RepositoryException {
108: DaisyHttpClient httpClient = getClient(user);
109: String encodedName = encodeNameForUseInPath("collection", name);
110: HttpMethod method = new GetMethod(
111: "/repository/collectionByName/" + encodedName);
112:
113: CollectionDocument collectionDocument = (CollectionDocument) httpClient
114: .executeMethod(method, CollectionDocument.class, true);
115: CollectionDocument.Collection documentXml = collectionDocument
116: .getCollection();
117: DocumentCollectionImpl document = instantiateCollectionFromXml(
118: documentXml, user);
119: return document;
120: }
121:
122: private DocumentCollectionImpl instantiateCollectionFromXml(
123: CollectionDocument.Collection collectionXml,
124: AuthenticatedUser user) {
125: DocumentCollectionImpl docColl = new DocumentCollectionImpl(
126: this , collectionXml.getName(), user);
127:
128: DocumentCollectionImpl.IntimateAccess collInt = docColl
129: .getIntimateAccess(this );
130: collInt.setId(collectionXml.getId());
131: collInt.setLastModified(collectionXml.getLastModified()
132: .getTime());
133: collInt.setLastModifier(collectionXml.getLastModifier());
134: collInt.setUpdateCount(collectionXml.getUpdatecount());
135:
136: return docColl;
137: }
138:
139: public Collection<DocumentCollectionImpl> loadCollections(
140: AuthenticatedUser user) throws RepositoryException {
141: DaisyHttpClient httpClient = getClient(user);
142: HttpMethod method = new GetMethod("/repository/collection");
143:
144: CollectionsDocument documentCollectionsDocument = (CollectionsDocument) httpClient
145: .executeMethod(method, CollectionsDocument.class, true);
146: CollectionsDocument.Collections collections = documentCollectionsDocument
147: .getCollections();
148: List<CollectionDocument.Collection> collectionsXml = collections
149: .getCollectionList();
150: List<DocumentCollectionImpl> documentCollections = new ArrayList<DocumentCollectionImpl>(
151: collectionsXml.size());
152: for (CollectionDocument.Collection collectionXml : collectionsXml) {
153: DocumentCollectionImpl documentCollection = instantiateCollectionFromXml(
154: collectionXml, user);
155: documentCollections.add(documentCollection);
156: }
157: return documentCollections;
158: }
159:
160: public void deleteCollection(long collectionId,
161: AuthenticatedUser user) throws RepositoryException {
162: DaisyHttpClient httpClient = getClient(user);
163: DeleteMethod method = new DeleteMethod(
164: "/repository/collection/" + collectionId);
165: httpClient.executeMethod(method, null, true);
166: context.getCommonRepository().fireRepositoryEvent(
167: RepositoryEventType.COLLECTION_DELETED,
168: new Long(collectionId), -1);
169: }
170: }
|