Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / dynobjectutils / DynObjectFeatureFacade.java @ 42775

History | View | Annotate | Download (5.7 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.feature.impl.dynobjectutils;
24

    
25
import java.util.Map;
26
import org.gvsig.fmap.dal.DALLocator;
27
import org.gvsig.fmap.dal.feature.EditableFeature;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FacadeOfAFeature;
30
import org.gvsig.fmap.dal.feature.FeatureStore;
31
import org.gvsig.fmap.dal.feature.FeatureType;
32
import org.gvsig.fmap.dal.feature.FeatureTypeDefinitionsManager;
33
import org.gvsig.tools.dynobject.DynClass;
34
import org.gvsig.tools.dynobject.DynField;
35
import org.gvsig.tools.dynobject.DynField_v2;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
38
import org.gvsig.tools.dynobject.exception.DynMethodException;
39
import org.gvsig.tools.lang.Cloneable;
40

    
41
/**
42
 * {@link DynObject} implementation to facade a Feature and allow to be used as
43
 * a {@link DynObject}.
44
 *
45
 * This implementation may be reused to be used with many {@link Feature}
46
 * objects, but not at the same time.
47
 *
48
 * @author gvSIG Team
49
 * @version $Id$
50
 */
51
public class DynObjectFeatureFacade implements DynObject, Cloneable, FacadeOfAFeature {
52

    
53
    private static FeatureTypeDefinitionsManager featureTypeDefinitionsManager = null;
54

    
55
    private Feature feature;
56
    private DynClass dynClass;
57
    private EditableFeature editable;
58

    
59
    private final Object lock = new Object();
60

    
61
    /**
62
     * Empty constructor.
63
     */
64
    public DynObjectFeatureFacade() {
65
        this(null);
66
    }
67
    /**
68
     * Creates a facade over a {@link Feature}.
69
     *
70
     * @param feature
71
     */
72
    public DynObjectFeatureFacade(Feature feature) {
73
        if( featureTypeDefinitionsManager==null ) {
74
            featureTypeDefinitionsManager = DALLocator.getFeatureTypeDefinitionsManager();
75
        }
76
        this.feature = feature;
77
        this.editable = null;
78
        this.dynClass = null;
79
    }
80

    
81
    @Override
82
    public DynClass getDynClass() {
83
        if( this.dynClass == null ) {
84
            Feature feature = this.getFeature();
85
            FeatureType featureType = feature.getType();
86
            FeatureStore store = feature.getStore();
87
            this.dynClass = featureTypeDefinitionsManager.get(store,featureType);
88
        }
89
        return this.dynClass;
90
    }
91

    
92
    @Override
93
    public Object getDynValue(String name) throws DynFieldNotFoundException {
94
        try {
95
            Object value = this.getFeature().get(name);
96
            return value;
97
        } catch(IllegalArgumentException ex) {
98
            DynClass dynClass = this.getDynClass();
99
            DynField field = dynClass.getDynField(name);
100
            if( field instanceof DynField_v2 && ((DynField_v2)field).isCalculated() ) {
101
                return  ((DynField_v2)field).getCalculatedValue(this);
102
            }
103
            throw ex;
104
        }
105
    }
106

    
107
    @Override
108
    public void setDynValue(String name, Object value)
109
            throws DynFieldNotFoundException {
110
        synchronized (lock) {
111
            if (editable == null) {
112
                editable = feature.getEditable();
113
            }
114
        }
115
        editable.set(name, value);
116
    }
117

    
118
    @Override
119
    public boolean hasDynValue(String name) {
120
        DynClass dynClass = this.getDynClass();
121
        DynField field = dynClass.getDynField(name);
122
        return field!=null;
123
    }
124

    
125
    @Override
126
    public Object invokeDynMethod(String name, Object[] args)
127
            throws DynMethodException {
128
        throw new UnsupportedOperationException();
129
    }
130

    
131
    @Override
132
    public Object invokeDynMethod(int code, Object[] args)
133
            throws DynMethodException {
134
        throw new UnsupportedOperationException();
135
    }
136

    
137
    @Override
138
    public void implement(DynClass dynClass) {
139
        throw new UnsupportedOperationException();
140
    }
141

    
142
    @Override
143
    public void delegate(DynObject dynObject) {
144
        throw new UnsupportedOperationException();
145
    }
146

    
147
    @Override
148
    public void clear() {
149
        // Nothing to do
150
    }
151

    
152
    @Override
153
    public Feature getFeature() {
154
        return editable == null ? feature : editable;
155
    }
156

    
157
    public void setFeature(Feature feature) {
158
        synchronized (lock) {
159
            this.feature = feature;
160
            this.editable = null;
161
        }
162
    }
163

    
164
    @Override
165
    public EditableFeature getEditableFeature() {
166
        return editable;
167
    }
168

    
169
    @Override
170
    public String toString() {
171
        if (editable != null) {
172
            return editable.toString();
173
        }
174
        return this.feature.toString();
175
    }
176

    
177
    @Override
178
    public Object clone() throws CloneNotSupportedException {
179
        DynObjectFeatureFacade other = (DynObjectFeatureFacade) super.clone();
180
        if (feature != null) {
181
            other.feature = this.feature.getCopy();
182
        }
183
        if (editable != null) {
184
            other.editable = (EditableFeature) this.editable.getCopy();
185
        }
186
        return other;
187
    }
188
}