package org.opennms.web.rest.config;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.opennms.core.config.api.ConfigurationResourceException;
import org.opennms.core.xml.AbstractJaxbConfigDao;
import org.opennms.netmgt.config.DataCollectionConfigDao;
import org.opennms.netmgt.config.datacollection.DatacollectionConfig;
import org.opennms.netmgt.config.datacollection.SnmpCollection;
import org.opennms.netmgt.config.internal.collection.DataCollectionConfigConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.sun.jersey.api.core.ResourceContext;
import com.sun.jersey.spi.resource.PerRequest;

@Component
@PerRequest
@Scope("prototype")
public class DataCollectionConfigResource implements InitializingBean {
    private static final Logger LOG = LoggerFactory.getLogger(DataCollectionConfigResource.class);

    @Resource(name="dataCollectionConfigDao")
    private DataCollectionConfigDao m_dataCollectionConfigDao;

    @Context
    private ResourceContext m_context;

    @Context 
    private UriInfo m_uriInfo;

    public void setDataCollectionConfigDao(final DataCollectionConfigDao dao) {
        m_dataCollectionConfigDao = dao;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(m_dataCollectionConfigDao, "DataCollectionConfigDao must be set!");
        Assert.isTrue(m_dataCollectionConfigDao instanceof AbstractJaxbConfigDao<?,?>);
    }

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML})
    public Response getDataCollectionConfiguration() throws ConfigurationResourceException {
        LOG.info("getDatacollectionConfigurationForLocation()");

        @SuppressWarnings("unchecked")
        final AbstractJaxbConfigDao<DatacollectionConfig,DatacollectionConfig> dao = (AbstractJaxbConfigDao<DatacollectionConfig,DatacollectionConfig>)m_dataCollectionConfigDao;
        final DatacollectionConfig dcc = dao.getContainer().getObject();
        if (dcc == null) {
            return Response.status(404).build();
        }

        // we want our own copy so we don't modify anything in the datacollection config dao
        final DatacollectionConfig modifiable = new DatacollectionConfig();
        modifiable.setRrdRepository(dcc.getRrdRepository());

        final String resourceTypeName = "__resource_type_collection";
        final SnmpCollection resourceTypeCollection = dcc.getSnmpCollection(resourceTypeName);
        
        for (final SnmpCollection collection : dcc.getSnmpCollections()) {
            if (resourceTypeName.equals(collection.getName())) {
                // skip the special case collection
                continue;
            }
            final SnmpCollection cloned = collection.clone();
            if (resourceTypeCollection != null) {
                cloned.setResourceTypes(resourceTypeCollection.getResourceTypes());
            }
            modifiable.addSnmpCollection(cloned);
        }

        final DataCollectionConfigConverter converter = new DataCollectionConfigConverter();
        modifiable.visit(converter);

        return Response.ok(converter.getDataCollectionConfig()).build();
    }
}

@@ -31,16 +31,70 @@
 onfig;%0A%0A
+import java.util.Collections;%0Aimport java.util.List;%0A%0A
 import j
@@ -554,16 +554,83 @@
 Config;%0A
+import org.opennms.netmgt.config.datacollection.IncludeCollection;%0A
 import o
@@ -1337,16 +1337,111 @@
 .class);
+%0A    private static final List%3CIncludeCollection%3E EMPTY_INCLUDE_LIST = Collections.emptyList();
 %0A%0A    @R
@@ -3370,24 +3370,192 @@
 on.clone();%0A
+            // DefaultDataCollectionConfigDao already does all the include work, so don't pass them along%0A            cloned.setIncludeCollections(EMPTY_INCLUDE_LIST);%0A
             

empty the include collections