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 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 42611 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 42611 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 42611 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
20 | 42611 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.fmap.dal.spi; |
||
24 | |||
25 | 43758 | jjdelcerro | import java.io.File; |
26 | import java.io.IOException; |
||
27 | 40435 | jjdelcerro | import java.util.Iterator; |
28 | 43758 | jjdelcerro | 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 | 40435 | jjdelcerro | |
36 | 42566 | fdiaz | import org.slf4j.Logger; |
37 | import org.slf4j.LoggerFactory; |
||
38 | |||
39 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.DataParameters; |
40 | import org.gvsig.fmap.dal.exception.CopyParametersException; |
||
41 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
42 | 42611 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
43 | 40435 | jjdelcerro | 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 | 42611 | jjdelcerro | import org.gvsig.tools.dynobject.DynObjectEncoder; |
48 | import org.gvsig.tools.dynobject.DynObjectManager; |
||
49 | 40435 | jjdelcerro | 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 | 42611 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(AbstractDataParameters.class); |
61 | 40435 | jjdelcerro | |
62 | 42611 | jjdelcerro | @Override
|
63 | public Object getDynValue(String name) { |
||
64 | return getDelegatedDynObject().getDynValue(name);
|
||
65 | } |
||
66 | 42103 | jjdelcerro | |
67 | 43400 | jjdelcerro | 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 | 42611 | jjdelcerro | @Override
|
76 | public String toString() { |
||
77 | DynObjectEncoder encoder = ToolsLocator.getDynObjectManager().createSimpleDynObjectEncoder(); |
||
78 | return encoder.encode(this); |
||
79 | } |
||
80 | 40435 | jjdelcerro | |
81 | 42611 | jjdelcerro | @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 | 40435 | jjdelcerro | |
96 | 42611 | jjdelcerro | @Override
|
97 | public void clear() { |
||
98 | DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
||
99 | manager.clear(this);
|
||
100 | } |
||
101 | 40435 | jjdelcerro | |
102 | 42611 | jjdelcerro | protected void copyValuesTo(AbstractDataParameters target) { |
103 | DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
||
104 | manager.copy(this, target);
|
||
105 | } |
||
106 | 40435 | jjdelcerro | |
107 | 42611 | jjdelcerro | @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 | 40435 | jjdelcerro | |
120 | 42611 | jjdelcerro | @Override
|
121 | public void saveToState(PersistentState state) throws PersistenceException { |
||
122 | DynField[] fields = getDelegatedDynObject().getDynClass().getDynFields();
|
||
123 | 40435 | jjdelcerro | |
124 | 42611 | jjdelcerro | 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 | 40435 | jjdelcerro | |
133 | 42611 | jjdelcerro | @Override
|
134 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
135 | @SuppressWarnings("rawtypes") |
||
136 | 42566 | fdiaz | Iterator it = state.getNames();
|
137 | 42611 | jjdelcerro | 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 | 40435 | jjdelcerro | |
148 | 42611 | jjdelcerro | @Override
|
149 | public void delegate(DynObject dynObject) { |
||
150 | getDelegatedDynObject().delegate(dynObject); |
||
151 | 40435 | jjdelcerro | |
152 | 42611 | jjdelcerro | } |
153 | 40435 | jjdelcerro | |
154 | 42611 | jjdelcerro | @Override
|
155 | public DynClass getDynClass() {
|
||
156 | return getDelegatedDynObject().getDynClass();
|
||
157 | } |
||
158 | 40435 | jjdelcerro | |
159 | 42611 | jjdelcerro | @Override
|
160 | public boolean hasDynValue(String name) { |
||
161 | return getDelegatedDynObject().hasDynValue(name);
|
||
162 | } |
||
163 | 40435 | jjdelcerro | |
164 | 42611 | jjdelcerro | @Override
|
165 | public void implement(DynClass dynClass) { |
||
166 | getDelegatedDynObject().implement(dynClass); |
||
167 | } |
||
168 | 40435 | jjdelcerro | |
169 | 42611 | jjdelcerro | @Override
|
170 | 42775 | jjdelcerro | public Object invokeDynMethod(String name, Object[] args) |
171 | 42611 | jjdelcerro | throws DynMethodException {
|
172 | 42775 | jjdelcerro | return getDelegatedDynObject().invokeDynMethod(this, name, args); |
173 | 42611 | jjdelcerro | } |
174 | 40435 | jjdelcerro | |
175 | 42611 | jjdelcerro | @Override
|
176 | 42775 | jjdelcerro | public Object invokeDynMethod(int code, Object[] args) |
177 | 42611 | jjdelcerro | throws DynMethodException {
|
178 | 42775 | jjdelcerro | return getDelegatedDynObject().invokeDynMethod(this, code, args); |
179 | 42611 | jjdelcerro | } |
180 | 40435 | jjdelcerro | |
181 | 42611 | jjdelcerro | @Override
|
182 | public void validate() throws ValidateDataParametersException { |
||
183 | try {
|
||
184 | 40435 | jjdelcerro | this.getDynClass().validate(this); |
185 | } catch (DynObjectValidateException e) {
|
||
186 | throw new ValidateDataParametersException(e); |
||
187 | } |
||
188 | 42611 | jjdelcerro | } |
189 | 40435 | jjdelcerro | |
190 | 43758 | jjdelcerro | 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 | 42611 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
227 | 40596 | jjdelcerro | } |