Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / spi / AbstractDataParameters.java @ 44346

History | View | Annotate | Download (8.83 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.spi;
24

    
25
import java.io.ByteArrayOutputStream;
26
import java.io.File;
27
import java.io.IOException;
28
import java.util.Iterator;
29
import java.util.List;
30
import org.apache.commons.io.FileUtils;
31
import org.apache.commons.io.FilenameUtils;
32
import org.apache.commons.lang3.StringUtils;
33
import org.cresques.cts.ICRSFactory;
34
import org.cresques.cts.IProjection;
35
import org.gvsig.fmap.crs.CRSFactory;
36

    
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
import org.gvsig.fmap.dal.DataParameters;
41
import org.gvsig.fmap.dal.exception.CopyParametersException;
42
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DelegatedDynObject;
45
import org.gvsig.tools.dynobject.DynClass;
46
import org.gvsig.tools.dynobject.DynField;
47
import org.gvsig.tools.dynobject.DynObject;
48
import org.gvsig.tools.dynobject.DynObjectEncoder;
49
import org.gvsig.tools.dynobject.DynObjectManager;
50
import org.gvsig.tools.dynobject.exception.DynMethodException;
51
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
52
import org.gvsig.tools.persistence.PersistenceManager;
53
import org.gvsig.tools.persistence.PersistentState;
54
import org.gvsig.tools.persistence.exception.PersistenceException;
55

    
56
/**
57
 * @author jmvivo
58
 *
59
 */
60
@SuppressWarnings("UseSpecificCatch")
61
public abstract class AbstractDataParameters implements DataParameters {
62

    
63
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractDataParameters.class);
64

    
65
    @Override
66
    public Object getDynValue(String name) {
67
        return getDelegatedDynObject().getDynValue(name);
68
    }
69

    
70
    public String getProviderName() {
71
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
72
    }
73
    
74
    public String getDataStoreName() {
75
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
76
    }
77

    
78
    public String getDescription() {
79
        return this.getDynClass().getDescription();
80
    }
81
    
82
    @Override
83
    public String toString() {
84
        DynObjectEncoder encoder = ToolsLocator.getDynObjectManager().createSimpleDynObjectEncoder();
85
        return encoder.encode(this);
86
    }
87

    
88
    @Override
89
    public void setDynValue(String name, Object value) {
90
        DelegatedDynObject delegated = getDelegatedDynObject();
91
        if (delegated.getDynClass().getDynField(name) != null) {
92
            delegated.setDynValue(name, value);
93
        } else {
94
            try {
95
                throw new IllegalArgumentException(name);
96
            } catch (IllegalArgumentException ex) {
97
                LOGGER.warn("Attribute '" + name + "' is not defined in "
98
                        + delegated.getDynClass().getFullName() + " definition", ex);
99
            }
100
        }
101
    }
102

    
103
    @Override
104
    public void clear() {
105
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
106
        manager.clear(this);
107
    }
108

    
109
    protected void copyValuesTo(DataParameters target) {
110
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
111
        manager.copy(this, target);
112
    }
113

    
114
    @Override
115
    public DataParameters getCopy() {
116
        DataParameters copy;
117
        try {
118
            if( this instanceof Cloneable ) {
119
                copy = (DataParameters) this.clone();
120
            } else {
121
                copy = (DataParameters) this.getClass().newInstance();
122
                this.copyValuesTo(copy);
123
            }
124
        } catch (Exception e) {
125
            throw new CopyParametersException("data parameters", e);
126
        }
127
        return copy;
128
    }
129

    
130
    @Override
131
    public void saveToState(PersistentState state) throws PersistenceException {
132
        DynField[] fields = getDelegatedDynObject().getDynClass().getDynFields();
133

    
134
        for (DynField field : fields) {
135
            if (field.isPersistent()) {
136
                String name = field.getName();
137
                Object value = this.getDynValue(name);
138
                state.set(name, value);
139
            }
140
        }
141
    }
142

    
143
    @Override
144
    public void loadFromState(PersistentState state) throws PersistenceException {
145
        @SuppressWarnings("rawtypes")
146
        Iterator it = state.getNames();
147
        while (it.hasNext()) {
148
            String name = (String) it.next();
149
            try {
150
                Object value = state.get(name);
151
                this.setDynValue(name, value);
152
            } catch (Throwable t) {
153
                LOGGER.warn("Can't load '" + name + "' property", t);
154
            }
155
        }
156
    }
157

    
158
    @Override
159
    public void delegate(DynObject dynObject) {
160
        getDelegatedDynObject().delegate(dynObject);
161

    
162
    }
163

    
164
    @Override
165
    public DynClass getDynClass() {
166
        return getDelegatedDynObject().getDynClass();
167
    }
168

    
169
    @Override
170
    public boolean hasDynValue(String name) {
171
        return getDelegatedDynObject().hasDynValue(name);
172
    }
173

    
174
    @Override
175
    public void implement(DynClass dynClass) {
176
        getDelegatedDynObject().implement(dynClass);
177
    }
178

    
179
    @Override
180
    public Object invokeDynMethod(String name, Object[] args)
181
            throws DynMethodException {
182
        return getDelegatedDynObject().invokeDynMethod(this, name, args);
183
    }
184

    
185
    @Override
186
    public Object invokeDynMethod(int code, Object[] args)
187
            throws DynMethodException {
188
        return getDelegatedDynObject().invokeDynMethod(this, code, args);
189
    }
190

    
191
    @Override
192
    public void validate() throws ValidateDataParametersException {
193
        try {
194
            this.getDynClass().validate(this);
195
        } catch (DynObjectValidateException e) {
196
            throw new ValidateDataParametersException(e);
197
        }
198
    }
199

    
200
    protected void loadPRJ(File file, String parameterName) {
201
        File prjFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + ".prj");
202
        if (prjFile.exists()) {
203
            try {
204
                String contentFile = FileUtils.readFileToString(prjFile);
205
                if (StringUtils.isNotEmpty(contentFile)) {
206
                    IProjection crs = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, contentFile);
207
                    this.setDynValue(parameterName, crs);
208
                }
209
            } catch (IOException e) {
210
                LOGGER.warn("Couldn't read prj file ''{}''", prjFile.getAbsolutePath());
211
            }
212
        }
213
    }
214

    
215
    protected void loadWLD(File file, String parameterName) {
216
        File wldFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + ".wld");
217
        if (wldFile.exists()) {
218
            try {
219
                List<String> lines = FileUtils.readLines(wldFile);
220
                if (lines != null && lines.size() == 6) {
221
                    this.setDynValue(parameterName, lines);
222
                }
223

    
224
            } catch (IOException e) {
225
                LOGGER.warn("Couldn't read wld file ''{}''", wldFile.getAbsolutePath());
226
            }
227
        }
228
    }
229

    
230
    @Override
231
    public byte[] toByteArray() {
232
            try {
233
            PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
234
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
235
            PersistentState state = persistenceManager.getState(this);
236
            persistenceManager.saveState(state, stream);
237
            return stream.toByteArray();
238
        } catch (Exception ex) {
239
            LOGGER.warn("Can't get byte[] from parameters.",ex);
240
            return null;
241
        }
242
    }
243

    
244
    @Override
245
    public boolean equals(Object obj) {
246
        if( !(obj instanceof DynObject) ) {
247
            return false;
248
        }
249
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
250
        return manager.equals(this, (DynObject) obj);
251
    }
252

    
253
    @Override
254
    public int hashCode() {
255
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
256
        return manager.hashCode(this);
257
    }
258

    
259
    /**
260
     * Returns an instance of the {@link DynObject} to delegate to.
261
     *
262
     * @return the delegate {@link DynObject}
263
     */
264
    protected abstract DelegatedDynObject getDelegatedDynObject();
265

    
266
}