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 / DefaultFeatureExtraColumns.java @ 45345

History | View | Annotate | Download (6 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.Json;
11
import javax.json.JsonArray;
12
import javax.json.JsonArrayBuilder;
13
import org.apache.commons.lang3.StringUtils;
14
import org.gvsig.fmap.dal.DALLocator;
15
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
16
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

    
23
/**
24
 *
25
 * @author osc
26
 */
27
public class DefaultFeatureExtraColumns implements FeatureExtraColumns {
28

    
29
    private List<EditableFeatureAttributeDescriptor> extraColumns;
30
    
31
    public DefaultFeatureExtraColumns() {
32
        this.extraColumns = new ArrayList();
33
    }
34

    
35
    @Override
36
    public boolean isEmpty() {
37
      return this.extraColumns.isEmpty();
38
    }
39
    
40
    @Override
41
    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
        EditableFeatureAttributeDescriptor newAttr = new DefaultEditableFeatureAttributeDescriptor(null, false);
55
        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
    public void merge(FeatureExtraColumns other) {
67
        this.extraColumns.addAll(other.getColumns());
68
    }
69

    
70
    @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.equalsIgnoreCase(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
    @Override
87
    public int size() {
88
        return extraColumns.size();
89
    }
90

    
91
    @Override
92
    public void copyFrom(FeatureExtraColumns extraColumn) {
93
        this.extraColumns.clear();
94
        for (EditableFeatureAttributeDescriptor col : extraColumn.getColumns()) {
95
            //TODO revisar si getCopy o no
96
            this.extraColumns.add(col);
97
        }
98
    }
99

    
100
    @Override
101
    public void clear() {
102
        this.extraColumns.clear();
103
    }
104
    
105
    @Override
106
    public FeatureExtraColumns getCopy() {
107
        DefaultFeatureExtraColumns other = new DefaultFeatureExtraColumns();
108
        ArrayList otherCols = new ArrayList();
109
        for (EditableFeatureAttributeDescriptor extraColumn : this.extraColumns) {
110
            EditableFeatureAttributeDescriptor newAttr = DALLocator.getDataManager().createFeatureAttributeDescriptor();
111
            newAttr.copyFrom(extraColumn);
112
            otherCols.add(newAttr);
113
        }
114
        other.extraColumns = otherCols;
115
        return other;
116
    }
117

    
118
    @Override
119
    public void saveToState(PersistentState state) throws PersistenceException {
120
        
121
//        ArrayList<DefaultFeatureAttributeDescriptor> extraListCols = new ArrayList<>();
122
//        for (EditableFeatureAttributeDescriptor extraColumn : extraColumns) {
123
//            DefaultEditableFeatureAttributeDescriptor mycol = (DefaultEditableFeatureAttributeDescriptor) extraColumn;
124
//            DefaultFeatureAttributeDescriptor fad = new DefaultFeatureAttributeDescriptor();
125
//            fad.copyFrom(mycol);
126
//            extraListCols.add(fad);
127
//        }
128
         state.set("extraColumns", extraColumns);
129
    }
130

    
131
    @Override
132
    public void loadFromState(PersistentState state) throws PersistenceException {
133
        List<DefaultFeatureAttributeDescriptor> extraColumnsDescriptors = (List) state.getList("extraColumns");
134
        ArrayList<EditableFeatureAttributeDescriptor> cols = new ArrayList<EditableFeatureAttributeDescriptor>();
135
        for (DefaultFeatureAttributeDescriptor extraColumnsDescriptor : extraColumnsDescriptors) {
136
            DefaultEditableFeatureAttributeDescriptor myEditableDescriptor = new DefaultEditableFeatureAttributeDescriptor(extraColumnsDescriptor);
137
            cols.add(myEditableDescriptor);
138
        }
139
        this.extraColumns = cols;
140
    }
141

    
142
    public JsonArray toJson() {
143
        return this.toJsonBuilder().build();
144
    }
145

    
146
    public JsonArrayBuilder toJsonBuilder() {
147
        JsonArrayBuilder builder = Json.createArrayBuilder();
148
        for (EditableFeatureAttributeDescriptor extraColumn : extraColumns) {
149
            builder.add(((DefaultFeatureAttributeDescriptor)extraColumns).toJsonBuilder());
150
        }
151
        return builder;
152
    }
153
    
154
    public static void registerPersistent() {
155
        DynStruct definition =
156
            ToolsLocator.getPersistenceManager()
157
            .addDefinition(DefaultFeatureExtraColumns.class,
158
                "DefaultFeatureExtraColumn",
159
                "DefaultFeatureExtraColumn Persistent definition",
160
                null,
161
                null);
162

    
163
        definition.addDynFieldArray("extraColumns")
164
                .setClassOfItems(FeatureAttributeDescriptor.class)
165
                .setMandatory(false);
166

    
167
    }
168

    
169
    @Override
170
    public void remove(String attribute) {
171
        for (EditableFeatureAttributeDescriptor extraColumn : extraColumns) {
172
            if (extraColumn.getName().equalsIgnoreCase(attribute)) {
173
                this.extraColumns.remove(extraColumn);
174
                break;
175
            }
176
        }
177
    }
178
}