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

History | View | Annotate | Download (5.84 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.spi;
24

    
25
import java.util.Iterator;
26

    
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29

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

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

    
51
    private static final Logger logger = LoggerFactory.getLogger(AbstractDataParameters.class);
52

    
53
    @Override
54
    public Object getDynValue(String name) {
55
        return getDelegatedDynObject().getDynValue(name);
56
    }
57

    
58
    @Override
59
    public String toString() {
60
        DynObjectEncoder encoder = ToolsLocator.getDynObjectManager().createSimpleDynObjectEncoder();
61
        return encoder.encode(this);
62
    }
63

    
64
    @Override
65
    public void setDynValue(String name, Object value) {
66
        DelegatedDynObject delegated = getDelegatedDynObject();
67
        if (delegated.getDynClass().getDynField(name) != null) {
68
            delegated.setDynValue(name, value);
69
        } else {
70
            try {
71
                throw new IllegalArgumentException(name);
72
            } catch (IllegalArgumentException ex) {
73
                logger.warn("Attribute '" + name + "' is not defined in "
74
                        + delegated.getDynClass().getFullName() + " definition", ex);
75
            }
76
        }
77
    }
78

    
79
    @Override
80
    public void clear() {
81
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
82
        manager.clear(this);
83
    }
84

    
85
    protected void copyValuesTo(AbstractDataParameters target) {
86
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
87
        manager.copy(this, target);
88
    }
89

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

    
103
    @Override
104
    public void saveToState(PersistentState state) throws PersistenceException {
105
        DynField[] fields = getDelegatedDynObject().getDynClass().getDynFields();
106

    
107
        for (DynField field : fields) {
108
            if (field.isPersistent()) {
109
                String name = field.getName();
110
                Object value = this.getDynValue(name);
111
                state.set(name, value);
112
            }
113
        }
114
    }
115

    
116
    @Override
117
    public void loadFromState(PersistentState state) throws PersistenceException {
118
        @SuppressWarnings("rawtypes")
119
        Iterator it = state.getNames();
120
        while (it.hasNext()) {
121
            String name = (String) it.next();
122
            try {
123
                Object value = state.get(name);
124
                this.setDynValue(name, value);
125
            } catch (Throwable t) {
126
                logger.warn("Can't load '" + name + "' property", t);
127
            }
128
        }
129
    }
130

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

    
135
    }
136

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

    
142
    @Override
143
    public boolean hasDynValue(String name) {
144
        return getDelegatedDynObject().hasDynValue(name);
145
    }
146

    
147
    @Override
148
    public void implement(DynClass dynClass) {
149
        getDelegatedDynObject().implement(dynClass);
150
    }
151

    
152
    @Override
153
    public Object invokeDynMethod(String name, Object[] args)
154
            throws DynMethodException {
155
        return getDelegatedDynObject().invokeDynMethod(this, name, args);
156
    }
157

    
158
    @Override
159
    public Object invokeDynMethod(int code, Object[] args)
160
            throws DynMethodException {
161
        return getDelegatedDynObject().invokeDynMethod(this, code, args);
162
    }
163

    
164
    @Override
165
    public void validate() throws ValidateDataParametersException {
166
        try {
167
            this.getDynClass().validate(this);
168
        } catch (DynObjectValidateException e) {
169
            throw new ValidateDataParametersException(e);
170
        }
171
    }
172

    
173
    /**
174
     * Returns an instance of the {@link DynObject} to delegate to.
175
     *
176
     * @return the delegate {@link DynObject}
177
     */
178
    protected abstract DelegatedDynObject getDelegatedDynObject();
179

    
180
}