KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > layer > application > SubscriptionTarget


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: SubscriptionTarget.java,v 1.3 2005/08/12 22:38:05 wfro Exp $
5  * Description: openCRX application plugin
6  * Revision: $Revision: 1.3 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2005/08/12 22:38:05 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004-2005, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.kernel.layer.application;
57
58 import java.util.Iterator JavaDoc;
59 import java.util.List JavaDoc;
60
61 import org.openmdx.application.log.AppLog;
62 import org.openmdx.base.exception.ServiceException;
63 import org.openmdx.compatibility.base.dataprovider.cci.AttributeSelectors;
64 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject;
65 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject_1_0;
66 import org.openmdx.compatibility.base.dataprovider.cci.Directions;
67 import org.openmdx.compatibility.base.dataprovider.cci.RequestCollection;
68 import org.openmdx.compatibility.base.dataprovider.cci.ServiceHeader;
69 import org.openmdx.compatibility.base.dataprovider.cci.SystemAttributes;
70 import org.openmdx.compatibility.base.naming.Path;
71 import org.openmdx.kernel.id.UUIDs;
72
73 public class SubscriptionTarget {
74
75     //-----------------------------------------------------------------------
76
public SubscriptionTarget(
77         OpenCrxKernel_1 plugin,
78         RequestCollection delegation
79     ) {
80         this.plugin = plugin;
81         this.delegation = delegation;
82     }
83
84     //-------------------------------------------------------------------------
85
private List JavaDoc getSubscriptions(
86         Path userHomeIdentity,
87         DataproviderObject_1_0 source
88     ) throws ServiceException {
89         List JavaDoc subscriptions = this.delegation.addFindRequest(
90             userHomeIdentity.getChild("subscription"),
91             null,
92             AttributeSelectors.ALL_ATTRIBUTES,
93             0,
94             Integer.MAX_VALUE,
95             Directions.ASCENDING
96         );
97         return subscriptions;
98     }
99
100     //-------------------------------------------------------------------------
101
private DataproviderObject_1_0 findMatchingSubscription(
102         ServiceHeader header,
103         List JavaDoc subscriptions,
104         DataproviderObject_1_0 source,
105         boolean matchExact
106     ) throws ServiceException {
107         DataproviderObject_1_0 matchingSubscription = null;
108         for(
109             Iterator JavaDoc i = subscriptions.iterator();
110             i.hasNext();
111         ) {
112             DataproviderObject_1_0 subscription = (DataproviderObject_1_0)i.next();
113             if(subscription.values("topic").size() > 0) {
114                 DataproviderObject_1_0 topic = this.plugin.retrieveObjectFromLocal(header, (Path)subscription.values("topic").get(0));
115                 if(topic.values("topicPathPattern").size() > 0) {
116                     // Verify whether the subscription contains a filterName=identity and
117
// the corresponding filter value matches the source.path()
118
boolean filterMatches = false;
119                     for(
120                         int j = 0;
121                         j < 5;
122                         j++
123                     ) {
124                         String JavaDoc filterName = (String JavaDoc)subscription.values("filterName" + j).get(0);
125                         String JavaDoc filterValue = (String JavaDoc)subscription.values("filterValue" + j).get(0);
126                         filterMatches =
127                             (filterName != null) && SystemAttributes.OBJECT_IDENTITY.equals(filterName) &&
128                             (filterValue != null) && filterValue.equals(source.path().toXri());
129                         if(filterMatches) break;
130                     }
131                     if(
132                         source.path().isLike(new Path((String JavaDoc)topic.values("topicPathPattern").get(0))) &&
133                         ((!matchExact && !filterMatches) || (matchExact && filterMatches))
134                     ) {
135                         matchingSubscription = subscription;
136                         break;
137                     }
138                 }
139             }
140         }
141         return matchingSubscription;
142     }
143     
144     //-------------------------------------------------------------------------
145
public DataproviderObject_1_0 findTopic(
146         DataproviderObject_1_0 source
147     ) throws ServiceException {
148         List JavaDoc topics = this.delegation.addFindRequest(
149             new Path("xri:@openmdx:org.opencrx.kernel.workflow1/provider").getDescendant(new String JavaDoc[]{source.path().get(2), "segment", source.path().get(4), "topic"}),
150             null,
151             AttributeSelectors.ALL_ATTRIBUTES,
152             0,
153             Integer.MAX_VALUE,
154             Directions.ASCENDING
155         );
156         DataproviderObject_1_0 matchingTopic = null;
157         for(
158             Iterator JavaDoc i = topics.iterator();
159             i.hasNext();
160         ) {
161             DataproviderObject_1_0 topic = (DataproviderObject_1_0)i.next();
162             if(
163                 (topic.values("topicPathPattern").size() > 0) &&
164                 source.path().isLike(new Path((String JavaDoc)topic.values("topicPathPattern").get(0)))
165             ) {
166                 matchingTopic = topic;
167                 break;
168             }
169         }
170         return matchingTopic;
171     }
172     
173     //-------------------------------------------------------------------------
174
public void addSubscription(
175         ServiceHeader header,
176         DataproviderObject_1_0 source,
177         DataproviderObject param
178     ) {
179         try {
180             DataproviderObject_1_0 userHome = this.plugin.getUserHome(header, source.path());
181             List JavaDoc subscriptions = this.getSubscriptions(userHome.path(), source);
182             DataproviderObject_1_0 matchingSubscription = this.findMatchingSubscription(header, subscriptions, source, true);
183             if(matchingSubscription == null) {
184                 DataproviderObject_1_0 topic = this.findTopic(source);
185                 if(topic != null) {
186                     DataproviderObject newSubscription = new DataproviderObject(
187                         userHome.path().getDescendant(new String JavaDoc[]{"subscription", UUIDs.getGenerator().next().toString()})
188                     );
189                     newSubscription.values(SystemAttributes.OBJECT_CLASS).add(
190                         "org:opencrx:kernel:home1:Subscription"
191                     );
192                     newSubscription.values("name").addAll(topic.values("name"));
193                     newSubscription.values("description").addAll(topic.values("description"));
194                     newSubscription.values("topic").add(topic.path());
195                     newSubscription.values("isActive").add(Boolean.TRUE);
196                     newSubscription.values("filterName0").add(SystemAttributes.OBJECT_IDENTITY);
197                     newSubscription.values("filterValue0").add(source.path().toXri());
198                     this.delegation.addCreateRequest(
199                         newSubscription
200                     );
201                 }
202             }
203         }
204         catch(ServiceException e) {
205             AppLog.warning(e.getMessage(), e.getCause(), 1);
206         }
207     }
208     
209     //-------------------------------------------------------------------------
210
public void removeSubscription(
211         ServiceHeader header,
212         DataproviderObject_1_0 source,
213         DataproviderObject param
214     ) {
215         try {
216             DataproviderObject_1_0 userHome = this.plugin.getUserHome(header, source.path());
217             List JavaDoc subscriptions = this.getSubscriptions(userHome.path(), source);
218             DataproviderObject_1_0 matchingSubscription = this.findMatchingSubscription(header, subscriptions, source, true);
219             if(matchingSubscription != null) {
220                 this.delegation.addRemoveRequest(
221                     matchingSubscription.path()
222                 );
223             }
224         }
225         catch(ServiceException e) {
226             AppLog.warning(e.getMessage(), e.getCause(), 1);
227         }
228     }
229     
230     //-------------------------------------------------------------------------
231
public void addSubscriptionForParent(
232         ServiceHeader header,
233         DataproviderObject_1_0 source,
234         DataproviderObject param
235     ) {
236         try {
237             DataproviderObject_1_0 userHome = this.plugin.getUserHome(header, source.path());
238             List JavaDoc subscriptions = this.getSubscriptions(userHome.path(), source);
239             DataproviderObject_1_0 matchingSubscription = this.findMatchingSubscription(header, subscriptions, source, false);
240             if(matchingSubscription == null) {
241                 DataproviderObject_1_0 topic = this.findTopic(source);
242                 if(topic != null) {
243                     DataproviderObject newSubscription = new DataproviderObject(
244                         userHome.path().getDescendant(new String JavaDoc[]{"subscription", UUIDs.getGenerator().next().toString()})
245                     );
246                     newSubscription.values(SystemAttributes.OBJECT_CLASS).add(
247                         "org:opencrx:kernel:home1:Subscription"
248                     );
249                     newSubscription.values("name").addAll(topic.values("name"));
250                     newSubscription.values("description").addAll(topic.values("description"));
251                     newSubscription.values("topic").add(topic.path());
252                     newSubscription.values("isActive").add(Boolean.TRUE);
253                     this.delegation.addCreateRequest(
254                         newSubscription
255                     );
256                 }
257             }
258         }
259         catch(ServiceException e) {
260             AppLog.warning(e.getMessage(), e.getCause(), 1);
261         }
262     }
263     
264     //-------------------------------------------------------------------------
265
public void removeSubscriptionForParent(
266         ServiceHeader header,
267         DataproviderObject_1_0 source,
268         DataproviderObject param
269     ) {
270         try {
271             DataproviderObject_1_0 userHome = this.plugin.getUserHome(header, source.path());
272             List JavaDoc subscriptions = this.getSubscriptions(userHome.path(), source);
273             DataproviderObject_1_0 matchingSubscription = this.findMatchingSubscription(header, subscriptions, source, false);
274             if(matchingSubscription != null) {
275                 this.delegation.addRemoveRequest(
276                     matchingSubscription.path()
277                 );
278             }
279         }
280         catch(ServiceException e) {
281             AppLog.warning(e.getMessage(), e.getCause(), 1);
282         }
283     }
284     
285     //-------------------------------------------------------------------------
286
// Members
287
//-------------------------------------------------------------------------
288
private final OpenCrxKernel_1 plugin;
289     private final RequestCollection delegation;
290     
291 }
292
293 //--- End of File -----------------------------------------------------------
294
Popular Tags