Class PersistableAdapter

java.lang.Object
com.ptc.core.lwc.server.PersistableAdapter

public final class PersistableAdapter extends Object
PersistableAdapter provides a convenient means to access values of attributes of a Persistable by name, regardless of the underlying storage mechanism used for that attribute. I.e., it provides a standardized API for getting and setting the values of hard (modeled) attributes, global soft attributes (IBAs), standard soft attributes, alias attributes, etc.

Supported API: true
Note: This is only supported on the server side.
The general usage pattern is to construct a PersistableAdapter by passing a Persistable or the name of a type, identifying the list of attributes that will be subsequently accessed, accessing the values of those attributes, and then applying any changes.
Note: In order for constraints to be enforced the correct locale *and* operation identifier *must* be provided when constructing the persistable adapter instance.
Example usage:

Passing CONTAINER INFORMATION when constructing the persistable adapter instance

ObjectIdentifier oid = ObjectIdentifier.newObjectIdentifier("wt.pdmlink.PDMLinkProduct:154482");
PDMLinkProduct product = (PDMLinkProduct) PersistenceHelper.manager.refresh(oid);
WTContainerRef containerRef = WTContainerRef.newWTContainerRef(product);
Locale locale = SessionHelper.getLocale(); PersistableAdapter obj = new PersistableAdapter("com.acme.AcmePart",locale,new CreateOperationIdentifier(), containerRef);
obj.load("containerReference");
obj.set("name","my name");
obj.set("number","12345");
obj.persist();

CREATE

Locale locale = SessionHelper.getLocale(); PersistableAdapter obj = new PersistableAdapter("com.acme.AcmePart",locale,new CreateOperationIdentifier());
obj.load("name","number");
obj.set("name","my name");
obj.set("number","12345");
obj.persist();

RETRIEVE
PersistableAdapter obj = new PersistableAdapter(my_persistable,null,locale,null);
obj.load("name","number");
Object nameValue = obj.get("name");
Object numberValue = obj.get("number");

UPDATE
PersistableAdapter obj = new PersistableAdapter(my_persistable,null,locale,new UpdateOperationIdentifier());
obj.load("attributeA","attribtueB");
obj.set("attributeA",Boolean.TRUE);
obj.set("attribtueB","PURPLE");
obj.apply();
...
PersistenceHelper.manager.modify(my_persistable);
  • Constructor Details

    • PersistableAdapter

      public PersistableAdapter(Persistable p, String type, Locale locale, OperationIdentifier operation) throws WTException
      When this constructor is used, all values loaded into this object will be loaded from the given Persistable if possible; otherwise, values will be loaded from the database. A subsequent apply() call will cause all loaded values to be applied the this underlying Peristable. If the provided type is null, the Persistable will retain its current type. If the provided locale is null, descriptors and constraints will not be retrieved by subsequent load calls. In addition, getAsString() and set() may throw exceptions if a Locale is required to format or parse a value. If the provided operation is null, constraints will not be retrieved by subsequent load calls.

      Supported API: true
      Parameters:
      p - The Persistable that will back this object.
      type - The type to set on the object.
      locale - The locale to be used when retrieving descriptors and formatting and parsing values.
      operation - The operation to be used when retrieving constraints.
      Throws:
      WTException
    • PersistableAdapter

      public PersistableAdapter(Persistable p, String type, Locale locale, OperationIdentifier operation, WTContainerRef containerRef) throws WTException
      When this constructor is used, all values loaded into this object will be loaded from the given Persistable if possible; otherwise, values will be loaded from the database. A subsequent apply() call will cause all loaded values to be applied the this underlying Peristable. If the provided type is null, the Persistable will retain its current type. If the provided locale is null, descriptors and constraints will not be retrieved by subsequent load calls. In addition, getAsString() and set() may throw exceptions if a Locale is required to format or parse a value. If the provided operation is null, constraints will not be retrieved by subsequent load calls.

      Supported API: true
      Parameters:
      p - The Persistable that will back this object.
      type - The type to set on the object.
      locale - The locale to be used when retrieving descriptors and formatting and parsing values.
      operation - The operation to be used when retrieving constraints.
      containerRef - A WTContainerRef object.Once the containerRef is set then container level constraints are used. If the provided containerRef is null, then site level constraint are used.
      Throws:
      WTException
    • PersistableAdapter

      public PersistableAdapter(ObjectReference ref, String type, Locale locale, OperationIdentifier operation) throws WTException
      When this constructor is used, all values loaded into this object will be loaded from the database. If the provided type is null, the Persistable will retain its current type. If the provided locale is null, descriptors and constraints will not be retrieved by subsequent load calls. In addition, getAsString() and set() may throw exceptions if a Locale is required to format or parse a value. If the provided operation is null, constraints will not be retrieved by subsequent load calls.

      Supported API: true
      Parameters:
      ref - The identifier of the Persistable that will back this object.
      type - The type to set on the object.
      locale - The locale to be used when retrieving descriptors and formatting and parsing values.
      operation - The operation to be used when retrieving constraints.
      Throws:
      WTException
    • PersistableAdapter

      public PersistableAdapter(ObjectReference ref, String type, Locale locale, OperationIdentifier operation, WTContainerRef containerRef) throws WTException
      When this constructor is used, all values loaded into this object will be loaded from the database. If the provided type is null, the Persistable will retain its current type. If the provided locale is null, descriptors and constraints will not be retrieved by subsequent load calls. In addition, getAsString() and set() may throw exceptions if a Locale is required to format or parse a value. If the provided operation is null, constraints will not be retrieved by subsequent load calls.

      Supported API: true
      Parameters:
      ref - The identifier of the Persistable that will back this object.
      type - The type to set on the object.
      locale - The locale to be used when retrieving descriptors and formatting and parsing values.
      operation - The operation to be used when retrieving constraints.
      containerRef - A WTContainerRef object.Once the containerRef is set then container level constraints are used. If the provided containerRef is null, then site level constraint are used.
      Throws:
      WTException
    • PersistableAdapter

      public PersistableAdapter(TypeInstanceIdentifier tii, String type, Locale locale, OperationIdentifier operation) throws WTException


      Supported API: true
      When this constructor is used, all values loaded into this object will be loaded from the database. If the provided type is null, the Persistable will retain its current type. If the provided locale is null, descriptors and constraints will not be retrieved by subsequent load calls. In addition, getAsString() and set() may throw exceptions if a Locale is required to format or parse a value. If the provided operation is null, constraints will not be retrieved by subsequent load calls.
      Parameters:
      tii - The identifier of the Persistable that will back this object.
      type - The type to set on the object.
      locale - The locale to be used when retrieving descriptors and formatting and parsing values.
      operation - The operation to be used when retrieving constraints.
      Throws:
      WTException
    • PersistableAdapter

      public PersistableAdapter(TypeInstanceIdentifier tii, String type, Locale locale, OperationIdentifier operation, WTContainerRef containerRef) throws WTException


      Supported API: true
      When this constructor is used, all values loaded into this object will be loaded from the database. If the provided type is null, the Persistable will retain its current type. If the provided locale is null, descriptors and constraints will not be retrieved by subsequent load calls. In addition, getAsString() and set() may throw exceptions if a Locale is required to format or parse a value. If the provided operation is null, constraints will not be retrieved by subsequent load calls.
      Parameters:
      tii - The identifier of the Persistable that will back this object.
      type - The type to set on the object.
      locale - The locale to be used when retrieving descriptors and formatting and parsing values.
      operation - The operation to be used when retrieving constraints.
      containerRef - A WTContainerRef object.Once the containerRef is set then container level constraints are used. If the provided containerRef is null, then site level constraint are used.
      Throws:
      WTException
    • PersistableAdapter

      public PersistableAdapter(String type, Locale locale, OperationIdentifier operation) throws WTException
      When this constructor is used, all values loaded into this object will be loaded from OIRS, defaults, and/or the database.
      If the provided locale is null, descriptors and constraints will not be retrieved by subsequent load calls. In addition, getAsString() and set() may throw exceptions if a Locale is required to format or parse a value. If the provided operation is null, constraints will not be retrieved by subsequent load calls.

      Supported API: true
      Parameters:
      type - The type to set on the object.
      locale - The locale to be used when retrieving descriptors and formatting and parsing values.
      operation - The operation to be used when retrieving constraints.
      Throws:
      WTException
    • PersistableAdapter

      public PersistableAdapter(String type, Locale locale, OperationIdentifier operation, WTContainerRef containerRef) throws WTException
      When this constructor is used, all values loaded into this object will be loaded from OIRS, defaults, and/or the database.
      If the provided locale is null, descriptors and constraints will not be retrieved by subsequent load calls. In addition, getAsString() and set() may throw exceptions if a Locale is required to format or parse a value. If the provided operation is null, constraints will not be retrieved by subsequent load calls. Note: Once containerRef is passed to the constructor, no need to override that using set(-,-) method on PersistableAdapter class object.

      Supported API: true
      Parameters:
      type - The type to set on the object.
      locale - The locale to be used when retrieving descriptors and formatting and parsing values.
      operation - The operation to be used when retrieving constraints.
      containerRef - A WTContainerRef object.Once the containerRef is set then container level constraints are used. If the provided containerRef is null, then site level constraint are used.
      Throws:
      WTException
  • Method Details

    • load

      public void load(String... attributes) throws WTException
      Loads the values of the indicated attributes into the object. If there is a backing Persistable, values will be loaded from that Persistable if possible; otherwise, values will be loaded from the database. If Locale is not null, descriptors will also be loaded for that Locale.
      If operation and Locale are not null, constraints will also be loaded for that operation.

      Supported API: true
      Parameters:
      attributes - The set of attribute names to be loaded.
      Throws:
      WTException
    • load

      public void load(Collection<String> attributes) throws WTException
      Loads the values of the indicated attributes into the object. If there is a backing Persistable, values will be loaded from that Persistable if possible; otherwise, values will be loaded from the database. If Locale is not null, descriptors will also be loaded for that Locale.
      If operation and Locale are not null, constraints will also be loaded for that operation.

      Supported API: true
      Parameters:
      attributes - The set of attribute names to be loaded.
      Throws:
      WTException
    • get

      public Object get(String attribute) throws WTException
      Returns the value(s) of the attribute with the given name from the object. If the attribute is multi-valued, an Object[] will be returned. Throws an exception if the attribute has not been previously loaded into the object.

      Supported API: true
      Parameters:
      attribute - The name of the attribute
      Returns:
      The value of the given attribute
      Throws:
      WTException
    • isLoaded

      public boolean isLoaded(String attribute) throws WTException
      Checks whether the attribute has been loaded or not.

      Supported API: true
      Parameters:
      attribute - The name of the attribute
      Returns:
      Returns true, if the attribute was successfully loaded
      Throws:
      WTException
    • getAsString

      public Object getAsString(String attribute) throws WTException
      Returns the value(s) of the attribute with the given name from the object, converted to localized Strings. If the attribute is multi-valued, a collated Object[] will be returned. Throws an exception if the attribute has not been previously loaded into the object. Throws an exception if locale is not currently set. For unit attributes, base SI units are assumed.

      Supported API: true
      Parameters:
      attribute - The name of the attribute
      Returns:
      The value of the given attribute as a localized String
      Throws:
      WTException
    • set

      public Object set(String attribute, Object value) throws WTException
      Sets the values of the indicated attribute to the indicated value. If the attribute is multi-valued, an Object[] can be passed.
      Values can be passed as Strings. If a descriptor has not been loaded for the attribute, and the value is passed as a String, a conversion will not be performed. This may result in an exception being thrown when a subsequent apply() or persist() call is made. An exception will be thrown if there are any parsing errors for a passed localized string.

      Supported API: true
      Parameters:
      attribute - The name of the attribute
      value - The value of the given attribute
      Returns:
      The value actually set on the object
      Throws:
      WTException
    • getTypeDescriptor

      public DefinitionDescriptor getTypeDescriptor() throws WTException
      Returns the descriptor for the type of the object for the associated locale. An exception will be thrown if no attributes have been loaded.

      Supported API: true
      Returns:
      A descriptor for the type of the object
      Throws:
      WTException
    • getAttributeDescriptor

      public AttributeTypeSummary getAttributeDescriptor(String attribute) throws WTException
      Returns the descriptor for the given attribute for the associated locale. An exception will be thrown if the attribute has not been loaded. The descriptor will not contain meaningful constraint information if constraints have not been loaded.

      Supported API: true
      Parameters:
      attribute - The name of the attribute
      Returns:
      A descriptor for the attribute
      Throws:
      WTException
    • validate

      public void validate() throws WTException
      Validate all loaded data against all loaded constraints. Throws an exception if any constraint is violated.

      Supported API: true
      Throws:
      WTException
    • apply

      public Persistable apply() throws WTException
      Validate all loaded data against all loaded constraints. Throws an exception if any constraint is violated. If there is no current underlying Persistable, retrieve it from the db or construct it. Apply the loaded changes to the underlying Persistable. This method does not persist the changes. A subsequent call to persist the changes to the Persistable is required.

      Supported API: true
      Returns:
      The underlying Persistable.
      Throws:
      WTException
    • persist

      public TypeInstanceIdentifier persist() throws WTException
      Validate all loaded data against all loaded constraints. Throws an exception if any constraint is violated. Discard any current underlying Persistable. Apply the loaded changes to the database.

      Supported API: true
      Returns:
      The ThypeInstanceIdentifier of the newly persisted object.
      Throws:
      WTException