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 @ 44307
History | View | Annotate | Download (8.28 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(DataParameters target) { |
106 |
DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
107 |
manager.copy(this, target);
|
108 |
} |
109 |
|
110 |
@Override
|
111 |
public DataParameters getCopy() {
|
112 |
DataParameters copy; |
113 |
try {
|
114 |
if( this instanceof Cloneable ) { |
115 |
copy = (DataParameters) this.clone();
|
116 |
} else {
|
117 |
copy = (DataParameters) this.getClass().newInstance();
|
118 |
this.copyValuesTo(copy);
|
119 |
} |
120 |
} catch (Exception e) { |
121 |
throw new CopyParametersException("data parameters", e); |
122 |
} |
123 |
return copy;
|
124 |
} |
125 |
|
126 |
@Override
|
127 |
public void saveToState(PersistentState state) throws PersistenceException { |
128 |
DynField[] fields = getDelegatedDynObject().getDynClass().getDynFields();
|
129 |
|
130 |
for (DynField field : fields) {
|
131 |
if (field.isPersistent()) {
|
132 |
String name = field.getName();
|
133 |
Object value = this.getDynValue(name); |
134 |
state.set(name, value); |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void loadFromState(PersistentState state) throws PersistenceException { |
141 |
@SuppressWarnings("rawtypes") |
142 |
Iterator it = state.getNames();
|
143 |
while (it.hasNext()) {
|
144 |
String name = (String) it.next(); |
145 |
try {
|
146 |
Object value = state.get(name);
|
147 |
this.setDynValue(name, value);
|
148 |
} catch (Throwable t) { |
149 |
LOGGER.warn("Can't load '" + name + "' property", t); |
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public void delegate(DynObject dynObject) { |
156 |
getDelegatedDynObject().delegate(dynObject); |
157 |
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public DynClass getDynClass() {
|
162 |
return getDelegatedDynObject().getDynClass();
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
public boolean hasDynValue(String name) { |
167 |
return getDelegatedDynObject().hasDynValue(name);
|
168 |
} |
169 |
|
170 |
@Override
|
171 |
public void implement(DynClass dynClass) { |
172 |
getDelegatedDynObject().implement(dynClass); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public Object invokeDynMethod(String name, Object[] args) |
177 |
throws DynMethodException {
|
178 |
return getDelegatedDynObject().invokeDynMethod(this, name, args); |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public Object invokeDynMethod(int code, Object[] args) |
183 |
throws DynMethodException {
|
184 |
return getDelegatedDynObject().invokeDynMethod(this, code, args); |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public void validate() throws ValidateDataParametersException { |
189 |
try {
|
190 |
this.getDynClass().validate(this); |
191 |
} catch (DynObjectValidateException e) {
|
192 |
throw new ValidateDataParametersException(e); |
193 |
} |
194 |
} |
195 |
|
196 |
protected void loadPRJ(File file, String parameterName) { |
197 |
File prjFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + ".prj"); |
198 |
if (prjFile.exists()) {
|
199 |
try {
|
200 |
String contentFile = FileUtils.readFileToString(prjFile);
|
201 |
if (StringUtils.isNotEmpty(contentFile)) {
|
202 |
IProjection crs = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, contentFile); |
203 |
this.setDynValue(parameterName, crs);
|
204 |
} |
205 |
} catch (IOException e) { |
206 |
LOGGER.warn("Couldn't read prj file ''{}''", prjFile.getAbsolutePath());
|
207 |
} |
208 |
} |
209 |
} |
210 |
|
211 |
protected void loadWLD(File file, String parameterName) { |
212 |
File wldFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + ".wld"); |
213 |
if (wldFile.exists()) {
|
214 |
try {
|
215 |
List<String> lines = FileUtils.readLines(wldFile); |
216 |
if (lines != null && lines.size() == 6) { |
217 |
this.setDynValue(parameterName, lines);
|
218 |
} |
219 |
|
220 |
} catch (IOException e) { |
221 |
LOGGER.warn("Couldn't read wld file ''{}''", wldFile.getAbsolutePath());
|
222 |
} |
223 |
} |
224 |
} |
225 |
|
226 |
@Override
|
227 |
public byte[] toByteArray() { |
228 |
try {
|
229 |
PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
230 |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
231 |
PersistentState state = persistenceManager.getState(this);
|
232 |
persistenceManager.saveState(state, stream); |
233 |
return stream.toByteArray();
|
234 |
} catch (Exception ex) { |
235 |
LOGGER.warn("Can't get byte[] from parameters.",ex);
|
236 |
return null; |
237 |
} |
238 |
} |
239 |
|
240 |
/**
|
241 |
* Returns an instance of the {@link DynObject} to delegate to.
|
242 |
*
|
243 |
* @return the delegate {@link DynObject}
|
244 |
*/
|
245 |
protected abstract DelegatedDynObject getDelegatedDynObject(); |
246 |
|
247 |
} |