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 @ 44259
History | View | Annotate | Download (7.61 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.File; |
26 |
import java.io.IOException; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
import org.apache.commons.io.FileUtils; |
30 |
import org.apache.commons.io.FilenameUtils; |
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.cresques.cts.ICRSFactory; |
33 |
import org.cresques.cts.IProjection; |
34 |
import org.gvsig.fmap.crs.CRSFactory; |
35 |
|
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
import org.gvsig.fmap.dal.DataParameters; |
40 |
import org.gvsig.fmap.dal.exception.CopyParametersException; |
41 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
44 |
import org.gvsig.tools.dynobject.DynClass; |
45 |
import org.gvsig.tools.dynobject.DynField; |
46 |
import org.gvsig.tools.dynobject.DynObject; |
47 |
import org.gvsig.tools.dynobject.DynObjectEncoder; |
48 |
import org.gvsig.tools.dynobject.DynObjectManager; |
49 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
50 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
51 |
import org.gvsig.tools.persistence.PersistentState; |
52 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
53 |
|
54 |
/**
|
55 |
* @author jmvivo
|
56 |
*
|
57 |
*/
|
58 |
public abstract class AbstractDataParameters implements DataParameters { |
59 |
|
60 |
private static final Logger logger = LoggerFactory.getLogger(AbstractDataParameters.class); |
61 |
|
62 |
@Override
|
63 |
public Object getDynValue(String name) { |
64 |
return getDelegatedDynObject().getDynValue(name);
|
65 |
} |
66 |
|
67 |
public String getDataStoreName() { |
68 |
return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
69 |
} |
70 |
|
71 |
public String getDescription() { |
72 |
return this.getDynClass().getDescription(); |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public String toString() { |
77 |
DynObjectEncoder encoder = ToolsLocator.getDynObjectManager().createSimpleDynObjectEncoder(); |
78 |
return encoder.encode(this); |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void setDynValue(String name, Object value) { |
83 |
DelegatedDynObject delegated = getDelegatedDynObject(); |
84 |
if (delegated.getDynClass().getDynField(name) != null) { |
85 |
delegated.setDynValue(name, value); |
86 |
} else {
|
87 |
try {
|
88 |
throw new IllegalArgumentException(name); |
89 |
} catch (IllegalArgumentException ex) { |
90 |
logger.warn("Attribute '" + name + "' is not defined in " |
91 |
+ delegated.getDynClass().getFullName() + " definition", ex);
|
92 |
} |
93 |
} |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void clear() { |
98 |
DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
99 |
manager.clear(this);
|
100 |
} |
101 |
|
102 |
protected void copyValuesTo(AbstractDataParameters target) { |
103 |
DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
104 |
manager.copy(this, target);
|
105 |
} |
106 |
|
107 |
@Override
|
108 |
public DataParameters getCopy() {
|
109 |
// TODO Delegar en el DynObject cuando tenga este servicio
|
110 |
AbstractDataParameters copy; |
111 |
try {
|
112 |
copy = (AbstractDataParameters) this.getClass().newInstance();
|
113 |
} catch (InstantiationException | IllegalAccessException e) { |
114 |
throw new CopyParametersException("data parameters", e); |
115 |
} |
116 |
this.copyValuesTo(copy);
|
117 |
return copy;
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void saveToState(PersistentState state) throws PersistenceException { |
122 |
DynField[] fields = getDelegatedDynObject().getDynClass().getDynFields();
|
123 |
|
124 |
for (DynField field : fields) {
|
125 |
if (field.isPersistent()) {
|
126 |
String name = field.getName();
|
127 |
Object value = this.getDynValue(name); |
128 |
state.set(name, value); |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void loadFromState(PersistentState state) throws PersistenceException { |
135 |
@SuppressWarnings("rawtypes") |
136 |
Iterator it = state.getNames();
|
137 |
while (it.hasNext()) {
|
138 |
String name = (String) it.next(); |
139 |
try {
|
140 |
Object value = state.get(name);
|
141 |
this.setDynValue(name, value);
|
142 |
} catch (Throwable t) { |
143 |
logger.warn("Can't load '" + name + "' property", t); |
144 |
} |
145 |
} |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public void delegate(DynObject dynObject) { |
150 |
getDelegatedDynObject().delegate(dynObject); |
151 |
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
public DynClass getDynClass() {
|
156 |
return getDelegatedDynObject().getDynClass();
|
157 |
} |
158 |
|
159 |
@Override
|
160 |
public boolean hasDynValue(String name) { |
161 |
return getDelegatedDynObject().hasDynValue(name);
|
162 |
} |
163 |
|
164 |
@Override
|
165 |
public void implement(DynClass dynClass) { |
166 |
getDelegatedDynObject().implement(dynClass); |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public Object invokeDynMethod(String name, Object[] args) |
171 |
throws DynMethodException {
|
172 |
return getDelegatedDynObject().invokeDynMethod(this, name, args); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public Object invokeDynMethod(int code, Object[] args) |
177 |
throws DynMethodException {
|
178 |
return getDelegatedDynObject().invokeDynMethod(this, code, args); |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public void validate() throws ValidateDataParametersException { |
183 |
try {
|
184 |
this.getDynClass().validate(this); |
185 |
} catch (DynObjectValidateException e) {
|
186 |
throw new ValidateDataParametersException(e); |
187 |
} |
188 |
} |
189 |
|
190 |
protected void loadPRJ(File file, String parameterName) { |
191 |
File prjFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + ".prj"); |
192 |
if (prjFile.exists()) {
|
193 |
try {
|
194 |
String contentFile = FileUtils.readFileToString(prjFile);
|
195 |
if (StringUtils.isNotEmpty(contentFile)) {
|
196 |
IProjection crs = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, contentFile); |
197 |
this.setDynValue(parameterName, crs);
|
198 |
} |
199 |
} catch (IOException e) { |
200 |
logger.warn("Couldn't read prj file ''{}''", prjFile.getAbsolutePath());
|
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
protected void loadWLD(File file, String parameterName) { |
206 |
File wldFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + ".wld"); |
207 |
if (wldFile.exists()) {
|
208 |
try {
|
209 |
List<String> lines = FileUtils.readLines(wldFile); |
210 |
if (lines != null && lines.size() == 6) { |
211 |
this.setDynValue(parameterName, lines);
|
212 |
} |
213 |
|
214 |
} catch (IOException e) { |
215 |
logger.warn("Couldn't read wld file ''{}''", wldFile.getAbsolutePath());
|
216 |
} |
217 |
} |
218 |
} |
219 |
|
220 |
/**
|
221 |
* Returns an instance of the {@link DynObject} to delegate to.
|
222 |
*
|
223 |
* @return the delegate {@link DynObject}
|
224 |
*/
|
225 |
protected abstract DelegatedDynObject getDelegatedDynObject(); |
226 |
|
227 |
} |