/*
 * #%L
 * Protempa Framework
 * %%
 * Copyright (C) 2012 - 2013 Emory University
 * %%
 * 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.
 * #L%
 */
package org.protempa.backend.dsb;


import java.util.Comparator;
import java.util.Set;
import org.protempa.BackendCloseException;
import org.protempa.DataSourceBackendSourceSystem;
import org.protempa.DataSourceReadException;
import org.protempa.DataSourceWriteException;
import org.protempa.KeySetSpec;
import org.protempa.backend.AbstractBackend;
import org.protempa.backend.DataSourceBackendUpdatedEvent;
import org.protempa.proposition.value.AbsoluteTimeGranularityFactory;
import org.protempa.proposition.value.AbsoluteTimeUnitFactory;
import org.protempa.proposition.value.GranularityFactory;
import org.protempa.proposition.value.UnitFactory;


/**
 * Convenience class for implementing a data source backend.
 * 
 * @author Andrew Post
 */
public abstract class AbstractDataSourceBackend extends
		AbstractBackend<DataSourceBackendUpdatedEvent> 
                implements DataSourceBackend {
    private static final AbsoluteTimeUnitFactory absTimeUnitFactory
            = new AbsoluteTimeUnitFactory();
    private static final AbsoluteTimeGranularityFactory absTimeGranularityFactory
            = new AbsoluteTimeGranularityFactory();
    
    private String keyType;
    private GranularityFactory granularityFactory;
    private UnitFactory unitFactory;
    private Comparator<Object> keyIdComparator;

    protected AbstractDataSourceBackend() {
        this.granularityFactory = absTimeGranularityFactory;
        this.unitFactory = absTimeUnitFactory;
    }
    
    @Override
    public String getKeyType() {
        return keyType;
    }

    public void setKeyType(String keyType) {
        this.keyType = keyType;
    }

    @Override
    public GranularityFactory getGranularityFactory() {
        return granularityFactory;
    }

    public void setGranularityFactory(GranularityFactory granularityFactory) {
        if (granularityFactory == null) {
            this.granularityFactory = absTimeGranularityFactory;
        } else {
            this.granularityFactory = granularityFactory;
        }
    }

    @Override
    public UnitFactory getUnitFactory() {
        return unitFactory;
    }

    public void setUnitFactory(UnitFactory unitFactory) {
        if (unitFactory == null) {
            this.unitFactory = absTimeUnitFactory;
        } else {
            this.unitFactory = unitFactory;
        }
    }
    
    public void setKeyIdComparator(Comparator<Object> keyIdComparator) {
        this.keyIdComparator = keyIdComparator;
    }

    @Override
    public Comparator<Object> getKeyIdComparator() {
        return this.keyIdComparator;
    }
    
    @Override
    public void close() throws BackendCloseException {
    }

    @Override
    public String getKeyTypeDisplayName() {
        return getKeyType();
    }

    @Override
    public String getKeyTypePluralDisplayName() {
        return getKeyTypeDisplayName() + "s";
    }

    @Override
    public KeySetSpec[] getSelectedKeySetSpecs() throws DataSourceReadException {
        return KeySetSpec.EMPTY_KEY_SET_SPEC_ARRAY;
    }
    
    @Override
    public void deleteAllKeys() throws DataSourceWriteException {
    }
    
    @Override
    public void writeKeys(Set<String> keyIds) throws DataSourceWriteException {
    }

    @Override
    public DataSourceBackendSourceSystem getSourceSystem() {
        return DataSourceBackendSourceSystem.getInstance(getId());
    }
    
}

@@ -687,17 +687,16 @@
 d.dsb;%0A%0A
-%0A
 import j
@@ -979,31 +979,279 @@
 mpa.
-backend.AbstractBackend
+KnowledgeSource;%0Aimport org.protempa.KnowledgeSourceReadException;%0Aimport org.protempa.backend.AbstractBackend;%0Aimport org.protempa.backend.DataSourceBackendFailedConfigurationValidationException;%0Aimport org.protempa.backend.DataSourceBackendFailedDataValidationException
 ;%0Aim
@@ -1550,17 +1550,16 @@
 ctory;%0A%0A
-%0A
 /**%0A * C
@@ -1614,20 +1614,426 @@
 backend.
+ It assumes data%0A * that uses absolute time units by default, but that can be overridden using%0A * %7B@link #setGranularityFactory(org.protempa.proposition.value.GranularityFactory) %7D%0A * and %7B@link #setUnitFactory(org.protempa.proposition.value.UnitFactory) %7D. The%0A * key type must be set using %7B@link #setKeyType(java.lang.String) %7D to %0A * the id of the proposition definition represented by keys in the data.
 %0A *
- 
 %0A * @aut
@@ -2112,10 +2112,16 @@
 nds%0A
-%09%09
+        
 Abst
@@ -2166,21 +2166,17 @@
 ent%3E
- %0A   
+%0A
         
     
@@ -2171,21 +2171,16 @@
         
-     
 implemen
@@ -2202,16 +2202,17 @@
 ckend %7B%0A
+%0A
     priv
@@ -2450,20 +2450,16 @@
 tory();%0A
-    
 %0A    pri
@@ -2766,36 +2766,32 @@
 tFactory;%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -3635,28 +3635,24 @@
     %7D%0A    %7D%0A
-    
 %0A    public 
@@ -3870,36 +3870,32 @@
 mparator;%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -4316,28 +4316,24 @@
 RRAY;%0A    %7D%0A
-    
 %0A    @Overri
@@ -4407,20 +4407,16 @@
 %7B%0A    %7D%0A
-    
 %0A    @Ov
@@ -4657,15 +4657,431 @@
 ;%0A    %7D%0A
+%0A
     
+@Override%0A    public DataValidationEvent%5B%5D validateData(KnowledgeSource knowledgeSource) throws DataSourceBackendFailedDataValidationException, KnowledgeSourceReadException %7B%0A        return new DataValidationEvent%5B0%5D;%0A    %7D%0A    %0A    @Override%0A    public void validateConfiguration(KnowledgeSource knowledgeSource) throws DataSourceBackendFailedConfigurationValidationException, KnowledgeSourceReadException %7B%0A    %7D%0A
 %0A%7D%0A

Add more default implementations.