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

import edu.northwestern.bioinformatics.studycalendar.domain.StudySubjectAssignment;
import edu.northwestern.bioinformatics.studycalendar.domain.Subject;
import edu.northwestern.bioinformatics.studycalendar.xml.AbstractStudyCalendarXmlCollectionSerializer;
import edu.northwestern.bioinformatics.studycalendar.xml.AbstractStudyCalendarXmlSerializer;
import static edu.northwestern.bioinformatics.studycalendar.xml.XsdAttribute.*;
import edu.northwestern.bioinformatics.studycalendar.xml.XsdElement;
import static edu.northwestern.bioinformatics.studycalendar.xml.XsdElement.SUBJECT_ASSIGNMENT;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Required;

/**
 * @author John Dzak
 */
public class StudySubjectAssignmentXmlSerializer extends AbstractStudyCalendarXmlCollectionSerializer<StudySubjectAssignment> {
    private AbstractStudyCalendarXmlSerializer<Subject> subjectXmlSerializer;

    protected XsdElement rootElement() { return XsdElement.SUBJECT_ASSIGNMENT; }
    protected XsdElement collectionRootElement() { return XsdElement.SUBJECT_ASSIGNMENTS; }

    @Override
    public Element createElement(StudySubjectAssignment assignment, boolean inCollection) {
        Element elt = SUBJECT_ASSIGNMENT.create();
        ASSIGNMENT_STUDY_NAME.addTo(elt,  assignment.getStudySite().getStudy().getName());
        ASSIGNMENT_SITE_NAME.addTo(elt,  assignment.getStudySite().getSite().getName());
        ASSIGNMENT_START_DATE.addTo(elt,  assignment.getStartDateEpoch());
        ASSIGNMENT_END_DATE.addTo(elt,  assignment.getEndDateEpoch());
        ASSIGNMENT_SUBJECT_COORD.addTo(elt,  assignment.getSubjectCoordinator().getName());
        ASSIGNMENT_CURRENT_AMENDMENT.addTo(elt, assignment.getCurrentAmendment().getNaturalKey());
        ASSIGNMENT_ID.addTo(elt, assignment.getGridId());

        elt.add(subjectXmlSerializer.createElement(assignment.getSubject()));

        return elt;
    }

    @Override
    public StudySubjectAssignment readElement(Element elt) {
        throw new UnsupportedOperationException("Reading Study Assignment elements not allowed");
    }

    ////// Bean Setters
    @Required
    public void setSubjectXmlSerializer(AbstractStudyCalendarXmlSerializer<Subject> subjectXmlSerializer) {
        this.subjectXmlSerializer = subjectXmlSerializer;
    }
}
@@ -978,17 +978,135 @@
 alizer;%0A
+    private ScheduledCalendarXmlSerializer scheduledCalendarXmlSerializer;%0A    private Boolean subjectCentric = false;
 %0A
-
     prot
@@ -1999,25 +1999,59 @@
 tGridId());%0A
-%0A
+        if (!subjectCentric) %7B%0A    
         elt.
@@ -2116,16 +2116,167 @@
 ct()));%0A
+        %7D%0A        if (subjectCentric) %7B%0A            elt.add(scheduledCalendarXmlSerializer.createElement(assignment.getScheduledCalendar()));%0A        %7D
 %0A       
@@ -2617,24 +2617,24 @@
 rializer) %7B%0A
-
         this
@@ -2684,10 +2684,323 @@
 r;%0A    %7D
+%0A%0A    public void setSubjectCentric(Boolean subjectCentric) %7B%0A        this.subjectCentric = subjectCentric;%0A    %7D%0A%0A    public void setScheduledCalendarXmlSerializer(ScheduledCalendarXmlSerializer scheduledCalendarXmlSerializer) %7B%0A        this.scheduledCalendarXmlSerializer = scheduledCalendarXmlSerializer;%0A    %7D
 %0A%7D

Add scheduled calendar element for the subject centric schedule. Related to #648