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; } }
public void commitChange(DynamicType type) { if (!hasType(type)) { return; } Collection<String> removedKeys = new ArrayList<String>(); Map<Attribute, Attribute> attributeMapping = new HashMap<Attribute, Attribute>(); for (String key : data.keySet()) { Attribute attribute = getType().getAttribute(key); Attribute attribute2 = type.getAttribute(key); // key now longer availabe so remove it if (attribute2 == null) { removedKeys.add(key); } if (attribute == null) { continue; } String attId = attribute.getId(); Attribute newAtt = findAttributeById(type, attId); if (newAtt != null) { attributeMapping.put(attribute, newAtt); } } for (Attribute attribute : attributeMapping.keySet()) { Collection<Object> convertedValues = new ArrayList<Object>(); Collection<?> valueCollection = getValues(attribute); Attribute newAttribute = attributeMapping.get(attribute); for (Object oldValue : valueCollection) { Object newValue = newAttribute.convertValue(oldValue); if (newValue != null) { convertedValues.add(newValue); } } setValues(newAttribute, convertedValues); } for (String key : removedKeys) { data.remove(key); } this.type = type.getKey(); name = null; }