Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / spi / AbstractDataParameters.java @ 24496

History | View | Annotate | Download (6.79 KB)

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

    
3
import java.util.HashMap;
4
import java.util.Iterator;
5
import java.util.Map;
6

    
7
import org.gvsig.fmap.dal.DataParameterDescriptor;
8
import org.gvsig.fmap.dal.DataParameters;
9
import org.gvsig.fmap.dal.exceptions.CopyParametersException;
10
import org.gvsig.tools.persistence.AbstractPersistenceManager;
11
import org.gvsig.tools.persistence.PersistenceException;
12
import org.gvsig.tools.persistence.PersistenceValueNotFoundException;
13
import org.gvsig.tools.persistence.PersistentState;
14

    
15

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

    
38
/*
39
 * AUTHORS (In addition to CIT):
40
 * 2008 IVER T.I. S.A.   {{Task}}
41
 */
42

    
43
/**
44
 *
45
 */
46
/**
47
 * @author jmvivo
48
 *
49
 */
50
public abstract class AbstractDataParameters implements DataParameters {
51
        private Map params;
52
        private Map alias;
53

    
54
        public class DefaultDataParameter implements DataParameterDescriptor {
55
                private int dataType;
56
                private String name;
57
                private Object defaultValue;
58

    
59
                private int availableValuesType;
60
                private Object[] availableValues;
61
                private Object minValue;
62
                private Object maxValue;
63
                Object value;
64
                private String description;
65

    
66
                DefaultDataParameter(String name, int dataType, String description,
67
                                Object defaultValue) {
68
                        this.name = name;
69
                        this.description = description;
70
                        this.dataType = dataType;
71
                        this.defaultValue = defaultValue;
72
                        this.availableValuesType = SINGLE;
73

    
74
                        this.minValue = null;
75
                        this.maxValue = null;
76
                        this.availableValues = null;
77

    
78
                        this.value = defaultValue;
79

    
80
                }
81

    
82
                DefaultDataParameter(String name, int dataType, String description,
83
                                Object defaultValue, Object[] avaliableValues) {
84
                        this.name = name;
85
                        this.description = description;
86
                        this.dataType = dataType;
87
                        this.defaultValue = defaultValue;
88
                        this.availableValuesType = CHOICE;
89
                        this.availableValues = avaliableValues;
90
                        this.minValue = null;
91
                        this.maxValue = null;
92

    
93
                        this.value = defaultValue;
94
                }
95

    
96
                DefaultDataParameter(String name, int dataType, String description,
97
                                Object defaultValue, Object minValue, Object maxValue) {
98
                        this.name = name;
99
                        this.description = description;
100
                        this.dataType = dataType;
101
                        this.defaultValue = defaultValue;
102
                        this.availableValuesType = RANGE;
103
                        this.availableValues = null;
104
                        this.minValue = minValue;
105
                        this.maxValue = maxValue;
106

    
107
                        this.value = defaultValue;
108
                }
109

    
110
                public Object[] getAvailableValues() {
111
                        return this.availableValues;
112
                }
113

    
114
                public Object getDefaultValue() {
115
                        return this.defaultValue;
116
                }
117

    
118
                public String getName() {
119
                        return this.name;
120
                }
121

    
122
                public int getDataType() {
123
                        return this.dataType;
124
                }
125

    
126
                void clear() {
127
                        value = defaultValue;
128
                }
129

    
130
                Object getValue() {
131
                        return value;
132
                }
133

    
134
                void setValue(Object value) {
135
                        // FIXME: hacer comprobaciones de tipos.
136
                        this.value = value;
137
                }
138

    
139
                public int getAvailableValuesType() {
140
                        return this.availableValuesType;
141
                }
142

    
143
                public String getDescription() {
144
                        return this.description;
145
                }
146

    
147
                public Object getMaxValue() {
148
                        return this.maxValue;
149
                }
150

    
151
                public Object getMinValue() {
152
                        return this.minValue;
153
                }
154
        }
155

    
156
        public AbstractDataParameters() {
157
                this.params = new HashMap();
158
                this.alias = new HashMap();
159
        }
160

    
161
        protected DataParameterDescriptor addParameter(String name, int dataType,
162
                        String description, Object defaultValue) {
163
                this.params.put(name.toLowerCase(), new DefaultDataParameter(name,
164
                                dataType,
165
                                description, defaultValue));
166
                return null;
167
        }
168

    
169
        protected DataParameterDescriptor addParameter(String name, int dataType,
170
                        String description, Object defaultValue, Object[] avaliableValues) {
171
                this.params.put(name.toLowerCase(), new DefaultDataParameter(name,
172
                                dataType,
173
                                description,
174
                                defaultValue, avaliableValues));
175
                return null;
176
        }
177

    
178
        protected DataParameterDescriptor addParameter(String name, int dataType,
179
                        String description, Object defaultValue, Object minValue,
180
                        Object maxValue) {
181
                this.params.put(name, new DefaultDataParameter(name, dataType,
182
                                description, defaultValue, minValue, maxValue));
183
                return null;
184
        }
185

    
186
        public DataParameterDescriptor addAttributeAlias(String name, String alias) {
187
                Object param = this.params.get(name);
188
                if( param == null  ){
189
                        return null ;
190
                }
191
                this.alias.put(alias, param);
192
                return (DataParameterDescriptor) param;
193
        }
194

    
195
        public Object getAttribute(String name) {
196
                return ((DefaultDataParameter) this.params.get(name.toLowerCase()))
197
                                .getValue();
198
        }
199

    
200
        public DataParameters setAttribute(String name, Object value) {
201
                ((DefaultDataParameter) this.params.get(name.toLowerCase()))
202
                                .setValue(value);
203
                return this;
204
        }
205

    
206
        public void clear() {
207
                Iterator it = params.values().iterator();
208
                while (it.hasNext()) {
209
                        ((DefaultDataParameter) it.next()).clear();
210
                }
211
        }
212

    
213
        public Iterator getAttributes() {
214
                return params.values().iterator();
215
        }
216

    
217
        protected void copyValuesTo(AbstractDataParameters target) {
218
                Iterator it = params.values().iterator();
219
                while (it.hasNext()) {
220
                        DefaultDataParameter p = ((DefaultDataParameter) it.next());
221
                        target.setAttribute(p.getName(), p.getValue());
222
                }
223
        }
224

    
225
        public DataParameters getCopy() {
226
                AbstractDataParameters copy;
227
                try {
228
                        copy = (AbstractDataParameters) this.getClass().newInstance();
229
                } catch (InstantiationException e) {
230
                        throw new CopyParametersException("data parameters", e);
231
                } catch (IllegalAccessException e) {
232
                        throw new CopyParametersException("data parameters", e);
233
                }
234
                this.copyValuesTo(copy);
235
                return copy;
236
        }
237

    
238
        public PersistentState getState() throws PersistenceException {
239
                return AbstractPersistenceManager.getState(this);
240
        }
241

    
242
        public void loadState(PersistentState state) throws PersistenceException {
243
                Iterator it = params.values().iterator();
244
                while (it.hasNext()) {
245
                        DefaultDataParameter p = ((DefaultDataParameter) it.next());
246
                        state.set(p.getName(), p.getValue());
247
                }
248
        }
249

    
250
        public void setState(PersistentState state) throws PersistenceException {
251
                Iterator it = state.getNames();
252
                while (it.hasNext()) {
253
                        String name = (String) it.next();
254
                        try {
255
                                this.setAttribute(name, state.get(name));
256
                        } catch (PersistenceValueNotFoundException e) {
257
                                // Ignore
258
                        }
259
                }
260
        }
261

    
262
}