@Override public boolean hasChanged(Field field) { final Map<Integer, Field> savedFields = new HashMap<Integer, Field>(); if (field != null) for (Field subfield : field.getFields()) savedFields.put(subfield.getId(), subfield); for (StructureHolder holder : oneToMany.getSelected()) { // Field has never been saved, if there are changes, let's save. if (holder.getField() == null || holder.getField().getId() == 0) { if (holder.hasChanged()) { Debug.println("NEW: {0} says it has changed", holder.getStructure().getClass().getName()); return true; } else return false; } else { // We are working with previously saved data that could have changed. Field dataField = savedFields.remove(holder.getField().getId()); if (dataField == null) { // How?? Debug.println("OneToMany hasChanged badness, save to clean up."); return true; } if (holder.getStructure().hasChanged(dataField)) { Debug.println("OLD: {0} says it has changed", holder.getStructure().getClass().getName()); return true; } } } return !savedFields.isEmpty(); }
@Override public void save(Field parent, Field field) { if (field == null) { field = new Field(); field.setName(getId()); if (parent != null) { field.setParent(parent); parent.getFields().add(field); } } final List<StructureHolder> unsaved = new ArrayList<StructureHolder>(); final List<Integer> saved = new ArrayList<Integer>(); saved.add(0); for (StructureHolder cur : oneToMany.getSelected()) { if (cur.getField() == null) unsaved.add(cur); else { cur.getStructure().save(field, cur.getField()); saved.add(cur.getField().getId()); } } for (StructureHolder cur : unsaved) { Field subfield = new Field(field.getName() + "Subfield", null); subfield.setParent(field); cur.getStructure().save(field, subfield); field.getFields().add(subfield); cur.setField(subfield); } final HashSet<Field> savedSet = new HashSet<Field>(); for (Field subfield : field.getFields()) if (saved.contains(subfield.getId())) savedSet.add(subfield); field.setFields(savedSet); }