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 @ 44242

History | View | Annotate | Download (9.6 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.DataStoreProviderFactory;
34
import org.gvsig.fmap.dal.FileHelper;
35
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
36
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
37
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
38
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
39
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.dataTypes.DataTypesManager;
42
import org.gvsig.tools.dynobject.DelegatedDynObject;
43
import org.gvsig.tools.persistence.PersistentState;
44
import org.gvsig.tools.persistence.exception.PersistenceException;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

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

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

    
53

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

    
63
    private DelegatedDynObject parameters;
64
    private SimpleSequentialReaderFactory readerFactory;
65

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

    
77
    @Override
78
    public String getDataStoreName() {
79
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
80
    }
81

    
82
    @Override
83
    public void loadFromState(PersistentState state) throws PersistenceException {
84
        String providerName = (String) state.get(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
85
        
86
        DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
87
        SimpleSequentialReaderStoreProviderFactory provider = 
88
                (SimpleSequentialReaderStoreProviderFactory)  dataman.getStoreProviderFactory(
89
                        providerName
90
        );
91
        this.readerFactory = provider.getReaderFactory();
92
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(readerFactory.getName());
93
        
94
        super.loadFromState(state);
95
    }
96
    
97
    @Override
98
    public String getDescription() {
99
        return this.getDynClass().getDescription();
100
    }
101

    
102
    @Override
103
    protected DelegatedDynObject getDelegatedDynObject() {
104
        return parameters;
105
    }
106

    
107
    @Override
108
    public void setDynValue(String name, Object value) {
109
        super.setDynValue(name, value);
110
        if( this.readerFactory!=null && StringUtils.equalsIgnoreCase(name, FILE) ) {
111
            this.readerFactory.fetchDefaultParameters(this);
112
        }
113
    }
114

    
115
    @Override
116
    public boolean isValid() {
117
        if ( this.getPathName() == null ) {
118
            return false;
119
        }
120
        return true;
121
    }
122

    
123
    @Override
124
    public File getFile() {
125
        return (File) this.getDynValue(FILE);
126
    }
127

    
128
    @Override
129
    public void setFile(File file) {
130
        this.setDynValue(FILE, file);
131
    }
132

    
133
    public IProjection getCRS() {
134
        return (IProjection) this.parameters.getDynValue(CRS);
135
    }
136

    
137
    public String getPathName() {
138
        File f = (File) this.parameters.getDynValue(FILE);
139
        if ( f == null ) {
140
            return null;
141
        }
142
        return f.getPath();
143
    }
144

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

    
177
    public String[] getPointDimensionNames() {
178
        String s = (String) this.parameters.getDynValue(POINT);
179
        if ( StringUtils.isEmpty(s) ) {
180
            return null;
181
        }
182
        String sep = this.getDelimiter(s);
183
        if ( sep == null ) {
184
            return new String[] { s };
185
        }
186
        return s.split(sep);
187
    }
188

    
189
    public boolean getIgnoreErrors() {
190
        Boolean b = (Boolean) this.parameters.getDynValue(IGNOREERRORS);
191
        if ( b == null ) {
192
            return false;
193
        }
194
        return b;
195
    }
196

    
197
    private String getDelimiter(String line) {
198
        String sep = null;
199
        // Cuiaddo con los ":", los he puesto al final a proposito
200
        // ya que podian estar en la cadena para separar el size
201
        // de cada tipo.
202
        String seps = ",;-|@#/+$%&!:";
203
        for ( int i = 0; i < seps.length(); i++ ) {
204
            sep = seps.substring(i, 1);
205
            if ( line.contains(seps.substring(i, 1)) ) {
206
                break;
207
            }
208
            sep = null;
209
        }
210
        return sep;
211
    }
212

    
213
    public List<String> getFieldNames() {
214
        String s = (String) this.parameters.getDynValue(FIELDNAMES);
215
        if ( StringUtils.isEmpty(s) ) {
216
            return null;
217
        }
218
        String sep = this.getDelimiter(s);
219
        if ( sep == null ) {
220
            return null;
221
        }
222
        String fieldNames[] = s.split("[" + sep + "]");
223
        for (int i = 0; i < fieldNames.length; i++) {
224
            fieldNames[i] = fieldNames[i].trim();
225
        }
226
        return Arrays.asList(fieldNames);
227
    }
228
    
229
    public int[] getFieldTypes() {
230
        String s = (String) this.parameters.getDynValue(FIELDTYPES);
231
        if ( StringUtils.isEmpty(s) ) {
232
            return null;
233
        }
234
        String sep = this.getDelimiter(s);
235
        if ( sep == null ) {
236
            return null;
237
        }
238
        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
239
        String fieldTypeNames[] = s.split("[" + sep + "]");
240
        int fieldTypes[] = new int[fieldTypeNames.length];
241
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
242
            s = fieldTypeNames[i].trim();
243
            if ( s.contains(":") ) {
244
                s = s.split(":")[0];
245
            }
246
            fieldTypes[i] = dataTypeManager.getType(s);
247
        }
248
        return fieldTypes;
249
    }
250

    
251
    @SuppressWarnings("UseSpecificCatch")
252
    public int[] getFieldSizes() {
253
        String s = (String) this.parameters.getDynValue(FIELDTYPES);
254
        if ( StringUtils.isEmpty(s) ) {
255
            return null;
256
        }
257
        String sep = this.getDelimiter(s);
258
        if ( sep == null ) {
259
            return null;
260
        }
261
        String fieldTypeNames[] = s.split("[" + sep + "]");
262
        int fieldSizes[] = new int[fieldTypeNames.length];
263
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
264
            String fieldtype = fieldTypeNames[i].trim();
265
            if ( fieldtype.contains(":") ) {
266
                try {
267
                    s = fieldtype.split(":")[1];
268
                    fieldSizes[i] = Integer.parseInt(s);
269
                } catch (Exception ex) {
270
                    LOGGER.warn("Can't get size of field " + i + " (" + fieldtype + ").", ex);
271
                }
272
            } else {
273
                fieldSizes[i] = 0;
274
            }
275
        }
276
        return fieldSizes;
277
    }
278

    
279
    public boolean getAutomaticTypesDetection() {
280
        Boolean b = (Boolean) this.parameters.getDynValue(AUTOMATICTYPESDETECTION);
281
        if ( b == null ) {
282
            return false;
283
        }
284
        return b;
285
    }
286

    
287
}