Statistics
| Revision:

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 @ 40435

History | View | Annotate | Download (4.9 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 IVER T.I. S.A.   {{Task}}
26
 */
27

    
28
package org.gvsig.fmap.dal.spi;
29

    
30
import java.util.Arrays;
31
import java.util.Iterator;
32

    
33
import org.gvsig.fmap.dal.DataParameters;
34
import org.gvsig.fmap.dal.exception.CopyParametersException;
35
import org.gvsig.fmap.dal.exception.ParameterMissingException;
36
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
37
import org.gvsig.tools.dynobject.DelegatedDynObject;
38
import org.gvsig.tools.dynobject.DynClass;
39
import org.gvsig.tools.dynobject.DynField;
40
import org.gvsig.tools.dynobject.DynObject;
41
import org.gvsig.tools.dynobject.exception.DynMethodException;
42
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
43
import org.gvsig.tools.persistence.PersistentState;
44
import org.gvsig.tools.persistence.exception.PersistenceException;
45

    
46
/**
47
 * @author jmvivo
48
 *
49
 */
50
public abstract class AbstractDataParameters implements DataParameters {
51

    
52
        public Object getDynValue(String name) {
53
                return getDelegatedDynObject().getDynValue(name);
54
        }
55

    
56
        public String toString() {
57
                return getDelegatedDynObject().toString();
58
        }
59
        
60
        public void setDynValue(String name, Object value) {
61
                getDelegatedDynObject().setDynValue(name, value);
62
                // return this ???
63
        }
64

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

    
68
                DynField[] fields =
69
                                getDelegatedDynObject().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 =
80
                                getDelegatedDynObject().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 void saveToState(PersistentState state) throws PersistenceException {
105
                DynField[] fields =
106
                                getDelegatedDynObject().getDynClass().getDynFields();
107

    
108
                for (int i = 0; i < fields.length; i++) {
109
                    if (fields[i].isPersistent()) {
110
                            String name = fields[i].getName(); 
111
                            Object value = this.getDynValue(fields[i].getName()); 
112
                                state.set(name,value);
113
                        }
114
                }
115
        }
116

    
117
        public void loadFromState(PersistentState state) throws PersistenceException {
118
                Iterator it = state.getNames();
119
                while (it.hasNext()) {
120
                        String name = (String) it.next();
121
                        this.setDynValue(name, state.get(name));
122
                }
123
        }
124

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

    
128
        }
129

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

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

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

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

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

    
152
        public void validate() throws ValidateDataParametersException {
153
            try {
154
            this.getDynClass().validate(this);
155
        } catch (DynObjectValidateException e) {
156
            throw new ValidateDataParametersException(e);
157
        }
158
        }
159

    
160
        /**
161
         * Returns an instance of the {@link DynObject} to delegate to.
162
         * 
163
         * @return the delegate {@link DynObject}
164
         */
165
        protected abstract DelegatedDynObject getDelegatedDynObject();
166

    
167
}