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.lib / src / main / java / org / gvsig / fmap / dal / feature / spi / simpleprovider / SimpleSequentialReaderStoreParameters.java @ 44307

History | View | Annotate | Download (9.92 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.feature.spi.simpleprovider;
25

    
26
import java.io.File;
27
import java.util.Arrays;
28
import java.util.List;
29
import java.util.Locale;
30
import org.apache.commons.lang3.StringUtils;
31
import org.cresques.cts.IProjection;
32
import org.gvsig.fmap.dal.DALLocator;
33
import org.gvsig.fmap.dal.FileHelper;
34
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
35
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
36
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
37
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
38
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dataTypes.DataTypesManager;
41
import org.gvsig.tools.dynobject.DelegatedDynObject;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.gvsig.tools.persistence.exception.PersistenceException;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

    
47
public class SimpleSequentialReaderStoreParameters 
48
        extends AbstractDataParameters 
49
        implements  OpenFeatureStoreParameters, FilesystemStoreParameters, Cloneable
50
    {
51

    
52
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleSequentialReaderStoreParameters.class);
53

    
54

    
55
    private static final String FILE = "file";
56
    private static final String IGNOREERRORS = "ignoreErrors";
57
    private static final String CRS = "CRS";
58
    private static final String FIELDTYPES = "fieldtypes";
59
    private static final String FIELDNAMES = "fieldnames";
60
    private static final String LOCALE = "locale";
61
    private static final String POINT = "pointFields";
62
    private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
63

    
64
    private DelegatedDynObject parameters;
65
    private SimpleSequentialReaderFactory readerFactory;
66

    
67
    public SimpleSequentialReaderStoreParameters() {
68
        
69
    }
70
    
71
    public SimpleSequentialReaderStoreParameters(SimpleSequentialReaderFactory readerFactory) {
72
        super();
73
        this.readerFactory = readerFactory;
74
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(readerFactory.getName());
75
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, readerFactory.getName());
76
    }
77

    
78
    @Override
79
    protected Object clone() throws CloneNotSupportedException {
80
        SimpleSequentialReaderStoreParameters other = (SimpleSequentialReaderStoreParameters) super.clone();
81
        other.parameters = (DelegatedDynObject) FileHelper.newParameters(readerFactory.getName());
82
        this.copyValuesTo(other);
83
        return other;
84
    }
85

    
86
    
87
    @Override
88
    public String getDataStoreName() {
89
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
90
    }
91

    
92
    @Override
93
    public void loadFromState(PersistentState state) throws PersistenceException {
94
        String providerName = (String) state.get(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
95
        
96
        DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
97
        SimpleSequentialReaderStoreProviderFactory provider = 
98
                (SimpleSequentialReaderStoreProviderFactory)  dataman.getStoreProviderFactory(
99
                        providerName
100
        );
101
        this.readerFactory = provider.getReaderFactory();
102
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(readerFactory.getName());
103
        
104
        super.loadFromState(state);
105
    }
106
    
107
    @Override
108
    public String getDescription() {
109
        return this.getDynClass().getDescription();
110
    }
111

    
112
    @Override
113
    protected DelegatedDynObject getDelegatedDynObject() {
114
        return parameters;
115
    }
116

    
117
    @Override
118
    public void setDynValue(String name, Object value) {
119
        super.setDynValue(name, value);
120
        if( this.readerFactory!=null && StringUtils.equalsIgnoreCase(name, FILE) ) {
121
            this.readerFactory.fetchDefaultParameters(this);
122
        }
123
    }
124

    
125
    @Override
126
    public boolean isValid() {
127
        if ( this.getPathName() == null ) {
128
            return false;
129
        }
130
        return true;
131
    }
132

    
133
    @Override
134
    public File getFile() {
135
        return (File) this.getDynValue(FILE);
136
    }
137

    
138
    @Override
139
    public void setFile(File file) {
140
        this.setDynValue(FILE, file);
141
    }
142

    
143
    public IProjection getCRS() {
144
        return (IProjection) this.parameters.getDynValue(CRS);
145
    }
146

    
147
    public String getPathName() {
148
        File f = (File) this.parameters.getDynValue(FILE);
149
        if ( f == null ) {
150
            return null;
151
        }
152
        return f.getPath();
153
    }
154

    
155
    @SuppressWarnings("UseSpecificCatch")
156
    public Locale getLocale() {
157
        try {
158
            String s = (String) this.parameters.getDynValue(LOCALE);
159
            if ( s.trim().length() == 0 ) {
160
                return null;
161
            }
162
            if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) {
163
                return Locale.getDefault();
164
            }
165
            Locale locale;
166
            // locale = Locale.forLanguageTag(s); // Since java 1.7
167
            String[] ss = s.split("-");
168
            switch (ss.length) {
169
            case 1:
170
                locale = new Locale(ss[0]);
171
                break;
172
            case 2:
173
                locale = new Locale(ss[0], ss[1]);
174
                break;
175
            case 3:
176
            default:
177
                locale = new Locale(ss[0], ss[1], ss[2]);
178
                break;
179
            }
180
            return locale;
181
        } catch (Exception ex) {
182
            LOGGER.warn("Can't get locale from ODS parameters.", ex);
183
            return null;
184
        }
185
    }
186

    
187
    public String[] getPointDimensionNames() {
188
        String s = (String) this.parameters.getDynValue(POINT);
189
        if ( StringUtils.isEmpty(s) ) {
190
            return null;
191
        }
192
        String sep = this.getDelimiter(s);
193
        if ( sep == null ) {
194
            return new String[] { s };
195
        }
196
        return s.split(sep);
197
    }
198

    
199
    public boolean getIgnoreErrors() {
200
        Boolean b = (Boolean) this.parameters.getDynValue(IGNOREERRORS);
201
        if ( b == null ) {
202
            return false;
203
        }
204
        return b;
205
    }
206

    
207
    private String getDelimiter(String line) {
208
        String sep = null;
209
        // Cuiaddo con los ":", los he puesto al final a proposito
210
        // ya que podian estar en la cadena para separar el size
211
        // de cada tipo.
212
        String seps = ",;-|@#/+$%&!:";
213
        for ( int i = 0; i < seps.length(); i++ ) {
214
            sep = seps.substring(i, 1);
215
            if ( line.contains(seps.substring(i, 1)) ) {
216
                break;
217
            }
218
            sep = null;
219
        }
220
        return sep;
221
    }
222

    
223
    public List<String> getFieldNames() {
224
        String s = (String) this.parameters.getDynValue(FIELDNAMES);
225
        if ( StringUtils.isEmpty(s) ) {
226
            return null;
227
        }
228
        String sep = this.getDelimiter(s);
229
        if ( sep == null ) {
230
            return null;
231
        }
232
        String fieldNames[] = s.split("[" + sep + "]");
233
        for (int i = 0; i < fieldNames.length; i++) {
234
            fieldNames[i] = fieldNames[i].trim();
235
        }
236
        return Arrays.asList(fieldNames);
237
    }
238
    
239
    public int[] getFieldTypes() {
240
        String s = (String) this.parameters.getDynValue(FIELDTYPES);
241
        if ( StringUtils.isEmpty(s) ) {
242
            return null;
243
        }
244
        String sep = this.getDelimiter(s);
245
        if ( sep == null ) {
246
            return null;
247
        }
248
        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
249
        String fieldTypeNames[] = s.split("[" + sep + "]");
250
        int fieldTypes[] = new int[fieldTypeNames.length];
251
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
252
            s = fieldTypeNames[i].trim();
253
            if ( s.contains(":") ) {
254
                s = s.split(":")[0];
255
            }
256
            fieldTypes[i] = dataTypeManager.getType(s);
257
        }
258
        return fieldTypes;
259
    }
260

    
261
    @SuppressWarnings("UseSpecificCatch")
262
    public int[] getFieldSizes() {
263
        String s = (String) this.parameters.getDynValue(FIELDTYPES);
264
        if ( StringUtils.isEmpty(s) ) {
265
            return null;
266
        }
267
        String sep = this.getDelimiter(s);
268
        if ( sep == null ) {
269
            return null;
270
        }
271
        String fieldTypeNames[] = s.split("[" + sep + "]");
272
        int fieldSizes[] = new int[fieldTypeNames.length];
273
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
274
            String fieldtype = fieldTypeNames[i].trim();
275
            if ( fieldtype.contains(":") ) {
276
                try {
277
                    s = fieldtype.split(":")[1];
278
                    fieldSizes[i] = Integer.parseInt(s);
279
                } catch (Exception ex) {
280
                    LOGGER.warn("Can't get size of field " + i + " (" + fieldtype + ").", ex);
281
                }
282
            } else {
283
                fieldSizes[i] = 0;
284
            }
285
        }
286
        return fieldSizes;
287
    }
288

    
289
    public boolean getAutomaticTypesDetection() {
290
        Boolean b = (Boolean) this.parameters.getDynValue(AUTOMATICTYPESDETECTION);
291
        if ( b == null ) {
292
            return false;
293
        }
294
        return b;
295
    }
296

    
297
}