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

import edu.northwestern.bioinformatics.studycalendar.xml.AbstractStudyCalendarXmlSerializer;
import edu.northwestern.bioinformatics.studycalendar.xml.XsdAttribute;
import edu.northwestern.bioinformatics.studycalendar.xml.XsdElement;
import edu.northwestern.bioinformatics.studycalendar.domain.Activity;
import edu.northwestern.bioinformatics.studycalendar.domain.ActivityType;
import edu.northwestern.bioinformatics.studycalendar.domain.Source;
import edu.northwestern.bioinformatics.studycalendar.StudyCalendarValidationException;
import edu.northwestern.bioinformatics.studycalendar.dao.SourceDao;
import edu.northwestern.bioinformatics.studycalendar.dao.ActivityDao;
import org.dom4j.Element;

/**
 * @author Rhett Sutphin
 */
public class ActivityXmlSerializer extends AbstractStudyCalendarXmlSerializer<Activity> {
    private boolean embeddedInSource;
    private ActivityDao activityDao;
    private SourceDao sourceDao;

    public ActivityXmlSerializer() {
        this(false);
    }

    public ActivityXmlSerializer(boolean embeddedInSource) {
        this.embeddedInSource = embeddedInSource;
    }

    @Override
    public Element createElement(Activity a) {
        Element aElt = XsdElement.ACTIVITY.create();
        XsdAttribute.ACTIVITY_NAME.addTo(aElt, a.getName());
        XsdAttribute.ACTIVITY_CODE.addTo(aElt, a.getCode());
        XsdAttribute.ACTIVITY_DESC.addTo(aElt, a.getDescription());
        XsdAttribute.ACTIVITY_TYPE.addTo(aElt, a.getType().getId());
        if (!embeddedInSource && a.getSource() != null) {
            XsdAttribute.ACTIVITY_SOURCE.addTo(aElt, a.getSource().getNaturalKey());
        }
        return aElt;
    }

    @Override
    public Activity readElement(Element element) {
        Activity activity = new Activity();
        activity.setName(XsdAttribute.ACTIVITY_NAME.from(element));
        activity.setCode(XsdAttribute.ACTIVITY_CODE.from(element));
        activity.setDescription(XsdAttribute.ACTIVITY_DESC.from(element));
        try {
            String typeAttr = XsdAttribute.ACTIVITY_TYPE.from(element);
            if (typeAttr == null) {
                throw new StudyCalendarValidationException("Type is required for activities");
            }
            int typeId = Integer.parseInt(typeAttr);
            ActivityType type = ActivityType.getById(typeId);
            if (typeAttr == null) {
                throw new StudyCalendarValidationException("Type id %d is not valid", typeId);
            }
            activity.setType(type);
        } catch (NumberFormatException nfe) {
            throw new StudyCalendarValidationException("Type attribute must be an integer", nfe);
        }
        if (!embeddedInSource) {
            Source sourceTemplate = new Source();
            sourceTemplate.setName(XsdAttribute.ACTIVITY_SOURCE.from(element));
            activity.setSource(sourceTemplate);
        }

        return activity;
    }

    public void setActivityDao(ActivityDao activityDao) {
        this.activityDao = activityDao;
    }

    public void setSourceDao(SourceDao sourceDao) {
        this.sourceDao = sourceDao;
    }
}

@@ -118,20 +118,8 @@
 dar.
-xml.Abstract
 Stud
@@ -131,161 +131,27 @@
 ndar
-XmlSerializer;%0Aimport edu.northwestern.bioinformatics.studycalendar.xml.XsdAttribute;%0Aimport edu.northwestern.bioinformatics.studycalendar.xml.XsdElement
+ValidationException
 ;%0Aim
@@ -405,32 +405,44 @@
 s.studycalendar.
+xml.Abstract
 StudyCalendarVal
@@ -438,35 +438,29 @@
 Calendar
-ValidationException
+XmlSerializer
 ;%0Aimport
@@ -510,21 +510,24 @@
 dar.
-dao.SourceDao
+xml.XsdAttribute
 ;%0Aim
@@ -581,23 +581,22 @@
 dar.
-dao.ActivityDao
+xml.XsdElement
 ;%0Aim
@@ -743,16 +743,16 @@
 vity%3E %7B%0A
+
     priv
@@ -784,78 +784,8 @@
 rce;
-%0A    private ActivityDao activityDao;%0A    private SourceDao sourceDao;
 %0A%0A  
@@ -2776,207 +2776,7 @@
    %7D
-%0A%0A    public void setActivityDao(ActivityDao activityDao) %7B%0A        this.activityDao = activityDao;%0A    %7D%0A%0A    public void setSourceDao(SourceDao sourceDao) %7B%0A        this.sourceDao = sourceDao;%0A    %7D
 %0A%7D%0A

Remove unused injected dependencies