KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > emailnotifier > clientimpl > RemoteEmailSubscriptionManager


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.emailnotifier.clientimpl;
17
18 import org.outerj.daisy.emailnotifier.EmailSubscriptionManager;
19 import org.outerj.daisy.emailnotifier.Subscription;
20 import org.outerj.daisy.emailnotifier.Subscriptions;
21 import org.outerj.daisy.emailnotifier.Subscribers;
22 import org.outerj.daisy.emailnotifier.commonimpl.*;
23 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryImpl;
24 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
25 import org.outerj.daisy.repository.RepositoryException;
26 import org.outerj.daisy.repository.LocaleHelper;
27 import org.outerj.daisy.repository.VariantKey;
28 import org.apache.commons.httpclient.HttpMethod;
29 import org.apache.commons.httpclient.NameValuePair;
30 import org.apache.commons.httpclient.methods.GetMethod;
31 import org.apache.commons.httpclient.methods.PostMethod;
32 import org.apache.commons.httpclient.methods.DeleteMethod;
33 import org.outerx.daisy.x10.*;
34
35 import java.util.Locale JavaDoc;
36
37 public class RemoteEmailSubscriptionManager implements EmailSubscriptionManager {
38     private RemoteRepositoryImpl repository;
39     private SubscriptionStrategyImpl subscriptionStrategy = new SubscriptionStrategyImpl();
40
41     public RemoteEmailSubscriptionManager(RemoteRepositoryImpl repository) {
42         this.repository = repository;
43     }
44
45     public Subscription getSubscription() throws RepositoryException {
46         return getSubscription(repository.getUserId());
47     }
48
49     public Subscription getSubscription(long userId) throws RepositoryException {
50         DaisyHttpClient httpClient = repository.getHttpClient();
51         HttpMethod method = new GetMethod("/emailnotifier/subscription/" + userId);
52
53         SubscriptionDocument subscriptionDocument = (SubscriptionDocument)httpClient.executeMethod(method, SubscriptionDocument.class, true);
54         SubscriptionDocument.Subscription subscriptionXml = subscriptionDocument.getSubscription();
55         SubscriptionImpl subscription = instantiateSubscriptionFromXml(subscriptionXml);
56         return subscription;
57     }
58
59     private SubscriptionImpl instantiateSubscriptionFromXml(SubscriptionDocument.Subscription subscriptionXml) {
60         SubscriptionImpl subscription = new SubscriptionImpl(subscriptionStrategy, subscriptionXml.getUserId());
61         subscription.setFromXml(subscriptionXml);
62         return subscription;
63     }
64
65     public void deleteSubscription() throws RepositoryException {
66         deleteSubscription(repository.getUserId());
67     }
68
69     public void deleteSubscription(long userId) throws RepositoryException {
70         DaisyHttpClient httpClient = repository.getHttpClient();
71         HttpMethod method = new DeleteMethod("/emailnotifier/subscription/" + userId);
72
73         httpClient.executeMethod(method, null, true);
74     }
75
76     public Subscriptions getSubscriptions() throws RepositoryException {
77         return getSubscriptions("/emailnotifier/subscription");
78     }
79
80     private Subscriptions getSubscriptions(String JavaDoc path) throws RepositoryException {
81         DaisyHttpClient httpClient = repository.getHttpClient();
82         HttpMethod method = new GetMethod(path);
83
84         SubscriptionsDocument subscriptionsDocument = (SubscriptionsDocument)httpClient.executeMethod(method, SubscriptionsDocument.class, true);
85         SubscriptionDocument.Subscription[] subscriptionsXml = subscriptionsDocument.getSubscriptions().getSubscriptionArray();
86         SubscriptionImpl[] subscriptions = new SubscriptionImpl[subscriptionsXml.length];
87         for (int i = 0; i < subscriptionsXml.length; i++) {
88             subscriptions[i] = instantiateSubscriptionFromXml(subscriptionsXml[i]);
89         }
90         return new SubscriptionsImpl(subscriptions);
91     }
92
93     private void storeSubscription(SubscriptionImpl subscription) throws RepositoryException {
94         DaisyHttpClient httpClient = repository.getHttpClient();
95         PostMethod method = new PostMethod("/emailnotifier/subscription/" + subscription.getUserId());
96         method.setRequestBody(subscription.getXml().newInputStream());
97
98         httpClient.executeMethod(method, null, true);
99     }
100
101     public void addDocumentSubscription(VariantKey variantKey) throws RepositoryException {
102         addDocumentSubscription(repository.getUserId(), variantKey);
103     }
104
105     public void addDocumentSubscription(long userId, VariantKey variantKey) throws RepositoryException {
106         DaisyHttpClient httpClient = repository.getHttpClient();
107         PostMethod method = new PostMethod("/emailnotifier/documentSubscription/" + variantKey.getDocumentId());
108         NameValuePair[] queryString = {
109             new NameValuePair("action", "add"),
110             new NameValuePair("userId", String.valueOf(userId)),
111             new NameValuePair("branch", String.valueOf(variantKey.getBranchId())),
112             new NameValuePair("language", String.valueOf(variantKey.getLanguageId()))};
113         method.setQueryString(queryString);
114
115         httpClient.executeMethod(method, null, true);
116     }
117
118     public void deleteDocumentSubscription(VariantKey variantKey) throws RepositoryException {
119         deleteDocumentSubscription(repository.getUserId(), variantKey);
120     }
121
122     public void deleteDocumentSubscription(long userId, VariantKey variantKey) throws RepositoryException {
123         DaisyHttpClient httpClient = repository.getHttpClient();
124         PostMethod method = new PostMethod("/emailnotifier/documentSubscription/" + variantKey.getDocumentId());
125         NameValuePair[] queryString = {
126             new NameValuePair("action", "remove"),
127             new NameValuePair("userId", String.valueOf(userId)),
128             new NameValuePair("branch", String.valueOf(variantKey.getBranchId())),
129             new NameValuePair("language", String.valueOf(variantKey.getLanguageId()))};
130         method.setQueryString(queryString);
131
132         httpClient.executeMethod(method, null, true);
133     }
134
135     public boolean isSubsribed(VariantKey variantKey) throws RepositoryException {
136         return isSubsribed(repository.getUserId(), variantKey);
137     }
138
139     public boolean isSubsribed(long userId, VariantKey variantKey) throws RepositoryException {
140         DaisyHttpClient httpClient = repository.getHttpClient();
141         GetMethod method = new GetMethod("/emailnotifier/documentSubscription/" + variantKey.getDocumentId() + "/" + userId);
142         NameValuePair[] queryString = {
143             new NameValuePair("branch", String.valueOf(variantKey.getBranchId())),
144             new NameValuePair("language", String.valueOf(variantKey.getLanguageId()))
145         };
146         method.setQueryString(queryString);
147         SubscribedDocument subscribedDocument = (SubscribedDocument)httpClient.executeMethod(method, SubscribedDocument.class, true);
148         return subscribedDocument.getSubscribed();
149     }
150
151     public void deleteAllSubscriptionsForDocument(long documentId) throws RepositoryException {
152         DaisyHttpClient httpClient = repository.getHttpClient();
153         DeleteMethod method = new DeleteMethod("/emailnotifier/documentSubscription/" + documentId);
154
155         httpClient.executeMethod(method, null, true);
156     }
157
158     public void deleteAllSubscriptionsForDocumentVariant(VariantKey variantKey) throws RepositoryException {
159         DaisyHttpClient httpClient = repository.getHttpClient();
160         DeleteMethod method = new DeleteMethod("/emailnotifier/documentSubscription/" + variantKey.getDocumentId());
161         NameValuePair[] queryString = {
162             new NameValuePair("branch", String.valueOf(variantKey.getBranchId())),
163             new NameValuePair("language", String.valueOf(variantKey.getLanguageId()))
164         };
165         method.setQueryString(queryString);
166         httpClient.executeMethod(method, null, true);
167     }
168
169     public void deleteAllSubscriptionsForCollection(long collectionId) throws RepositoryException {
170         DaisyHttpClient httpClient = repository.getHttpClient();
171         DeleteMethod method = new DeleteMethod("/emailnotifier/collectionSubscription/" + collectionId);
172
173         httpClient.executeMethod(method, null, true);
174     }
175
176     public Subscribers getAllDocumentEventSubscribers(long documentId, long branchId, long languageId, long[] collections) throws RepositoryException {
177         return getAllEventSubscribersForDocumentOrCollections(documentId, branchId, languageId, collections, "/emailnotifier/documentEventsSubscribers");
178     }
179
180     public Subscribers getAllCommentEventSubscribers(long documentId, long branchId, long languageId, long[] collections) throws RepositoryException {
181         return getAllEventSubscribersForDocumentOrCollections(documentId, branchId, languageId, collections, "/emailnotifier/commentEventsSubscribers");
182     }
183
184     private Subscribers getAllEventSubscribersForDocumentOrCollections(long documentId, long branchId, long languageId, long[] collections, String JavaDoc subscriptionResource) throws RepositoryException {
185         DaisyHttpClient httpClient = repository.getHttpClient();
186         GetMethod method = new GetMethod(subscriptionResource);
187
188         StringBuffer JavaDoc collectionParam = new StringBuffer JavaDoc(collections.length * 4);
189         for (int i = 0; i < collections.length; i++) {
190             if (i > 0)
191                 collectionParam.append(',');
192             collectionParam.append(collections[i]);
193         }
194         NameValuePair[] queryString = {
195             new NameValuePair("documentId", String.valueOf(documentId)),
196             new NameValuePair("branch", String.valueOf(branchId)),
197             new NameValuePair("language", String.valueOf(languageId)),
198             new NameValuePair("collectionIds", collectionParam.toString())};
199         method.setQueryString(queryString);
200
201         SubscribersDocument subscribersDocument = (SubscribersDocument)httpClient.executeMethod(method, SubscribersDocument.class, true);
202         return instantiateSubscribers(subscribersDocument);
203     }
204
205     private Subscribers instantiateSubscribers(SubscribersDocument subscribersDocument) {
206         SubscriberDocument.Subscriber[] subscribersXml = subscribersDocument.getSubscribers().getSubscriberArray();
207         SubscriberImpl[] subscribers = new SubscriberImpl[subscribersXml.length];
208         for (int i = 0; i < subscribersXml.length; i++) {
209             String JavaDoc localeString = subscribersXml[i].getLocale();
210             Locale JavaDoc locale = localeString != null ? LocaleHelper.parseLocale(localeString) : null;
211             subscribers[i] = new SubscriberImpl(subscribersXml[i].getUserId(), locale);
212         }
213         return new SubscribersImpl(subscribers);
214     }
215
216     public Subscribers getAllUserEventSubscribers() throws RepositoryException {
217         return getSubscribers("user");
218     }
219
220     public Subscribers getAllCollectionEventSubscribers() throws RepositoryException {
221         return getSubscribers("collection");
222     }
223
224     public Subscribers getAllSchemaEventSubscribers() throws RepositoryException {
225         return getSubscribers("schema");
226     }
227
228     public Subscribers getAllAclEventSubscribers() throws RepositoryException {
229         return getSubscribers("acl");
230     }
231
232     private Subscribers getSubscribers(String JavaDoc eventName) throws RepositoryException {
233         DaisyHttpClient httpClient = repository.getHttpClient();
234         GetMethod method = new GetMethod("/emailnotifier/" + eventName + "EventsSubscribers");
235
236         SubscribersDocument subscribersDocument = (SubscribersDocument)httpClient.executeMethod(method, SubscribersDocument.class, true);
237         return instantiateSubscribers(subscribersDocument);
238     }
239
240     class SubscriptionStrategyImpl implements SubscriptionStrategy {
241         public void storeSubscription(SubscriptionImpl subscription) throws RepositoryException {
242             RemoteEmailSubscriptionManager.this.storeSubscription(subscription);
243         }
244     }
245 }
246
Popular Tags