/*
 * Copyright (c) 2014, Arjuna Technologies Limited, Newcastle-upon-Tyne, England. All rights reserved.
 */

package com.arjuna.dbplugins.ckan.filestore.dataflownodes;

import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.arjuna.databroker.data.DataConsumer;
import com.arjuna.databroker.data.DataFlow;
import com.arjuna.databroker.data.DataProvider;
import com.arjuna.databroker.data.DataService;
import com.arjuna.databroker.data.DataSink;
import com.arjuna.databroker.data.jee.annotation.DataConsumerInjection;
import com.arjuna.databroker.data.jee.annotation.DataProviderInjection;

public class FileStoreDataService implements DataService
{
    private static final Logger logger = Logger.getLogger(FileStoreDataService.class.getName());

    public static final String SERVICEROOTURL_PROPERTYNAME = "Service Root URL";

    public FileStoreDataService(String name, Map<String, String> properties)
    {
        logger.log(Level.FINE, "FileStoreDataService: " + name + ", " + properties);

        _name       = name;
        _properties = properties;

        _serviceRootURL = properties.get(SERVICEROOTURL_PROPERTYNAME);
    }

    @Override
    public DataFlow getDataFlow()
    {
        return _dataFlow;
    }

    @Override
    public void setDataFlow(DataFlow dataFlow)
    {
        _dataFlow = dataFlow;
    }

    @Override
    public String getName()
    {
        return _name;
    }

    @Override
    public void setName(String name)
    {
        _name = name;
    }

    @Override
    public Map<String, String> getProperties()
    {
        return Collections.unmodifiableMap(_properties);
    }

    @Override
    public void setProperties(Map<String, String> properties)
    {
        _properties = properties;
    }

    public void consume(Document data)
    {
        logger.log(Level.FINE, "DOMDocumentPushDataSink.consume");

        try
        {
            MessageFactory messageFactory   = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
            SOAPMessage    request          = messageFactory.createMessage();
            SOAPPart       requestPart      = request.getSOAPPart();
            SOAPEnvelope   requestEnvelope  = requestPart.getEnvelope();
            SOAPBody       requestBody      = requestEnvelope.getBody();
            Document       requestData      = (Document) data.cloneNode(true);
            Element        requestElement   = requestData.getDocumentElement();
            Element        requestIdElement = requestData.createElementNS(CommonDefs.INTERCONNECT_NAMESPACE, CommonDefs.INTERCONNECT_PARAMETERNAME_ID);
            requestIdElement.appendChild(requestData.createTextNode(_endpointPath));
            requestElement.appendChild(requestIdElement);
            requestBody.addDocument(requestData);

            if (logger.isLoggable(Level.FINE))
            {
                ByteArrayOutputStream requestOutputStream = new ByteArrayOutputStream();
                request.writeTo(requestOutputStream);
                logger.log(Level.FINE, "DOMDocumentPushDataSink.consume: request = " + requestOutputStream.toString());
                requestOutputStream.close();
            }

            SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection        connection        = connectionFactory.createConnection();
            URL                   serviceURL        = new URL(_serviceRootURL + "/" + CommonDefs.INTERCONNECT_SERVICE_PATH + "/" + CommonDefs.INTERCONNECT_SERVICENAME_ACCEPTOR);

            SOAPMessage responce = connection.call(request, serviceURL);

            if (logger.isLoggable(Level.FINE))
            {
                ByteArrayOutputStream responceOutputStream = new ByteArrayOutputStream();
                responce.writeTo(responceOutputStream);
                logger.log(Level.FINE, "DOMDocumentPushDataSink.consume: responce = " + responceOutputStream.toString());
                responceOutputStream.close();
            }
        }
        catch (Throwable throwable)
        {
            logger.log(Level.WARNING, "Problems with web service invoke", throwable);
        }
    }

    @Override
    public Collection<Class<?>> getDataProviderDataClasses()
    {
        Set<Class<?>> dataConsumerDataClasses = new HashSet<Class<?>>();

        return dataConsumerDataClasses;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> DataProvider<T> getDataProvider(Class<T> dataClass)
    {
        return null;
    }

    @Override
    public Collection<Class<?>> getDataConsumerDataClasses()
    {
        Set<Class<?>> dataConsumerDataClasses = new HashSet<Class<?>>();

        dataConsumerDataClasses.add(Document.class);

        return dataConsumerDataClasses;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> DataConsumer<T> getDataConsumer(Class<T> dataClass)
    {
        if (dataClass == Document.class)
            return (DataConsumer<T>) _dataConsumer;
        else
            return null;
    }

    private String _serviceRootURL;
    private String _endpointPath;

    private DataFlow               _dataFlow;
    private String                 _name;
    private Map<String, String>    _properties;
    @DataConsumerInjection(methodName="consume")
    private DataConsumer<Document> _dataConsumer;
}

@@ -2384,2179 +2384,62 @@
 E, %22
-DOMDocumentPushDataSink.consume%22);%0A%0A        try%0A        %7B%0A            MessageFactory messageFactory   = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);%0A            SOAPMessage    request          = messageFactory.createMessage();%0A            SOAPPart       requestPart      = request.getSOAPPart();%0A            SOAPEnvelope   requestEnvelope  = requestPart.getEnvelope();%0A            SOAPBody       requestBody      = requestEnvelope.getBody();%0A            Document       requestData      = (Document) data.cloneNode(true);%0A            Element        requestElement   = requestData.getDocumentElement();%0A            Element        requestIdElement = requestData.createElementNS(CommonDefs.INTERCONNECT_NAMESPACE, CommonDefs.INTERCONNECT_PARAMETERNAME_ID);%0A            requestIdElement.appendChild(requestData.createTextNode(_endpointPath));%0A            requestElement.appendChild(requestIdElement);%0A            requestBody.addDocument(requestData);%0A%0A            if (logger.isLoggable(Level.FINE))%0A            %7B%0A                ByteArrayOutputStream requestOutputStream = new ByteArrayOutputStream();%0A                request.writeTo(requestOutputStream);%0A                logger.log(Level.FINE, %22DOMDocumentPushDataSink.consume: request = %22 + requestOutputStream.toString());%0A                requestOutputStream.close();%0A            %7D%0A%0A            SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();%0A            SOAPConnection        connection        = connectionFactory.createConnection();%0A            URL                   serviceURL        = new URL(_serviceRootURL + %22/%22 + CommonDefs.INTERCONNECT_SERVICE_PATH + %22/%22 + CommonDefs.INTERCONNECT_SERVICENAME_ACCEPTOR);%0A%0A            SOAPMessage responce = connection.call(request, serviceURL);%0A%0A            if (logger.isLoggable(Level.FINE))%0A            %7B%0A                ByteArrayOutputStream responceOutputStream = new ByteArrayOutputStream();%0A                responce.writeTo(responceOutputStream);%0A                logger.log(Level.FINE, %22DOMDocumentPushDataSink.consume: responce = %22 + responceOutputStream.toString());%0A                responceOutputStream.close();%0A            %7D
+FileStoreDataService.consume%22);%0A%0A        try%0A        %7B
 %0A   
@@ -2548,19 +2548,26 @@
 ith 
-web service
+ckan filestore api
  inv

Remove to template