SourceForge.net Logo

How to set/retrieve attribute values?

refGetValue() and refSetValue() allow you to retrieve and set feature values in a reflective way.

Unlike the typed JMI accessors there are no type safe operations to set or retrieve attribute values when using the reflective JMI accessors - this is in the nature of reflective programming. It is your responsibility to set or retrieve an attribute value according to the correct attribute type. openMDX ensures that attribute access is done in a model compliant way at runtime. In case of a model violation, an exception is thrown. You will receive a JmiServiceException (with exception code NOT_FOUND) when you try to access an attribute that does not exist.

The following example shows how to set attribute values using the reflective JMI accessors.

Example 6-4. Set attribute values

// set single-valued attribute values
// (attribute type org::w3c::string)
person.refSetValue(
  "org:openmdx:example:lab1:Person:givenName",
  "Hans"
);
person.refSetValue(
  "org:openmdx:example:lab1:Person:lastName",
  "Muster"
);
person.refSetValue(
  "org:openmdx:example:lab1:Person:middleName",
  "Fritz"
);
// (attribute type org::w3c::boolean)
person.refSetValue(
  "org:openmdx:example:lab1:Person:isMarried", 
  new Boolean(true)
);
// (attribute type org::w3c::short)
person.refSetValue(
  "org:openmdx:example:lab1:Person:numberOfChildren", 
  new Short((short)2)
);

// set multi-valued attribute values
// (attribute type org::w3c::string and attribute multiplicity list)
postalAddress.refSetValue(
  "org:openmdx:example:lab1:PostalAddress:addressLine", 
  new String[] {
    "Postfach 99", 
    "Musterstrasse 999"
  }
);

The following example shows how to retrieve attribute values.

Example 6-5. Get attribute values

// get single-valued attribute values
// (attribute type org::w3c::string)
String givenName = (String)person.refGetValue(
  "org:openmdx:example:lab1:Person:givenName"
);
String lastName = (String)person.refGetValue(
  "org:openmdx:example:lab1:Person:lastName"
);
String middleName = (String)person.refGetValue(
  "org:openmdx:example:lab1:Person:middleName"
);
// (attribute type org::w3c::boolean)
boolean isMarried = ((Boolean)person.refGetValue(
  "org:openmdx:example:lab1:Person:isMarried"
)).booleanValue();
// (attribute type org::w3c::short)
short numberOfChildren = ((Number)person.refGetValue(
  "org:openmdx:example:lab1:Person:numberOfChildren"
)).shortValue();

// get multi-valued attribute values
// (attribute type org::w3c::string and attribute multiplicity list)
List addressLines = (List)postalAddress.refGetValue(
  "org:openmdx:example:lab1:PostalAddress:addressLine"
);

Since attribute names must be unique within the scope of a class, you can also use just the attribute name itself (e.g. lastName) instead of using the fully qualified attribute name (e.g. org:openmdx:example:lab1:Person:lastName).