package edu.duke.cabig.c3pr.utils;

import edu.duke.cabig.c3pr.domain.StudySubject;
import edu.duke.cabig.c3pr.domain.SystemAssignedIdentifier;
import edu.duke.cabig.c3pr.esb.CaXchangeMessageResponseHandler;
import edu.duke.cabig.c3pr.service.StudySubjectService;
import edu.duke.cabig.c3pr.xml.XmlMarshaller;
import gov.nih.nci.caxchange.MessagePayload;
import gov.nih.nci.caxchange.Response;
import gov.nih.nci.caxchange.TargetResponseMessage;
import gov.nih.nci.common.exception.XMLUtilityException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;

import java.io.StringReader;

/**
 * Handles c3d patient position
 * <p/>
 * Created by IntelliJ IDEA.
 * User: kherm
 * Date: Nov 27, 2007
 * Time: 10:11:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class C3DPatientPositionResponseHandler implements CaXchangeMessageResponseHandler {

    public static final String C3D_SERVICE_IDENTIFIER = "C3D";

    Logger log = Logger.getLogger(C3DPatientPositionResponseHandler.class);
    private XmlMarshaller marshaller;
    private StudySubjectService subjectService;


    public void handleMessageResponse(String string, Response response) {
        log.debug("Looking for c3d identifier");

        for (TargetResponseMessage tResponse : response.getTargetResponse()) {

            if (tResponse.getTargetServiceIdentifier().indexOf(C3D_SERVICE_IDENTIFIER) > -1) {
                log.debug("Found c3d response. Processing...");
                MessagePayload payload = tResponse.getTargetBusinessMessage();
                StudySubject c3dSubject;
                try {
                    c3dSubject = (StudySubject) marshaller.fromXML(new StringReader(payload.toString()));
                    for (SystemAssignedIdentifier sId : c3dSubject.getSystemAssignedIdentifiers()) {
                        if (sId.getSystemName().toUpperCase().indexOf(C3D_SERVICE_IDENTIFIER) > -1) {
                            log.debug("Found c3d identifier.processing");
                            subjectService.assignC3DIdentifier(c3dSubject, sId.getValue());
                        }
                    }
                    ;

                } catch (XMLUtilityException e) {
                    log.error("Could not deserialize c3d response." + e.getMessage());
                }
            }

        }
    }

    public XmlMarshaller getMarshaller() {
        return marshaller;
    }

    @Required
    public void setMarshaller(XmlMarshaller marshaller) {
        this.marshaller = marshaller;
    }

    public StudySubjectService getSubjectService() {
        return subjectService;
    }

    @Required
    public void setSubjectService(StudySubjectService subjectService) {
        this.subjectService = subjectService;
    }

}

@@ -524,24 +524,71 @@
 g4j.Logger;%0A
+import org.apache.axis.message.MessageElement;%0A
 import org.s
@@ -1023,16 +1023,99 @@
 = %22C3D%22;
+%0A    public static final String REGISTRATION_MESSAGE_ELEMENT_NAME = %22registration%22;
 %0A%0A    Lo
@@ -1369,15 +1369,17 @@
 ug(%22
-Looking
+Will look
  for
@@ -1393,16 +1393,36 @@
 entifier
+ in response message
 %22);%0A%0A   
@@ -1736,16 +1736,329 @@
 sage();%0A
+%0A                MessageElement%5B%5D elems = payload.get_any();%0A                for(MessageElement elem : elems)%7B%0A                    if(REGISTRATION_MESSAGE_ELEMENT_NAME.equalsIgnoreCase(elem.getTagName()))%7B%0A%0A                        log.debug(%22Found Registration element in c3d response. Processing....%22);%0A%0A        
         
@@ -2110,14 +2110,30 @@
     
+   
+     
 try %7B%0A
+        
     
@@ -2212,23 +2212,20 @@
 gReader(
-payload
+elem
 .toStrin
@@ -2247,24 +2247,32 @@
             
+      
+  
 for (SystemA
@@ -2360,24 +2360,32 @@
             
+        
 if (sId.getS
@@ -2470,32 +2470,40 @@
                 
+     
+   
 log.debug(%22Found
@@ -2528,24 +2528,32 @@
 ocessing%22);%0A
+        
             
@@ -2648,34 +2648,50 @@
                 
+   
-%7D%0A
+     %7D%0A        
                 
@@ -2716,19 +2716,34 @@
         
-;%0A%0A
+        ;%0A%0A       
         
@@ -2750,16 +2750,17 @@
         
+ 
 %7D catch 
@@ -2781,24 +2781,32 @@
 eption e) %7B%0A
+        
             
@@ -2876,24 +2876,72 @@
 Message());%0A
+                        %7D%0A                    %7D%0A
             
@@ -2938,32 +2938,34 @@
               %7D%0A
+%0A%0A
             %7D%0A%0A 
@@ -2958,25 +2958,24 @@
           %7D%0A
-%0A
         %7D%0A  

fix for ccts