1 17 18 package org.pentaho.core.repository; 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.messages.Messages; 30 import org.pentaho.core.repository.IRuntimeElement; 31 import org.pentaho.core.repository.RepositoryException; 32 import org.pentaho.core.session.IPentahoSession; 33 import org.pentaho.core.system.PentahoBase; 34 import org.pentaho.core.util.XmlHelper; 35 36 public class SimpleRuntimeElement extends PentahoBase implements IRuntimeElement { 37 public static final int ClassVersionNumber = 2; 38 39 private static final long serialVersionUID = 5024690844237335928L; 40 41 private static Log logger = LogFactory.getLog(SimpleRuntimeElement.class); 42 43 private String instanceId; 44 45 private String parentId; 46 47 private String solutionId; 48 49 private String parentType; 50 51 private int revision; 52 53 private Map typesMap = new HashMap (); 55 57 private Map paramMapSS = new HashMap (); 59 private Map paramMapLS = new HashMap (); 61 private Map paramMapBD = new HashMap (); 63 private Map paramMapDT = new HashMap (); 65 private Map paramMapLong = new HashMap (); 67 private Map paramMapCPLX = new HashMap (); 69 71 private static final int MAXSSLENGH = 254; 72 73 private static final ThreadLocal allowableReadAttributeNames = new ThreadLocal (); 74 75 private boolean loaded; 76 77 private boolean readOnly; 78 79 82 85 protected SimpleRuntimeElement() { 86 87 } 88 89 95 public SimpleRuntimeElement(String instId) { 96 instanceId = instId; 97 } 98 99 109 public SimpleRuntimeElement(String instId, String parId, String parType) { 110 instanceId = instId; 111 parentId = parId; 112 parentType = parType; 113 } 114 115 127 public SimpleRuntimeElement(String instId, String parId, String parType, String solnId) { 128 instanceId = instId; 129 parentId = parId; 130 parentType = parType; 131 solutionId = solnId; 132 } 133 134 public List getMessages() { 135 return null; 136 } 137 138 protected void setPentahoSession(IPentahoSession sess) { 139 genLogIdFromSession(sess); 140 } 141 142 145 public String getParentId() { 146 return parentId; 147 } 148 149 153 public void setParentId(String parentId) { 154 this.updateOk(); 155 this.parentId = parentId; 156 } 157 158 161 public String getParentType() { 162 return parentType; 163 } 164 165 169 public void setParentType(String parentType) { 170 this.updateOk(); 171 this.parentType = parentType; 172 } 173 174 177 public String getInstanceId() { 178 return instanceId; 179 } 180 181 185 public void setInstanceId(String instId) { 186 this.updateOk(); 187 this.instanceId = instId; 188 } 189 190 193 public String getSolutionId() { 194 return solutionId; 195 } 196 197 201 public void setSolutionId(String solutionId) { 202 this.updateOk(); 203 this.solutionId = solutionId; 204 } 205 206 211 public int getRevision() { 212 return revision; 213 } 214 215 221 protected void setRevision(int rev) { 222 revision = rev; 223 } 224 225 230 public boolean equals(Object other) { 231 if (this == other) { 232 return true; 233 } 234 if (!(other instanceof IRuntimeElement)) { 235 return false; 236 } 237 final IRuntimeElement otherRE = (IRuntimeElement) other; 238 return this.getInstanceId().equals(otherRE.getInstanceId()); 239 } 240 241 public int hashCode() { 242 return this.getInstanceId().hashCode(); 243 } 244 245 protected Map getParamMapSS() { 246 return paramMapSS; 247 } 248 249 protected Map getParamMapLS() { 250 return paramMapLS; 251 } 252 253 protected Map getParamMapDT() { 254 return paramMapDT; 255 } 256 257 protected Map getParamMapBD() { 258 return paramMapBD; 259 } 260 261 protected Map getParamMapLong() { 262 return paramMapLong; 263 } 264 265 protected Map getParamMapCPLX() { 266 return paramMapCPLX; 267 } 268 269 protected void setParamMapSS(Map ss) { 270 paramMapSS = ss; 271 } 272 273 protected void setParamMapLS(Map ls) { 274 paramMapLS = ls; 275 } 276 277 protected void setParamMapDT(Map dt) { 278 paramMapDT = dt; 279 } 280 281 protected void setParamMapBD(Map bd) { 282 paramMapBD = bd; 283 } 284 285 protected void setParamMapLong(Map lng) { 286 paramMapLong = lng; 287 } 288 289 protected void setParamMapCPLX(Map cplx) { 290 paramMapCPLX = cplx; 291 } 292 293 300 public String getStringProperty(String key) { 301 return getStringProperty(key, null); 302 } 303 304 314 public String getStringProperty(String key, String defaultValue) { 315 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getString", key)); Object prop = getParamMapSS().get(key); 317 if (prop == null) { 318 prop = getParamMapLS().get(key); 319 } 320 return (prop != null) ? prop.toString() : defaultValue; 321 } 322 323 protected void checkType(String key, String type, boolean setIt) { 324 Map localTypesMap = getTypesMap(); 325 String curType = (String ) localTypesMap.get(key); 326 if (curType != null) { 327 if (!curType.equals(type)) { 328 throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0001_INVALIDTYPE", curType, type)); } 330 } 331 if (setIt) { 332 localTypesMap.put(key, type); 333 } 334 } 335 336 346 public void setStringProperty(String key, String value) { 347 this.updateOk(); 348 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setString", key)); checkType(key, value.getClass().getName(), true); 350 Map theMapSS = getParamMapSS(); 351 Map theMapLS = getParamMapLS(); 352 if (value != null) { 353 if (value.length() > MAXSSLENGH) { 354 theMapSS.remove(key); theMapLS.put(key, new StringBuffer (value)); 357 } else { 358 theMapLS.remove(key); 359 theMapSS.put(key, value); 360 } 361 } else { 362 theMapSS.remove(key); 363 theMapLS.remove(key); 364 } 365 } 366 367 374 public BigDecimal getBigDecimalProperty(String key) { 375 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getBigDecimal", key)); return getBigDecimalProperty(key, null); 377 } 378 379 389 public BigDecimal getBigDecimalProperty(String key, BigDecimal defaultValue) { 390 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getBigDecimal", key)); Object prop = getParamMapBD().get(key); 392 return (prop != null) ? new BigDecimal ((String ) prop) : defaultValue; 393 } 394 395 405 public void setBigDecimalProperty(String key, BigDecimal value) { 406 this.updateOk(); 407 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setBigDecimal", key)); checkType(key, value.getClass().getName(), true); 409 Map theMap = getParamMapBD(); 410 if (value != null) { 411 theMap.put(key, value.toString()); 412 } else { 413 theMap.remove(key); 414 } 415 } 416 417 424 public Date getDateProperty(String key) { 425 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getDate", key)); return getDateProperty(key, null); 427 } 428 429 440 public Date getDateProperty(String key, Date defaultValue) { 441 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getDate", key)); Object prop = getParamMapDT().get(key); 443 return (prop != null) ? (Date ) prop : defaultValue; 444 } 445 446 457 public void setDateProperty(String key, Date value) { 458 this.updateOk(); 459 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setDate", key)); checkType(key, value.getClass().getName(), true); 461 Map theMap = getParamMapDT(); 462 if (value != null) { 463 theMap.put(key, value); 464 } else { 465 theMap.remove(key); 466 } 467 } 468 469 480 public Long getLongProperty(String key, Long defaultValue) { 481 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getLong", key)); Object prop = getParamMapLong().get(key); 483 return (prop != null) ? (Long ) prop : defaultValue; 484 } 485 486 497 public long getLongProperty(String key, long defaultValue) { 498 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getLong", key)); Object prop = getParamMapLong().get(key); 500 return (prop != null) ? ((Long ) prop).longValue() : defaultValue; 501 } 502 503 514 public void setLongProperty(String key, Long value) { 515 this.updateOk(); 516 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setLong", key)); checkType(key, value.getClass().getName(), true); 518 Map theMap = getParamMapLong(); 519 if (value != null) { 520 theMap.put(key, value); 521 } else { 522 theMap.remove(key); 523 } 524 } 525 526 534 public void setLongProperty(String key, long value) { 535 this.updateOk(); 536 setLongProperty(key, new Long (value)); 537 } 538 539 546 public List getListProperty(String key) { 547 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getList", key)); Object prop = getParamMapCPLX().get(key); 549 return (List ) prop; 550 } 551 552 559 public Map getMapProperty(String key) { 560 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getMap", key)); Object prop = getParamMapCPLX().get(key); 562 return (Map ) prop; 563 } 564 565 575 public void setListProperty(String key, List value) { 576 this.updateOk(); 577 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setList", key)); checkType(key, value.getClass().getName(), true); 579 Map theMap = getParamMapCPLX(); 580 if (value != null) { 581 theMap.put(key, value); 582 } else { 583 theMap.remove(key); 584 } 585 } 586 587 597 public void setMapProperty(String key, Map value) { 598 this.updateOk(); 599 trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setMap", key)); checkType(key, value.getClass().getName(), true); 601 Map theMap = getParamMapCPLX(); 602 if (value != null) { 603 theMap.put(key, value); 604 } else { 605 theMap.remove(key); 606 } 607 } 608 609 616 public String toXML() { 617 StringBuffer rtn = new StringBuffer (); 618 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(); 632 } 633 634 private String getXMLString(String str, String tag, String indent) { 635 return indent + "<" + tag + "><![CDATA[" + str + "]]></" + tag + ">\r"; } 637 638 private String getMapXML(Map theMap, String tag) { 639 StringBuffer sb = new StringBuffer (); 640 sb.append(" <").append(tag).append(">\r"); sb.append(XmlHelper.mapToXML(theMap, " ")); sb.append(" </").append(tag).append(">\r"); return sb.toString(); 644 } 645 646 647 public Log getLogger() { 648 return logger; 649 } 650 651 public void setAllowableAttributeNames(Collection allowedReadNames) { 652 allowableReadAttributeNames.set(allowedReadNames); 653 } 654 655 658 protected Map getTypesMap() { 659 return typesMap; 660 } 661 662 666 protected void setTypesMap(Map typesMap) { 667 this.typesMap = typesMap; 668 } 669 670 public Set getParameterNames() { 671 return getTypesMap().keySet(); 672 } 673 674 public String getParameterType(String parameterName) { 675 return (String ) getTypesMap().get(parameterName); 676 } 677 678 public void setLoaded(boolean value) { 679 this.loaded = value; 680 } 681 682 public boolean getLoaded() { 683 return this.loaded; 684 } 685 686 private void updateOk() { 687 if (!loaded) { 688 return; 689 } 690 if (readOnly) { 691 throw new IllegalStateException (Messages.getErrorString("RTELEMENT.ERROR_0001_INVALIDUPDATE")); } 693 } 694 695 public boolean getReadOnly() { 696 return readOnly; 697 } 698 699 public void setReadOnly(boolean value) { 700 this.readOnly = value; 701 } 702 703 public void forceSave() { 704 } 705 } 706 | Popular Tags |