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