public AssociationList setAssociations(String associations, AssociationFormat associationFormat) throws FieldParseException {
AssociationListFormat format = AssociationListFormat.getInstance(associationFormat);
AssociationList result = (AssociationList)format.parseObject(associations,new ParsePosition(0));
if (result == null) {
System.out.println(associationFormat.getParameters().getError());
throw new FieldParseException(associationFormat.getParameters().getError());
}
LinkedList oldList = list; // (LinkedList) list.clone(); // make a copy of original list since we'll be modifying real list
LinkedList newList = result.list;
// validate each element in new list
try {
result.testValid(true);
} catch (InvalidAssociationException e) {
// newList = oldList;
System.out.println(e.getMessage());
throw new FieldParseException(e.getMessage());
}
Association association;
Iterator i;
// check for duplicates
for (i = newList.iterator(); i.hasNext();) {
association = (Association)i.next();
// if duplicate
if (AssociationList.findAssociation(newList,association.getLeft(),association.getRight(),association) != null) {
// newList = oldList;
throw new FieldParseException("Duplicate association"); //TODO better message
}
}
// At this point, the newList is valid, so now merge