KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > emailnotifier > commonimpl > SubscriptionImpl


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.commonimpl;
17
18 import org.outerj.daisy.emailnotifier.Subscription;
19 import org.outerj.daisy.emailnotifier.CollectionSubscriptionKey;
20 import org.outerj.daisy.repository.RepositoryException;
21 import org.outerj.daisy.repository.LocaleHelper;
22 import org.outerj.daisy.repository.VariantKey;
23 import org.outerx.daisy.x10.SubscriptionDocument;
24
25 import java.util.Locale JavaDoc;
26
27 public class SubscriptionImpl implements Subscription {
28     private long userId;
29     private boolean receiveDocumentEvents;
30     private boolean receiveSchemaEvents;
31     private boolean receiveUserEvents;
32     private boolean receiveCollectionEvents;
33     private boolean receiveAclEvents;
34     private boolean receiveCommentEvents;
35     private Locale JavaDoc locale;
36     private VariantKey[] subscribedVariantKeys = new VariantKey[0];
37     private CollectionSubscriptionKey[] subscribedCollectionKeys = new CollectionSubscriptionKey[0];
38     private SubscriptionStrategy subscriptionStrategy;
39
40     public SubscriptionImpl(SubscriptionStrategy subscriptionStrategy, long userId) {
41         this.subscriptionStrategy = subscriptionStrategy;
42         this.userId = userId;
43     }
44
45     public long getUserId() {
46         return userId;
47     }
48
49     public void setReceiveDocumentEvents(boolean value) {
50         this.receiveDocumentEvents = value;
51     }
52
53     public boolean getReceiveDocumentEvents() {
54         return receiveDocumentEvents;
55     }
56
57     public void setReceiveSchemaEvents(boolean value) {
58         this.receiveSchemaEvents = value;
59     }
60
61     public boolean getReceiveSchemaEvents() {
62         return receiveSchemaEvents;
63     }
64
65     public void setReceiveUserEvents(boolean value) {
66         this.receiveUserEvents = value;
67     }
68
69     public boolean getReceiveUserEvents() {
70         return receiveUserEvents;
71     }
72
73     public void setReceiveCollectionEvents(boolean value) {
74         this.receiveCollectionEvents = value;
75     }
76
77     public boolean getReceiveCollectionEvents() {
78         return receiveCollectionEvents;
79     }
80
81     public void setReceiveAclEvents(boolean value) {
82         this.receiveAclEvents = value;
83     }
84
85     public boolean getReceiveAclEvents() {
86         return receiveAclEvents;
87     }
88
89     public void setReceiveCommentEvents(boolean value) {
90         this.receiveCommentEvents = value;
91     }
92
93     public boolean getReceiveCommentEvents() {
94         return receiveCommentEvents;
95     }
96
97     public Locale JavaDoc getLocale() {
98         return locale;
99     }
100
101     public void setLocale(Locale JavaDoc locale) {
102         this.locale = locale;
103     }
104
105     public VariantKey[] getSubscribedVariantKeys() {
106         return subscribedVariantKeys;
107     }
108
109     public void setSubscribedVariantKeys(VariantKey[] keys) {
110         subscribedVariantKeys = keys;
111     }
112
113     public CollectionSubscriptionKey[] getSubscribedCollectionKeys() {
114         return subscribedCollectionKeys;
115     }
116
117     public void setSubscribedCollectionKeys(CollectionSubscriptionKey[] keys) {
118         subscribedCollectionKeys = keys;
119     }
120
121     public void save() throws RepositoryException {
122         subscriptionStrategy.storeSubscription(this);
123     }
124
125     public SubscriptionDocument getXml() {
126         SubscriptionDocument subscriptionDocument = SubscriptionDocument.Factory.newInstance();
127         SubscriptionDocument.Subscription subscriptionXml = subscriptionDocument.addNewSubscription();
128         subscriptionXml.setUserId(getUserId());
129         subscriptionXml.setReceiveDocumentEvents(getReceiveDocumentEvents());
130         subscriptionXml.setReceiveUserEvents(getReceiveUserEvents());
131         subscriptionXml.setReceiveCollectionEvents(getReceiveCollectionEvents());
132         subscriptionXml.setReceiveSchemaEvents(getReceiveSchemaEvents());
133         subscriptionXml.setReceiveAclEvents(getReceiveAclEvents());
134         subscriptionXml.setReceiveCommentEvents(getReceiveCommentEvents());
135         subscriptionXml.addNewSubscribedDocuments().setVariantArray(getSubscribedDocumentsInXml());
136         subscriptionXml.addNewSubscribedCollections().setCollectionArray(getSubscribedCollectionsInXml());
137         if (locale != null)
138             subscriptionXml.setLocale(LocaleHelper.getString(locale));
139         return subscriptionDocument;
140     }
141
142     private SubscriptionDocument.Subscription.SubscribedDocuments.Variant[] getSubscribedDocumentsInXml() {
143         SubscriptionDocument.Subscription.SubscribedDocuments.Variant[] subscribedVariantsXml = new SubscriptionDocument.Subscription.SubscribedDocuments.Variant[subscribedVariantKeys.length];
144         for (int i = 0; i < subscribedVariantKeys.length; i++) {
145             SubscriptionDocument.Subscription.SubscribedDocuments.Variant variantXml = SubscriptionDocument.Subscription.SubscribedDocuments.Variant.Factory.newInstance();
146             variantXml.setDocumentId(subscribedVariantKeys[i].getDocumentId());
147             variantXml.setBranchId(subscribedVariantKeys[i].getBranchId());
148             variantXml.setLanguageId(subscribedVariantKeys[i].getLanguageId());
149             subscribedVariantsXml[i] = variantXml;
150         }
151         return subscribedVariantsXml;
152     }
153
154     private SubscriptionDocument.Subscription.SubscribedCollections.Collection[] getSubscribedCollectionsInXml() {
155         SubscriptionDocument.Subscription.SubscribedCollections.Collection[] collectionsXml = new SubscriptionDocument.Subscription.SubscribedCollections.Collection[subscribedCollectionKeys.length];
156         for (int i = 0; i < subscribedCollectionKeys.length; i++) {
157             SubscriptionDocument.Subscription.SubscribedCollections.Collection collectionXml = SubscriptionDocument.Subscription.SubscribedCollections.Collection.Factory.newInstance();
158             collectionXml.setCollectionId(subscribedCollectionKeys[i].getCollectionId());
159             collectionXml.setBranchId(subscribedCollectionKeys[i].getBranchId());
160             collectionXml.setLanguageId(subscribedCollectionKeys[i].getLanguageId());
161             collectionsXml[i] = collectionXml;
162         }
163         return collectionsXml;
164     }
165
166     public void setFromXml(SubscriptionDocument.Subscription subscriptionXml) {
167         setReceiveDocumentEvents(subscriptionXml.getReceiveDocumentEvents());
168         setReceiveSchemaEvents(subscriptionXml.getReceiveSchemaEvents());
169         setReceiveUserEvents(subscriptionXml.getReceiveUserEvents());
170         setReceiveCollectionEvents(subscriptionXml.getReceiveCollectionEvents());
171         setReceiveAclEvents(subscriptionXml.getReceiveAclEvents());
172         setReceiveCommentEvents(subscriptionXml.getReceiveCommentEvents());
173         setSubscribedVariantKeys(instantiateVariantKeysFromXml(subscriptionXml.getSubscribedDocuments().getVariantArray()));
174         setSubscribedCollectionKeys(instantiateCollectionKeysFromXml(subscriptionXml.getSubscribedCollections().getCollectionArray()));
175         if (subscriptionXml.getLocale() != null)
176             this.locale = LocaleHelper.parseLocale(subscriptionXml.getLocale());
177         else
178             this.locale = null;
179     }
180
181     private VariantKey[] instantiateVariantKeysFromXml(SubscriptionDocument.Subscription.SubscribedDocuments.Variant[] variantsXml) {
182         VariantKey[] variantKeys = new VariantKey[variantsXml.length];
183         for (int i = 0; i < variantsXml.length; i++) {
184             SubscriptionDocument.Subscription.SubscribedDocuments.Variant variantXml = variantsXml[i];
185             variantKeys[i] = new VariantKey(variantXml.getDocumentId(), variantXml.getBranchId(), variantXml.getLanguageId());
186         }
187         return variantKeys;
188     }
189
190     private CollectionSubscriptionKey[] instantiateCollectionKeysFromXml(SubscriptionDocument.Subscription.SubscribedCollections.Collection[] collectionsXml) {
191         CollectionSubscriptionKey[] collectionKeys = new CollectionSubscriptionKey[collectionsXml.length];
192         for (int i = 0; i < collectionKeys.length; i++) {
193             SubscriptionDocument.Subscription.SubscribedCollections.Collection collectionXml = collectionsXml[i];
194             collectionKeys[i] = new CollectionSubscriptionKey(collectionXml.getCollectionId(), collectionXml.getBranchId(), collectionXml.getLanguageId());
195         }
196         return collectionKeys;
197     }
198 }
199
Popular Tags