1 19 package org.openharmonise.rm.resources.metadata.properties.ranges; 20 21 import java.util.*; 22 import java.util.logging.*; 23 24 import org.openharmonise.commons.dsi.*; 25 import org.openharmonise.rm.*; 26 import org.openharmonise.rm.dsi.DataStoreInterfaceFactory; 27 import org.openharmonise.rm.factory.*; 28 import org.openharmonise.rm.metadata.ChildObjectPropertyInstance; 29 import org.openharmonise.rm.publishing.*; 30 import org.openharmonise.rm.resources.*; 31 import org.openharmonise.rm.resources.publishing.Template; 32 import org.w3c.dom.*; 33 34 35 43 public class AbsoluteChildObjectRange 44 extends AbstractRange 45 implements ChildObjectRange, Publishable { 46 47 50 public final static String TAG_ABSOLUTECHILDOBJECT_RANGE = "AbsoluteChildObjectRange"; 51 52 55 public static final String TAG_AVAILABLEVALUES = "AvailableValues"; 56 57 61 private List m_parent_restrictions = null; 62 63 67 static final private String SEPARATOR = "|"; 68 69 private static Logger m_logger = Logger.getLogger(AbsoluteChildObjectRange.class.getName()); 70 71 75 public AbsoluteChildObjectRange() { 76 super(); 77 } 78 79 87 public AbsoluteChildObjectRange(String sObjClassName, String sDetails) { 88 super(sObjClassName, sDetails); 89 } 90 91 97 public AbsoluteChildObjectRange(String sObjClassName) { 98 super(sObjClassName); 99 } 100 101 110 public AbsoluteChildObjectRange(String sObjClassName, AbstractParentObject parent) 111 throws DataAccessException { 112 super(sObjClassName, parent.getFullPath()); 113 } 114 115 118 public boolean isValid(Object obj) { 119 boolean bIsValid = false; 120 121 if (obj instanceof AbstractChildObject) { 122 123 try { 124 Class clss = Class.forName(m_sObjectClassName); 125 126 if (clss.isInstance(obj) == true) { 127 if (m_sDetails == null || m_sDetails.length() == 0) { 128 bIsValid = true; 129 } else { 130 AbstractChildObject child = (AbstractChildObject) obj; 131 132 Iterator iter = getAllowedParents().iterator(); 133 134 while (iter.hasNext() && bIsValid == false) { 135 String sPath = (String ) iter.next(); 136 bIsValid = child.getPath().startsWith(sPath); 137 } 138 139 } 140 } 141 } catch (DataAccessException e) { 142 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 144 bIsValid = false; 145 } catch (ClassNotFoundException e) { 146 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 148 bIsValid = false; 149 } 150 151 } 152 153 return bIsValid; 154 } 155 156 164 public List getAvailableValues() throws DataAccessException { 165 Vector result = new Vector(); 166 try { 167 AbstractDataStoreInterface dsi = 168 DataStoreInterfaceFactory.getDataStoreInterface(); 169 170 String sParentClassName = AbstractChildObject.getParentObjectClassName(m_sObjectClassName); 171 172 Iterator iter = getAllowedParents().iterator(); 173 174 while (iter.hasNext()) { 175 String sPath = (String ) iter.next(); 176 177 AbstractParentObject parent = 178 (AbstractParentObject) HarmoniseObjectFactory.instantiateHarmoniseObject( 179 dsi, 180 sParentClassName, 181 sPath); 182 183 addAllChildObjectsToList(result, parent); 184 185 } 186 187 188 } catch (HarmoniseFactoryException e) { 189 throw new DataAccessException( 190 "Error occured getting parent from factory",e); 191 } catch (DataStoreException e) { 192 throw new DataAccessException( 193 "Error occured getting datastoreinterface",e); 194 } 195 196 return result; 197 } 198 199 202 public Element publish(Element topEl, HarmoniseOutput output, State state) 203 throws PublishException { 204 Element el = null; 205 206 if (topEl.getTagName().equals(TAG_AVAILABLEVALUES) == true) { 207 el = output.createElement(TAG_AVAILABLEVALUES); 208 209 try { 210 AbstractDataStoreInterface dsi = 211 DataStoreInterfaceFactory.getDataStoreInterface(); 212 213 NodeList templates = 214 topEl.getElementsByTagName(Template.TAG_TEMPLATE); 215 216 if (templates.getLength() > 0) { 217 Element templateEl = (Element) templates.item(0); 218 219 Template template = 220 (Template) HarmoniseObjectFactory.instantiateHarmoniseObject( 221 dsi, 222 templateEl, 223 state); 224 225 List values = getAvailableValues(); 226 227 Iterator iter = values.iterator(); 228 229 while (iter.hasNext()) { 230 Publishable val = (Publishable) iter.next(); 231 232 el.appendChild(val.publish(template, output, state)); 233 } 234 } 235 } catch (HarmoniseFactoryException e) { 236 throw new PublishException( 237 "Error occured getting template from factory",e); 238 } catch (DataAccessException e) { 239 throw new PublishException( 240 "Error occured getting available values",e); 241 } catch (DataStoreException e) { 242 throw new PublishException( 243 "Error occured getting data store interface",e); 244 } 245 246 } else { 247 el = super.publish(topEl, output, state); 248 } 249 250 return el; 251 } 252 253 256 257 267 protected void addAllChildObjectsToList( 268 List result, 269 AbstractParentObject parent) 270 throws DataAccessException { 271 Iterator iter = parent.getChildren().iterator(); 272 273 while (iter.hasNext()) { 274 AbstractChildObject child = (AbstractChildObject) iter.next(); 275 276 if(isValid(child)) { 277 result.add(child); 278 } 279 280 if (child instanceof AbstractParentObject) { 281 addAllChildObjectsToList(result, (AbstractParentObject) child); 282 } 283 } 284 } 285 286 289 public boolean equals(Object obj) { 290 boolean bResult = false; 291 292 if (obj instanceof AbsoluteChildObjectRange) { 293 bResult = super.equals(obj); 294 } 295 296 return bResult; 297 } 298 299 307 public List getAllowedParents() throws DataAccessException { 308 validateAllowedParents(); 309 310 List allowedParent = new ArrayList(); 311 312 if(m_parent_restrictions != null) { 313 allowedParent.addAll(m_parent_restrictions); 314 } 315 316 return allowedParent; 317 } 318 319 325 public void setAllowedParents(List allowedParents) { 326 if ((m_parent_restrictions == null && allowedParents != null) || m_parent_restrictions.equals(allowedParents) == false) { 327 m_parent_restrictions = new ArrayList(allowedParents); 328 isChanged(true); 329 } 330 } 331 332 337 public void addAllowedParent(String sPath) { 338 if(m_parent_restrictions == null) { 339 m_parent_restrictions = new ArrayList(); 340 } 341 342 if(m_parent_restrictions.contains(sPath) == false) { 343 m_parent_restrictions.add(sPath); 344 isChanged(true); 345 } 346 347 } 348 349 352 public String getDetails() { 353 if (isChanged()) { 354 StringBuffer strbuf = new StringBuffer (); 355 if (m_parent_restrictions != null) { 356 try { 357 validateAllowedParents(); 358 } catch (DataAccessException e) { 359 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 360 } 361 Iterator iter = m_parent_restrictions.iterator(); 362 363 while (iter.hasNext()) { 364 String sPath = (String ) iter.next(); 365 strbuf.append(sPath); 366 367 if (iter.hasNext()) { 368 strbuf.append(SEPARATOR); 369 } 370 } 371 372 } 373 super.setDetails(strbuf.toString()); 374 } 375 376 return super.getDetails(); 377 } 378 379 382 public void setDetails(String sDetails) { 383 if (m_parent_restrictions == null) { 384 m_parent_restrictions = new ArrayList(); 385 } 386 387 if (sDetails != null) { 388 StringTokenizer tokenizer = 389 new StringTokenizer(sDetails, SEPARATOR); 390 391 392 int i = 0; 393 int nTmp = 0; 394 while (tokenizer.hasMoreTokens()) { 395 String token = tokenizer.nextToken(); 396 397 if (token != null && token.length() > 0) { 398 m_parent_restrictions.add(token); 399 } 400 } 401 } 402 super.setDetails(sDetails); 403 } 404 405 410 public void setObjectRestriction(String sClassname) { 411 if((m_sObjectClassName == null && sClassname != null) || m_sObjectClassName.equals(sClassname) == false) { 412 m_sObjectClassName = sClassname; 413 isChanged(true); 414 } 415 416 } 417 418 421 public String getChildObjectValueClassName(AbstractChildObject child) { 422 return getObject(); 423 } 424 425 428 public Class getPropertyInstanceClass() throws ClassNotFoundException { 429 430 return ChildObjectPropertyInstance.class; 431 } 432 433 436 public String getTagName() { 437 return TAG_ABSOLUTECHILDOBJECT_RANGE; 438 } 439 440 443 public void populate(Element xmlElement, State state) 444 throws PopulateException { 445 String sTagname = xmlElement.getTagName(); 446 447 if(sTagname.equals(TAG_PATH_RESTRICTION)) { 448 String sPath = xmlElement.getFirstChild().getNodeValue(); 449 addAllowedParent(sPath); 450 } else { 451 super.populate(xmlElement, state); 452 } 453 454 } 455 456 459 public String getChildObjectValueClassName(Class clss) { 460 return getObject(); 461 } 462 463 private void validateAllowedParents() throws DataAccessException { 464 if(m_parent_restrictions != null && m_sObjectClassName != null) { 465 try { 466 AbstractDataStoreInterface dsi = 467 DataStoreInterfaceFactory.getDataStoreInterface(); 468 ListIterator iter = m_parent_restrictions.listIterator(); 469 String sParentClassName = AbstractChildObject.getParentObjectClassName(m_sObjectClassName); 470 471 if(sParentClassName != null) { 472 while (iter.hasNext()) { 473 String sPath = (String ) iter.next(); 474 475 AbstractObject obj = HarmoniseObjectFactory.instantiateHarmoniseObject(dsi, sParentClassName, sPath); 476 477 if(obj == null || obj.exists() == false) { 478 iter.remove(); 479 } 480 } 481 } 482 483 } catch (HarmoniseFactoryException e) { 484 throw new DataAccessException(e); 485 } catch (DataStoreException e) { 486 throw new DataAccessException(e); 487 } 488 } 489 } 490 491 } 492 | Popular Tags |