package org.bitrepository.integrityservice.tools;

import java.util.List;

import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.settings.XMLFileSettingsLoader;
import org.bitrepository.common.utils.SettingsUtils;
import org.bitrepository.integrityservice.cache.database.IntegrityDBTools;
import org.bitrepository.service.ServiceSettingsProvider;
import org.bitrepository.service.database.DBConnector;
import org.bitrepository.settings.referencesettings.ServiceType;

/**
 * Class to handle removal and additions of colletions in the integrityDB 
 */
public class CollectionsAdmin {

    final String method;
    final String collectionID;
    final Settings settings;
    IntegrityDBTools tools;
    
    public CollectionsAdmin(String method, String collectionID, String pathToSettings) {
        this.method = method;
        this.collectionID = collectionID;
        
        ServiceSettingsProvider settingsLoader =
                new ServiceSettingsProvider(new XMLFileSettingsLoader(pathToSettings), ServiceType.INTEGRITY_SERVICE);
        settings = settingsLoader.getSettings();
        SettingsUtils.initialize(settings);
        tools = new IntegrityDBTools(new DBConnector(settings.getReferenceSettings().getIntegrityServiceSettings().getIntegrityDatabase()));
    }
    
    /**
     * Method to run the requested action on the database. Checks to see if the requested actions is supported.
     * And if we can operate on the requested collectionID
     * @throws InvalidMethodException if the method/action is not supported
     * @throws UnknownCollectionException if the requested collectionID is not found in settings. 
     */
    public void invoke() throws InvalidMethodException, UnknownCollectionException {
        hasCollection();
        if(method.equals("add")) {
            tools.addCollection(collectionID);
        } else if(method.equals("remove")) {
            tools.removeCollection(collectionID);
        } else {
            throw new InvalidMethodException("The method '" + method + "' is not supported, use: 'remove' or 'add'.");
        }
    }
    
    /**
     * Method to determine if a collection is present in the RepositorySettings. 
     * @throws UnknownCollectionException if the collection is not present 
     */
    private void hasCollection() throws UnknownCollectionException {
        List<String> collections = SettingsUtils.getAllCollectionsIDs();
        if(!collections.contains(collectionID)) {
            throw new UnknownCollectionException("The collection '" + collectionID + "' is not present in RepositorySettings");
        }
    }
}

@@ -606,24 +606,32 @@
 dmin %7B%0A%0A    
+private 
 final String
@@ -639,24 +639,32 @@
 method;%0A    
+private 
 final String
@@ -682,16 +682,24 @@
 ID;%0A    
+private 
 final Se

make member variables private