Revision 46426 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

View differences:

DefaultFeatureStore.java
221 221
    private List<FeatureType> featureTypes = new ArrayList<>();
222 222

  
223 223
    private int mode = MODE_QUERY;
224
    private int lastMode = MODE_QUERY;
225
    
224 226
    private long versionOfUpdate = 0;
225 227
    private boolean hasStrongChanges = true;
226 228

  
......
250 252
    private DataTransaction transaction;
251 253

  
252 254
    private String editingSessionCode;
255
    private String lastEditingSessionCode;
253 256
    
254 257
    private CachedValue<Feature> sampleFeatureCache;
255 258
    
......
1057 1060

  
1058 1061
    @Override
1059 1062
    public FeatureStoreNotification notifyChange(String notification) {
1060
        return notifyChange(new DefaultFeatureStoreNotification(this, notification));
1063
        return notifyChange(
1064
                new DefaultFeatureStoreNotification(
1065
                        this, notification, 
1066
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1067
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode
1068
                )
1069
        );
1061 1070
    }
1062 1071

  
1063 1072
    public FeatureStoreNotification notifyChange(String notification, String editingSessionCode) {
......
1069 1078
    }
1070 1079

  
1071 1080
    public FeatureStoreNotification notifyChange(String notification,
1072
            String editingSessionCode,
1081
//            String editingSessionCode,
1073 1082
            Iterator<FeatureReference> deleteds,
1074 1083
            Iterator<EditableFeature> inserteds,
1075 1084
            Iterator<EditableFeature> updateds,
1076 1085
            Iterator<FeatureTypeChanged> featureTypesChanged,
1077 1086
            boolean isSelectionCompromised) {
1078
        return notifyChange(new DefaultFeatureStoreNotification(this, notification, editingSessionCode,
1079
                deleteds, inserteds, updateds, featureTypesChanged, isSelectionCompromised));
1087
        return notifyChange(
1088
                new DefaultFeatureStoreNotification(
1089
                        this, notification, 
1090
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1091
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1092
                        deleteds, inserteds, updateds, featureTypesChanged, isSelectionCompromised
1093
                )
1094
        );
1080 1095
    }
1081 1096

  
1082 1097
    @Override
......
1093 1108
    }
1094 1109

  
1095 1110
    public FeatureStoreNotification notifyChange(String notification, Feature feature) {
1096
        return notifyChange(new DefaultFeatureStoreNotification(this, notification,
1097
                feature));
1111
        return notifyChange(
1112
                new DefaultFeatureStoreNotification(
1113
                        this, notification, 
1114
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1115
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1116
                        feature
1117
                )
1118
        );
1098 1119
    }
1099 1120

  
1121
    public FeatureStoreNotification notifyChange(String notification, Expression expression) {
1122
        return notifyChange(
1123
                new DefaultFeatureStoreNotification(
1124
                        this, notification, 
1125
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1126
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1127
                        expression
1128
                )
1129
        );
1130
    }
1131

  
1100 1132
    public FeatureStoreNotification notifyChange(String notification, Command command) {
1101
        return notifyChange(new DefaultFeatureStoreNotification(this, notification,
1102
                command));
1133
        return notifyChange(
1134
                new DefaultFeatureStoreNotification(
1135
                        this, notification, 
1136
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1137
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1138
                        command
1139
                )
1140
        );
1103 1141
    }
1104 1142

  
1105 1143
    public FeatureStoreNotification notifyChange(String notification, EditableFeatureType type) {
1106
        return notifyChange(new DefaultFeatureStoreNotification(this, notification,
1107
                type));
1144
        return notifyChange(
1145
                new DefaultFeatureStoreNotification(
1146
                        this, notification, 
1147
                        this.editingSessionCode==null?this.lastEditingSessionCode:this.editingSessionCode,
1148
                        this.mode == FeatureStore.MODE_QUERY?this.lastMode:this.mode,
1149
                        type
1150
                )
1151
        );
1108 1152
    }
1109 1153

  
1110 1154
    @Override
1111 1155
    public FeatureStoreNotification notifyChange(String notification, Resource resource) {
1112
        return notifyChange(new DefaultFeatureStoreNotification(this,
1113
                DataStoreNotification.RESOURCE_CHANGED));
1156
        return notifyChange(
1157
                new DefaultFeatureStoreNotification(
1158
                        this, DataStoreNotification.RESOURCE_CHANGED
1159
                )
1160
        );
1114 1161
    }
1115 1162

  
1116 1163
    //
......
1266 1313

  
1267 1314
        featureCount = null;
1268 1315

  
1316
        this.lastMode = this.mode;
1269 1317
        mode = MODE_QUERY;
1270 1318
        hasStrongChanges = true; // Lo deja a true por si las moscas
1271 1319

  
1320
        this.lastEditingSessionCode = this.editingSessionCode;
1272 1321
        this.editingSessionCode = null;
1273 1322
    }
1274 1323

  
......
1514 1563

  
1515 1564
    @Override
1516 1565
    public void delete(Expression filter) {
1517
        // TODO: Optimizar pasandolo directamente al proveedor si no estamos en edicion y lo soporta.
1518 1566
        if (filter == null) {
1519 1567
            return;
1520 1568
        }
......
1531 1579
                case MODE_FULLEDIT:
1532 1580
                    break;
1533 1581
                case MODE_PASS_THROUGH:
1582
                    if (notifyChange(FeatureStoreNotification.BEFORE_DELETE, filter).isCanceled()) {
1583
                        return;
1584
                    }
1534 1585
                    this.provider.passThroughDelete(filter);
1586
                    notifyChange(FeatureStoreNotification.AFTER_DELETE, filter);                    
1535 1587
                    return;
1536 1588
                default:
1537 1589
                    throw new IllegalStateException("Mode " + this.mode + " not supported.");
......
1892 1944
                    if (this.selection instanceof FeatureReferenceSelection) {
1893 1945
                        clearSelection = this.hasStrongChanges || this.featureManager.hasNews();
1894 1946
                    }
1895
                    if (notifyChange(FeatureStoreNotification.BEFORE_CANCELEDITING,this.editingSessionCode).isCanceled()) {
1947
                    if (notifyChange(FeatureStoreNotification.BEFORE_CANCELEDITING).isCanceled()) {
1896 1948
                        return;
1897 1949
                    }
1898 1950
                    exitEditingMode();
......
1900 1952
                        ((FeatureSelection) this.getSelection()).deselectAll();
1901 1953
                    }
1902 1954
                    updateIndexes();
1903
                    notifyChange(FeatureStoreNotification.AFTER_CANCELEDITING,this.editingSessionCode);
1955
                    notifyChange(FeatureStoreNotification.AFTER_CANCELEDITING);
1904 1956
                    break;
1905 1957

  
1906 1958
                case MODE_PASS_THROUGH:
......
1933 1985
    synchronized public void finishEditing() throws DataException {
1934 1986
        LOGGER.debug("finish editing of mode: {}", mode);
1935 1987
        try {
1936

  
1937
            /*
1938
             * Selection needs to be cleared when editing stops
1939
             * to prevent conflicts with selection remaining from
1940
             * editing mode.
1941
             */
1942
//            ((FeatureSelection) this.getSelection()).deselectAll();
1943 1988
            Map<String, List<FeatureAttributeDescriptor>> computedFields = this.getComputedFields();
1944 1989
            switch (mode) {
1945 1990
                case MODE_QUERY:
......
1949 1994
                    if (selection != null) {
1950 1995
                        selection = null;
1951 1996
                    }
1952
                    if (notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING, this.editingSessionCode).isCanceled()) {
1997
                    if (notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING).isCanceled()) {
1953 1998
                        return;
1954 1999
                    }
1955 2000
                    saveDALFile();
......
1958 2003
                    this.updateComputedFields(computedFields);
1959 2004
                    loadDALFile();
1960 2005
                    updateIndexes();
1961
                    notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING, this.editingSessionCode);
2006
                    notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
1962 2007
                    break;
1963 2008

  
1964 2009
                case MODE_FULLEDIT:
......
1966 2011
                        if (hasStrongChanges && !this.allowWrite()) {
1967 2012
                            throw new WriteNotAllowedException(getName());
1968 2013
                        }
1969
                        if (notifyChange(FeatureStoreNotification.PREPARING_FINISHEDITING,
1970
                                this.editingSessionCode).isCanceled()) {
2014
                        if (notifyChange(FeatureStoreNotification.PREPARING_FINISHEDITING).isCanceled()) {
1971 2015
                            return;
1972 2016
                        }
1973 2017
                        if (hasStrongChanges) {
1974 2018
                            validateFeaturesAtFinishEditing();
1975 2019
                        }
1976 2020
                        if (notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING,
1977
                                this.editingSessionCode,
1978 2021
                                featureManager.getDeleted(),
1979 2022
                                featureManager.getInsertedFeatures(),
1980 2023
                                featureManager.getUpdatedFeatures(),
......
1987 2030
                            selection = null;
1988 2031
                        }
1989 2032
                        if (hasStrongChanges) {
1990
                            /*
1991
                         * This will throw a PerformEditingExceptionif the provider
1992
                         * does not accept the changes (for example, an invalid field name)
1993
                             */
2033
                            // This will throw a PerformEditingException if the provider
2034
                            // does not accept the changes (for example, an invalid field name)
1994 2035
                            provider.performChanges(featureManager.getDeleted(),
1995 2036
                                    featureManager.getInserted(),
1996 2037
                                    featureManager.getUpdated(),
......
2001 2042
                        exitEditingMode();
2002 2043
                        loadDALFile();
2003 2044
                        updateIndexes();
2004
                        notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING, this.editingSessionCode);
2045
                        notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
2005 2046
                    } else {
2006 2047
                        exitEditingMode();
2007 2048
                    }
......
2010 2051
                    if (selection != null) {
2011 2052
                        selection = null;
2012 2053
                    }
2013
                    if (notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING, this.editingSessionCode).isCanceled()) {
2054
                    if (notifyChange(FeatureStoreNotification.BEFORE_FINISHEDITING).isCanceled()) {
2014 2055
                        return;
2015 2056
                    }
2016 2057
                    exitEditingMode();
2017 2058
                    updateIndexes();
2018
                    notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING, this.editingSessionCode);
2059
                    notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
2019 2060
                    break;
2020 2061
            }
2021 2062
        } catch (ValidateFeaturesException | WriteNotAllowedException ex) {
2022 2063
            // Don't notify failed.
2023 2064
            throw ex;
2024 2065
        } catch (PerformEditingException pee) {
2025
            notifyChange(FeatureStoreNotification.FAILED_FINISHEDITING, this.editingSessionCode);
2066
            notifyChange(FeatureStoreNotification.FAILED_FINISHEDITING);
2026 2067
            throw new WriteException(provider.getSourceId().toString(), pee);
2027 2068
        } catch (Exception e) {
2028
            notifyChange(FeatureStoreNotification.FAILED_FINISHEDITING, this.editingSessionCode);
2069
            notifyChange(FeatureStoreNotification.FAILED_FINISHEDITING);
2029 2070
            throw new FinishEditingException(e);
2030 2071
        }
2031 2072
    }
......
2347 2388
    //
2348 2389
    @Override
2349 2390
    public DataSet getDataSet() throws DataException {
2350
        checkNotInAppendMode();
2351
        FeatureQuery query
2352
                = new DefaultFeatureQuery(this.getDefaultFeatureType());
2353
        return new DefaultFeatureSet(this, query);
2391
        return this.getFeatureSet((FeatureQuery)null);
2354 2392
    }
2355 2393

  
2356 2394
    @Override
2357 2395
    public DataSet getDataSet(DataQuery dataQuery) throws DataException {
2358
        checkNotInAppendMode();
2359
        return new DefaultFeatureSet(this, (FeatureQuery) dataQuery);
2396
        return this.getFeatureSet((FeatureQuery)dataQuery);
2360 2397
    }
2361 2398

  
2362 2399
    @Override
......
2383 2420
        checkNotInAppendMode();
2384 2421
        if (featureQuery == null) {
2385 2422
            featureQuery = new DefaultFeatureQuery(this.getDefaultFeatureType());
2423
        } else if( featureQuery.hasAggregateFunctions() || featureQuery.hasGroupByColumns() ) {
2424
            // Si tenemos datos por persistir en la bbdd (bien por que estamos en modo 
2425
            // append(batchsize) o por que estamos en modo fulledit y hay cambios
2426
            // realizados, las agrupaciones y funciones de agregado, como las gestiona
2427
            // la bbdd, pueden no dar resultados correctos. Asi que no dejamos hacer
2428
            // estas operaciones.
2429
            if( this.mode==MODE_APPEND  ) {
2430
                throw new UnsupportedOperationException("Can't create a set with aggregate functions or group by columns in append mode");
2431
            }
2432
            if( this.mode == MODE_FULLEDIT ) {
2433
                if( this.featureManager==null || this.featureManager.getPendingChangesCount()>0 ) {
2434
                   throw new UnsupportedOperationException("Can't create a set with aggregate functions or group by columns with editing changes");
2435
                }
2436
            }
2386 2437
        }
2387 2438
        return new DefaultFeatureSet(this, featureQuery);
2388 2439
    }

Also available in: Unified diff