package gov.nih.nci.cabig.caaers.service.migrator;

import gov.nih.nci.cabig.caaers.CaaersSystemException;
import gov.nih.nci.cabig.caaers.dao.StudyDao;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.service.DomainObjectImportOutcome;
import gov.nih.nci.cabig.caaers.service.StudyImportServiceImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class is intended save/update or delete studies.
 * 
 * @author Monish Dombla
 *
 */
public class StudyProcessor{
	
	private static Log logger = LogFactory.getLog(StudyProcessor.class);
	
	//Injected through spring
	private StudyImportServiceImpl studyImportService;
	private StudyDao studyDao;
	private StudyConverter studyConverter;
	
	public void setStudyImportService(final StudyImportServiceImpl studyImportService) {
        this.studyImportService = studyImportService;
    }
	
	public void setStudyDao(StudyDao studyDao){
		this.studyDao = studyDao;
	}
	
	public void setStudyConverter(StudyConverter studyConverter){
		this.studyConverter = studyConverter;
	}
	
	public DomainObjectImportOutcome<Study> createStudy(gov.nih.nci.cabig.caaers.webservice.Study studyDto) {
		logger.info("Entering createStudy() in StudyProcessor");
		
		DomainObjectImportOutcome<Study> studyImportOutcome = null;
		Study study = new Study();
		
		//Convert JAXB StudyType to Domain Study
		try{
			studyConverter.convertStudyDtoToStudyDomain(studyDto, study);
			logger.info("StudyDto converted to Study");
		}catch(CaaersSystemException caEX){
			studyImportOutcome = new DomainObjectImportOutcome<Study>();
			logger.error("StudyDto to StudyDomain Conversion Failed " , caEX);
			studyImportOutcome.addErrorMessage("StudyDto to StudyDomain Conversion Failed " , DomainObjectImportOutcome.Severity.ERROR);
		}
		
		if(studyImportOutcome == null){
			studyImportOutcome = studyImportService.importStudy(study);
			if(studyImportOutcome.isSavable()){
				studyDao.save(studyImportOutcome.getImportedDomainObject());
			}
		}else{
			//TODO parse studyImportOutcome for errormessages and 
			//propogate appropriate error message to the caller 
		}
		
		logger.info("Leaving createStudy() in StudyProcessor");
		return studyImportOutcome;
	}
	
	public DomainObjectImportOutcome<Study> updateStudy(Study study){
		logger.info("Entering updateStudy() in StudyProcessor");
		// TODO
		//Call needs to be made Study Object Graph walker to get the id's
		//Sync with 
		logger.info("Leaving updateStudy() in StudyProcessor");
		return null;
	}
}

@@ -146,16 +146,67 @@
 udyDao;%0A
+import gov.nih.nci.cabig.caaers.domain.Identifier;%0A
 import g
@@ -243,16 +243,16 @@
 .Study;%0A
-
 import g
@@ -373,16 +373,88 @@
 iceImpl;
+%0Aimport gov.nih.nci.cabig.caaers.service.synchronizer.StudySynchronizer;
 %0A%0Aimport
@@ -880,16 +880,62 @@
 verter;%0A
+%09private StudySynchronizer studySynchronizer;%0A
 %09%0A%09publi
@@ -1258,24 +1258,231 @@
 erter;%0A%09%7D%0A%09%0A
+%09public StudySynchronizer getStudySynchronizer() %7B%0A%09%09return studySynchronizer;%0A%09%7D%0A%0A%09public void setStudySynchronizer(StudySynchronizer studySynchronizer) %7B%0A%09%09this.studySynchronizer = studySynchronizer;%0A%09%7D%0A%09%0A
 %09public Doma
@@ -1510,22 +1510,20 @@
 %3CStudy%3E 
-create
+sync
 Study(go
@@ -2335,16 +2335,318 @@
 ble())%7B%0A
+%09%09%09%09//Check if Study Exists%0A%09%09%09%09Study dbStudy = fetchStudy(studyImportOutcome.getImportedDomainObject());%0A%09%09%09%09if(dbStudy != null)%7B%0A%09%09%09%09%09studySynchronizer.migrate(dbStudy, studyImportOutcome.getImportedDomainObject(), studyImportOutcome);%0A%09%09%09%09%09studyImportOutcome.setImportedDomainObject(dbStudy);%0A%09%09%09%09%7D%0A
 %09%09%09%09stud
@@ -3221,13 +3221,469 @@
 n null;%0A
-
 %09%7D%0A
+%09%0A%09/**%0A%09 * This method fetches a Study from the DB based identifiers.%0A%09 * @param importedStudy%0A%09 * @return%0A%09 */%0A%09private Study fetchStudy(Study importedStudy)%7B%0A%09%09Study dbStudy = null;%0A%09%09for (Identifier identifier : importedStudy.getIdentifiers()) %7B%0A            dbStudy = studyDao.getStudyDesignByIdentifier(identifier);%0A            studyDao.evict(dbStudy);%0A            if(dbStudy != null)%7B%0A            %09break;%0A            %7D%0A        %7D%0A%09%09return dbStudy;%0A%09%7D%0A%0A
 %7D%0A

Add Study Update Flow