/**
 * Copyright (C) 2014 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dashbuilder.dataprovider.backend;

import java.io.File;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.inject.Named;

import org.dashbuilder.dataprovider.DataSetProvider;
import org.dashbuilder.dataprovider.DataSetProviderType;
import org.dashbuilder.dataset.DataSet;
import org.dashbuilder.dataset.DataSetGenerator;
import org.dashbuilder.dataset.DataSetLookup;
import org.dashbuilder.dataset.def.BeanDataSetDef;
import org.dashbuilder.dataset.def.CSVDataSetDef;
import org.dashbuilder.dataset.def.DataSetDef;
import org.dashbuilder.dataset.events.DataSetDefModifiedEvent;
import org.slf4j.Logger;

import static org.uberfire.commons.validation.PortablePreconditions.*;

@ApplicationScoped
@Named("csv")
public class BeanDataSetProvider implements DataSetProvider {

    @Inject
    protected StaticDataSetProvider staticDataSetProvider;

    @Inject
    protected Logger log;

    public DataSetProviderType getType() {
        return DataSetProviderType.BEAN;
    }

    public DataSet lookupDataSet(DataSetDef def, DataSetLookup lookup) throws Exception {
        // Look first into the static data set provider since CSV data set are statically registered once loaded.
        DataSet dataSet = staticDataSetProvider.lookupDataSet(def, null);
        if (dataSet == null) {
            // If not exists or is outdated then load from the CSV file
            BeanDataSetDef beanDef = (BeanDataSetDef) def;
            Class generatorClass = Class.forName(beanDef.getGeneratorClass());
            DataSetGenerator dataSetGenerator = (DataSetGenerator) generatorClass.newInstance();
            dataSet = dataSetGenerator.buildDataSet(beanDef.getParamaterMap());
            dataSet.setUUID(def.getUUID());
            dataSet.setDefinition(def);

            // Make the data set static before return
            staticDataSetProvider.registerDataSet(dataSet);

        }
        // Always do the lookup on the statically registered data set.
        return staticDataSetProvider.lookupDataSet(def, lookup);
    }

    public boolean isDataSetOutdated(DataSetDef def) {
        return false;
    }
}

@@ -1393,11 +1393,12 @@
 ed(%22
-csv
+bean
 %22)%0Ap
@@ -2792,11 +2792,512 @@
 e;%0A    %7D
+%0A%0A    // Listen to changes on the data set definition registry%0A%0A    private void onDataSetDefModifiedEvent(@Observes DataSetDefModifiedEvent event) %7B%0A        checkNotNull(%22event%22, event);%0A        checkNotNull(%22event%22, event.getOldDataSetDef());%0A%0A        DataSetDef oldDef = event.getOldDataSetDef();%0A        if (DataSetProviderType.BEAN.equals(oldDef.getProvider())) %7B%0A            String uuid = event.getOldDataSetDef().getUUID();%0A            staticDataSetProvider.removeDataSet(uuid);%0A        %7D%0A    %7D
 %0A%7D%0A

Fix bean provider name