package cz.abclinuxu.datoveschranky.impl;

import cz.abclinuxu.datoveschranky.common.entities.Attachment;
import cz.abclinuxu.datoveschranky.common.entities.Message;
import cz.abclinuxu.datoveschranky.common.entities.MessageType;
import cz.abclinuxu.datoveschranky.common.entities.Validator;
import cz.abclinuxu.datoveschranky.common.impl.DataBoxException;
import cz.abclinuxu.datoveschranky.common.impl.Utils;
import cz.abclinuxu.datoveschranky.common.interfaces.DataBoxUploadService;
import cz.abclinuxu.datoveschranky.ws.dm.DmOperationsPortType;
import cz.abclinuxu.datoveschranky.ws.dm.TFilesArray;
import cz.abclinuxu.datoveschranky.ws.dm.TFilesArray.DmFile;
import cz.abclinuxu.datoveschranky.ws.dm.TMessageEnvelopeSub;
import cz.abclinuxu.datoveschranky.ws.dm.TStatus;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.xml.ws.Holder;

/**
 *
 * @author xrosecky
 */
public class DataBoxUploadServiceImpl implements DataBoxUploadService {

    protected DmOperationsPortType dmOp;

    public DataBoxUploadServiceImpl(DmOperationsPortType dmOp) {
        this.dmOp = dmOp;
    }

    public void sendMessage(Message message) {
        Validator.assertValidMessageForSending(message);
        TMessageEnvelopeSub envelope = new TMessageEnvelopeSub();
        envelope.setDbIDRecipient(message.getEnvelope().getRecipient().getdataBoxID());
        envelope.setDmAnnotation(message.getEnvelope().getAnnotation());
	envelope.setDmRecipientIdent(message.getEnvelope().getRecipientIdent().getIdent());
	envelope.setDmRecipientRefNumber(message.getEnvelope().getRecipientIdent().getRefNumber());
	envelope.setDmSenderIdent(message.getEnvelope().getSenderIdent().getIdent());
	envelope.setDmSenderRefNumber(message.getEnvelope().getSenderIdent().getRefNumber());
        TFilesArray files = new TFilesArray();
        for (Attachment attachment : message.getAttachments()) {
            DmFile file = new DmFile();
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                Utils.copy(attachment.getContent().getInputStream(), out);
                file.setDmEncodedContent(out.toByteArray());
            } catch (IOException ioe) {
                throw new DataBoxException("IO chyba pri cteni prilohy.", ioe);
            }
            file.setDmMimeType(attachment.getMimeType());
            file.setDmFileMetaType(attachment.getMetaType());
            file.setDmFileDescr(attachment.getDescription());
            files.getDmFile().add(file);
        }
        Holder<String> messageID = new Holder<String>();
        Holder<TStatus> status = new Holder<TStatus>();
        dmOp.createMessage(envelope, files, messageID, status);
        ErrorHandling.throwIfError("Poslani zpravy se nezdarilo", status.value);
        message.getEnvelope().setType(MessageType.SENT);
        message.getEnvelope().setMessageID(messageID.value);
    }
}

@@ -1435,25 +1435,94 @@
 otation());%0A
-%09
+        if (message.getEnvelope().getRecipientIdent() != null) %7B%0A%09    
 envelope.set
@@ -1586,32 +1586,36 @@
 ).getIdent());%0A%09
+    
 envelope.setDmRe
@@ -1687,24 +1687,93 @@
 Number());%0A%09
+%7D%0A        if (message.getEnvelope().getSenderIdent() != null) %7B%0A%09    
 envelope.set
@@ -1839,16 +1839,20 @@
 nt());%0A%09
+    
 envelope
@@ -1925,24 +1925,34 @@
 fNumber());%0A
+        %7D%0A
         TFil

getRecipientIdent() and getSenderIdent() in Envelope can be null