/*
* Geotoolkit - An Open Source Java GIS Toolkit
* http://www.geotoolkit.org
*
* (C) 2011, Geomatys
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotoolkit.data.dbf;
import org.geotoolkit.data.FileFeatureStoreFactory;
import org.geotoolkit.data.AbstractFolderFeatureStoreFactory;
import org.apache.sis.metadata.iso.identification.DefaultServiceIdentification;
import org.geotoolkit.nio.IOUtilities;
import org.opengis.metadata.identification.Identification;
import org.opengis.parameter.ParameterDescriptor;
import org.opengis.parameter.ParameterDescriptorGroup;
import org.opengis.parameter.ParameterValueGroup;
import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import org.geotoolkit.storage.DataStores;
import org.geotoolkit.storage.DataType;
import org.geotoolkit.storage.DefaultFactoryMetadata;
import org.geotoolkit.storage.FactoryMetadata;
/**
* FeatureStore for a folder of DBF files.
*
* @author Johann Sorel (Geomatys)
* @module
*/
public class DbaseFolderFeatureStoreFactory extends AbstractFolderFeatureStoreFactory{
/** factory identification **/
public static final DefaultServiceIdentification IDENTIFICATION = derivateIdentification(DbaseFeatureStoreFactory.IDENTIFICATION);
public static final String NAME = IDENTIFICATION.getCitation().getTitle().toString();
public static final ParameterDescriptor<String> IDENTIFIER = createFixedIdentifier(NAME);
public static final ParameterDescriptorGroup PARAMETERS_DESCRIPTOR =
derivateDescriptor(IDENTIFIER,DbaseFeatureStoreFactory.PARAMETERS_DESCRIPTOR);
public DbaseFolderFeatureStoreFactory(){
super(PARAMETERS_DESCRIPTOR);
}
/**
* {@inheritDoc}
*/
@Override
public Identification getIdentification() {
return IDENTIFICATION;
}
/**
* {@inheritDoc}
*/
@Override
public FileFeatureStoreFactory getSingleFileFactory() {
return DataStores.getAllFactories(DbaseFeatureStoreFactory.class).iterator().next();
}
/**
* {@inheritDoc}
*/
@Override
public CharSequence getDescription() {
return Bundle.formatInternational(Bundle.Keys.databaseFolderDescription);
}
/**
* {@inheritDoc}
*/
@Override
public CharSequence getDisplayName() {
return Bundle.formatInternational(Bundle.Keys.databaseFolderTitle);
}
/**
* {@inheritDoc}
*/
@Override
public boolean canProcess(final ParameterValueGroup params) {
final boolean valid = super.canProcess(params);
if (!valid) {
return false;
}
final Object obj = params.parameter(FOLDER_PATH.getName().toString()).getValue();
if(!(obj instanceof URI)){
return false;
}
final URI path = (URI)obj;
Path pathFile = Paths.get(path);
if (Files.isDirectory(pathFile)){
boolean dbfFiles = false;
boolean shpFiles = false;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(pathFile)) {
for (Path candidate : stream) {
final String ext = IOUtilities.extension(candidate);
if ("dbf".equalsIgnoreCase(ext)) {
dbfFiles = true;
}
if ("shp".equalsIgnoreCase(ext)) {
shpFiles = true;
}
}
} catch (IOException e) {
LOGGER.log(Level.FINE, e.getLocalizedMessage());
}
return (dbfFiles && shpFiles);
}
return false;
}
@Override
public FactoryMetadata getMetadata() {
return new DefaultFactoryMetadata(DataType.VECTOR, true, true, true, false, DefaultFactoryMetadata.GEOMS_NONE);
}
}