package edu.northwestern.bioinformatics.studycalendar.xml.writers;

import edu.northwestern.bioinformatics.studycalendar.domain.delta.Add;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Change;
import edu.northwestern.bioinformatics.studycalendar.domain.Study;
import edu.northwestern.bioinformatics.studycalendar.domain.PlanTreeNode;
import org.dom4j.Element;

public class AddXmlSerializer extends AbstractChangeXmlSerializer {
    private static final String ADD = "add";
    private static final String INDEX = "index";

    public AddXmlSerializer(Study study) {
        super(study);
    }

    protected Change changeInstance() {
        return new Add();
    }

    protected String elementName() {
        return ADD;
    }

    protected void addAdditionalAttributes(final Change change, Element element) {
        element.addAttribute(INDEX, ((Add)change).getIndex().toString());
        PlanTreeNode<?> planTreeNode = ((Add)change).getChild();
        AbstractPlanTreeNodeXmlSerializer serializer = getPlanTreeNodeSerializerFactory().createPlanTreeNodeXmlSerializer(planTreeNode);
        Element ePlanTreeNode = serializer.createElement(planTreeNode);
        element.add(ePlanTreeNode);
    }

    protected void setAdditionalProperties(final Element element, Change change) {
        ((Add)change).setIndex(new Integer(element.attributeValue(INDEX)));
        // TODO: Call PlanTreeNodeXmlSerializer
    }
}

@@ -374,16 +374,40 @@
 ement;%0A%0A
+import java.util.List;%0A%0A
 public c
@@ -1317,22 +1317,19 @@
  Change 
-change
+add
 ) %7B%0A    
@@ -1338,22 +1338,19 @@
   ((Add)
-change
+add
 ).setInd
@@ -1409,47 +1409,372 @@
     
-// TODO: Call PlanTreeNodeXmlSerializer
+List%3CElement%3E ePlanTreeNodes = element.elements();%0A        Element ePlanTreeNode = ePlanTreeNodes.get(0);%0A        AbstractPlanTreeNodeXmlSerializer serializer = getPlanTreeNodeSerializerFactory().createPlanTreeNodeXmlSerializer(ePlanTreeNode);%0A        PlanTreeNode%3C?%3E planTreeNode = serializer.readElement(ePlanTreeNode);%0A        ((Add)add).setChild(planTreeNode);
 %0A   

Add change will now de-serialize a PlanTreeNode children when readElement is called.