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 @ 45154
History | View | Annotate | Download (5.92 KB)
1 | 44753 | omartinez | /*
|
---|---|---|---|
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 | 45154 | jjdelcerro | import javax.json.Json; |
11 | import javax.json.JsonArray; |
||
12 | import javax.json.JsonArrayBuilder; |
||
13 | 44753 | omartinez | import org.apache.commons.lang3.StringUtils; |
14 | import org.gvsig.fmap.dal.DALLocator; |
||
15 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
16 | 44829 | omartinez | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
17 | import org.gvsig.tools.ToolsLocator; |
||
18 | import org.gvsig.tools.dynobject.DynStruct; |
||
19 | import org.gvsig.tools.persistence.PersistentState; |
||
20 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
21 | import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
||
22 | 44753 | omartinez | |
23 | /**
|
||
24 | *
|
||
25 | * @author osc
|
||
26 | */
|
||
27 | 44829 | omartinez | public class DefaultFeatureExtraColumns implements FeatureExtraColumns { |
28 | 44753 | omartinez | |
29 | 44801 | omartinez | private List<EditableFeatureAttributeDescriptor> extraColumns; |
30 | 44764 | jjdelcerro | |
31 | 44829 | omartinez | public DefaultFeatureExtraColumns() {
|
32 | 44753 | omartinez | this.extraColumns = new ArrayList(); |
33 | } |
||
34 | 44764 | jjdelcerro | |
35 | 44753 | omartinez | @Override
|
36 | 44764 | jjdelcerro | public boolean isEmpty() { |
37 | return this.extraColumns.isEmpty(); |
||
38 | } |
||
39 | |||
40 | @Override
|
||
41 | 44753 | omartinez | public EditableFeatureAttributeDescriptor get(String columnName) { |
42 | for (EditableFeatureAttributeDescriptor extraColumn : this.extraColumns) { |
||
43 | if (StringUtils.equals(extraColumn.getName(), columnName)) {
|
||
44 | return extraColumn;
|
||
45 | } |
||
46 | } |
||
47 | return null; |
||
48 | } |
||
49 | |||
50 | @Override
|
||
51 | public EditableFeatureAttributeDescriptor add(String name) { |
||
52 | //TODO que no hayan dos columnas iguales
|
||
53 | // exi.addExtraColumn()
|
||
54 | 44764 | jjdelcerro | EditableFeatureAttributeDescriptor newAttr = new DefaultEditableFeatureAttributeDescriptor(null, false); |
55 | 44753 | omartinez | newAttr.setName(name); |
56 | extraColumns.add(newAttr); |
||
57 | return newAttr;
|
||
58 | } |
||
59 | |||
60 | @Override
|
||
61 | public List<EditableFeatureAttributeDescriptor> getColumns() { |
||
62 | return this.extraColumns; |
||
63 | } |
||
64 | |||
65 | @Override
|
||
66 | 44829 | omartinez | public void merge(FeatureExtraColumns other) { |
67 | 44753 | omartinez | this.extraColumns.addAll(other.getColumns());
|
68 | } |
||
69 | |||
70 | 44778 | omartinez | @Override
|
71 | public int getIndexOf(String name) { |
||
72 | for (int i = 0; i < extraColumns.size(); i++) { |
||
73 | EditableFeatureAttributeDescriptor extraColumn = extraColumns.get(i); |
||
74 | if (StringUtils.equals(extraColumn.getName(), name)) {
|
||
75 | return i;
|
||
76 | } |
||
77 | } |
||
78 | return -1; |
||
79 | } |
||
80 | |||
81 | @Override
|
||
82 | public EditableFeatureAttributeDescriptor get(int index) { |
||
83 | return extraColumns.get(index);
|
||
84 | } |
||
85 | |||
86 | 44779 | omartinez | @Override
|
87 | public int size() { |
||
88 | return extraColumns.size();
|
||
89 | } |
||
90 | |||
91 | 44791 | omartinez | @Override
|
92 | 44829 | omartinez | public void copyFrom(FeatureExtraColumns extraColumn) { |
93 | 44791 | omartinez | this.extraColumns.clear();
|
94 | for (EditableFeatureAttributeDescriptor col : extraColumn.getColumns()) {
|
||
95 | this.extraColumns.add(col);
|
||
96 | } |
||
97 | } |
||
98 | |||
99 | @Override
|
||
100 | public void clear() { |
||
101 | this.extraColumns.clear();
|
||
102 | } |
||
103 | 44801 | omartinez | |
104 | 44829 | omartinez | public FeatureExtraColumns getDeepCopy() {
|
105 | DefaultFeatureExtraColumns other = new DefaultFeatureExtraColumns();
|
||
106 | 44801 | omartinez | ArrayList otherCols = new ArrayList(); |
107 | for (EditableFeatureAttributeDescriptor extraColumn : this.extraColumns) { |
||
108 | EditableFeatureAttributeDescriptor newAttr = DALLocator.getDataManager().createFeatureAttributeDescriptor(); |
||
109 | newAttr.copyFrom(extraColumn); |
||
110 | otherCols.add(newAttr); |
||
111 | } |
||
112 | other.extraColumns = otherCols; |
||
113 | return other;
|
||
114 | } |
||
115 | 44791 | omartinez | |
116 | 44801 | omartinez | @Override
|
117 | 44829 | omartinez | public FeatureExtraColumns getCopy() {
|
118 | DefaultFeatureExtraColumns other = new DefaultFeatureExtraColumns();
|
||
119 | 44801 | omartinez | ArrayList otherCols = new ArrayList(); |
120 | otherCols.addAll(extraColumns); |
||
121 | other.extraColumns = otherCols; |
||
122 | return other;
|
||
123 | } |
||
124 | |||
125 | 44829 | omartinez | @Override
|
126 | public void saveToState(PersistentState state) throws PersistenceException { |
||
127 | |||
128 | // ArrayList<DefaultFeatureAttributeDescriptor> extraListCols = new ArrayList<>();
|
||
129 | // for (EditableFeatureAttributeDescriptor extraColumn : extraColumns) {
|
||
130 | // DefaultEditableFeatureAttributeDescriptor mycol = (DefaultEditableFeatureAttributeDescriptor) extraColumn;
|
||
131 | // DefaultFeatureAttributeDescriptor fad = new DefaultFeatureAttributeDescriptor();
|
||
132 | // fad.copyFrom(mycol);
|
||
133 | // extraListCols.add(fad);
|
||
134 | // }
|
||
135 | state.set("extraColumns", extraColumns);
|
||
136 | } |
||
137 | |||
138 | @Override
|
||
139 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
140 | List<DefaultFeatureAttributeDescriptor> extraColumnsDescriptors = (List) state.getList("extraColumns"); |
||
141 | ArrayList<EditableFeatureAttributeDescriptor> cols = new ArrayList<EditableFeatureAttributeDescriptor>(); |
||
142 | for (DefaultFeatureAttributeDescriptor extraColumnsDescriptor : extraColumnsDescriptors) {
|
||
143 | DefaultEditableFeatureAttributeDescriptor myEditableDescriptor = new DefaultEditableFeatureAttributeDescriptor(extraColumnsDescriptor);
|
||
144 | cols.add(myEditableDescriptor); |
||
145 | } |
||
146 | this.extraColumns = cols;
|
||
147 | } |
||
148 | |||
149 | 45154 | jjdelcerro | public JsonArray toJson() {
|
150 | return this.toJsonBuilder().build(); |
||
151 | } |
||
152 | |||
153 | public JsonArrayBuilder toJsonBuilder() {
|
||
154 | JsonArrayBuilder builder = Json.createArrayBuilder(); |
||
155 | for (EditableFeatureAttributeDescriptor extraColumn : extraColumns) {
|
||
156 | builder.add(((DefaultFeatureAttributeDescriptor)extraColumns).toJsonBuilder()); |
||
157 | } |
||
158 | return builder;
|
||
159 | } |
||
160 | |||
161 | 44829 | omartinez | public static void registerPersistent() { |
162 | DynStruct definition = |
||
163 | ToolsLocator.getPersistenceManager() |
||
164 | .addDefinition(DefaultFeatureExtraColumns.class, |
||
165 | "DefaultFeatureExtraColumn",
|
||
166 | "DefaultFeatureExtraColumn Persistent definition",
|
||
167 | null,
|
||
168 | null);
|
||
169 | |||
170 | definition.addDynFieldArray("extraColumns")
|
||
171 | .setClassOfItems(FeatureAttributeDescriptor.class) |
||
172 | .setMandatory(false);
|
||
173 | |||
174 | } |
||
175 | 44753 | omartinez | } |