Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / simplereader / SimpleReaderFilesystemServerProvider.java @ 47643

History | View | Annotate | Download (5.22 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.simplereader;
25

    
26
import java.io.File;
27
import java.io.IOException;
28

    
29
import org.gvsig.fmap.dal.DataServerExplorer;
30
import org.gvsig.fmap.dal.DataStoreParameters;
31
import org.gvsig.fmap.dal.exception.FileNotFoundException;
32
import org.gvsig.fmap.dal.exception.RemoveException;
33
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
34
import org.gvsig.fmap.dal.feature.EditableFeatureType;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
37
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
38
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider;
39
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider;
40
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices;
41
import org.gvsig.tools.dataTypes.DataTypes;
42

    
43
public abstract class SimpleReaderFilesystemServerProvider extends AbstractFilesystemServerExplorerProvider
44
        implements FilesystemServerExplorerProvider, ResourceConsumer {
45

    
46
    protected FilesystemServerExplorerProviderServices serverExplorer;
47

    
48
    protected abstract String getFileExtension(); // ej: .csv .gml .kml
49
    
50
    @Override
51
    public int getMode() {
52
        return DataServerExplorer.MODE_FEATURE | DataServerExplorer.MODE_GEOMETRY;
53
    }
54

    
55
    @Override
56
    public boolean accept(File file) {
57
        String pathname = file.getName().toLowerCase();
58
        if ( pathname.endsWith(this.getFileExtension())){
59
            return true;
60
        }
61
        return false;
62
    }
63

    
64
    @Override
65
    public boolean canCreate() {
66
        return false;
67
    }
68

    
69
    @Override
70
    public void initialize(FilesystemServerExplorerProviderServices serverExplorer) {
71
        this.serverExplorer = serverExplorer;
72

    
73
    }
74

    
75
    @Override
76
    public void remove(DataStoreParameters parameters) throws RemoveException {
77
        SimpleReaderStoreParameters params = (SimpleReaderStoreParameters) parameters;
78
        File file = params.getFile();
79
        if (!file.exists()) {
80
            throw new RemoveException(this.getDataStoreProviderName(),
81
                    new FileNotFoundException(params.getFile()));
82
        }
83
        if (!file.delete()) {
84
            throw new RemoveException(this.getDataStoreProviderName(),
85
                    new IOException()); // FIXME Exception
86
        }
87
    }
88

    
89
    @Override
90
    public boolean closeResourceRequested(ResourceProvider resource) {
91
        // while it is using a resource anyone can't close it
92
        return !(this.equals(resource));
93
    }
94

    
95
    /*
96
         * (non-Javadoc)
97
         *
98
         * @see
99
         * org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
100
         * gvsig.fmap.dal.resource.spi.ResourceProvider)
101
     */
102
    @Override
103
    public void resourceChanged(ResourceProvider resource) {
104
        // Do nothing
105
    }
106

    
107
    protected EditableFeatureType fixFeatureType(FeatureType featureType) {
108
        EditableFeatureType newFeatureType;
109

    
110
        if (featureType instanceof EditableFeatureType) {
111
            newFeatureType = (EditableFeatureType) featureType.getCopy();
112
        } else {
113
            newFeatureType = featureType.getEditable();
114
        }
115

    
116
        for (int i = 0; i < newFeatureType.size(); i++) {
117
            EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) newFeatureType.getAttributeDescriptor(i);
118
            String s;
119
            switch (attr.getType()) {
120
                case DataTypes.INT:
121
                    s = String.valueOf(Integer.MAX_VALUE);
122
                    attr.setSize(18);
123
                    attr.setPrecision(0);
124
                    attr.setScale(0);
125
                    break;
126
                case DataTypes.LONG:
127
                    s = String.valueOf(Long.MAX_VALUE);
128
                    attr.setSize(18);
129
                    attr.setPrecision(0);
130
                    attr.setScale(0);
131
                    break;
132
                case DataTypes.FLOAT:
133
                    attr.setSize(18);
134
                    attr.setPrecision(6);
135
                    attr.setScale(0);
136
                    break;
137
                case DataTypes.DOUBLE:
138
                    attr.setSize(18);
139
                    attr.setPrecision(6);
140
                    attr.setScale(0);
141
                    break;
142
            }
143
        }
144
        return newFeatureType;
145
    }
146
}