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

History | View | Annotate | Download (5.31 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
                } else {
63
                    try {
64
                      throw new IllegalArgumentException(name);
65
                    } catch(IllegalArgumentException ex) {
66
                        logger.warn("Attribute '"+name+"' is not defined in "
67
                                        +delegated.getDynClass().getFullName()+ " definition",ex);
68
                    }
69
                }
70
        }
71

    
72
        public void clear() {
73
                // TODO Delegar en el DynObject cuando tenga este servicio
74

    
75
                DynField[] fields =
76
                                getDelegatedDynObject().getDynClass()
77
                                .getDeclaredDynFields();
78

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

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

    
90

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

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

    
111
        public void saveToState(PersistentState state) throws PersistenceException {
112
                DynField[] fields =
113
                                getDelegatedDynObject().getDynClass().getDynFields();
114

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

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

    
132
        public void delegate(DynObject dynObject) {
133
                getDelegatedDynObject().delegate(dynObject);
134

    
135
        }
136

    
137
        public DynClass getDynClass() {
138
                return getDelegatedDynObject().getDynClass();
139
        }
140

    
141
        public boolean hasDynValue(String name) {
142
                return getDelegatedDynObject().hasDynValue(name);
143
        }
144

    
145
        public void implement(DynClass dynClass) {
146
                getDelegatedDynObject().implement(dynClass);
147
        }
148

    
149
        public Object invokeDynMethod(String name, DynObject context)
150
                        throws DynMethodException {
151
                return getDelegatedDynObject().invokeDynMethod(this, name, context);
152
        }
153

    
154
        public Object invokeDynMethod(int code, DynObject context)
155
                        throws DynMethodException {
156
                return getDelegatedDynObject().invokeDynMethod(this, code, context);
157
        }
158

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

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

    
174
}