if(representation == null){
throw new IllegalArgumentException("The parsed Representation MUST NOT be NULL!");
}
//parse only the id of the representation, because we need the current
//stored version of the entity!
Entity entity = loadEntity(representation.getId());
//now we need to check if the parsed representation is the data or the
//metadata of the Entity
ManagedEntity updated;
if(entity == null || representation.getId().equals(entity.getRepresentation().getId())){
//update the data or create a new entity
updated = ManagedEntity.init(new EntityImpl(config.getID(), representation,
entity != null ? entity.getMetadata() : null),
config.getDefaultManagedEntityState());
if(entity == null){ //add creation date
updated.setCreated(new Date());
}
} else {
//update the metadata
entity = new EntityImpl(config.getID(), entity.getRepresentation(),
representation);
//we need to validate the metadata
updated = ManagedEntity.init(
entity, config.getDefaultManagedEntityState());
}