Statistics
| Revision:

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

History | View | Annotate | Download (5.25 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.PersistenceException;
16
import org.gvsig.tools.persistence.PersistenceValueNotFoundException;
17
import org.gvsig.tools.persistence.PersistentState;
18

    
19

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

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

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

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

    
61
        public void setDynValue(String name, Object value) {
62
                this.delegatedDynObject.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 = delegatedDynObject.getDynClass()
70
                                .getDeclaredDynFields();
71

    
72
                for (int i = 0; i < fields.length; i++) {
73
                        this.setDynValue(fields[i].getName(), fields[i].getDefaultValue());
74
                }
75
        }
76

    
77
        protected void copyValuesTo(AbstractDataParameters target) {
78
                // TODO Delegar en el DynObject cuando tenga este servicio
79
                DynField[] fields = delegatedDynObject.getDynClass()
80
                                .getDynFields();
81

    
82

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

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

    
103
        public void saveToState(PersistentState state) throws PersistenceException {
104
                DynField[] fields = delegatedDynObject.getDynClass()
105
                                .getDeclaredDynFields();
106

    
107
                for (int i = 0; i < fields.length; i++) {
108
                        state.set(fields[i].getName(), this
109
                                        .getDynValue(fields[i].getName()));
110
                }
111
        }
112

    
113
        public void setState(PersistentState state) throws PersistenceException {
114
                Iterator it = state.getNames();
115
                while (it.hasNext()) {
116
                        String name = (String) it.next();
117
                        try {
118
                                this.setDynValue(name, state.get(name));
119
                        } catch (PersistenceValueNotFoundException e) {
120
                                // Ignore
121
                        }
122
                }
123
        }
124

    
125
        public void delegate(DynObject dynObject) {
126
                this.delegatedDynObject.delegate(dynObject);
127

    
128
        }
129

    
130
        public DynClass getDynClass() {
131
                return this.delegatedDynObject.getDynClass();
132
        }
133

    
134
        public boolean hasDynValue(String name) {
135
                return this.delegatedDynObject.hasDynValue(name);
136
        }
137

    
138
        public void implement(DynClass dynClass) {
139
                this.delegatedDynObject.implement(dynClass);
140
        }
141

    
142
        public Object invokeDynMethod(String name, DynObject context)
143
                        throws DynMethodException {
144
                return this.delegatedDynObject.invokeDynMethod(this, name, context);
145
        }
146

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

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

    
176
                }
177
                if (exception.size() > 0) {
178
                        throw exception;
179
                }
180

    
181
        }
182

    
183
}