package org.mitallast.queue.action.queue.enqueue;

import com.google.inject.Inject;
import org.mitallast.queue.action.AbstractAction;
import org.mitallast.queue.action.ActionListener;
import org.mitallast.queue.action.ActionRequestValidationException;
import org.mitallast.queue.common.settings.Settings;
import org.mitallast.queue.queue.QueueTypeMismatch;
import org.mitallast.queue.queue.service.QueueService;
import org.mitallast.queue.queues.QueueMissingException;
import org.mitallast.queue.queues.QueuesService;

public class EnQueueAction extends AbstractAction<EnQueueRequest, EnQueueResponse> {

    private final QueuesService queuesService;

    @Inject
    public EnQueueAction(Settings settings, QueuesService queuesService) {
        super(settings);
        this.queuesService = queuesService;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void execute(EnQueueRequest request, ActionListener<EnQueueResponse> listener) {
        ActionRequestValidationException validationException = request.validate();
        if (validationException != null) {
            listener.onFailure(validationException);
            return;
        }
        if (!queuesService.hasQueue(request.getQueue())) {
            listener.onFailure(new QueueMissingException(request.getQueue()));
        }
        QueueService queueService = queuesService.queue(request.getQueue());
        if (queueService == null) {
            throw new QueueMissingException(request.getQueue());
        }
        if (!queueService.isSupported(request.getMessage())) {
            listener.onFailure(new QueueTypeMismatch());
        }
        queueService.enqueue(request.getMessage());
        listener.onResponse(new EnQueueResponse(request.getMessage().getUuid()));
    }
}
@@ -298,16 +298,85 @@
 ttings;%0A
+import org.mitallast.queue.queue.QueueMessageUuidDuplicateException;%0A
 import o
@@ -1362,24 +1362,44 @@
 tQueue()));%0A
+            return;%0A
         %7D%0A  
@@ -1525,14 +1525,27 @@
     
-throw 
+listener.onFailure(
 new 
@@ -1577,32 +1577,53 @@
 uest.getQueue())
+);%0A            return
 ;%0A        %7D%0A    
@@ -1738,34 +1738,72 @@
 tch());%0A        
-%7D%0A
+    return;%0A        %7D%0A        try %7B%0A    
         queueSer
@@ -1834,24 +1834,28 @@
 Message());%0A
+    
         list
@@ -1920,19 +1920,121 @@
 etUuid()));%0A
+        %7D catch (QueueMessageUuidDuplicateException e) %7B%0A            listener.onFailure(e);%0A        %7D%0A
     %7D%0A%7D

fix enqueue action