1 56 package org.opencrx.kernel.layer.application; 57 58 import java.util.Iterator ; 59 import java.util.List ; 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 public SubscriptionTarget( 77 OpenCrxKernel_1 plugin, 78 RequestCollection delegation 79 ) { 80 this.plugin = plugin; 81 this.delegation = delegation; 82 } 83 84 private List getSubscriptions( 86 Path userHomeIdentity, 87 DataproviderObject_1_0 source 88 ) throws ServiceException { 89 List 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 private DataproviderObject_1_0 findMatchingSubscription( 102 ServiceHeader header, 103 List subscriptions, 104 DataproviderObject_1_0 source, 105 boolean matchExact 106 ) throws ServiceException { 107 DataproviderObject_1_0 matchingSubscription = null; 108 for( 109 Iterator 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 boolean filterMatches = false; 119 for( 120 int j = 0; 121 j < 5; 122 j++ 123 ) { 124 String filterName = (String )subscription.values("filterName" + j).get(0); 125 String filterValue = (String )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 )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 public DataproviderObject_1_0 findTopic( 146 DataproviderObject_1_0 source 147 ) throws ServiceException { 148 List topics = this.delegation.addFindRequest( 149 new Path("xri:@openmdx:org.opencrx.kernel.workflow1/provider").getDescendant(new String []{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 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 )topic.values("topicPathPattern").get(0))) 165 ) { 166 matchingTopic = topic; 167 break; 168 } 169 } 170 return matchingTopic; 171 } 172 173 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 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 []{"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 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 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 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 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 []{"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 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 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 private final OpenCrxKernel_1 plugin; 289 private final RequestCollection delegation; 290 291 } 292 293 | Popular Tags |