/**
  * Handle a C-Data message, and return the answer
  *
  * @param CDicomPDV      $find_rq_pdv    The PDV who contains the C-Find-RQ message
  * @param CDicomPDV      $find_data_pdv  The PDV who contains the C-Find-Data message
  * @param CExchangeDicom $dicom_exchange The Exchange Dicom
  *
  * @todo Le code est spécifique à la Source, il faudrait le rendre générique
  *
  * @return array
  */
 protected static function handleCDatas($find_rq_pdv, $find_data_pdv, $dicom_exchange)
 {
     $msg_rq = $find_rq_pdv->getMessage();
     $msg_data = $find_data_pdv->getMessage();
     /** If the message is a request : **/
     if (is_null($dicom_exchange->_responses)) {
         $requested_datas = $msg_data->getRequestedDatas();
         $sender = $dicom_exchange->loadRefSender();
         if (!$sender->_id) {
             return array("event" => "AAbort_Prepared", "datas" => 5);
         }
         /* Loading the objects linked to the Dicom sender */
         $linked_objects = CObjectToInteropSender::loadAllObjectsFor($sender->_id);
         $find_rsp_pending_datas = array("PDVs" => array(array("pres_context_id" => $find_data_pdv->getPresContextId(), "message_control_header" => 0x3, "message" => array("type" => 0x8020, "datas" => array("affected_sop_class" => $msg_rq->getAffectedSopClass()->getValue(), "command_field" => 0x8020, "message_id_request" => $msg_rq->getMessageId()->getValue(), "command_data_set" => 0xfefe, "status" => 0xff00)))));
         /* Encoding the PDU that separate the patients data */
         $find_rsp_pending = CDicomPDUFactory::encodePDU(0x4, $find_rsp_pending_datas, $dicom_exchange->_presentation_contexts);
         $calling_ae_title = self::getRequestedAETitle($requested_datas);
         $modality = self::getRequestedModality($requested_datas, $dicom_exchange);
         /** Getting the requested encoding **/
         $encoding = null;
         $rq_datasets = $find_data_pdv->getMessage()->getDatasets();
         if (isset($rq_datasets[0x8][0x5])) {
             $encoding_dataset = $rq_datasets[0x8][0x5];
             $encoding = $encoding_dataset->getValue();
         }
         $responses = array();
         /* Loading the objects (CSejour or COperation) from the linked objects */
         $objects = self::loadObjectsFromLinkedObjects($linked_objects);
         if ($objects) {
             foreach ($objects as $_object) {
                 $responses[] = $find_rsp_pending;
                 $find_rsp_datas = array("PDVs" => array(array("pres_context_id" => $find_data_pdv->getPresContextId(), "message_control_header" => 0x2, "message" => array("type" => "data", "datas" => self::getDataFromObject($_object, $encoding, $modality, $calling_ae_title, $dicom_exchange->getConfigs())))));
                 $responses[] = CDicomPDUFactory::encodePDU(0x4, $find_rsp_datas, $dicom_exchange->_presentation_contexts);
             }
         }
         $find_rsp_success_datas = array("PDVs" => array(array("pres_context_id" => $find_data_pdv->getPresContextId(), "message_control_header" => 0x3, "message" => array("type" => 0x8020, "datas" => array("affected_sop_class" => $msg_rq->getAffectedSopClass()->getValue(), "command_field" => 0x8020, "message_id_request" => $msg_rq->getMessageId()->getValue(), "command_data_set" => 0x101, "status" => 0x0)))));
         $responses[] = CDicomPDUFactory::encodePDU(0x4, $find_rsp_success_datas, $dicom_exchange->_presentation_contexts);
         if (!$dicom_exchange->_responses) {
             $dicom_exchange->_responses = $responses;
         } else {
             array_merge($dicom_exchange->_responses, $responses);
         }
         return array("event" => "PDataTF_Prepared", "datas" => $responses);
     } else {
         /** The message is a response : **/
         return array("event" => "AAbort_Prepared", "datas" => 5);
     }
 }