1 17 18 package org.pentaho.repository.runtime; 19 20 import java.math.BigDecimal ; 21 import java.util.Collection ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.pentaho.core.repository.IRuntimeElement; 30 import org.pentaho.core.session.IPentahoSession; 31 import org.pentaho.core.system.PentahoBase; 32 import org.pentaho.core.util.XmlHelper; 33 import org.pentaho.messages.Messages; 34 import org.pentaho.repository.HibernateUtil; 35 import org.pentaho.core.repository.RepositoryException; 36 37 public class RuntimeElement extends PentahoBase implements IRuntimeElement { 38 public static final int ClassVersionNumber = 3; 39 40 private static final long serialVersionUID = 5024690844237335928L; 41 42 private static Log logger = LogFactory.getLog(RuntimeElement.class); 43 44 private String instanceId; 45 46 private String parentId; 47 48 private String solutionId; 49 50 private String parentType; 51 52 private int revision; 53 54 private Map typesMap = new HashMap (); 56 58 private Map paramMapSS = new HashMap (); 60 private Map paramMapLS = new HashMap (); 62 private Map paramMapBD = new HashMap (); 64 private Map paramMapDT = new HashMap (); 66 private Map paramMapLong = new HashMap (); 68 private Map paramMapCPLX = new HashMap (); 70 private Date createdDate = new Date (); 72 74 private static final int MAXSSLENGH = 254; 75 76 private static final ThreadLocal allowableReadAttributeNames = new ThreadLocal (); 77 78 private boolean loaded; 79 80 private boolean readOnly; 81 82 85 88 protected RuntimeElement() { 89 90 } 91 92 98 public RuntimeElement(String instId) { 99 instanceId = instId; 100 } 101 102 112 public RuntimeElement(String instId, String parId, String parType) { 113 instanceId = instId; 114 parentId = parId; 115 parentType = parType; 116 } 117 118 130 public RuntimeElement(String instId, String parId, String parType, String solnId) { 131 instanceId = instId; 132 parentId = parId; 133 parentType = parType; 134 solutionId = solnId; 135 } 136 137 public List getMessages() { 138 return null; 139 } 140 141 protected void setPentahoSession(IPentahoSession sess) { 142 genLogIdFromSession(sess); 143 } 144 145 148 public String getParentId() { 149 return parentId; 150 } 151 152 156 public void setParentId(String parentId) { 157 this.updateOk(); 158 this.parentId = parentId; 159 } 160 161 164 public String getParentType() { 165 return parentType; 166 } 167 168 172 public void setParentType(String parentType) { 173 this.updateOk(); 174 this.parentType = parentType; 175 } 176 177 180 public String getInstanceId() { 181 return instanceId; 182 } 183 184 188 public void setInstanceId(String instId) { 189 this.updateOk(); 190 this.instanceId = instId; 191 } 192 193 196 public String getSolutionId() { 197 return solutionId; 198 } 199 200 204 public void setSolutionId(String solutionId) { 205 this.updateOk(); 206 this.solutionId = solutionId; 207 } 208 209 214 public int getRevision() { 215 return revision; 216 } 217 218 224 protected void setRevision(int rev) { 225 revision = rev; 226 } 227 228 233 public boolean equals(Object other) { 234 if (this == other) { 235 return true; 236 } 237 if (!(other instanceof RuntimeElement)) { 238 return false; 239 } 240 final RuntimeElement otherRE = (RuntimeElement) other; 241 return this.getInstanceId().equals(otherRE.getInstanceId()); 242 } 243 244 public int hashCode() { 245 return this.getInstanceId().hashCode(); 246 } 247 248 protected Map getParamMapSS() { 249 return paramMapSS; 250 } 251 252 protected Map getParamMapLS() { 253 return paramMapLS; 254 } 255 256 protected Map getParamMapDT() { 257 return paramMapDT; 258 } 259 260 protected Map getParamMapBD() { 261 return paramMapBD; 262 } 263 264 protected Map getParamMapLong() { 265 return paramMapLong; 266 } 267 268 protected Map getParamMapCPLX() { 269 return paramMapCPLX; 270 } 271 272 protected void setParamMapSS(Map ss) { 273 paramMapSS = ss; 274 } 275 276 protected void setParamMapLS(Map ls) { 277 paramMapLS = ls; 278 } 279 280 protected void setParamMapDT(Map dt) { 281 paramMapDT = dt; 282 } 283 284 protected void setParamMapBD(Map bd) { 285 paramMapBD = bd; 286 } 287 288 protected void setParamMapLong(Map lng) { 289 paramMapLong = lng; 290 } 291 292 protected void setParamMapCPLX(Map cplx) { 293 paramMapCPLX = cplx; 294 } 295 296 297 public Date getCreateDate() { 298 return createdDate; 299 } 300 301 public void setCreateDate(Date value) { 302 createdDate = value; 303 } 304 305 312 public String getStringProperty(String key) { 313 return getStringProperty(key, null); 314 } 315 316 326 public String getStringProperty(String key, String defaultValue) { 327 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getString", key)); Object prop = getParamMapSS().get(key); 329 if (prop == null) { 330 prop = getParamMapLS().get(key); 331 } 332 return (prop != null) ? prop.toString() : defaultValue; 333 } 334 335 protected void checkType(String key, String type, boolean setIt) { 336 Map localTypesMap = getTypesMap(); 337 String curType = (String ) localTypesMap.get(key); 338 if (curType != null) { 339 if (!curType.equals(type)) { 340 throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0001_INVALIDTYPE", curType, type)); } 342 } 343 if (setIt) { 344 localTypesMap.put(key, type); 345 } 346 } 347 348 358 public void setStringProperty(String key, String value) { 359 this.updateOk(); 360 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setString", key)); checkType(key, value.getClass().getName(), true); 362 Map theMapSS = getParamMapSS(); 363 Map theMapLS = getParamMapLS(); 364 if (value != null) { 365 if (value.length() > MAXSSLENGH) { 366 theMapSS.remove(key); theMapLS.put(key, new StringBuffer (value)); 369 } else { 370 theMapLS.remove(key); 371 theMapSS.put(key, value); 372 } 373 } else { 374 theMapSS.remove(key); 375 theMapLS.remove(key); 376 } 377 } 378 379 386 public BigDecimal getBigDecimalProperty(String key) { 387 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getBigDecimal", key)); return getBigDecimalProperty(key, null); 389 } 390 391 401 public BigDecimal getBigDecimalProperty(String key, BigDecimal defaultValue) { 402 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getBigDecimal", key)); Object prop = getParamMapBD().get(key); 404 return (prop != null) ? new BigDecimal ((String ) prop) : defaultValue; 405 } 406 407 417 public void setBigDecimalProperty(String key, BigDecimal value) { 418 this.updateOk(); 419 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setBigDecimal", key)); checkType(key, value.getClass().getName(), true); 421 Map theMap = getParamMapBD(); 422 if (value != null) { 423 theMap.put(key, value.toString()); 424 } else { 425 theMap.remove(key); 426 } 427 } 428 429 436 public Date getDateProperty(String key) { 437 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getDate", key)); return getDateProperty(key, null); 439 } 440 441 452 public Date getDateProperty(String key, Date defaultValue) { 453 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getDate", key)); Object prop = getParamMapDT().get(key); 455 return (prop != null) ? (Date ) prop : defaultValue; 456 } 457 458 469 public void setDateProperty(String key, Date value) { 470 this.updateOk(); 471 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setDate", key)); checkType(key, value.getClass().getName(), true); 473 Map theMap = getParamMapDT(); 474 if (value != null) { 475 theMap.put(key, value); 476 } else { 477 theMap.remove(key); 478 } 479 } 480 481 492 public Long getLongProperty(String key, Long defaultValue) { 493 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getLong", key)); Object prop = getParamMapLong().get(key); 495 return (prop != null) ? (Long ) prop : defaultValue; 496 } 497 498 509 public long getLongProperty(String key, long defaultValue) { 510 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getLong", key)); Object prop = getParamMapLong().get(key); 512 return (prop != null) ? ((Long ) prop).longValue() : defaultValue; 513 } 514 515 526 public void setLongProperty(String key, Long value) { 527 this.updateOk(); 528 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setLong", key)); checkType(key, value.getClass().getName(), true); 530 Map theMap = getParamMapLong(); 531 if (value != null) { 532 theMap.put(key, value); 533 } else { 534 theMap.remove(key); 535 } 536 } 537 538 546 public void setLongProperty(String key, long value) { 547 this.updateOk(); 548 setLongProperty(key, new Long (value)); 549 } 550 551 558 public List getListProperty(String key) { 559 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getList", key)); Object prop = getParamMapCPLX().get(key); 561 return (List ) prop; 562 } 563 564 571 public Map getMapProperty(String key) { 572 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getMap", key)); Object prop = getParamMapCPLX().get(key); 574 return (Map ) prop; 575 } 576 577 587 public void setListProperty(String key, List value) { 588 this.updateOk(); 589 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setList", key)); checkType(key, value.getClass().getName(), true); 591 Map theMap = getParamMapCPLX(); 592 if (value != null) { 593 theMap.put(key, value); 594 } else { 595 theMap.remove(key); 596 } 597 } 598 599 609 public void setMapProperty(String key, Map value) { 610 this.updateOk(); 611 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setMap", key)); checkType(key, value.getClass().getName(), true); 613 Map theMap = getParamMapCPLX(); 614 if (value != null) { 615 theMap.put(key, value); 616 } else { 617 theMap.remove(key); 618 } 619 } 620 621 628 public String toXML() { 629 StringBuffer rtn = new StringBuffer (); 630 rtn.append("<runtime-element>\r"); rtn.append(getXMLString(getInstanceId(), "instance-id", " ")); rtn.append(getXMLString(Integer.toString(getRevision()), "revision", " ")); rtn.append(getXMLString(getParentId(), "parent-id", " ")); rtn.append(getXMLString(getParentType(), "parent-type", " ")); rtn.append(getXMLString(getSolutionId(), "solution-id", " ")); rtn.append(getMapXML(this.getParamMapSS(), "small-string-map")); rtn.append(getMapXML(this.getParamMapLS(), "large-string-map")); rtn.append(getMapXML(this.getParamMapDT(), "date-map")); rtn.append(getMapXML(this.getParamMapBD(), "big-decimal-map")); rtn.append(getMapXML(this.getParamMapLong(), "long-map")); rtn.append(getMapXML(this.getParamMapCPLX(), "complex-map")); rtn.append("</runtime-element>\r"); return rtn.toString(); 644 } 645 646 private String getXMLString(String str, String tag, String indent) { 647 return indent + "<" + tag + "><![CDATA[" + str + "]]></" + tag + ">\r"; } 649 650 private String getMapXML(Map theMap, String tag) { 651 StringBuffer sb = new StringBuffer (); 652 sb.append(" <").append(tag).append(">\r"); sb.append(XmlHelper.mapToXML(theMap, " ")); sb.append(" </").append(tag).append(">\r"); return sb.toString(); 656 } 657 658 659 public Log getLogger() { 660 return logger; 661 } 662 663 public void setAllowableAttributeNames(Collection allowedReadNames) { 664 allowableReadAttributeNames.set(allowedReadNames); 665 } 666 667 670 protected Map getTypesMap() { 671 return typesMap; 672 } 673 674 678 protected void setTypesMap(Map typesMap) { 679 this.typesMap = typesMap; 680 } 681 682 public Set getParameterNames() { 683 return getTypesMap().keySet(); 684 } 685 686 public String getParameterType(String parameterName) { 687 return (String ) getTypesMap().get(parameterName); 688 } 689 690 public void setLoaded(boolean value) { 691 this.loaded = value; 692 } 693 694 public boolean getLoaded() { 695 return this.loaded; 696 } 697 698 private void updateOk() { 699 if (!loaded) { 700 return; 701 } 702 if (readOnly) { 703 throw new IllegalStateException (Messages.getErrorString("RTELEMENT.ERROR_0001_INVALIDUPDATE")); } 705 } 706 707 public boolean getReadOnly() { 708 return readOnly; 709 } 710 711 public void setReadOnly(boolean value) { 712 this.readOnly = value; 713 } 714 715 public void forceSave() { 716 try { 717 HibernateUtil.commitTransaction(); 718 HibernateUtil.flushSession(); 719 } finally { 720 HibernateUtil.beginTransaction(); 721 } 722 } 723 } 724 | Popular Tags |