public Classification newClassification(Classification original) { if (!isReadOnly()) { throw new IllegalStateException( "You can only create Classifications from a persistant Version of DynamicType"); } final ClassificationImpl newClassification = (ClassificationImpl) newClassification(true); { Attribute[] attributes = original.getAttributes(); for (int i = 0; i < attributes.length; i++) { Attribute originalAttribute = attributes[i]; String attributeKey = originalAttribute.getKey(); Attribute newAttribute = newClassification.getAttribute(attributeKey); Object defaultValue = originalAttribute.defaultValue(); Object originalValue = original.getValue(attributeKey); if (newAttribute != null && newAttribute.getType().equals(originalAttribute.getType())) { Object newDefaultValue = newAttribute.defaultValue(); // If the default value of the new type differs from the old one and the value is the same // as the old default then use the new default if (newDefaultValue != null && ((defaultValue == null && originalValue == null) || (defaultValue != null && originalValue != null && !newDefaultValue.equals(defaultValue) && (originalValue.equals(defaultValue))))) { newClassification.setValue(newAttribute, newDefaultValue); } else { newClassification.setValue(newAttribute, newAttribute.convertValue(originalValue)); } } } return newClassification; } }
/** * @param newType * @param attributeId */ public boolean hasAttributeChanged(DynamicTypeImpl newType, String attributeId) { Attribute oldAttribute = findAttributeForId(attributeId); Attribute newAttribute = newType.findAttributeForId(attributeId); if (oldAttribute == null && newAttribute == null) { return false; } if ((newAttribute == null) || (oldAttribute == null)) { return true; } String newKey = newAttribute.getKey(); String oldKey = oldAttribute.getKey(); if (!newKey.equals(oldKey)) { return true; } if (!newAttribute.getType().equals(oldAttribute.getType())) { return true; } { String[] keys = newAttribute.getConstraintKeys(); String[] oldKeys = oldAttribute.getConstraintKeys(); if (keys.length != oldKeys.length) { return true; } for (int i = 0; i < keys.length; i++) { if (!keys[i].equals(oldKeys[i])) return true; Object oldConstr = oldAttribute.getConstraint(keys[i]); Object newConstr = newAttribute.getConstraint(keys[i]); if (oldConstr == null && newConstr == null) continue; if (oldConstr == null || newConstr == null) return true; if (!oldConstr.equals(newConstr)) return true; } } return false; }
/** * returns the string representation of the given value. if attribute is a reference then the id * of the referenced object is returned. */ private String toStringValue(Attribute attribute, Object value) { String stringValue = null; Class<? extends Entity> refType = attribute.getRefType(); AttributeType attributeType = attribute.getType(); if (refType != null) { if (value instanceof Entity && ((Entity) value).getTypeClass() == refType) { stringValue = ((Entity) value).getId(); } else { throw new IllegalArgumentException( "entity expected. but id used please use addRefValue instead of addValue in reading"); } } else if (attributeType.equals(AttributeType.DATE)) { return new SerializableDateTimeFormat().formatDate((Date) value); } else if (value != null) { stringValue = value.toString(); } return stringValue; }