1 19 package org.openharmonise.rm.metadata; 20 21 import java.util.*; 22 import java.util.logging.*; 23 24 import org.openharmonise.commons.dsi.*; 25 import org.openharmonise.commons.dsi.dml.*; 26 import org.openharmonise.commons.xml.XMLUtils; 27 import org.openharmonise.rm.*; 28 import org.openharmonise.rm.publishing.*; 29 import org.openharmonise.rm.resources.*; 30 import org.openharmonise.rm.resources.lifecycle.EditException; 31 import org.openharmonise.rm.resources.metadata.properties.Property; 32 import org.openharmonise.rm.resources.publishing.Template; 33 import org.w3c.dom.*; 34 35 36 44 public class ProfilePropertyInstance 45 extends AbstractPropertyInstance 46 implements Publishable { 47 48 52 protected static final String CLMN_PROFILE_PARENT = "parent_profile_id"; 53 54 58 public static final String TAG_PROPERTYINSTANCE_GROUP = "PropertyInstanceGroup"; 59 60 63 public static final Logger m_logger = Logger.getLogger(ProfilePropertyInstance.class.getName()); 64 65 68 public ProfilePropertyInstance() { 69 super(); 70 71 } 72 73 78 public ProfilePropertyInstance(AbstractDataStoreInterface dbint) { 79 super(dbint); 80 81 } 82 83 91 public ProfilePropertyInstance( 92 AbstractDataStoreInterface dbintrf, 93 Profile profile) { 94 super(dbintrf, profile); 95 96 } 97 98 108 public ProfilePropertyInstance( 109 AbstractDataStoreInterface dbintrf, 110 int nPropertyId, 111 Profile profile) { 112 super(dbintrf, nPropertyId, profile); 113 114 } 115 116 123 public ProfilePropertyInstance( 124 AbstractDataStoreInterface dbintrf, 125 Property prop) { 126 super(dbintrf, prop); 127 } 128 129 139 public ProfilePropertyInstance( 140 AbstractDataStoreInterface dbintrf, 141 Property property, 142 Profile profile) { 143 super(dbintrf, property, profile); 144 145 } 146 147 154 public void addValue(Profile prof) throws PopulateException { 155 if(prof != null) { 156 super.addValue(prof); 157 } 158 } 159 160 168 public void addValue(Profile prof, int nId) throws PopulateException { 169 170 if(prof != null) { 171 super.addValue(prof,nId); 172 } 173 174 } 175 176 183 public void removeValue(Profile prof) 184 throws InvalidPropertyValueException { 185 super.removeValue(prof); 186 } 187 188 191 public void populate(Element xmlElement, State state) 192 throws PopulateException { 193 String sTagName = xmlElement.getTagName(); 194 Text txt = null; 195 196 if (sTagName.equals(TAG_PROP_INSTANCE_VALUES) == true) { 197 NodeList nodes = 198 xmlElement.getElementsByTagName(Profile.TAG_PROFILE); 199 200 for (int i = 0; i < nodes.getLength(); i++) { 201 Element profEl = (Element) nodes.item(i); 202 Profile tmpProf = 203 new Profile(m_dsi, m_profile.getProfiledObject()); 204 try { 205 tmpProf.populate(profEl, state); 206 addValue(tmpProf); 207 } catch (InvalidPropertyValueException e) { 208 throw new PopulateException( 209 "Invalid value for PropertyInstance",e); 210 } 211 } 212 } else { 213 super.populate(xmlElement, state); 214 } 215 } 216 217 220 public Element publish(Element formEl, HarmoniseOutput xmlDoc, State state) 221 throws PublishException { 222 223 Element resultEl = null; 224 String sTagname = formEl.getTagName(); 225 226 if (sTagname.equals(TAG_PROPERTYINSTANCE) == true) { 227 resultEl = super.publish(formEl, xmlDoc, state); 228 } else if(sTagname.equals(TAG_PROP_INSTANCE_VALUES)){ 229 resultEl = xmlDoc.createElement(TAG_PROP_INSTANCE_VALUES); 230 231 if (m_values != null) { 232 233 boolean bAllProps = false; 234 Map propInstMap = new Hashtable(); 237 List propInstEls = XMLUtils.getChildrenByName(formEl, TAG_PROPERTYINSTANCE); 238 Element publishPropInstEl = null; 239 Element publishValueEl = null; 240 Element publishAllPropEl = null; 241 Element publishTemplateEl = null; 242 243 if(propInstEls.size() > 0) { 244 245 Iterator iter = propInstEls.iterator(); 247 248 while (iter.hasNext()) { 249 Element propInstEl = (Element) iter.next(); 250 251 List propEls = XMLUtils.getChildrenByName(propInstEl, Property.TAG_PROPERTY); 252 253 if(propEls.size() > 0) { 254 Element nameEl = (Element) ((Element)propEls.get(0)).getElementsByTagName(AbstractObject.TAG_NAME).item(0); 255 256 String sName = nameEl.getChildNodes().item(0).getNodeValue(); 257 258 propInstMap.put(sName,propInstEl); 259 } 260 261 } 262 } else { 263 bAllProps = true; 264 265 NamedNodeMap attrs = formEl.getAttributes(); 266 267 publishPropInstEl = xmlDoc.createElement(TAG_PROPERTYINSTANCE); 268 269 Element propEl = xmlDoc.createElement(Property.TAG_PROPERTY); 270 publishPropInstEl.appendChild(propEl); 271 272 publishValueEl = xmlDoc.createElement(AbstractPropertyInstance.TAG_PROP_INSTANCE_VALUES); 273 274 publishPropInstEl.appendChild(publishValueEl); 275 276 List allprop = XMLUtils.getChildrenByName(formEl, Profile.TAG_ALLPROPERTYINSTANCES); 277 278 if(allprop.size() > 0) { 279 Element allpropEl = (Element) allprop.get(0); 280 281 Element templateEl = (Element) XMLUtils.getFirstNamedChild(allpropEl, Template.TAG_TEMPLATE); 282 283 if(templateEl != null) { 284 publishTemplateEl = (Element) xmlDoc.importNode(templateEl, true); 285 286 if(publishAllPropEl != null) { 287 publishAllPropEl.appendChild(publishTemplateEl.cloneNode(true)); 288 } 289 } 290 291 publishAllPropEl = xmlDoc.createElement(Profile.TAG_ALLPROPERTYINSTANCES); 292 293 for(int i=0;i<attrs.getLength();i++) { 294 Attr attr = (Attr) attrs.item(i); 295 publishAllPropEl.setAttribute(attr.getName(), attr.getValue()); 296 publishValueEl.setAttribute(attr.getName(), attr.getValue()); 297 } 298 299 300 Element origPropEl = XMLUtils.getFirstNamedChild(allpropEl, Property.TAG_PROPERTY); 302 303 if(origPropEl != null) { 304 NodeList nodes = origPropEl.getChildNodes(); 305 306 for (int i = 0; i < nodes.getLength(); i++) { 307 Node node = 308 (Node) nodes.item(i); 309 propEl.appendChild(xmlDoc.importNode(node,true)); 310 } 311 } 312 313 Element clonePropEl = (Element) propEl.cloneNode(true); 314 publishAllPropEl.appendChild(clonePropEl); 315 316 317 } 318 319 } 320 321 322 for (int j = 0; j < m_values.size(); j++) { 323 324 try { 325 Element groupEl = xmlDoc.createElement(ProfilePropertyInstance.TAG_PROPERTYINSTANCE_GROUP); 326 327 Profile tmpProf = (Profile) m_values.get(j); 328 329 List propInsts = tmpProf.getPropertyInstances(); 330 331 Iterator iter = propInsts.iterator(); 332 333 while (iter.hasNext()) { 334 AbstractPropertyInstance propInst = (AbstractPropertyInstance) iter.next(); 335 String sPropName = propInst.getName(); 336 if(bAllProps == true) { 337 338 if(publishTemplateEl != null) { 341 if(propInst instanceof ChildObjectPropertyInstance) { 342 publishValueEl.appendChild(publishTemplateEl); 343 } 344 } 345 346 if(publishAllPropEl != null && propInst instanceof ProfilePropertyInstance) { 347 publishValueEl.appendChild(publishAllPropEl); 348 } 349 350 Element publishedEl = propInst.publish(publishPropInstEl, xmlDoc, state); 351 groupEl.appendChild(publishedEl); 352 353 if(publishTemplateEl != null) { 354 if(propInst instanceof ChildObjectPropertyInstance) { 355 publishValueEl.removeChild(publishTemplateEl); 356 } 357 } 358 359 if(publishAllPropEl != null && propInst instanceof ProfilePropertyInstance) { 360 publishValueEl.removeChild(publishAllPropEl); 361 } 362 363 } else if(propInstMap.containsKey(sPropName)) { 364 Element tmpEl = (Element) propInstMap.get(sPropName); 365 366 Element publishedEl = propInst.publish(tmpEl, xmlDoc, state); 367 368 groupEl.appendChild(publishedEl); 369 } 370 } 371 372 resultEl.appendChild(groupEl); 373 } catch (DataAccessException e) { 374 throw new PublishException(e.getLocalizedMessage(),e); 375 } 376 } 377 378 } 379 380 } else { 381 resultEl = super.publish(formEl, xmlDoc, state); 382 } 383 384 if (resultEl == null) { 385 resultEl = xmlDoc.createElement(TAG_ERROR); 386 resultEl.appendChild( 387 xmlDoc.createTextNode("Problem publishing " + sTagname)); 388 } 389 390 return resultEl; 391 } 392 393 396 public boolean match(AbstractPropertyInstance propInst) 397 throws ProfileException { 398 boolean bMatch = false; 399 400 if (propInst instanceof ProfilePropertyInstance) { 401 List otherProfs = propInst.getValues(); 402 403 boolean bLooping = true; 404 405 List vals = getValues(); 406 407 if (vals.size() == otherProfs.size()) { 409 410 Iterator iter = vals.iterator(); 411 Iterator otherIter = otherProfs.iterator(); 412 413 while (iter.hasNext() && bLooping == true) { 415 Profile tmpProf = (Profile) iter.next(); 416 boolean bFound = false; 417 418 while (otherIter.hasNext() == true && bFound == false) { 419 Profile otherProf = (Profile) otherIter.next(); 420 421 if (tmpProf.match(otherProf) == true) { 422 bFound = true; 423 } 424 } 425 426 if (bFound == false) { 427 bLooping = false; 428 } 429 } 430 } 431 432 if (bLooping == true) { 435 bMatch = true; 436 } 437 } 438 439 return bMatch; 440 } 441 442 445 public JoinConditions getInstanceJoinConditions( 446 String sObjectTag, 447 boolean bIsOuter) 448 throws DataStoreException { 449 throw new UnsupportedOperationException (); 450 } 451 452 455 public List processResultSet(CachedResultSet resultSet, SelectStatement select) { 456 throw new UnsupportedOperationException (); 457 } 458 459 462 public List processResultSet(CachedResultSet resultSet, SelectStatement select, int limit) { 463 throw new UnsupportedOperationException (); 464 } 465 466 469 public ColumnRef getInstanceColumnRef(String sColumn, boolean bIsHist) 470 throws DataStoreException { 471 ColumnRef returnColRef = null; 472 String sDBTable = getDBTableName(); 473 474 if (sColumn.equals(CLMN_PROFILE_PARENT) == true) { 475 returnColRef = 476 new ColumnRef(sDBTable, CLMN_PROFILE_PARENT, ColumnRef.NUMBER); 477 } 478 479 if (returnColRef != null) { 480 return returnColRef; 481 } else { 482 return super.getInstanceColumnRef(sColumn, bIsHist); 483 } 484 } 485 486 499 static public ColumnRef getColumnRef(AbstractProfiledObject profObj, String sColumn, boolean bIsHist) throws DataStoreException { 500 ColumnRef returnColRef = null; 501 String sDBTable = Profile.getDBTableName(profObj); 502 503 if (sColumn.equals(TAG_VALUE) == true || sColumn.equals(CLMN_PROFILE_PARENT) == true) { 504 returnColRef = 505 new ColumnRef(sDBTable, CLMN_PROFILE_PARENT, ColumnRef.NUMBER); 506 } 507 508 if (returnColRef != null) { 509 return returnColRef; 510 } else { 511 throw new InvalidColumnReferenceException(sColumn); 512 } 513 } 514 515 524 public static String getDBTableName(Profile profile) { 525 return profile.getDBTableName(); 526 } 527 528 529 532 533 536 protected void save(Profile prof) throws ProfileException, EditException { 537 if ((m_bIsTemporary == false) && hasValues() == true) { 538 setDBTable(prof); 540 541 m_profile = prof; 542 543 UpdateStatement update = new UpdateStatement(); 544 String sTable = getDBTableName(); 545 546 for(int i = 0; i < m_values.size() ;i++) { 547 try { 548 549 Profile tmpProf = (Profile) m_values.get(i); 550 551 update.clear(); 552 553 tmpProf = tmpProf.save(prof.getProfiledObject()); 554 555 m_values.set(i, tmpProf); 556 557 boolean bIsHist = isHistorical(); 558 559 update.setTable(sTable); 560 561 update.addColumnValue( 562 this.getInstanceColumnRef(CLMN_PROFILE_PARENT, bIsHist), 563 m_profile.getId()); 564 update.addColumnValue( 565 this.getInstanceColumnRef(CLMN_PROPERTY_ID, bIsHist), 566 getProperty().getId()); 567 568 update.addWhereCondition( 569 this.getInstanceColumnRef(CLMN_ID, bIsHist), 570 "=", 571 tmpProf.getId()); 572 573 m_dsi.execute(update); 574 } catch (DataStoreException e) { 575 throw new ProfileException(e.getMessage(),e); 576 } catch (DataAccessException e) { 577 throw new ProfileException(e.getMessage(),e); 578 } 579 } 580 581 update.clear(); 582 583 m_values2Add.clear(); 584 m_values2Remove.clear(); 585 586 setIsChanged(false); 587 setIsPopulated(true); 588 } 589 } 590 591 592 595 protected void update(Profile prof) throws ProfileException, EditException { 596 if (isPopulated() == true && isChanged() == true) { 597 UpdateStatement update = new UpdateStatement(); 598 setDBTable(prof); 600 601 m_profile = prof; 602 603 boolean bIsHist = isHistorical(); 604 605 String sTable = getDBTableName(); 606 607 for(int i = 0; i < m_values2Add.size() ;i++) { 608 Profile profVal = (Profile) m_values2Add.get(i); 609 int nIndex = m_values.indexOf(profVal); 610 611 update.clear(); 612 try { 613 614 Profile tmpProf = profVal.save(prof.getProfiledObject()); 615 616 if(nIndex >= 0) { 617 m_values.set(nIndex, tmpProf); 618 } else { 619 m_values.add(tmpProf); 620 } 621 622 update.setTable(sTable); 623 624 update.addColumnValue( 625 this.getInstanceColumnRef(CLMN_PROFILE_PARENT, bIsHist), 626 m_profile.getId()); 627 update.addColumnValue( 628 this.getInstanceColumnRef(CLMN_PROPERTY_ID, bIsHist), 629 getProperty().getId()); 630 631 update.addWhereCondition( 632 this.getInstanceColumnRef(CLMN_ID, bIsHist), 633 "=", 634 tmpProf.getId()); 635 636 m_dsi.execute(update); 637 } catch (DataStoreException e) { 638 throw new ProfileException( 639 "Error occurred processing insert",e); 640 } catch (DataAccessException e) { 641 throw new ProfileException( 642 "Error occurred processing insert",e); 643 } 644 } 645 646 Iterator iter = m_values2Remove.iterator(); 647 648 while(iter.hasNext()) { 649 Profile tmpProf = (Profile) iter.next(); 650 int nIndex = m_values.indexOf(tmpProf); 651 652 if(nIndex >= 0) { 653 m_values.remove(nIndex); 654 } 655 656 tmpProf.delete(); 657 } 658 } else if(isPopulated() == false) { 659 save(prof); 661 } 662 663 m_values2Add.clear(); 664 m_values2Remove.clear(); 665 666 setIsChanged(false); 667 setIsPopulated(true); 668 669 } 670 671 678 protected void delete() throws ProfileException { 679 Iterator iter = m_values.iterator(); 680 681 while (iter.hasNext()) { 682 Profile tmpProf = (Profile) iter.next(); 683 684 tmpProf.delete(); 685 } 686 687 m_values.clear(); 688 } 689 690 693 protected ColumnRef getColumnForData() throws DataStoreException { 694 throw new UnsupportedOperationException (); 695 } 696 697 700 protected Object getValueToStoreForValue(Object val) 701 throws ProfileException { 702 throw new UnsupportedOperationException (); 703 } 704 705 708 protected void setDBTable(Profile profile) { 709 m_sDataTable = profile.getDBTableName(); 710 } 711 714 protected boolean isChanged() { 715 boolean bIsChanged = super.isChanged(); 716 717 if(bIsChanged == false) { 718 Iterator iter = m_values.iterator(); 719 720 while (iter.hasNext() && bIsChanged == false) { 721 Profile prof = (Profile) iter.next(); 722 try { 723 bIsChanged = prof.isChanged(); 724 } catch (DataAccessException e) { 725 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 726 } 727 } 728 } 729 730 return bIsChanged; 731 } 732 733 736 protected void setHistorical(boolean bIsHist) { 737 738 if(hasValues() == true) { 739 Iterator iter = m_values.iterator(); 740 741 while (iter.hasNext()) { 742 Profile tmpProf = (Profile) iter.next(); 743 tmpProf.setHistorical(bIsHist); 744 } 745 } 746 747 super.setHistorical(bIsHist); 748 } 749 750 } 751 | Popular Tags |