Revision 43981

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app.document.table.app/org.gvsig.app.document.table.app.mainplugin/src/main/java/org/gvsig/app/project/documents/table/gui/CreateNewAttributePanel.java
47 47
import org.gvsig.andami.messages.NotificationManager;
48 48
import org.gvsig.andami.ui.mdiManager.IWindow;
49 49
import org.gvsig.andami.ui.mdiManager.WindowInfo;
50
import org.gvsig.app.ApplicationLocator;
51
import org.gvsig.app.ApplicationManager;
50 52
import org.gvsig.fmap.dal.DataTypes;
51 53
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
52 54
import org.gvsig.fmap.dal.feature.EditableFeatureType;
......
56 58
import org.gvsig.i18n.Messages;
57 59
import org.gvsig.tools.ToolsLocator;
58 60
import org.gvsig.tools.dataTypes.DataTypesManager;
61
import org.gvsig.tools.i18n.I18nManager;
59 62

  
60 63
/**
61 64
 * To create new FeatureAttributeDescriptor from the interface.
......
331 334
                PluginServices.getText(this, "field_already_exists"), null);
332 335
            return null;
333 336
        }
334
        EditableFeatureAttributeDescriptor ead;
337
        EditableFeatureAttributeDescriptor ead = null;
335 338
        if (getJRdbFieldVirtual().isSelected()) {
336 339
            String code = getJTxtExpression().getText();
337
            FeatureAttributeDescriptor[] descriptors = 
338
                    featureType.getAttributeDescriptors();
339
            String[] fields = new String[descriptors.length];
340
            for (int i = 0; i < descriptors.length; i++) {
341
                FeatureAttributeDescriptor descriptor = descriptors[i];
342
                fields[i] = descriptor.getName();
343
            }
344
            FeatureAttributeEmulator myEmulated = 
340
            EmulatedFieldExpression myEmulated = 
345 341
                    new EmulatedFieldExpression(
346
                            fields,
342
                            featureType,
347 343
                            code);
344
            if (!myEmulated.isValid()) {
345
                I18nManager i18n = ToolsLocator.getI18nManager();
346
                ApplicationManager application = ApplicationLocator.getManager();
347
                int x = application.confirmDialog(
348
                        "The expression used in this field is not valid."+
349
                                "\n\n"+
350
                                myEmulated.getErrorMessage()+
351
                                "\n\n"+
352
                                "Do you want add this field anyway?", 
353
                        "Add invalid field", 
354
                        JOptionPane.YES_NO_OPTION, 
355
                        JOptionPane.QUESTION_MESSAGE,
356
                        "_AddingInvalidField.DoYouWantAddAnyway");
357
                if (x==JOptionPane.YES_OPTION) {
358
                    ead = featureType.add(nameAttr, typeAttr, myEmulated)
359
                    .setSize(sizeAttr);
360
                }
361
            } else {
348 362
            ead = featureType.add(nameAttr, typeAttr, myEmulated)
349 363
                    .setSize(sizeAttr);
364
            }
350 365
        } else {
351 366
            defaultValueAttr = getJTxtDefaultValue().getText();
352 367
            if (defaultValueAttr.equals("")) {
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app.document.table.app/org.gvsig.app.document.table.app.mainplugin/src/main/java/org/gvsig/app/project/documents/table/gui/EmulatedFieldExpression.java
5 5
 */
6 6
package org.gvsig.app.project.documents.table.gui;
7 7

  
8
import java.util.ArrayList;
9
import java.util.List;
10
import java.util.logging.Level;
11
import java.util.logging.Logger;
12
import org.apache.commons.collections.ListUtils;
13
import org.apache.commons.lang3.StringUtils;
8 14
import org.gvsig.expressionevaluator.Code;
15
import org.gvsig.expressionevaluator.Code.Identifier;
9 16
import org.gvsig.fmap.dal.feature.EditableFeature;
10 17
import org.gvsig.fmap.dal.feature.Feature;
11 18
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
......
13 20
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
14 21
import org.gvsig.expressionevaluator.MutableSymbolTable;
15 22
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
23
import org.gvsig.fmap.dal.feature.FeatureType;
16 24
import org.gvsig.tools.ToolsLocator;
17 25
import org.gvsig.tools.dynobject.DynStruct;
26
import org.gvsig.tools.exception.BaseException;
18 27
import org.gvsig.tools.persistence.PersistenceManager;
19 28
import org.gvsig.tools.persistence.Persistent;
20 29
import org.gvsig.tools.persistence.PersistentState;
21 30
import org.gvsig.tools.persistence.exception.PersistenceException;
31
import org.gvsig.tools.visitor.VisitCanceledException;
32
import org.gvsig.tools.visitor.Visitor;
22 33

  
23 34
/**
24 35
 *
25 36
 * @author osc
26 37
 */
27 38
public class EmulatedFieldExpression implements FeatureAttributeEmulator, Persistent {
28
            
29
    String[] fields = null;
39

  
40
    String[] requiredFields = null;
30 41
    String code = "";
31
    private static final String EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME = 
32
            "EmulatedFieldExpression";
42
    Code codeCompiled = null;
43
    MutableSymbolTable symbolTable = null;
44
    ExpressionEvaluatorManager expressionManager = null;
45
    private static final String EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME
46
            = "EmulatedFieldExpression";
47
    private boolean valid;
48
    private String errorMessage;
33 49

  
34 50
    public EmulatedFieldExpression() {
35 51
    }
36 52

  
37
    
38
    EmulatedFieldExpression(String[] fields, String code){
39
        this.fields = fields;
53
    EmulatedFieldExpression(FeatureType featureType, String code) {
40 54
        this.setCode(code);
55
        this.checkVars(featureType);
56

  
41 57
    }
42
    
58

  
59
    public boolean isValid() {
60
        return this.valid;
61
    }
62

  
63
    private void checkVars(final FeatureType featureType) {
64
        this.valid = true;
65
        final List<String> theUsedFields = new ArrayList<>();
66
        final List<String> undefinedFields = new ArrayList<>();
67
        
68
        try {
69
            Code theCodeCompiled = this.getCodeCompiled();
70
            theCodeCompiled.accept(new Visitor() {
71
                @Override
72
                public void visit(Object obj) throws VisitCanceledException, BaseException {
73
                    Code code = (Code) obj;
74
                    if (code instanceof Identifier) {
75
                        String name = ((Identifier) code).name();
76
                        if (featureType.get(name) == null) {
77
                            undefinedFields.add(name);
78
                            valid = false;
79
                        } else {
80
                            theUsedFields.add(name);
81
                        }
82
                    }
83
                }
84
            });
85
            if(!undefinedFields.isEmpty()){
86
                this.errorMessage = "undefined fields " + StringUtils.join(undefinedFields,", ");
87
            }
88
        } catch (BaseException ex) {
89
            valid = false;
90
            this.errorMessage = ex.getMessage();
91
        }
92
        this.requiredFields = theUsedFields.toArray(new String[theUsedFields.size()]);
93
    }
94

  
43 95
    public static void registerPersistenceDefinition() {
44 96
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
45
    
46
        if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME) == 
47
                null) {
97

  
98
        if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME)
99
                == null) {
48 100
            DynStruct definition = manager.addDefinition(
49
                EmulatedFieldExpression.class,
50
                EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME, 
51
                EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME +       
52
                        " Persistent definition", 
53
                null, 
54
                null
101
                    EmulatedFieldExpression.class,
102
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME,
103
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME
104
                    + " Persistent definition",
105
                    null,
106
                    null
55 107
            );
56 108
            definition.addDynFieldString("code")
57
                .setMandatory(true)
58
                .setPersistent(true);
109
                    .setMandatory(true)
110
                    .setPersistent(true);
59 111
            definition.addDynFieldArray("fields")
60
                .setClassOfItems(String.class)
61
                .setMandatory(true)
62
                .setPersistent(true);
112
                    .setClassOfItems(String.class)
113
                    .setMandatory(true)
114
                    .setPersistent(true);
115
            definition.addDynFieldBoolean("valid")
116
                    .setMandatory(true)
117
                    .setPersistent(true);
63 118
        }
64 119
    }
65
        
120

  
121
    private Code getCodeCompiled() {
122
        if (this.codeCompiled == null) {
123
            this.codeCompiled = this.getExpressionManager().compile(code);
124
        }
125
        return this.codeCompiled;
126
    }
127

  
128
    private MutableSymbolTable getSymbolTable() {
129
        if (this.symbolTable == null) {
130
            this.symbolTable = this.getExpressionManager().createSymbolTable();
131
        }
132
        return this.symbolTable;
133
    }
134

  
135
    private ExpressionEvaluatorManager getExpressionManager() {
136
        if (this.expressionManager == null) {
137
            this.expressionManager
138
                    = ExpressionEvaluatorLocator.getManager();
139
        }
140
        return this.expressionManager;
141
    }
142

  
66 143
    @Override
67 144
    public Object get(Feature feature) {
68
        ExpressionEvaluatorManager expManager = 
69
                ExpressionEvaluatorLocator.getManager();
70
        MutableSymbolTable symbol = expManager.createSymbolTable();
71
        FeatureAttributeDescriptor[] descriptors = 
72
                feature.getType().getAttributeDescriptors();
73
        for (FeatureAttributeDescriptor descriptor : descriptors) {
74
            String fieldName = descriptor.getName();
145
        if (!this.valid) {
146
            return null;
147
        }
148
        MutableSymbolTable symbol = this.getSymbolTable();
149
        for (FeatureAttributeDescriptor descriptor : feature.getType()) {
75 150
            if (!descriptor.isComputed()) { // si eres tu mismo 
151
                String fieldName = descriptor.getName();
76 152
                Object value = feature.get(fieldName);
77 153
                symbol.setVar(fieldName, value);
78 154
            }
79 155
        }
80
        Code codeCompilation = expManager.compile(code);
81
        Object result = expManager.evaluate(symbol, codeCompilation);
156
        Code codeCompilation = this.getCodeCompiled();
157
        Object result = this.getExpressionManager().evaluate(symbol, codeCompilation);
82 158
        return result;
83 159
    }
84 160

  
......
94 170

  
95 171
    @Override
96 172
    public String[] getRequiredFieldNames() {
97
        return this.fields;
173
        return this.requiredFields;
98 174
    }
99
    
175

  
100 176
    public String getCode() {
101 177
        return this.code;
102 178
    }
103 179

  
104 180
    @Override
105 181
    public void saveToState(PersistentState state) throws PersistenceException {
106
        state.set("fields", this.getFields());
182
        state.set("fields", this.getRequiredFieldNames());
107 183
        state.set("code", this.getCode());
108
        
184
        state.set("valid", this.isValid());
185

  
109 186
    }
110 187

  
111 188
    @Override
112 189
    public void loadFromState(PersistentState state) throws PersistenceException {
113 190
        String stateCode = state.getString("code");
114 191
        String[] stateFields = state.getStringArray("fields");
192
        this.valid = state.getBoolean("valid");
115 193
        this.setCode(stateCode);
116 194
        this.setFields(stateFields);
117 195
    }
118 196

  
119
    public String[] getFields() {
120
        return this.fields;
121
    }
122 197
    private void setCode(String code) {
123 198
        this.code = code;
124 199
    }
125 200

  
126 201
    private void setFields(String[] fields) {
127
        this.fields = fields;
202
        this.requiredFields = fields;
128 203
    }
129
}
204

  
205
    public String getErrorMessage() {
206
        return this.errorMessage;
207
    }
208
}
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/ConfigurableFeatureTableModel.java
107 107

  
108 108
    @Override
109 109
    public String getColumnName(int column) {
110
        try {
110 111
        int originalIndex = getOriginalColumnIndex(column);
111 112
        return getAliasForColumn(getOriginalColumnName(originalIndex));
113
        } catch (Exception ex) {
114
            return "C" + column;
115
        }
112 116
    }
113 117

  
114 118
    @Override
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/DefaultFeatureStore.java
41 41
import java.util.Map.Entry;
42 42
import java.util.Set;
43 43
import java.util.logging.Level;
44
import org.apache.commons.collections4.ListUtils;
44 45

  
45 46
import org.apache.commons.io.FilenameUtils;
46 47
import org.apache.commons.lang3.StringUtils;
......
70 71
import org.gvsig.fmap.dal.feature.EditableFeatureType;
71 72
import org.gvsig.fmap.dal.feature.Feature;
72 73
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
74
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
73 75
import org.gvsig.fmap.dal.feature.FeatureCache;
74 76
import org.gvsig.fmap.dal.feature.FeatureIndex;
75 77
import org.gvsig.fmap.dal.feature.FeatureIndexes;
......
1186 1188
    synchronized public void update(EditableFeatureType type)
1187 1189
        throws DataException {
1188 1190
        try {
1189
            checkInEditingMode();
1190 1191
            if (type == null) {
1191 1192
                throw new NullFeatureTypeException(getName());
1192 1193
            }
1194
            boolean typehasStrongChanges = ((DefaultEditableFeatureType) type).hasStrongChanges();
1195
            if (typehasStrongChanges) {
1196
                checkInEditingMode();
1197
            }  else if(!this.isAppending()) {
1198
                throw new NeedEditingModeException(this.getName());
1199
            }
1193 1200
            // FIXME: Comprobar que es un featureType aceptable.
1194 1201
            notifyChange(FeatureStoreNotification.BEFORE_UPDATE_TYPE, type);
1195 1202
            newVersionOfUpdate();
1196

  
1197
            FeatureType oldt = type.getSource().getCopy();
1198
            FeatureType newt = type.getCopy();
1199
            commands.update(newt, oldt);
1200

  
1201
            if (((DefaultEditableFeatureType) type).hasStrongChanges()) {
1203
            
1204
            if (typehasStrongChanges) { 
1205
                FeatureType oldt = type.getSource().getCopy();
1206
                FeatureType newt = type.getCopy();
1207
                commands.update(newt, oldt);
1202 1208
                hasStrongChanges = true;
1209
            } else {
1210
                boolean ok = this.featureTypes.remove(this.defaultFeatureType);
1211
                this.defaultFeatureType = type.getCopy();
1212
                if (ok) {
1213
                    this.featureTypes.add(this.defaultFeatureType);
1214
                }
1203 1215
            }
1204 1216
            notifyChange(FeatureStoreNotification.AFTER_UPDATE_TYPE, type);
1205 1217
        } catch (Exception e) {
......
1433 1445
             * editing mode.
1434 1446
             */
1435 1447
//            ((FeatureSelection) this.getSelection()).deselectAll();
1436

  
1448
            Map<String,List<FeatureAttributeDescriptor>> computedFields = this.getComputedFields();
1437 1449
            switch (mode) {
1438 1450
            case MODE_QUERY:
1439 1451
                throw new NeedEditingModeException(this.getName());
......
1445 1457
                notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING);
1446 1458
                provider.endAppend();
1447 1459
                exitEditingMode();
1460
                this.updateComputedFields(computedFields);
1461
                saveDALFile();
1448 1462
                updateIndexes();
1449 1463
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1450 1464
                break;
......
1468 1482
                        featureManager.getInserted(),
1469 1483
                        featureManager.getUpdated(),
1470 1484
                        removeCalculatedAttributes(featureTypeManager.getFeatureTypesChanged()).iterator());
1485
                    
1471 1486
                }  
1487
                exitEditingMode();
1488
                this.updateComputedFields(computedFields);
1472 1489
                saveDALFile();
1473
                exitEditingMode();
1474 1490
                updateIndexes();
1475 1491
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1476 1492
                break;
......
1481 1497
            throw new FinishEditingException(e);
1482 1498
        }
1483 1499
    }
1484
    
1500
    private Map<String,List<FeatureAttributeDescriptor>> getComputedFields() throws DataException {
1501
        Map<String,List<FeatureAttributeDescriptor>> r = new HashMap<>();
1502
        
1503
        List<FeatureType> theTypes = new ArrayList<>();
1504
        theTypes.addAll(this.getFeatureTypes());
1505
        theTypes.add(this.getDefaultFeatureType());
1506
        for( int n=0; n<theTypes.size(); n++ ) {
1507
            FeatureType type = theTypes.get(n);
1508
                for (FeatureAttributeDescriptor attrdesc : type) {
1509
                    FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator();
1510
                    if( emulator!= null) {
1511
                        List<FeatureAttributeDescriptor> l = r.get(type.getId());
1512
                        if (l==null) {
1513
                            l = new ArrayList<>();
1514
                            r.put(type.getId(), l);
1515
                        }
1516
                        l.add(attrdesc);
1517
                    }
1518
            }
1519
        }
1520
        return r;
1521
    }
1522
    private void updateComputedFields(Map<String,List<FeatureAttributeDescriptor>> computedFields) throws DataException {
1523

  
1524
        List<FeatureType> theTypes = new ArrayList<>();
1525
        theTypes.addAll(this.getFeatureTypes());
1526
        theTypes.add(this.getDefaultFeatureType());
1527
        for( int n=0; n<theTypes.size(); n++ ) {
1528
            DefaultFeatureType type = (DefaultFeatureType) theTypes.get(n);
1529
            List<FeatureAttributeDescriptor> x = computedFields.get(type.getId());
1530
            if(x!=null && !x.isEmpty()) {
1531
                for (FeatureAttributeDescriptor attrdesc : x) {
1532
                    if (type.get(attrdesc.getName())==null) {
1533
                        type.add(attrdesc);
1534
                    }
1535
                }
1536
            }
1537
        }
1538
        
1539
    }
1485 1540
    private List<FeatureStoreProvider.FeatureTypeChanged> removeCalculatedAttributes(List<FeatureStoreProvider.FeatureTypeChanged> ftypes) {
1486 1541
        // FIXME: Falta por implementar
1487 1542
//        for (FeatureStoreProvider.FeatureTypeChanged ftype : ftypes) {
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/DefaultEditableFeatureType.java
85 85
        DefaultEditableFeatureAttributeDescriptor attr;
86 86
        while (iter.hasNext()) {
87 87
            attr = (DefaultEditableFeatureAttributeDescriptor) iter.next();
88
            if (attr.isComputed()) {
89
                continue;
90
            }
88 91
            if (attr.hasStrongChanges()) {
89 92
                return true;
90 93
            }
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/DefaultFeatureType.java
27 27
import java.util.ArrayList;
28 28
import java.util.Arrays;
29 29
import java.util.Collections;
30
import java.util.HashSet;
30 31
import java.util.Iterator;
31 32
import java.util.List;
33
import java.util.Set;
32 34
import java.util.zip.CRC32;
33 35
import org.apache.commons.lang3.ArrayUtils;
34 36
import org.apache.commons.lang3.StringUtils;
......
294 296
                    super(parent, false);
295 297
                    DefaultFeatureAttributeDescriptor attrcopy;
296 298
                    DefaultFeatureAttributeDescriptor attr;
297
                    List attrnames = null;
299
                    Set attrnames = null;
298 300

  
299 301
                    // Copy attributes
300 302
                    if ( names != null && names.length > 0 ) {
301
                        attrnames = new ArrayList();
303
                        attrnames = new HashSet();
302 304
                        attrnames.addAll(Arrays.asList(names));
303 305
                        if ( parent.hasEmulators ) {
304 306
                            for ( int i = 0; i < parent.size(); i++ ) {
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/editing/memory/FeatureTypeManager.java
220 220
            EditableFeatureAttributeDescriptor eatd;
221 221
            while (iter.hasNext()) {
222 222
                tAttr = (FeatureAttributeDescriptor) iter.next();
223
                if (!tAttr.isComputed()) {
223
                if (tAttr.isComputed()) {
224 224
                    continue;
225 225
                }
226 226
                if (ftTarget_editable != null) {
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/featureset/DefaultFeatureSet.java
524 524

  
525 525
        // TODO Tener en cuenta las transformaciones ???
526 526

  
527
        if (store.isEditing() && store.getFeatureManager().hasChanges()) {
527
        if (store.isEditing() && (store.getFeatureTypeManager().hasChanges() || store.getFeatureManager().hasChanges())) {
528 528
            if (this.query.hasOrder()) { // En edicion siempre ordeno yo.
529 529
                if (this.query.hasFilter()) {
530 530
                    return ORDERED_EDITED_FILTER;

Also available in: Unified diff