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 @ 43400
History | View | Annotate | Download (6.07 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.util.Iterator; |
26 |
|
27 |
import org.slf4j.Logger; |
28 |
import org.slf4j.LoggerFactory; |
29 |
|
30 |
import org.gvsig.fmap.dal.DataParameters; |
31 |
import org.gvsig.fmap.dal.exception.CopyParametersException; |
32 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
33 |
import org.gvsig.tools.ToolsLocator; |
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.DynObjectEncoder; |
39 |
import org.gvsig.tools.dynobject.DynObjectManager; |
40 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
41 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
42 |
import org.gvsig.tools.persistence.PersistentState; |
43 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
44 |
|
45 |
/**
|
46 |
* @author jmvivo
|
47 |
*
|
48 |
*/
|
49 |
public abstract class AbstractDataParameters implements DataParameters { |
50 |
|
51 |
private static final Logger logger = LoggerFactory.getLogger(AbstractDataParameters.class); |
52 |
|
53 |
@Override
|
54 |
public Object getDynValue(String name) { |
55 |
return getDelegatedDynObject().getDynValue(name);
|
56 |
} |
57 |
|
58 |
public String getDataStoreName() { |
59 |
return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
60 |
} |
61 |
|
62 |
public String getDescription() { |
63 |
return this.getDynClass().getDescription(); |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public String toString() { |
68 |
DynObjectEncoder encoder = ToolsLocator.getDynObjectManager().createSimpleDynObjectEncoder(); |
69 |
return encoder.encode(this); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public void setDynValue(String name, Object value) { |
74 |
DelegatedDynObject delegated = getDelegatedDynObject(); |
75 |
if (delegated.getDynClass().getDynField(name) != null) { |
76 |
delegated.setDynValue(name, value); |
77 |
} else {
|
78 |
try {
|
79 |
throw new IllegalArgumentException(name); |
80 |
} catch (IllegalArgumentException ex) { |
81 |
logger.warn("Attribute '" + name + "' is not defined in " |
82 |
+ delegated.getDynClass().getFullName() + " definition", ex);
|
83 |
} |
84 |
} |
85 |
} |
86 |
|
87 |
@Override
|
88 |
public void clear() { |
89 |
DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
90 |
manager.clear(this);
|
91 |
} |
92 |
|
93 |
protected void copyValuesTo(AbstractDataParameters target) { |
94 |
DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
95 |
manager.copy(this, target);
|
96 |
} |
97 |
|
98 |
@Override
|
99 |
public DataParameters getCopy() {
|
100 |
// TODO Delegar en el DynObject cuando tenga este servicio
|
101 |
AbstractDataParameters copy; |
102 |
try {
|
103 |
copy = (AbstractDataParameters) this.getClass().newInstance();
|
104 |
} catch (InstantiationException | IllegalAccessException e) { |
105 |
throw new CopyParametersException("data parameters", e); |
106 |
} |
107 |
this.copyValuesTo(copy);
|
108 |
return copy;
|
109 |
} |
110 |
|
111 |
@Override
|
112 |
public void saveToState(PersistentState state) throws PersistenceException { |
113 |
DynField[] fields = getDelegatedDynObject().getDynClass().getDynFields();
|
114 |
|
115 |
for (DynField field : fields) {
|
116 |
if (field.isPersistent()) {
|
117 |
String name = field.getName();
|
118 |
Object value = this.getDynValue(name); |
119 |
state.set(name, value); |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void loadFromState(PersistentState state) throws PersistenceException { |
126 |
@SuppressWarnings("rawtypes") |
127 |
Iterator it = state.getNames();
|
128 |
while (it.hasNext()) {
|
129 |
String name = (String) it.next(); |
130 |
try {
|
131 |
Object value = state.get(name);
|
132 |
this.setDynValue(name, value);
|
133 |
} catch (Throwable t) { |
134 |
logger.warn("Can't load '" + name + "' property", t); |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void delegate(DynObject dynObject) { |
141 |
getDelegatedDynObject().delegate(dynObject); |
142 |
|
143 |
} |
144 |
|
145 |
@Override
|
146 |
public DynClass getDynClass() {
|
147 |
return getDelegatedDynObject().getDynClass();
|
148 |
} |
149 |
|
150 |
@Override
|
151 |
public boolean hasDynValue(String name) { |
152 |
return getDelegatedDynObject().hasDynValue(name);
|
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void implement(DynClass dynClass) { |
157 |
getDelegatedDynObject().implement(dynClass); |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public Object invokeDynMethod(String name, Object[] args) |
162 |
throws DynMethodException {
|
163 |
return getDelegatedDynObject().invokeDynMethod(this, name, args); |
164 |
} |
165 |
|
166 |
@Override
|
167 |
public Object invokeDynMethod(int code, Object[] args) |
168 |
throws DynMethodException {
|
169 |
return getDelegatedDynObject().invokeDynMethod(this, code, args); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public void validate() throws ValidateDataParametersException { |
174 |
try {
|
175 |
this.getDynClass().validate(this); |
176 |
} catch (DynObjectValidateException e) {
|
177 |
throw new ValidateDataParametersException(e); |
178 |
} |
179 |
} |
180 |
|
181 |
/**
|
182 |
* Returns an instance of the {@link DynObject} to delegate to.
|
183 |
*
|
184 |
* @return the delegate {@link DynObject}
|
185 |
*/
|
186 |
protected abstract DelegatedDynObject getDelegatedDynObject(); |
187 |
|
188 |
} |