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