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 @ 40596
History | View | Annotate | Download (4.87 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 java.util.Arrays; |
28 |
import java.util.Iterator; |
29 |
|
30 |
import org.gvsig.fmap.dal.DataParameters; |
31 |
import org.gvsig.fmap.dal.exception.CopyParametersException; |
32 |
import org.gvsig.fmap.dal.exception.ParameterMissingException; |
33 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
34 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
35 |
import org.gvsig.tools.dynobject.DynClass; |
36 |
import org.gvsig.tools.dynobject.DynField; |
37 |
import org.gvsig.tools.dynobject.DynObject; |
38 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
39 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
40 |
import org.gvsig.tools.persistence.PersistentState; |
41 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
42 |
|
43 |
/**
|
44 |
* @author jmvivo
|
45 |
*
|
46 |
*/
|
47 |
public abstract class AbstractDataParameters implements DataParameters { |
48 |
|
49 |
public Object getDynValue(String name) { |
50 |
return getDelegatedDynObject().getDynValue(name);
|
51 |
} |
52 |
|
53 |
public String toString() { |
54 |
return getDelegatedDynObject().toString();
|
55 |
} |
56 |
|
57 |
public void setDynValue(String name, Object value) { |
58 |
getDelegatedDynObject().setDynValue(name, value); |
59 |
// return this ???
|
60 |
} |
61 |
|
62 |
public void clear() { |
63 |
// TODO Delegar en el DynObject cuando tenga este servicio
|
64 |
|
65 |
DynField[] fields =
|
66 |
getDelegatedDynObject().getDynClass() |
67 |
.getDeclaredDynFields(); |
68 |
|
69 |
for (int i = 0; i < fields.length; i++) { |
70 |
this.setDynValue(fields[i].getName(), fields[i].getDefaultValue());
|
71 |
} |
72 |
} |
73 |
|
74 |
protected void copyValuesTo(AbstractDataParameters target) { |
75 |
// TODO Delegar en el DynObject cuando tenga este servicio
|
76 |
DynField[] fields =
|
77 |
getDelegatedDynObject().getDynClass() |
78 |
.getDynFields(); |
79 |
|
80 |
|
81 |
for (int i = 0; i < fields.length; i++) { |
82 |
target.setDynValue(fields[i].getName(), this.getDynValue(fields[i]
|
83 |
.getName())); |
84 |
} |
85 |
} |
86 |
|
87 |
public DataParameters getCopy() {
|
88 |
// TODO Delegar en el DynObject cuando tenga este servicio
|
89 |
AbstractDataParameters copy; |
90 |
try {
|
91 |
copy = (AbstractDataParameters) this.getClass().newInstance();
|
92 |
} catch (InstantiationException e) { |
93 |
throw new CopyParametersException("data parameters", e); |
94 |
} catch (IllegalAccessException e) { |
95 |
throw new CopyParametersException("data parameters", e); |
96 |
} |
97 |
this.copyValuesTo(copy);
|
98 |
return copy;
|
99 |
} |
100 |
|
101 |
public void saveToState(PersistentState state) throws PersistenceException { |
102 |
DynField[] fields =
|
103 |
getDelegatedDynObject().getDynClass().getDynFields(); |
104 |
|
105 |
for (int i = 0; i < fields.length; i++) { |
106 |
if (fields[i].isPersistent()) {
|
107 |
String name = fields[i].getName();
|
108 |
Object value = this.getDynValue(fields[i].getName()); |
109 |
state.set(name,value); |
110 |
} |
111 |
} |
112 |
} |
113 |
|
114 |
public void loadFromState(PersistentState state) throws PersistenceException { |
115 |
Iterator it = state.getNames();
|
116 |
while (it.hasNext()) {
|
117 |
String name = (String) it.next(); |
118 |
this.setDynValue(name, state.get(name));
|
119 |
} |
120 |
} |
121 |
|
122 |
public void delegate(DynObject dynObject) { |
123 |
getDelegatedDynObject().delegate(dynObject); |
124 |
|
125 |
} |
126 |
|
127 |
public DynClass getDynClass() {
|
128 |
return getDelegatedDynObject().getDynClass();
|
129 |
} |
130 |
|
131 |
public boolean hasDynValue(String name) { |
132 |
return getDelegatedDynObject().hasDynValue(name);
|
133 |
} |
134 |
|
135 |
public void implement(DynClass dynClass) { |
136 |
getDelegatedDynObject().implement(dynClass); |
137 |
} |
138 |
|
139 |
public Object invokeDynMethod(String name, DynObject context) |
140 |
throws DynMethodException {
|
141 |
return getDelegatedDynObject().invokeDynMethod(this, name, context); |
142 |
} |
143 |
|
144 |
public Object invokeDynMethod(int code, DynObject context) |
145 |
throws DynMethodException {
|
146 |
return getDelegatedDynObject().invokeDynMethod(this, code, context); |
147 |
} |
148 |
|
149 |
public void validate() throws ValidateDataParametersException { |
150 |
try {
|
151 |
this.getDynClass().validate(this); |
152 |
} catch (DynObjectValidateException e) {
|
153 |
throw new ValidateDataParametersException(e); |
154 |
} |
155 |
} |
156 |
|
157 |
/**
|
158 |
* Returns an instance of the {@link DynObject} to delegate to.
|
159 |
*
|
160 |
* @return the delegate {@link DynObject}
|
161 |
*/
|
162 |
protected abstract DelegatedDynObject getDelegatedDynObject(); |
163 |
|
164 |
} |