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