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

History | View | Annotate | Download (8.26 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 getDataStoreName() {
71
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
72
    }
73

    
74
    public String getDescription() {
75
        return this.getDynClass().getDescription();
76
    }
77
    
78
    @Override
79
    public String toString() {
80
        DynObjectEncoder encoder = ToolsLocator.getDynObjectManager().createSimpleDynObjectEncoder();
81
        return encoder.encode(this);
82
    }
83

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

    
99
    @Override
100
    public void clear() {
101
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
102
        manager.clear(this);
103
    }
104

    
105
    protected void copyValuesTo(AbstractDataParameters target) {
106
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
107
        manager.copy(this, target);
108
    }
109

    
110
    @Override
111
    public DataParameters getCopy() {
112
        // TODO Delegar en el DynObject cuando tenga este servicio
113
        AbstractDataParameters copy;
114
        try {
115
            copy = (AbstractDataParameters) this.getClass().newInstance();
116
        } catch (InstantiationException | IllegalAccessException e) {
117
            throw new CopyParametersException("data parameters", e);
118
        }
119
        this.copyValuesTo(copy);
120
        return copy;
121
    }
122

    
123
    @Override
124
    public void saveToState(PersistentState state) throws PersistenceException {
125
        DynField[] fields = getDelegatedDynObject().getDynClass().getDynFields();
126

    
127
        for (DynField field : fields) {
128
            if (field.isPersistent()) {
129
                String name = field.getName();
130
                Object value = this.getDynValue(name);
131
                state.set(name, value);
132
            }
133
        }
134
    }
135

    
136
    @Override
137
    public void loadFromState(PersistentState state) throws PersistenceException {
138
        @SuppressWarnings("rawtypes")
139
        Iterator it = state.getNames();
140
        while (it.hasNext()) {
141
            String name = (String) it.next();
142
            try {
143
                Object value = state.get(name);
144
                this.setDynValue(name, value);
145
            } catch (Throwable t) {
146
                LOGGER.warn("Can't load '" + name + "' property", t);
147
            }
148
        }
149
    }
150

    
151
    @Override
152
    public void delegate(DynObject dynObject) {
153
        getDelegatedDynObject().delegate(dynObject);
154

    
155
    }
156

    
157
    @Override
158
    public DynClass getDynClass() {
159
        return getDelegatedDynObject().getDynClass();
160
    }
161

    
162
    @Override
163
    public boolean hasDynValue(String name) {
164
        return getDelegatedDynObject().hasDynValue(name);
165
    }
166

    
167
    @Override
168
    public void implement(DynClass dynClass) {
169
        getDelegatedDynObject().implement(dynClass);
170
    }
171

    
172
    @Override
173
    public Object invokeDynMethod(String name, Object[] args)
174
            throws DynMethodException {
175
        return getDelegatedDynObject().invokeDynMethod(this, name, args);
176
    }
177

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

    
184
    @Override
185
    public void validate() throws ValidateDataParametersException {
186
        try {
187
            this.getDynClass().validate(this);
188
        } catch (DynObjectValidateException e) {
189
            throw new ValidateDataParametersException(e);
190
        }
191
    }
192

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

    
208
    protected void loadWLD(File file, String parameterName) {
209
        File wldFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + ".wld");
210
        if (wldFile.exists()) {
211
            try {
212
                List<String> lines = FileUtils.readLines(wldFile);
213
                if (lines != null && lines.size() == 6) {
214
                    this.setDynValue(parameterName, lines);
215
                }
216

    
217
            } catch (IOException e) {
218
                LOGGER.warn("Couldn't read wld file ''{}''", wldFile.getAbsolutePath());
219
            }
220
        }
221
    }
222

    
223
    @Override
224
    public byte[] toByteArray() {
225
            try {
226
            PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
227
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
228
            PersistentState state = persistenceManager.getState(this);
229
            persistenceManager.saveState(state, stream);
230
            return stream.toByteArray();
231
        } catch (Exception ex) {
232
            LOGGER.warn("Can't get byte[] from parameters.",ex);
233
            return null;
234
        }
235
    }
236
    
237
    /**
238
     * Returns an instance of the {@link DynObject} to delegate to.
239
     *
240
     * @return the delegate {@link DynObject}
241
     */
242
    protected abstract DelegatedDynObject getDelegatedDynObject();
243

    
244
}