/**
  *
  * @param string $message  
  * @return array          
  */
 public function blockingCall($message)
 {
     $platformMessage = new Message(false, $message);
     $action = $platformMessage->getNextAction();
     if (count($action)) {
         if (isset($action[0]->headers->handler)) {
             $handler = (string) $action[0]->headers->handler;
         }
         $method = (string) $action[0]->headers->method;
         if (!$this->_serviceLocator->has($handler)) {
             throw new \Exception('Service ' . $handler . ' not found');
         }
         // set the default db
         if (isset($action[0]->data->auth->inst)) {
             $dm = $this->_serviceLocator->get('doctrine.documentmanager.odm_default');
             $dm->getConfiguration()->setDefaultDB('MoveIn4-Client-' . (string) $action[0]->data->auth->inst);
         } else {
             throw new \Exception('Instance ID not in message');
         }
         // get the worker service
         $worker = $this->_serviceLocator->get($handler);
         // send the data to the worker
         $res = $worker->{$method}($action[0]->data);
     }
     return $res;
 }
 /**
  * Creates an new ActivityIncomingEmailDocument for each new email imported
  * @param  array $data data to be manipulated
  * @return array
  */
 public function importIncomingEmail($data)
 {
     // for each email, create an activity
     $result = array();
     $router = $this->serviceManager->get('platform-router');
     $matchedPersons = array();
     foreach ($data->rows as $email) {
         //initilize the array of new activities
         $activityId = array();
         $dateRecieved = new \DateTime((string) $email->email_date);
         $body = $router->getFileResource((int) $email->email_body['fileRef']);
         $bodyPlain = $router->getFileResource((int) $email->email_body_plain['fileRef']);
         //get email data
         $emailData = array('emailImportId' => (int) $email->email_id, 'subject' => (string) $email->email_subject, 'messageHtml' => iconv(mb_detect_encoding((string) $body['file'], mb_detect_order(), true), "UTF-8//IGNORE", (string) $body['file']), 'message' => iconv(mb_detect_encoding((string) $bodyPlain['file'], mb_detect_order(), true), "UTF-8//IGNORE", (string) $bodyPlain['file']), 'fromEmailString' => (string) $email->email_from, 'dateRecieved' => $dateRecieved->format('d/m/Y h:i:A'), 'emailConfig' => (string) $data->emailConfigId);
         //get the attachments
         $attachmentArray = array();
         foreach ($email->email_attachment as $att) {
             $attachmentArray['file' . (string) $att->id] = array('id' => (string) $att->id, 'fileName' => (string) $att->filename, 'file' => $att->file);
         }
         //check to see if the person exists in the system
         //get the email address
         $emailAddress = explode('<', (string) $email->email_from);
         $emailAddress = rtrim($emailAddress[count($emailAddress) - 1], '>');
         if (!isset($matchedPersons[$emailAddress])) {
             //query for persons
             $query = $this->_dm->createQueryBuilder('MoveIn4Person\\Document\\PersonDocument')->field('active')->equals(true)->field('deleted')->equals(false)->field('email.email')->equals($emailAddress);
             $personResult = $query->getQuery()->execute();
             foreach ($personResult as $person) {
                 $matchedPersons[$emailAddress][] = $person->getId();
             }
         }
         //if matching person is found
         if (isset($matchedPersons[$emailAddress])) {
             foreach ($matchedPersons[$emailAddress] as $person) {
                 $message = new Message(array('env' => (string) $data->auth->env, 'inst' => (string) $data->auth->inst, 'userId' => (string) $data->auth->userId));
                 $emailData['fromPerson'] = $person;
                 $message->addAction('null', 'null', $emailData);
                 $action = $message->getNextAction();
                 $activityId[] = parent::edit($action[0]->data);
             }
             //if no matching person found
         } else {
             $message = new Message(array('env' => (string) $data->auth->env, 'inst' => (string) $data->auth->inst, 'userId' => (string) $data->auth->userId));
             $message->addAction('null', 'null', $emailData);
             $action = $message->getNextAction();
             $activityId[] = parent::edit($action[0]->data);
         }
         $result[] = $activityId;
         $auth = array('env' => (string) $data->auth->env, 'inst' => (string) $data->auth->inst, 'userId' => (string) $data->auth->userId);
         if (count($attachmentArray)) {
             //foreach attachment save in file storage and add to activity
             foreach ($attachmentArray as $attachment) {
                 $message = new Message($auth);
                 //save file
                 $mergeFileData = array('id' => null, 'file_attr' => array('fileRef' => (string) $attachment['file']["fileRef"]), 'options' => array('filename' => $attachment['fileName'], 'pathPrepend' => array($auth['env'], $auth['inst'])));
                 $fileStorage = $message->addAction('filestorage', 'saveFile', $mergeFileData, 'FileStorage');
                 //update activity
                 foreach ($activityId as $aId) {
                     $updateData['id'] = $aId['id'];
                     $updateData['attachment'] = array('fileName' => $attachment['fileName'], 'fileStorageId_attr' => array('xpath' => '//response/action[@id="' . $fileStorage . '"]/data/docId'));
                     $editId = $message->addAction('movein', 'editIncommingEmail', $updateData, 'MoveIn4Activity\\Model\\ActivityIncomingEmail');
                 }
                 $router->queue($message->asXml());
             }
         }
     }
     return array('rows' => $result);
 }