Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / spi / AbstractDataParameters.java @ 27723

History | View | Annotate | Download (5.43 KB)

1
package org.gvsig.fmap.dal.spi;
2

    
3
import java.util.Arrays;
4
import java.util.Iterator;
5

    
6
import org.gvsig.fmap.dal.DataParameters;
7
import org.gvsig.fmap.dal.exception.CopyParametersException;
8
import org.gvsig.fmap.dal.exception.ParameterMissingException;
9
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
10
import org.gvsig.tools.dynobject.DelegatedDynObject;
11
import org.gvsig.tools.dynobject.DynClass;
12
import org.gvsig.tools.dynobject.DynField;
13
import org.gvsig.tools.dynobject.DynObject;
14
import org.gvsig.tools.dynobject.exception.DynMethodException;
15
import org.gvsig.tools.persistence.AbstractPersistenceManager;
16
import org.gvsig.tools.persistence.PersistenceException;
17
import org.gvsig.tools.persistence.PersistenceValueNotFoundException;
18
import org.gvsig.tools.persistence.PersistentState;
19

    
20

    
21
/* gvSIG. Geographic Information System of the Valencian Government
22
 *
23
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
24
 * of the Valencian Government (CIT)
25
 *
26
 * This program is free software; you can redistribute it and/or
27
 * modify it under the terms of the GNU General Public License
28
 * as published by the Free Software Foundation; either version 2
29
 * of the License, or (at your option) any later version.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program; if not, write to the Free Software
38
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
39
 * MA  02110-1301, USA.
40
 *
41
 */
42

    
43
/*
44
 * AUTHORS (In addition to CIT):
45
 * 2008 IVER T.I. S.A.   {{Task}}
46
 */
47

    
48
/**
49
 *
50
 */
51
/**
52
 * @author jmvivo
53
 *
54
 */
55
public abstract class AbstractDataParameters implements DataParameters {
56
        protected DelegatedDynObject delegatedDynObject;
57

    
58
        public Object getDynValue(String name) {
59
                return this.delegatedDynObject.getDynValue(name);
60
        }
61

    
62
        public void setDynValue(String name, Object value) {
63
                this.delegatedDynObject.setDynValue(name, value);
64
                // return this ???
65
        }
66

    
67
        public void clear() {
68
                // TODO Delegar en el DynObject cuando tenga este servicio
69

    
70
                DynField[] fields = delegatedDynObject.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 = delegatedDynObject.getDynClass()
81
                                .getDynFields();
82

    
83

    
84
                for (int i = 0; i < fields.length; i++) {
85
                        target.setDynValue(fields[i].getName(), this.getDynValue(fields[i]
86
                                        .getName()));
87
                }
88
        }
89

    
90
        public DataParameters getCopy() {
91
                // TODO Delegar en el DynObject cuando tenga este servicio
92
                AbstractDataParameters copy;
93
                try {
94
                        copy = (AbstractDataParameters) this.getClass().newInstance();
95
                } catch (InstantiationException e) {
96
                        throw new CopyParametersException("data parameters", e);
97
                } catch (IllegalAccessException e) {
98
                        throw new CopyParametersException("data parameters", e);
99
                }
100
                this.copyValuesTo(copy);
101
                return copy;
102
        }
103

    
104
        public PersistentState getState() throws PersistenceException {
105
                return AbstractPersistenceManager.getState(this);
106
        }
107

    
108
        public void loadState(PersistentState state) throws PersistenceException {
109
                DynField[] fields = delegatedDynObject.getDynClass()
110
                                .getDeclaredDynFields();
111

    
112
                for (int i = 0; i < fields.length; i++) {
113
                        state.set(fields[i].getName(), this
114
                                        .getDynValue(fields[i].getName()));
115
                }
116
        }
117

    
118
        public void setState(PersistentState state) throws PersistenceException {
119
                Iterator it = state.getNames();
120
                while (it.hasNext()) {
121
                        String name = (String) it.next();
122
                        try {
123
                                this.setDynValue(name, state.get(name));
124
                        } catch (PersistenceValueNotFoundException e) {
125
                                // Ignore
126
                        }
127
                }
128
        }
129

    
130
        public void delegate(DynObject dynObject) {
131
                this.delegatedDynObject.delegate(dynObject);
132

    
133
        }
134

    
135
        public DynClass getDynClass() {
136
                return this.delegatedDynObject.getDynClass();
137
        }
138

    
139
        public boolean hasDynValue(String name) {
140
                return this.delegatedDynObject.hasDynValue(name);
141
        }
142

    
143
        public void implement(DynClass dynClass) {
144
                this.delegatedDynObject.implement(dynClass);
145
        }
146

    
147
        public Object invokeDynMethod(String name, DynObject context)
148
                        throws DynMethodException {
149
                return this.delegatedDynObject.invokeDynMethod(this, name, context);
150
        }
151

    
152
        public Object invokeDynMethod(int code, DynObject context)
153
                        throws DynMethodException {
154
                return this.delegatedDynObject.invokeDynMethod(this, code, context);
155
        }
156

    
157
        public void validate() throws ValidateDataParametersException {
158
                ValidateDataParametersException exception = new ValidateDataParametersException();
159
                Iterator iter = Arrays.asList(this.getDynClass().getDynFields())
160
                                .iterator();
161
                DynField field;
162
                while (iter.hasNext()) {
163
                        field = (DynField) iter.next();
164
                        if (field.isMandatory()) {
165
                                if (!this.hasDynValue(field.getName())) {
166
                                        if (field.getDefaultValue() != null) {
167
                                                this.setDynValue(field.getName(), field
168
                                                                .getDefaultValue());
169
                                        } else {
170
                                                exception.add(new ParameterMissingException(field
171
                                                                .getName()));
172
                                        }
173
                                }
174
                        } else {
175
                                if (field.getDefaultValue() != null
176
                                                && !this.hasDynValue(field.getName())) {
177
                                        this.setDynValue(field.getName(), field.getDefaultValue());
178
                                }
179
                        }
180

    
181
                }
182
                if (exception.size() > 0) {
183
                        throw exception;
184
                }
185

    
186
        }
187

    
188
}