private void reportPropertyTypes(DataObject data) throws Exception {
    ObjectType type = data.getObjectType();
    Iterator keys = type.getProperties();
    s_log.info("Properties for type: " + type.getName());
    while (keys.hasNext()) {
      Property p = (Property) keys.next();
      if (p.isAttribute()) {
        String msg = "Property " + p.getName() + " is attribute. Class is: " + p.getJavaClass();
        if (type.isKeyProperty(p)) {
          msg += " is key property.";
        }
        msg += " value is: " + data.get(p.getName());
        s_log.info(msg);

      } else {
        s_log.info(
            "Property "
                + p.getName()
                + "  is component: "
                + p.isComponent()
                + " is collection: "
                + p.isCollection()
                + " is role: "
                + p.isRole());
        s_log.info("ObjectType is is: " + p.getType().getName());
      }
    }

    s_log.info("END Properties for type: " + type.getName());
  }
 public void manipulate(Property p, DataObject dataInner) throws Exception {
   s_log.debug("checking property: " + p.getName());
   Object currentVal = dataInner.get(p.getName());
   DataObjectManipulator.SimpleTypeManipulator manip = m_manipulator.getManipulator(p);
   Object defaultVal = manip.getDefaultValue();
   manip.checkEquals(p.getName(), defaultVal, currentVal);
 }
  private void setDefaultProperties(DataObject data, DataObject associatedObject) throws Exception {

    final ObjectType type = data.getObjectType();
    s_log.info("");
    s_log.info("Making new object for: " + type.getQualifiedName());
    KeyGenerator.setKeyValues(data);

    ObjectType associated = (associatedObject == null) ? null : associatedObject.getObjectType();

    for (Iterator it = type.getKeyProperties(); it.hasNext(); ) {
      Property prop = (Property) it.next();
      if (prop.isAttribute()) {
        continue;
      }

      DataType propType = prop.getType();
      if (propType.equals(associated)) {
        data.set(prop.getName(), associatedObject);
      } else {
        makeChild(prop, data);
      }
    }

    PropertyManipulator.NonKeyManipulator manip =
        new PropertyManipulator.NonKeyManipulator(type) {
          public void manipulate(Property p, DataObject dataInner) throws Exception {
            m_manipulator.setDefaultProperty(p, dataInner);
          }
        };

    PropertyManipulator.manipulateProperties(data, manip);
    s_log.info("END new object.");
    s_log.info("");
    s_log.info("");
  }
 public SimpleTypeManipulator getManipulator(Property p) {
   SimpleTypeManipulator manip = (SimpleTypeManipulator) m_manipulators.get(p.getJavaClass());
   if (null == manip) {
     throw new IllegalArgumentException(
         "Unsupported java type: " + p.getJavaClass() + " for Property " + p.getName());
   } else {
     return manip;
   }
 }
 public void assertPropertyClass(Property p) {
   final boolean classesDiffer = !m_propertyClass.equals(p.getJavaClass());
   if (classesDiffer) {
     String msg = "Property: " + p.getName();
     msg += " has a different JavaClass! Expecting " + m_propertyClass;
     msg += " but property is " + p.getJavaClass();
     throw new IllegalArgumentException(msg);
   }
 }
 private static final Column getColumn(Property p) {
   Root root = SessionManager.getSession().getMetadataRoot().getRoot();
   ObjectMap om = root.getObjectMap(root.getObjectType(p.getContainer().getQualifiedName()));
   Mapping m = om.getMapping(Path.get(p.getName()));
   if (m instanceof Value) {
     return ((Value) m).getColumn();
   } else {
     return null;
   }
 }
예제 #7
0
  /**
   * Creates an OID with a single attribute for the key. To create a multi-valued OID, use a single
   * arg OID constructor, and add individual properties with the set method. This constructor should
   * be used when the object type being instantiated has a single primary key. For instance, if the
   * object type is <code>com.arsdigita.kernel.ACSObject</code> then the value should be the <code>
   * object_id</code>. So, if developers wanted to create the OID for ID zero, they would call
   * <code>new OID(acsObjectType, new BigDecimal(0))</code>. A <code>BigDecimal</code> is passed in
   * because the "id" attribute for the ACSObject type is declared as <code>BigDecimal</code> in the
   * PDL file.
   *
   * @param type The ObjectType of the ID
   * @param value The value of the ID
   * @exception PersistenceException will be thrown if the given object type does not have exactly a
   *     single key (if <code>type.getObjectMap().getObjectKey().getCount()
   *            != 1</code>).
   * @pre type != null
   * @pre type.getObjectMap().getObjectKey().getCount() == 1
   */
  public OID(ObjectType type, Object value) {
    this(type);
    Iterator it = type.getKeyProperties();
    if (!it.hasNext()) {
      throw new PersistenceException("Empty object key: " + type);
    }

    Property prop = (Property) it.next();

    if (it.hasNext()) {
      throw new PersistenceException("This object type has a compound key.");
    }

    String attr = prop.getName();
    set(attr, value);
  }
    /**
     * Iterates over all of the variantValues for the given Property and attempts to update the
     * property to that value. If an invalid failure to update occurs, an exception is thrown.
     *
     * @param p The property to update.
     * @param data The DataObject to save.
     * @pre p.equals(data.getObjectType.getProperty(p.getName()))
     */
    public void updateAllPropertyCombinations(Property p, DataObject data) throws Exception {
      OID id = data.getOID();
      s_log.info("Property " + p.getName() + " class is: " + p.getJavaClass());
      // Verify that nulls are handled correctly for all columns
      setProperty(p, data, null);
      data = SessionManager.getSession().retrieve(id);

      Iterator iter = getVariantValues().iterator();
      while (iter.hasNext()) {
        Object value = iter.next();
        setProperty(p, data, value);
        // It is neccessary to re-fetch the DataObject, since some 'failed' updates
        // will not cause the test to fail, but will leave the DataObject in an inconsistent state.
        // These are proper update failures, such as an oversized default String value being sent
        // to a constrained column.
        data = SessionManager.getSession().retrieve(id);
      }
    }
  private void makeChild(Property p, final DataObject parent) throws Exception {

    final String fullTypeName = p.getType().getQualifiedName();
    s_log.info(
        "Making child object: "
            + fullTypeName
            + " for ObjectType: "
            + parent.getObjectType().getQualifiedName());

    DataObject child = SessionManager.getSession().create(fullTypeName);
    reportPropertyTypes(child);

    initializeObject(child, parent);
    PropertyManipulator.AssociationManipulator manip =
        new PropertyManipulator.AssociationManipulator() {
          public boolean obeys(Property pInner) {
            final boolean isParentRef =
                super.obeys(pInner)
                    && !pInner.isCollection()
                    && pInner.getType().equals(parent.getObjectType());
            return isParentRef;
          }

          public void manipulate(Property pInner, DataObject data) throws Exception {
            s_log.info(
                "Setting parent role reference for: "
                    + fullTypeName
                    + " Property: "
                    + pInner.getName());
            data.set(pInner.getName(), parent);
          }
        };
    PropertyManipulator.manipulateProperties(child, manip);
    if (p.isCollection()) {
      DataAssociation children = (DataAssociation) parent.get(p.getName());
      children.add(child);

    } else {
      parent.set(p.getName(), child);
    }
  }
    /**
     * Logs the fact that a Property failed to update. If the column is specified, this is also
     * logged. The attemted update value is also logged.
     *
     * @param p The property that failed to update.
     * @param value The value that was used to update the property.
     */
    void logSetError(Property p, Object value) {

      s_log.debug("Failed to set property " + p.getName());
      final boolean columnIsSpecified = getColumn(p) != null;

      if (columnIsSpecified) {
        s_log.debug("Bound to column " + getColumn(p).getQualifiedName());
      } else {
        s_log.debug("Column is not specified for property");
      }
      s_log.debug("New Value is: " + value);
    }
  private void makeAssociation(Property p, DataObject data) throws Exception {

    String fullTypeName = p.getType().getQualifiedName();
    s_log.info(
        "Making associated object: "
            + fullTypeName
            + " for ObjectType: "
            + data.getObjectType().getQualifiedName());

    DataObject associatedObject = SessionManager.getSession().create(fullTypeName);
    reportPropertyTypes(associatedObject);
    initializeObject(associatedObject, data);
    associatedObject.save();
    reportPropertyTypes(associatedObject);
    s_log.info("Getting association:  " + p.getName());
    if (p.isCollection()) {
      DataAssociation assoc = (DataAssociation) data.get(p.getName());
      assoc.add(associatedObject);
    } else {
      data.set(p.getName(), associatedObject);
    }
  }
예제 #12
0
  /**
   * Adds a property to the OID. Is used as part of the key for the Object ID.
   *
   * @param propertyName Name of the property
   * @param value The property
   */
  public void set(String propertyName, Object value) {
    Property prop = m_type.getProperty(propertyName);

    // We do some type-checking here, to ensure that OIDs are being
    // created with legit types of values.
    if (prop == null) {
      throw new PersistenceException(
          "no such property: " + propertyName + " for type " + m_type.getName());
    }

    // null has no type
    // if prop isn't an attribute, not sure what to do with it.
    if (prop.isAttribute() && value != null) {
      // we can be sure this is a simpletype because
      // isAttribute was true.
      SimpleType expectedType = (SimpleType) prop.getType();
      if (!expectedType.getJavaClass().isAssignableFrom(value.getClass())) {
        throw new PersistenceException(
            "expected " + expectedType.getJavaClass() + "actual type " + value.getClass());
      }
    } else if (value != null) {
      if (value instanceof DataObject) {
        ObjectType ot = (ObjectType) prop.getType();
        DataObject dobj = (DataObject) value;
        ObjectType.verifySubtype(ot, dobj.getObjectType());
      } else {
        throw new PersistenceException(
            "expected DataObject for property " + propertyName + " but got " + value.getClass());
      }
    }

    if (hasProperty(propertyName)) {
      throw new PersistenceException(propertyName + " is already set to " + get(propertyName));
    }

    m_values.put(propertyName, value);
  }
예제 #13
0
 public static final com.redhat.persistence.metadata.Property prop(Root root, Property prop) {
   return type(root, prop.getContainer().getQualifiedName()).getProperty(prop.getName());
 }
    /**
     * Attempts to update the Property to a given value. If an invalid failure to update occurs, an
     * exception is thrown. If the update appears successful, the DataObject is re-fetched from the
     * database, and the property value compared to what it should be. If they differ, an exception
     * is thrown.
     *
     * @param p The property to update.
     * @param data The DataObject to save.
     * @param value The value to update the property with.
     * @pre p.equals(data.getObjectType.getProperty(p.getName()))
     */
    void setProperty(Property p, DataObject data, Object value) throws Exception {
      final String propName = p.getName();
      OID id = data.getOID();
      s_log.debug(
          "setting property : "
              + data.getObjectType().getQualifiedName()
              + "."
              + data.getObjectType().getName()
              + "."
              + p.getName()); // + " to value: " + value);
      s_log.debug("Old value was: " + data.get(p.getName()));
      final boolean valueIsNull =
          (value == null || value instanceof String && ((String) value).length() == 0);
      boolean savedNullInRequiredField = false;
      try {
        data.set(p.getName(), value);
        data.save();
        // This is neccessary since fail cannot be called here.
        // Its exception would be caught. Need to check this
        // value after the catch block
        savedNullInRequiredField = (p.isRequired() && valueIsNull);
      } catch (Exception t) {
        if (p.isRequired() && valueIsNull) {
          return;
        }
        if (p.isNullable() && valueIsNull) {
          String msg = "Failed to save DataObject: " + data.getObjectType().getName();
          msg += "\nTried to update nullable property " + p.getName();
          msg += " with null value and failed!";
          msg += "\nException is: " + t.getMessage();
          s_log.debug(msg);
          throw new CheckedWrapperException(msg, t);
        } else {
          s_log.debug("Failed to set property " + p.getName());
          s_log.debug("Checking error");
          checkSetError(t, p, data, value);
          //                    if (DbHelper.getDatabase(getSession().getConnection()) ==
          // DbHelper.DB_POSTGRES) {
          throw new AbortMetaTestException();
          //                    } else {
          //                        return;
          //                    }
        }
      }

      if (savedNullInRequiredField) {
        String msg = "DataObject saved null value in a required Property: " + p.getName();
        s_log.debug(msg);
        throw new Exception(msg);
      }
      DataObject fromDatabase = SessionManager.getSession().retrieve(id);
      Object newValue = fromDatabase.get(p.getName());
      checkEquals(p.getName(), value, newValue);
    }
 void setDefaultProperty(Property p, DataObject data) {
   assertPropertyClass(p);
   data.set(p.getName(), getDefaultValue());
 }
 private static boolean isNonKeyAttribute(Property p, ObjectType type) {
   return p.isAttribute() && !type.isKeyProperty(p);
 }