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 @ 42103
History | View | Annotate | Download (8.31 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
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.fmap.dal.spi; |
26 |
|
27 |
//import com.google.gson.stream.JsonReader;
|
28 |
//import com.google.gson.stream.JsonToken;
|
29 |
import java.io.StringWriter; |
30 |
import java.util.Iterator; |
31 |
|
32 |
import org.gvsig.fmap.dal.DataParameters; |
33 |
import org.gvsig.fmap.dal.exception.CopyParametersException; |
34 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
35 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
36 |
import org.gvsig.tools.dynobject.DynClass; |
37 |
import org.gvsig.tools.dynobject.DynField; |
38 |
import org.gvsig.tools.dynobject.DynObject; |
39 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
40 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
41 |
import org.gvsig.tools.persistence.PersistentState; |
42 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
//import com.google.gson.stream.JsonWriter;
|
46 |
//import java.io.IOException;
|
47 |
//import java.io.StringReader;
|
48 |
|
49 |
/**
|
50 |
* @author jmvivo
|
51 |
*
|
52 |
*/
|
53 |
public abstract class AbstractDataParameters implements DataParameters { |
54 |
private static final Logger logger = LoggerFactory.getLogger(AbstractDataParameters.class); |
55 |
|
56 |
public Object getDynValue(String name) { |
57 |
return getDelegatedDynObject().getDynValue(name);
|
58 |
} |
59 |
|
60 |
public String toString() { |
61 |
return getDelegatedDynObject().toString();
|
62 |
} |
63 |
|
64 |
// public boolean fromString(String values) {
|
65 |
// boolean ok = true;
|
66 |
// try {
|
67 |
// DynObject obj = getDelegatedDynObject();
|
68 |
// JsonReader reader = new JsonReader(new StringReader(values));
|
69 |
// reader.setLenient(true);
|
70 |
// reader.beginObject();
|
71 |
// String name = null;
|
72 |
// String value = null;
|
73 |
// while (reader.hasNext()) {
|
74 |
// name ="unknow";
|
75 |
// try {
|
76 |
// name = reader.nextName();
|
77 |
// if (reader.peek() == JsonToken.NULL) {
|
78 |
// value = null;
|
79 |
// } else {
|
80 |
// value = reader.nextString();
|
81 |
// }
|
82 |
// obj.setDynValue(name, value);
|
83 |
// } catch (Exception ex) {
|
84 |
// logger.warn("Can't restore parameter '"+name+"' from string "+values+".", ex);
|
85 |
// ok = false;
|
86 |
// }
|
87 |
// }
|
88 |
// reader.endObject();
|
89 |
// } catch (IOException ex) {
|
90 |
// logger.warn("Can't restore parameters from string "+values+".", ex);
|
91 |
// ok = false;
|
92 |
// }
|
93 |
// return ok;
|
94 |
// }
|
95 |
//
|
96 |
// public String toString() {
|
97 |
// try {
|
98 |
// DynObject obj = getDelegatedDynObject();
|
99 |
// DynClass dynClass = obj.getDynClass();
|
100 |
// DynField[] fields = dynClass.getDynFields();
|
101 |
// StringWriter swriter = new StringWriter();
|
102 |
// JsonWriter writer = new JsonWriter(swriter);
|
103 |
// writer.setSerializeNulls(true);
|
104 |
// writer.setLenient(true);
|
105 |
// writer.setHtmlSafe(false);
|
106 |
// writer.setIndent("");
|
107 |
// writer.beginObject();
|
108 |
// if ( fields != null && fields.length >0 ) {
|
109 |
// for ( int i = 0; i < fields.length; i++ ) {
|
110 |
// String fieldname = fields[i].getName();
|
111 |
// Object value = obj.getDynValue(fieldname);
|
112 |
// writer.name(fieldname);
|
113 |
// if( value == null ) {
|
114 |
// writer.value((String)null);
|
115 |
// } else {
|
116 |
// writer.value(value.toString());
|
117 |
// }
|
118 |
// }
|
119 |
// }
|
120 |
// writer.endObject();
|
121 |
// writer.close();
|
122 |
// return swriter.toString();
|
123 |
// } catch (IOException ex) {
|
124 |
// logger.warn("Can't serializar DataParamerters correctly.",ex);
|
125 |
// return getDelegatedDynObject().toString();
|
126 |
// }
|
127 |
// }
|
128 |
//
|
129 |
public void setDynValue(String name, Object value) { |
130 |
DelegatedDynObject delegated = getDelegatedDynObject(); |
131 |
if (delegated.getDynClass().getDynField(name)!=null){ |
132 |
delegated.setDynValue(name, value); |
133 |
} else {
|
134 |
try {
|
135 |
throw new IllegalArgumentException(name); |
136 |
} catch(IllegalArgumentException ex) { |
137 |
logger.warn("Attribute '"+name+"' is not defined in " |
138 |
+delegated.getDynClass().getFullName()+ " definition",ex);
|
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
public void clear() { |
144 |
// TODO Delegar en el DynObject cuando tenga este servicio
|
145 |
|
146 |
DynField[] fields =
|
147 |
getDelegatedDynObject().getDynClass() |
148 |
.getDeclaredDynFields(); |
149 |
|
150 |
for (int i = 0; i < fields.length; i++) { |
151 |
this.setDynValue(fields[i].getName(), fields[i].getDefaultValue());
|
152 |
} |
153 |
} |
154 |
|
155 |
protected void copyValuesTo(AbstractDataParameters target) { |
156 |
// TODO Delegar en el DynObject cuando tenga este servicio
|
157 |
DynField[] fields =
|
158 |
getDelegatedDynObject().getDynClass() |
159 |
.getDynFields(); |
160 |
|
161 |
|
162 |
for (int i = 0; i < fields.length; i++) { |
163 |
target.setDynValue(fields[i].getName(), this.getDynValue(fields[i]
|
164 |
.getName())); |
165 |
} |
166 |
} |
167 |
|
168 |
public DataParameters getCopy() {
|
169 |
// TODO Delegar en el DynObject cuando tenga este servicio
|
170 |
AbstractDataParameters copy; |
171 |
try {
|
172 |
copy = (AbstractDataParameters) this.getClass().newInstance();
|
173 |
} catch (InstantiationException e) { |
174 |
throw new CopyParametersException("data parameters", e); |
175 |
} catch (IllegalAccessException e) { |
176 |
throw new CopyParametersException("data parameters", e); |
177 |
} |
178 |
this.copyValuesTo(copy);
|
179 |
return copy;
|
180 |
} |
181 |
|
182 |
public void saveToState(PersistentState state) throws PersistenceException { |
183 |
DynField[] fields =
|
184 |
getDelegatedDynObject().getDynClass().getDynFields(); |
185 |
|
186 |
for (int i = 0; i < fields.length; i++) { |
187 |
if (fields[i].isPersistent()) {
|
188 |
String name = fields[i].getName();
|
189 |
Object value = this.getDynValue(fields[i].getName()); |
190 |
state.set(name,value); |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
public void loadFromState(PersistentState state) throws PersistenceException { |
196 |
Iterator it = state.getNames();
|
197 |
while (it.hasNext()) {
|
198 |
String name = (String) it.next(); |
199 |
this.setDynValue(name, state.get(name));
|
200 |
} |
201 |
} |
202 |
|
203 |
public void delegate(DynObject dynObject) { |
204 |
getDelegatedDynObject().delegate(dynObject); |
205 |
|
206 |
} |
207 |
|
208 |
public DynClass getDynClass() {
|
209 |
return getDelegatedDynObject().getDynClass();
|
210 |
} |
211 |
|
212 |
public boolean hasDynValue(String name) { |
213 |
return getDelegatedDynObject().hasDynValue(name);
|
214 |
} |
215 |
|
216 |
public void implement(DynClass dynClass) { |
217 |
getDelegatedDynObject().implement(dynClass); |
218 |
} |
219 |
|
220 |
public Object invokeDynMethod(String name, DynObject context) |
221 |
throws DynMethodException {
|
222 |
return getDelegatedDynObject().invokeDynMethod(this, name, context); |
223 |
} |
224 |
|
225 |
public Object invokeDynMethod(int code, DynObject context) |
226 |
throws DynMethodException {
|
227 |
return getDelegatedDynObject().invokeDynMethod(this, code, context); |
228 |
} |
229 |
|
230 |
public void validate() throws ValidateDataParametersException { |
231 |
try {
|
232 |
this.getDynClass().validate(this); |
233 |
} catch (DynObjectValidateException e) {
|
234 |
throw new ValidateDataParametersException(e); |
235 |
} |
236 |
} |
237 |
|
238 |
/**
|
239 |
* Returns an instance of the {@link DynObject} to delegate to.
|
240 |
*
|
241 |
* @return the delegate {@link DynObject}
|
242 |
*/
|
243 |
protected abstract DelegatedDynObject getDelegatedDynObject(); |
244 |
|
245 |
} |