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 / DefaultFeatureExtraColumns.java @ 45426
History | View | Annotate | Download (7.33 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.fmap.dal.feature.impl; |
7 |
|
8 |
import java.util.ArrayList; |
9 |
import java.util.List; |
10 |
import javax.json.JsonObject; |
11 |
import javax.json.JsonValue; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.gvsig.fmap.dal.DALLocator; |
14 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
15 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
16 |
import org.gvsig.tools.ToolsLocator; |
17 |
import org.gvsig.tools.dynobject.DynStruct; |
18 |
import org.gvsig.tools.persistence.PersistentState; |
19 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
20 |
import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
21 |
import org.gvsig.json.Json; |
22 |
import org.gvsig.json.JsonManager; |
23 |
import org.gvsig.json.JsonObjectBuilder; |
24 |
import org.gvsig.json.SupportToJson; |
25 |
|
26 |
/**
|
27 |
*
|
28 |
* @author osc
|
29 |
*/
|
30 |
public class DefaultFeatureExtraColumns implements FeatureExtraColumns { |
31 |
|
32 |
private List<EditableFeatureAttributeDescriptor> extraColumns; |
33 |
|
34 |
public DefaultFeatureExtraColumns() {
|
35 |
this.extraColumns = new ArrayList(); |
36 |
} |
37 |
|
38 |
@Override
|
39 |
public boolean isEmpty() { |
40 |
return this.extraColumns.isEmpty(); |
41 |
} |
42 |
|
43 |
@Override
|
44 |
public EditableFeatureAttributeDescriptor get(String columnName) { |
45 |
for (EditableFeatureAttributeDescriptor extraColumn : this.extraColumns) { |
46 |
if (StringUtils.equals(extraColumn.getName(), columnName)) {
|
47 |
return extraColumn;
|
48 |
} |
49 |
} |
50 |
return null; |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public EditableFeatureAttributeDescriptor add(String name) { |
55 |
//TODO que no hayan dos columnas iguales
|
56 |
// exi.addExtraColumn()
|
57 |
EditableFeatureAttributeDescriptor newAttr = new DefaultEditableFeatureAttributeDescriptor(null, false); |
58 |
newAttr.setName(name); |
59 |
extraColumns.add(newAttr); |
60 |
return newAttr;
|
61 |
} |
62 |
|
63 |
@Override
|
64 |
public List<EditableFeatureAttributeDescriptor> getColumns() { |
65 |
return this.extraColumns; |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public void merge(FeatureExtraColumns other) { |
70 |
this.extraColumns.addAll(other.getColumns());
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public int getIndexOf(String name) { |
75 |
for (int i = 0; i < extraColumns.size(); i++) { |
76 |
EditableFeatureAttributeDescriptor extraColumn = extraColumns.get(i); |
77 |
if (StringUtils.equalsIgnoreCase(extraColumn.getName(), name)) {
|
78 |
return i;
|
79 |
} |
80 |
} |
81 |
return -1; |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public EditableFeatureAttributeDescriptor get(int index) { |
86 |
return extraColumns.get(index);
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public int size() { |
91 |
return extraColumns.size();
|
92 |
} |
93 |
|
94 |
@Override
|
95 |
public void copyFrom(FeatureExtraColumns extraColumn) { |
96 |
this.extraColumns.clear();
|
97 |
for (EditableFeatureAttributeDescriptor col : extraColumn.getColumns()) {
|
98 |
//TODO revisar si getCopy o no
|
99 |
this.extraColumns.add(col);
|
100 |
} |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public void clear() { |
105 |
this.extraColumns.clear();
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public FeatureExtraColumns getCopy() {
|
110 |
DefaultFeatureExtraColumns other = new DefaultFeatureExtraColumns();
|
111 |
ArrayList otherCols = new ArrayList(); |
112 |
for (EditableFeatureAttributeDescriptor extraColumn : this.extraColumns) { |
113 |
EditableFeatureAttributeDescriptor newAttr = DALLocator.getDataManager().createFeatureAttributeDescriptor(); |
114 |
newAttr.copyFrom(extraColumn); |
115 |
otherCols.add(newAttr); |
116 |
} |
117 |
other.extraColumns = otherCols; |
118 |
return other;
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
public void saveToState(PersistentState state) throws PersistenceException { |
123 |
|
124 |
// ArrayList<DefaultFeatureAttributeDescriptor> extraListCols = new ArrayList<>();
|
125 |
// for (EditableFeatureAttributeDescriptor extraColumn : extraColumns) {
|
126 |
// DefaultEditableFeatureAttributeDescriptor mycol = (DefaultEditableFeatureAttributeDescriptor) extraColumn;
|
127 |
// DefaultFeatureAttributeDescriptor fad = new DefaultFeatureAttributeDescriptor();
|
128 |
// fad.copyFrom(mycol);
|
129 |
// extraListCols.add(fad);
|
130 |
// }
|
131 |
state.set("extraColumns", extraColumns);
|
132 |
} |
133 |
|
134 |
@Override
|
135 |
public void loadFromState(PersistentState state) throws PersistenceException { |
136 |
List<DefaultFeatureAttributeDescriptor> extraColumnsDescriptors = (List) state.getList("extraColumns"); |
137 |
ArrayList<EditableFeatureAttributeDescriptor> cols = new ArrayList<EditableFeatureAttributeDescriptor>(); |
138 |
for (DefaultFeatureAttributeDescriptor extraColumnsDescriptor : extraColumnsDescriptors) {
|
139 |
DefaultEditableFeatureAttributeDescriptor myEditableDescriptor = new DefaultEditableFeatureAttributeDescriptor(extraColumnsDescriptor);
|
140 |
cols.add(myEditableDescriptor); |
141 |
} |
142 |
this.extraColumns = cols;
|
143 |
} |
144 |
|
145 |
public static void registerPersistent() { |
146 |
DynStruct definition = |
147 |
ToolsLocator.getPersistenceManager() |
148 |
.addDefinition(DefaultFeatureExtraColumns.class, |
149 |
"DefaultFeatureExtraColumn",
|
150 |
"DefaultFeatureExtraColumn Persistent definition",
|
151 |
null,
|
152 |
null);
|
153 |
|
154 |
definition.addDynFieldArray("extraColumns")
|
155 |
.setClassOfItems(FeatureAttributeDescriptor.class) |
156 |
.setMandatory(false);
|
157 |
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public void remove(String attribute) { |
162 |
for (EditableFeatureAttributeDescriptor extraColumn : extraColumns) {
|
163 |
if (extraColumn.getName().equalsIgnoreCase(attribute)) {
|
164 |
this.extraColumns.remove(extraColumn);
|
165 |
break;
|
166 |
} |
167 |
} |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public void fromJson(JsonObject json) { |
172 |
this.clear();
|
173 |
if( json.containsKey("extraColumns") ) { |
174 |
for (JsonValue jsonValue : json.getJsonArray("extraColumns")) { |
175 |
JsonObject value = (JsonObject) jsonValue; |
176 |
EditableFeatureAttributeDescriptor newAttr = new DefaultEditableFeatureAttributeDescriptor(null, false); |
177 |
newAttr.fromJson(value); |
178 |
this.extraColumns.add(newAttr);
|
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public JsonObject toJson() {
|
185 |
return this.toJsonBuilder().build(); |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public JsonObjectBuilder toJsonBuilder() {
|
190 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
191 |
builder.add_class(this);
|
192 |
builder.add("extraColumns", Json.createArrayBuilder().addAll(this.extraColumns)); |
193 |
return builder;
|
194 |
} |
195 |
|
196 |
private static class TheJsonSerializer implements JsonManager.JsonSerializer { |
197 |
|
198 |
public TheJsonSerializer() {
|
199 |
} |
200 |
|
201 |
@Override
|
202 |
public Class getObjectClass() { |
203 |
return DefaultFeatureExtraColumns.class;
|
204 |
} |
205 |
|
206 |
@Override
|
207 |
public Object toObject(JsonObject json) { |
208 |
DefaultFeatureExtraColumns o = new DefaultFeatureExtraColumns();
|
209 |
o.fromJson(json); |
210 |
return o;
|
211 |
} |
212 |
|
213 |
@Override
|
214 |
public JsonObjectBuilder toJsonBuilder(Object value) { |
215 |
return ((SupportToJson)value).toJsonBuilder();
|
216 |
} |
217 |
|
218 |
} |
219 |
|
220 |
public static void selfRegister() { |
221 |
Json.registerSerializer(new TheJsonSerializer());
|
222 |
} |
223 |
|
224 |
|
225 |
} |