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

History | View | Annotate | Download (5.15 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.fmap.dal.spi;
26

    
27
import java.util.Iterator;
28

    
29
import org.gvsig.fmap.dal.DataParameters;
30
import org.gvsig.fmap.dal.exception.CopyParametersException;
31
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
32
import org.gvsig.tools.dynobject.DelegatedDynObject;
33
import org.gvsig.tools.dynobject.DynClass;
34
import org.gvsig.tools.dynobject.DynField;
35
import org.gvsig.tools.dynobject.DynObject;
36
import org.gvsig.tools.dynobject.exception.DynMethodException;
37
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
38
import org.gvsig.tools.persistence.PersistentState;
39
import org.gvsig.tools.persistence.exception.PersistenceException;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
/**
44
 * @author jmvivo
45
 *
46
 */
47
public abstract class AbstractDataParameters implements DataParameters {
48
        private static final Logger logger = LoggerFactory.getLogger(AbstractDataParameters.class);
49

    
50
        public Object getDynValue(String name) {
51
                return getDelegatedDynObject().getDynValue(name);
52
        }
53

    
54
        public String toString() {
55
                return getDelegatedDynObject().toString();
56
        }
57
        
58
        public void setDynValue(String name, Object value) {
59
                DelegatedDynObject delegated = getDelegatedDynObject();
60
                if (delegated.getDynClass().getDynField(name)!=null){
61
                        delegated.setDynValue(name, value);        
62
                }
63
                else {
64
                        logger.error("Attribute '"+name+"' is not defined in "
65
                                        +delegated.getDynClass().getFullName()+ " definition");
66
                }
67
        }
68

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

    
72
                DynField[] fields =
73
                                getDelegatedDynObject().getDynClass()
74
                                .getDeclaredDynFields();
75

    
76
                for (int i = 0; i < fields.length; i++) {
77
                        this.setDynValue(fields[i].getName(), fields[i].getDefaultValue());
78
                }
79
        }
80

    
81
        protected void copyValuesTo(AbstractDataParameters target) {
82
                // TODO Delegar en el DynObject cuando tenga este servicio
83
                DynField[] fields =
84
                                getDelegatedDynObject().getDynClass()
85
                                .getDynFields();
86

    
87

    
88
                for (int i = 0; i < fields.length; i++) {
89
                        target.setDynValue(fields[i].getName(), this.getDynValue(fields[i]
90
                                        .getName()));
91
                }
92
        }
93

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

    
108
        public void saveToState(PersistentState state) throws PersistenceException {
109
                DynField[] fields =
110
                                getDelegatedDynObject().getDynClass().getDynFields();
111

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

    
121
        public void loadFromState(PersistentState state) throws PersistenceException {
122
                Iterator it = state.getNames();
123
                while (it.hasNext()) {
124
                        String name = (String) it.next();
125
                        this.setDynValue(name, state.get(name));
126
                }
127
        }
128

    
129
        public void delegate(DynObject dynObject) {
130
                getDelegatedDynObject().delegate(dynObject);
131

    
132
        }
133

    
134
        public DynClass getDynClass() {
135
                return getDelegatedDynObject().getDynClass();
136
        }
137

    
138
        public boolean hasDynValue(String name) {
139
                return getDelegatedDynObject().hasDynValue(name);
140
        }
141

    
142
        public void implement(DynClass dynClass) {
143
                getDelegatedDynObject().implement(dynClass);
144
        }
145

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

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

    
156
        public void validate() throws ValidateDataParametersException {
157
            try {
158
            this.getDynClass().validate(this);
159
        } catch (DynObjectValidateException e) {
160
            throw new ValidateDataParametersException(e);
161
        }
162
        }
163

    
164
        /**
165
         * Returns an instance of the {@link DynObject} to delegate to.
166
         * 
167
         * @return the delegate {@link DynObject}
168
         */
169
        protected abstract DelegatedDynObject getDelegatedDynObject();
170

    
171
}