Revision 44190

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/H2SpatialExplorer.java
30 30
    public DataResource getResource(DataStore dataStore, String resourceName) throws DataException {
31 31
        String zipPath = this.getParameters().getFile().getAbsolutePath();
32 32
        zipPath = FilenameUtils.removeExtension(zipPath);
33
        zipPath = zipPath + "." + resourceName;
34
        H2SpatialResource resource = new H2SpatialResource(new File(zipPath), dataStore.getName());
33
        zipPath = zipPath + ".gvres";
34
        H2SpatialResource resource = new H2SpatialResource(new File(zipPath), dataStore.getName(),resourceName);
35 35
        return resource;
36 36
    }
37 37
    
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/H2SpatialResource.java
14 14
import java.nio.file.FileSystems;
15 15
import java.nio.file.Files;
16 16
import java.nio.file.Path;
17
import java.nio.file.StandardCopyOption;
17 18
import java.util.Collections;
18 19
import java.util.Objects;
19
import java.util.logging.Level;
20
import java.util.logging.Logger;
21 20
import org.apache.commons.io.IOUtils;
22 21
import org.gvsig.fmap.dal.AbstractDataResource;
23 22
import org.gvsig.fmap.dal.DataServerExplorer.DataResource;
23
import org.slf4j.LoggerFactory;
24 24

  
25 25
/**
26 26
 *
......
32 32
        implements DataResource 
33 33
    {
34 34
    
35
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(H2SpatialResource.class);
36
    
35 37
    private final File zipFile;
36 38
    private final String tableName;
37 39
    private InputStream in;
38 40
    private ByteArrayOutputStream out;
39 41
    private FileSystem zipfs;
40 42
    private final URI zipfsuri;
43
    private final String resourceName;
41 44

  
42
    public H2SpatialResource(File zipFile, String tableName) {
45
    public H2SpatialResource(File zipFile, String tableName, String resourceName) {
43 46
        this.zipFile = zipFile;
44 47
        this.tableName = tableName;
48
        this.resourceName = resourceName;
45 49
        try {
46 50
            this.zipfsuri = new URI("jar:" + this.zipFile.toURI().toString());
47 51
        } catch (URISyntaxException ex) {
......
49 53
        }
50 54
    }
51 55

  
52
    @Override
53 56
    public URL getURL() {
54 57
        try {
55
            return new URL(this.zipfsuri.toString()+"!"+this.tableName);
58
            return new URL(this.zipfsuri.toString()+"!/"+this.tableName+"."+this.resourceName);
56 59
        } catch (MalformedURLException ex) {
57 60
            return null;
58 61
        }
......
71 74
        }
72 75
        try {
73 76
            this.openzipfs(false);
74
            Path tablePath = this.zipfs.getPath(this.tableName);
77
            Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
75 78
            if (tablePath == null) {
76 79
                return false;
77 80
            }
78
            return true;
81
            if( Files.exists(tablePath) ) {
82
                return true;
83
            }
84
            return false;
79 85
        } catch (Exception ex) {
86
            LOGGER.warn("Can't access to the resource ("+Objects.toString(this.getURL())+").", ex);
80 87
            return false;
81 88
        }
82 89
    }
......
91 98
        }
92 99
        try {
93 100
            this.openzipfs(false);
94
            Path tablePath = this.zipfs.getPath(this.tableName);
101
            Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
95 102
            if (tablePath == null) {
96 103
                return null;
97 104
            }
98 105
            this.in = Files.newInputStream(tablePath);
99 106
            return this.in;
100 107
        } catch (Exception ex) {
108
            LOGGER.warn("Can't create input stream ("+Objects.toString(this.getURL())+").", ex);
101 109
            return null;
102 110
        }
103 111
    }
......
105 113
    @Override
106 114
    public OutputStream asOutputStream() throws IOException {
107 115
        if (this.in != null || this.out != null) {
108
            throw new IllegalStateException("Resource is already open (" + this.zipFile.toString() + "!" + this.tableName + ")");
116
            throw new IllegalStateException("Resource is already open ("+Objects.toString(this.getURL())+").");
109 117
        }
110 118
        this.out = new ByteArrayOutputStream();
111 119
        return this.out;
......
121 129
            ByteArrayInputStream pipe = null;
122 130
            try {
123 131
                this.openzipfs(true);
124
                Path tablePath = this.zipfs.getPath(this.tableName);
132
                Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
125 133
                pipe = new ByteArrayInputStream(this.out.toByteArray());
126
                Files.copy(pipe, tablePath);
134
                Files.copy(pipe, tablePath, StandardCopyOption.REPLACE_EXISTING);
127 135
            } catch (Exception ex) {
136
                LOGGER.warn("Can't write resource ("+Objects.toString(this.getURL())+").", ex);
128 137
            } finally {
129 138
                IOUtils.closeQuietly(pipe);
130 139
                IOUtils.closeQuietly(this.out);
......
133 142
        }
134 143
        if (this.zipfs != null) {
135 144
            IOUtils.closeQuietly(this.zipfs);
145
            this.zipfs = null;
136 146
        }
137 147
    }
138 148
    
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/spi/operations/PerformChangesOperation.java
286 286
                    attrOrgiginal.getName()
287 287
            );
288 288
            if (attrTarget == null) {
289
                if( attrOrgiginal.isComputed() ) {
290
                    continue;
291
                }
289 292
                sqlbuilder.alter_table().drop_column(attrOrgiginal.getName());
290 293
            } else if( !this.areEquals(attrOrgiginal, attrTarget) ) {
294
                if( attrTarget.isComputed() ) {
295
                    continue;
296
                }
291 297
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
292 298
                    sqlbuilder.alter_table().alter_geometry_column(
293 299
                            attrTarget.getName(),
......
313 319
            }
314 320
        }
315 321
        for (FeatureAttributeDescriptor attrTarget : target) {
322
            if( attrTarget.isComputed() ) {
323
                continue;
324
            }
316 325
            if (original.getAttributeDescriptor(attrTarget.getName()) == null) {
317 326
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
318 327
                    sqlbuilder.alter_table().add_geometry_column(
......
433 442
            try {
434 443
                st = conn.createStatement();
435 444
                for (String sql : sqls) {
436
                    JDBCUtils.execute(st, sql);
445
                    if( !StringUtils.isBlank(sql) ) {
446
                        JDBCUtils.execute(st, sql);
447
                    }
437 448
                }
438 449
            } catch (SQLException e) {
439 450
                throw new JDBCSQLException(e);
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.api/src/main/java/org/gvsig/fmap/dal/swing/expressionevaluator/FeatureStoreElement.java
9 9
 */
10 10
public interface FeatureStoreElement extends Element.GroupElement {
11 11

  
12
    public void setName(String name);
13
    
12 14
    public void setFeatureStore(FeatureStore store);
13 15
    
14 16
    public FeatureStore getFeatureStore();
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/fmap/dal/swing/impl/expressionevaluator/DefaultFeatureStoreElement.java
6 6
import java.util.Comparator;
7 7
import java.util.Iterator;
8 8
import java.util.List;
9
import org.apache.commons.lang3.StringUtils;
9 10
import org.gvsig.expressionevaluator.Function;
10 11
import org.gvsig.expressionevaluator.swing.Element;
12
import org.gvsig.expressionevaluator.swing.Element.SimpleElement;
11 13
import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig;
12 14
import org.gvsig.expressionevaluator.swing.spi.AbstractElement;
13 15
import org.gvsig.fmap.dal.exception.DataException;
......
21 23
 */
22 24
public class DefaultFeatureStoreElement 
23 25
        extends AbstractElement
24
        implements FeatureStoreElement 
26
        implements FeatureStoreElement , SimpleElement
25 27
{
26 28

  
27 29
    private FeatureStore store;
28 30
    private FeatureType type;
29 31
    private List<Element> elements;
30 32
    final ExpressionBuilderConfig configPanel;
33
    private String myName;
31 34

  
32 35
    @SuppressWarnings("OverridableMethodCallInConstructor")
33 36
    public DefaultFeatureStoreElement(ExpressionBuilderConfig configPanel, FeatureStore store) {
......
44 47
    }
45 48

  
46 49
    @Override
50
    public void setName(String name) {
51
        this.myName = name;
52
    }
53
    
54
    @Override
47 55
    public String getName() {
56
        if( !StringUtils.isBlank(this.myName) ) {
57
            return this.myName;
58
        }
48 59
        if (this.store == null) {
49 60
            return super.getName();
50 61
        }
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/DefaultEditableFeatureAttributeDescriptor.java
69 69
        hasStrongChanges = false;
70 70
    }
71 71

  
72
    public DefaultEditableFeatureAttributeDescriptor(FeatureType type) {
72
    public DefaultEditableFeatureAttributeDescriptor(FeatureType type, boolean strongChanges) {
73 73
        super(type);
74 74
        this.source = null;
75
        hasStrongChanges = false;
75
        hasStrongChanges = strongChanges;
76 76
    }
77 77

  
78 78
    public DefaultFeatureAttributeDescriptor getSource() {
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
24 24

  
25 25
package org.gvsig.fmap.dal.feature.impl;
26 26

  
27
import java.io.File;
28 27
import org.gvsig.fmap.dal.feature.impl.editing.memory.SpatialManager;
29 28
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureTypeManager;
30 29
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureManager;
31
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase;
32 30

  
33 31
import java.util.ArrayList;
34 32
import java.util.Collection;
......
40 38
import java.util.Map;
41 39
import java.util.Map.Entry;
42 40
import java.util.Set;
43
import java.util.logging.Level;
44 41

  
45 42
import org.apache.commons.io.FilenameUtils;
43
import org.apache.commons.io.IOUtils;
46 44
import org.apache.commons.lang3.StringUtils;
45
import org.apache.commons.lang3.mutable.MutableObject;
47 46
import org.cresques.cts.IProjection;
48 47
import org.gvsig.expressionevaluator.Expression;
49 48
import org.gvsig.expressionevaluator.ExpressionBuilder;
......
135 134
import org.gvsig.fmap.dal.spi.DataStoreInitializer2;
136 135
import org.gvsig.fmap.dal.spi.DataStoreProvider;
137 136
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
137
import org.gvsig.fmap.geom.Geometry;
138 138
import org.gvsig.fmap.geom.SpatialIndex;
139 139
import org.gvsig.fmap.geom.primitive.Envelope;
140 140
import org.gvsig.metadata.MetadataLocator;
......
167 167
import org.gvsig.tools.undo.UndoException;
168 168
import org.gvsig.tools.undo.command.Command;
169 169
import org.gvsig.tools.util.HasAFile;
170
import org.gvsig.tools.visitor.VisitCanceledException;
170 171
import org.gvsig.tools.visitor.Visitor;
171 172

  
172 173
import org.slf4j.Logger;
173 174
import org.slf4j.LoggerFactory;
174 175

  
176
@SuppressWarnings("UseSpecificCatch")
175 177
public class DefaultFeatureStore extends AbstractDisposable implements
176 178
    DataStoreInitializer2, FeatureStoreProviderServices, FeatureStore, Observer {
177 179

  
......
484 486
        if (hasDynValue(DataStore.METADATA_ENVELOPE)){
485 487
            return (Envelope)getDynValue(DataStore.METADATA_ENVELOPE);
486 488
        }
487
        return this.provider.getEnvelope();
489
        Envelope envelope = this.provider.getEnvelope();
490
        if( envelope!=null ) {
491
            return envelope;
492
        }
493
        FeatureAttributeDescriptor attrdesc = this.getDefaultFeatureType().getDefaultGeometryAttribute();
494
        if( attrdesc == null || !attrdesc.isComputed() ) {
495
            return null;
496
        }
497
        final int index = attrdesc.getIndex();
498
        final MutableObject<Envelope> envelopeValue = new MutableObject<>();
499
        try {
500
            this.accept(new Visitor() {
501
                @Override
502
                public void visit(Object obj) throws VisitCanceledException, BaseException {
503
                    Feature f = (Feature) obj;
504
                    Geometry g =  (Geometry) f.get(index);
505
                    if( g == null ) {
506
                        return;
507
                    }
508
                    if( envelopeValue.getValue()==null ) {
509
                        envelopeValue.setValue(g.getEnvelope());
510
                    } else {
511
                        envelopeValue.getValue().add(g);
512
                    }
513
                }
514
            });
515
        } catch (Throwable th) {
516
            LOG.warn("Can't calculate envelope", th);
517
            return null;
518
        }
519
        return envelopeValue.getValue();
488 520
    }
489 521

  
490 522
    /**
......
1548 1580
    }
1549 1581
    
1550 1582

  
1551
    @SuppressWarnings("UseSpecificCatch")
1552 1583
    private void saveDALFile() {       
1584
        DataResource resource = null;
1553 1585
        try {
1554 1586
            DataServerExplorer explorer = this.getExplorer();
1555 1587
            if( explorer == null ) {
1556 1588
                return;
1557 1589
            }
1558
            DataResource resource = explorer.getResource(this, "dal");
1590
            resource = explorer.getResource(this, "dal");
1559 1591
            if( resource == null ) {
1560 1592
                return;
1561 1593
            }
......
1564 1596
            if( !dalFile.isEmpty() ) {
1565 1597
                dalFile.write(resource);
1566 1598
            }
1567
        } catch (Exception ex) {
1568
            LOG.warn("Can't save DAL File", ex);
1599
        } catch (Throwable ex) {
1600
            LOG.warn("Can't save DAL resource", ex);
1601
        } finally {
1602
            IOUtils.closeQuietly(resource);
1569 1603
        }
1570 1604
    }
1571 1605
    
1572
    @SuppressWarnings("UseSpecificCatch")
1573 1606
    private void loadDALFile() {
1607
        DataResource resource = null;
1574 1608
        try {
1575 1609
            DataServerExplorer explorer = this.getExplorer();
1576 1610
            if( explorer == null ) {
1577 1611
                return;
1578 1612
            }
1579
            DataResource resource = explorer.getResource(this, "dal");
1613
            resource = explorer.getResource(this, "dal");
1580 1614
            if( resource == null || !resource.exists() ) {
1581 1615
                return;
1582 1616
            }
......
1584 1618
            if( !dalFile.isEmpty() ) {
1585 1619
                dalFile.updateStore(this);
1586 1620
            }
1587
        } catch (Exception ex) {
1588
            LOG.warn("Can't load DAL File", ex);
1621
        } catch (Throwable ex) {
1622
            LOG.warn("Can't load DAL resource", ex);
1623
        } finally {
1624
            IOUtils.closeQuietly(resource);
1589 1625
        }
1590 1626
    }
1591 1627
    
......
1813 1849
    public FeatureSet getFeatureSet(Expression filter, String sortBy) throws DataException {
1814 1850
        return this.getFeatureSet(filter, sortBy, true);
1815 1851
    }
1816
    
1852

  
1817 1853
    @Override
1818 1854
    public FeatureSet getFeatureSet(Expression filter, String sortBy, boolean asc) throws DataException {
1819
        FeatureQuery query = this.createFeatureQuery();
1820
        if( filter!=null ) {
1821
            query.setFilter(filter);
1822
        }
1823
        if( !StringUtils.isEmpty(sortBy) ) {
1824
            query.getOrder().add(sortBy, asc);
1825
        }
1826
        query.retrievesAllAttributes();
1855
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
1827 1856
        return this.getFeatureSet(query);
1828 1857
    }
1829 1858
    
1830 1859
    @Override
1831 1860
    public FeatureSet getFeatureSet(String filter, String sortBy, boolean asc) throws DataException {
1832
        FeatureQuery query = null;
1833
        if( !StringUtils.isEmpty(filter) ) {
1834
            query = this.createFeatureQuery();
1835
            query.setFilter(filter);
1836
        }
1837
        if( !StringUtils.isEmpty(sortBy) ) {
1838
            if( query == null ) {
1839
                query = this.createFeatureQuery();
1840
            }
1841
            query.getOrder().add(sortBy, asc);
1842
        }
1843
        if( query == null ) {
1844
            return this.getFeatureSet();
1845
        }
1846
        query.retrievesAllAttributes();
1861
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
1847 1862
        return this.getFeatureSet(query);
1848 1863
    }
1849 1864
    
......
1859 1874

  
1860 1875
    @Override
1861 1876
    public List<Feature> getFeatures(String filter, String sortBy, boolean asc)  {
1862
        FeatureQuery query = this.createFeatureQuery();
1863
        if( !StringUtils.isEmpty(filter) ) {
1864
            query.setFilter(filter);
1865
        }
1866
        if( !StringUtils.isEmpty(sortBy) ) {
1867
            query.getOrder().add(sortBy, asc);
1868
        }
1877
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
1869 1878
        return this.getFeatures(query, 100);
1870 1879
    }
1871 1880
    
......
1881 1890

  
1882 1891
    @Override
1883 1892
    public List<Feature> getFeatures(Expression filter, String sortBy, boolean asc)  {
1884
        FeatureQuery query = this.createFeatureQuery();
1885
        if( filter!=null ) {
1886
            query.setFilter(filter);
1887
        }
1888
        if( !StringUtils.isEmpty(sortBy) ) {
1889
            query.getOrder().add(sortBy, asc);
1890
        }
1893
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
1891 1894
        return this.getFeatures(query, 100);
1892 1895
    }
1893 1896
    
......
1913 1916

  
1914 1917
    @Override
1915 1918
    public Feature first() throws DataException {
1916
        return this.findFirst((String)null, null, true);
1919
        return this.findFirst((FeatureQuery)null);
1917 1920
    }
1918 1921
    
1919 1922
    @Override
......
1928 1931

  
1929 1932
    @Override
1930 1933
    public Feature findFirst(String filter, String sortBy, boolean asc) throws DataException {
1931
        FeatureSet set = this.getFeatureSet(filter, sortBy, asc);
1932
        if( set==null || set.isEmpty() ) {
1933
            return null;
1934
        }
1935
        DisposableIterator it = set.iterator();
1936
        Feature f = (Feature) it.next();
1937
        it.dispose();
1938
        return f;
1934
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
1935
        return findFirst(query);
1939 1936
    }
1940 1937
    
1941 1938
    @Override
......
1950 1947

  
1951 1948
    @Override
1952 1949
    public Feature findFirst(Expression filter, String sortBy, boolean asc) throws DataException {
1953
        FeatureSet set = this.getFeatureSet(filter, sortBy, asc);
1954
        if( set==null || set.isEmpty() ) {
1950
        FeatureQuery query = this.createFeatureQuery(filter, sortBy, asc);
1951
        return findFirst(query);
1952
    }
1953
    
1954
    public Feature findFirst(FeatureQuery query) throws DataException {
1955
        final MutableObject<Feature> feature = new MutableObject<>();
1956
        try {
1957
            this.accept(new Visitor() {
1958
                @Override
1959
                public void visit(Object obj) throws VisitCanceledException, BaseException {
1960
                    feature.setValue((Feature) obj);
1961
                    throw new VisitCanceledException();
1962
                }
1963
            }, query);
1955 1964
            return null;
1965
        } catch(VisitCanceledException ex) {
1966
            return feature.getValue();
1967
        } catch(DataException ex) {
1968
            throw ex;
1969
        } catch(Exception ex) {
1970
            throw new RuntimeException("", ex);
1956 1971
        }
1957
        DisposableIterator it = set.iterator();
1958
        Feature f = (Feature) it.next();
1959
        it.dispose();
1960
        return f;
1961 1972
    }
1962
    
1973

  
1963 1974
    @Override
1964 1975
    public void accept(Visitor visitor) throws BaseException {
1965
        FeatureSet set = getFeatureSet();
1966
        try {
1967
            set.accept(visitor);
1968
        } finally {
1969
            set.dispose();
1970
        }
1976
        this.accept(visitor, null);
1971 1977
    }
1972 1978

  
1973 1979
    @Override
......
2222 2228
            throw new GetFeatureTypeException(e, getName());
2223 2229
        }
2224 2230
    }
2225

  
2231
    
2226 2232
    private FeatureType avoidEditable(FeatureType ft) {
2227 2233
        if (ft instanceof EditableFeatureType) {
2228 2234
            return ((EditableFeatureType) ft).getNotEditableCopy();
......
2479 2485
    public FeatureQuery createFeatureQuery() {
2480 2486
        return new DefaultFeatureQuery();
2481 2487
    }
2482

  
2488
    
2489
    private FeatureQuery createFeatureQuery(Expression filter, String sortBy, boolean asc) {
2490
        FeatureQuery query = null;
2491
        if( filter!=null ) {
2492
            query = this.createFeatureQuery();
2493
            query.setFilter(filter);
2494
        }
2495
        if( !StringUtils.isEmpty(sortBy) ) {
2496
            if( query == null ) {
2497
                query = this.createFeatureQuery();
2498
            }
2499
            query.getOrder().add(sortBy, asc);
2500
        }
2501
        if( query != null ) {
2502
            query.retrievesAllAttributes();
2503
        }
2504
        return query;
2505
    }
2506
    
2507
    private FeatureQuery createFeatureQuery(String filter, String sortBy, boolean asc) {
2508
        FeatureQuery query = null;
2509
        if( !StringUtils.isEmpty(filter) ) {
2510
            query = this.createFeatureQuery();
2511
            query.setFilter(filter);
2512
        }
2513
        if( !StringUtils.isEmpty(sortBy) ) {
2514
            if( query == null ) {
2515
                query = this.createFeatureQuery();
2516
            }
2517
            query.getOrder().add(sortBy, asc);
2518
        }
2519
        if( query != null ) {
2520
            query.retrievesAllAttributes();
2521
        }
2522
        return query;
2523
    }
2524
    
2483 2525
    @Override
2484 2526
    public DataQuery createQuery() {
2485 2527
        return createFeatureQuery();
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/DefaultFeatureAttributeDescriptor.java
31 31
import java.util.Map.Entry;
32 32
import java.util.Objects;
33 33
import org.apache.commons.lang3.ArrayUtils;
34
import org.apache.commons.lang3.StringUtils;
34 35
import org.cresques.cts.IProjection;
35
import org.gvsig.fmap.crs.CRSFactory;
36 36
import org.gvsig.fmap.dal.DataStore;
37 37
import org.gvsig.fmap.dal.DataTypes;
38
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
39 38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40 39
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
41 40
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
......
44 43
import org.gvsig.fmap.geom.Geometry;
45 44
import org.gvsig.fmap.geom.GeometryException;
46 45
import org.gvsig.fmap.geom.GeometryLocator;
46
import org.gvsig.fmap.geom.GeometryUtils;
47 47
import org.gvsig.fmap.geom.type.GeometryType;
48 48
import org.gvsig.timesupport.Interval;
49
import org.gvsig.timesupport.RelativeInterval;
49 50
import org.gvsig.timesupport.TimeSupportLocator;
50 51
import org.gvsig.tools.ToolsLocator;
51 52
import org.gvsig.tools.dataTypes.CoercionException;
......
66 67
import org.gvsig.tools.evaluator.Evaluator;
67 68
import org.gvsig.tools.evaluator.EvaluatorData;
68 69
import org.gvsig.tools.evaluator.EvaluatorException;
70
import org.gvsig.tools.persistence.PersistenceManager;
69 71
import org.gvsig.tools.persistence.Persistent;
70 72
import org.gvsig.tools.persistence.PersistentState;
71 73
import org.gvsig.tools.persistence.exception.PersistenceException;
74
import org.slf4j.Logger;
75
import org.slf4j.LoggerFactory;
72 76

  
73 77
public class DefaultFeatureAttributeDescriptor implements
74 78
        FeatureAttributeDescriptor, Persistent, DynField_v2, DynField_LabelAttribute {
75 79

  
80
    protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureAttributeDescriptor.class);
81
    
76 82
    protected boolean allowNull;
77 83
    protected DataType dataType;
78 84
    protected String dataProfile; 
......
115 121
    private DynMethod calculateMethod;
116 122
    private WeakReference typeRef;
117 123

  
124
    public DefaultFeatureAttributeDescriptor() {
125
        // Usada en la persistencia
126
    }
127

  
118 128
    protected DefaultFeatureAttributeDescriptor(FeatureType type) {
119
        if( type == null ) {
120
            this.typeRef = null;
121
        } else {
122
            this.typeRef = new WeakReference(type);
123
        }
129
        setFeatureType(type);
124 130
        this.allowNull = true;
125 131
        this.dataType = null;
126 132
        this.dateFormat = null;
......
195 201
        this.dataProfile = other.dataProfile;
196 202
    }
197 203
    
204
    public void setFeatureType(FeatureType type) {
205
        // Usada en la persistencia
206
        if( type == null ) {
207
            this.typeRef = null;
208
        } else {
209
            this.typeRef = new WeakReference(type);
210
        }
211
    }
212
    
198 213
    @Override
199 214
    public String getDataTypeName() {
200 215
        if (this.getDataType() == null) {
......
480 495
            throws PersistenceException {
481 496
        allowNull = state.getBoolean("allowNull");
482 497
        dataType = ToolsLocator.getDataTypesManager().get(state.getInt("dataType"));
483
        // FIXME how persist dateFormat ???
484
        // dateFormat;
485
        defaultValue = state.get("defaultValue");
498
        dataProfile = state.getString("dataProfile");
499
        
500
//        FIXME: dateFormat;
501
        try {
502
            defaultValue = dataType.coerce(state.get("defaultValue"));
503
        } catch (CoercionException ex) {
504
        }
486 505

  
487 506
        index = state.getInt("index");
488 507
        maximumOccurrences = state.getInt("maximumOccurrences");
......
490 509
        size = state.getInt("size");
491 510
        name = state.getString("name");
492 511
        try {
493
            objectClass = Class.forName(state.getString("objectClass"));
494
        } catch (ClassNotFoundException e) {
495
            throw new PersistenceException(e);
512
            String objectClassName = state.getString("objectClass"); 
513
            if( !StringUtils.isBlank(objectClassName) ) { 
514
                objectClass = Class.forName(objectClassName); 
515
            }
516
        } catch (Throwable e) {
517
            LOGGER.warn("Can't restore the objectClass of the FeatureAttributreDescriptor", e);
496 518
        }
497 519
        precision = state.getInt("precision");
498 520
        evaluator = (Evaluator) state.get("evaluator");
499 521
        primaryKey = state.getBoolean("primaryKey");
500 522
        readOnly = state.getBoolean("readOnly");
501
        String srsId = state.getString("srsId");
502
        if (srsId != null) {
503
            SRS = CRSFactory.getCRS(srsId);
504
        }
523
        SRS = (IProjection) state.get("SRS");
505 524
        geometryType = state.getInt("geometryType");
506 525
        geometrySubType = state.getInt("geometrySubType");
507
        additionalInfo = (Map) state.get("aditionalInfo");
526
        if( geometryType!=Geometry.TYPES.UNKNOWN && 
527
                geometrySubType!=Geometry.SUBTYPES.UNKNOWN ) {
528
            geomType = GeometryUtils.getGeometryType(
529
                    geometryType, 
530
                    geometrySubType
531
            );
532
        }
533
//        additionalInfo = (Map) state.get("aditionalInfo");
508 534
        isAutomatic = state.getBoolean("isAutomatic");
509 535
        isTime = state.getBoolean("isTime");
510 536
        if( state.hasValue("intervalStart") ) {
511
            long intervalStart = state.getLong("intervalStart");
512
            long intervalEnd = state.getLong("intervalEnd");
537
            long intervalStart = state.getLong("interval_start");
538
            long intervalEnd = state.getLong("interval_end");
513 539
            interval = TimeSupportLocator.getManager().createRelativeInterval(intervalStart, intervalEnd);
514 540
        } else {
515 541
            interval = null;
516 542
        }
543
        featureAttributeEmulator = (FeatureAttributeEmulator) state.get("featureAttributeEmulator");
544
        indexed = state.getBoolean("indexed");
545
        isIndexAscending = state.getBoolean("isIndexAscending");
546
        allowIndexDuplicateds = state.getBoolean("allowIndexDuplicateds");
547

  
548
//        FIXME: availableValues 
549

  
550
        description = state.getString("description");
551
        minValue = state.get("minValue");
552
        maxValue = state.get("maxValue");
553
        label = state.getString("label");
554
        order = state.getInt("order");
555
        hidden = state.getBoolean("hidden");
556
        groupName = state.getString("groupName");
517 557
    }
518 558

  
519 559
    @Override
520 560
    public void saveToState(PersistentState state) throws PersistenceException {
521 561
        state.set("allowNull", allowNull);
522
        state.set("dataType", dataType);
523
        // FIXME how persist dateFormat ???
524
        // dateFormat;
562
        state.set("dataType", dataType.getType());
563
        state.set("dataProfile", dataProfile);
564
        
565
//        FIXME: dateFormat;
525 566

  
526
        defaultValue = state.get("defaultValue");
567
        state.set("defaultValue", Objects.toString(defaultValue));
527 568

  
528
        index = state.getInt("index");
529
        maximumOccurrences = state.getInt("maximumOccurrences");
530
        minimumOccurrences = state.getInt("minimumOccurrences");
531
        size = state.getInt("size");
532
        name = state.getString("name");
533
        try {
534
            objectClass = Class.forName(state.getString("objectClass"));
535
        } catch (ClassNotFoundException e) {
536
            throw new PersistenceException(e);
569
        state.set("index", index);
570
        state.set("maximumOccurrences", maximumOccurrences);
571
        state.set("minimumOccurrences", minimumOccurrences);
572
        state.set("size", size);
573
        state.set("name", name);
574
        state.set("objectClass", objectClass==null? null:objectClass.getName());
575
        state.set("precision", precision);
576
        state.set("evaluator", evaluator);
577
        
578
        state.set("primaryKey", primaryKey);
579
        state.set("readOnly", readOnly);
580
        state.set("SRS", SRS);
581
        GeometryType theGeomType = this.getGeomType();
582
        if( theGeomType==null ) {
583
            state.set("geometryType", Geometry.TYPES.UNKNOWN);
584
            state.set("geometrySubType", Geometry.SUBTYPES.UNKNOWN);
585
        } else {
586
            state.set("geometryType", theGeomType.getType());
587
            state.set("geometrySubType", theGeomType.getSubType());
537 588
        }
538
        precision = state.getInt("precision");
539
        evaluator = (Evaluator) state.get("evaluator");
540
        primaryKey = state.getBoolean("primaryKey");
541
        readOnly = state.getBoolean("readOnly");
542
        String srsId = state.getString("srsId");
543
        if (srsId != null) {
544
            SRS = CRSFactory.getCRS(srsId);
589

  
590
//      FIXME: additionalInfo
591

  
592
        state.set("isAutomatic", isAutomatic);
593
        state.set("isTime", isTime);
594
        if( this.interval==null ) {
595
            state.setNull("interval_start");
596
            state.setNull("interval_end");
597
        } else {
598
            state.set("interval_start", ((RelativeInterval)interval).getStart().toMillis());
599
            state.set("interval_end", ((RelativeInterval)interval).getEnd().toMillis());
545 600
        }
546
        geometryType = state.getInt("geometryType");
547
        geometrySubType = state.getInt("geometrySubType");
548
        additionalInfo = (Map) state.get("aditionalInfo");
549
        isAutomatic = state.getBoolean("isAutomatic");
601
        state.set("SRS", SRS);
602

  
603
//      FIXME: featureAttributeGetter
604

  
605
        if( featureAttributeEmulator instanceof Persistent ) {
606
            state.set("featureAttributeEmulator", featureAttributeEmulator);
607
        } else {
608
            state.setNull("featureAttributeEmulator");
609
        }
610

  
611
        state.set("indexed", indexed);
612
        state.set("isIndexAscending", isIndexAscending);
613
        state.set("allowIndexDuplicateds", allowIndexDuplicateds);
614
        
615
//        FIXME: availableValues 
616

  
617
        state.set("description", description);
618
        state.set("minValue", minValue);
619
        state.set("maxValue", maxValue);
620
        state.set("label", label);
621
        state.set("order", order);
622
        state.set("hidden", hidden);
623
        state.set("groupName", groupName);
624

  
550 625
    }
626
    
627
    private static final String FEATATTRDESC_PERSISTENCE_DEFINITION_NAME = "FeatureAttributeDescriptor";
551 628

  
629
    public static void registerPersistenceDefinition() {
630
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
631
        
632

  
633
        if (manager.getDefinition(FEATATTRDESC_PERSISTENCE_DEFINITION_NAME)
634
                == null) {
635
            DynStruct definition = manager.addDefinition(DefaultFeatureAttributeDescriptor.class,
636
                    FEATATTRDESC_PERSISTENCE_DEFINITION_NAME,
637
                    FEATATTRDESC_PERSISTENCE_DEFINITION_NAME
638
                        + " persistent definition",
639
                    null,
640
                    null
641
            );
642
            definition.addDynFieldBoolean("allowNull");
643
            definition.addDynFieldInt("dataType");
644
            definition.addDynFieldString("dataProfile");
645
//            definition.addDynFieldString("dateFormat");
646
            definition.addDynFieldString("defaultValue");
647
            definition.addDynFieldInt("index");
648
            definition.addDynFieldInt("maximumOccurrences");
649
            definition.addDynFieldInt("minimumOccurrences");
650
            definition.addDynFieldInt("size");
651
            definition.addDynFieldString("name");
652
            definition.addDynFieldString("objectClass");
653
            definition.addDynFieldInt("precision");
654
            definition.addDynFieldObject("evaluator")
655
                    .setClassOfValue(Evaluator.class);
656
            definition.addDynFieldBoolean("primaryKey");
657
            definition.addDynFieldBoolean("readOnly");
658
            definition.addDynFieldObject("SRS")
659
                    .setClassOfValue(IProjection.class);
660
            definition.addDynFieldInt("geometryType");
661
            definition.addDynFieldInt("geometrySubType");
662
//            definition.addDynFieldMap("additionalInfo");
663
            definition.addDynFieldBoolean("isAutomatic");
664
            definition.addDynFieldBoolean("isTime");
665
            definition.addDynFieldLong("interval_start");
666
            definition.addDynFieldLong("interval_end");
667
            definition.addDynFieldObject("featureAttributeEmulator")
668
                    .setClassOfValue(FeatureAttributeEmulator.class);
669
            definition.addDynFieldBoolean("indexed");
670
            definition.addDynFieldBoolean("isIndexAscending");
671
            definition.addDynFieldBoolean("allowIndexDuplicateds");
672
//            definition.addDynFieldInt("availableValues");
673
            definition.addDynFieldInt("description");
674
            definition.addDynFieldInt("minValue");
675
            definition.addDynFieldInt("maxValue");
676
            definition.addDynFieldInt("label");
677
            definition.addDynFieldInt("order");
678
            definition.addDynFieldBoolean("hidden");
679
            definition.addDynFieldInt("groupName");
680
        }
681
    }
682

  
683
    
552 684
    /*
553 685
     * Start of DynField interface Implementation
554 686
     *
......
1033 1165

  
1034 1166
    @Override
1035 1167
    public boolean isComputed() {
1036
        return featureAttributeEmulator!=null || evaluator!=null;
1168
        return featureAttributeEmulator!=null || evaluator!=null || isCalculated();
1037 1169
    }
1038 1170

  
1039 1171
    @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/DefaultEditableFeatureType.java
159 159
        return this.add(name, type, true);
160 160
    }
161 161

  
162
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) {
163
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this);
162
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
163
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this, strongChanges);
164 164
        Iterator iter = this.iterator();
165 165
        while (iter.hasNext()) {
166 166
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
......
197 197
        }
198 198
        attr.setDataType(type);
199 199
        attr.setIndex(this.size());
200
        
200 201
        super.add(attr);
201
        if (!hasStrongChanges && updateHasStrongChanges) {
202
            hasStrongChanges = true;
203
        }
204 202
        this.pk = null;
205 203
        return attr;
206 204
    }
......
252 250
        if (attr == null) {
253 251
            return null;
254 252
        }
255
        if (attr.getEvaluator() == null) {
253
        if ( !attr.isComputed() ) {
256 254
            hasStrongChanges = true;
257 255
        }
258 256
        super.remove(attr);
......
263 261

  
264 262
    @Override
265 263
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
266
        if (attribute.getEvaluator() != null) {
264
        if ( !attribute.isComputed() ) {
267 265
            hasStrongChanges = true;
268 266
        }
269 267
        if (!super.remove(attribute)) {
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/DALFile.java
4 4
import java.io.InputStream;
5 5
import java.io.OutputStream;
6 6
import java.util.ArrayList;
7
import java.util.HashSet;
8
import java.util.Iterator;
9 7
import java.util.List;
10
import java.util.Set;
11 8
import org.apache.commons.collections4.CollectionUtils;
12
import org.apache.commons.io.IOUtils;
13 9
import org.apache.commons.lang3.StringUtils;
14 10
import org.gvsig.fmap.dal.DataServerExplorer.DataResource;
15 11
import org.gvsig.fmap.dal.exception.DataException;
16
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
17
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
18
import org.gvsig.fmap.dal.feature.FeatureStore;
19 12
import org.gvsig.fmap.dal.feature.FeatureType;
20
import org.gvsig.timesupport.RelativeInterval;
21
import org.gvsig.timesupport.TimeSupportLocator;
22 13
import org.gvsig.tools.ToolsLocator;
23 14
import org.gvsig.tools.dynobject.DynStruct;
24 15
import org.gvsig.tools.persistence.PersistenceManager;
25 16
import org.gvsig.tools.persistence.Persistent;
26 17
import org.gvsig.tools.persistence.PersistentState;
27 18
import org.gvsig.tools.persistence.exception.PersistenceException;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
28 21

  
29 22

  
30 23
@SuppressWarnings("UseSpecificCatch")
31 24
public class DALFile implements Persistent {
32 25

  
33
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALFile";
34
    private static final String ATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileAttribute";
26
    private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class);
35 27
    
28
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources";
29
    
36 30
    public static DALFile getDALFile() {
37 31
        DALFile f = new DALFile();
38 32
        return f;
......
54 48
                null, 
55 49
                null
56 50
            );
57
            definition.addDynFieldList("attributes")
58
                .setClassOfItems(Attribute.class)
59
                .setMandatory(true)
51
            definition.addDynFieldString("defaultFeatureType");
52
            definition.addDynFieldList("featureTypes")
53
                .setClassOfItems(FeatureType.class)
60 54
                .setPersistent(true);
61 55
        }
62
        if (manager.getDefinition(ATTRIBUTE_PERSISTENCE_DEFINITION_NAME) == null) {
63
            DynStruct definition = manager.addDefinition(
64
                Attribute.class,
65
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME, 
66
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
67
                null, 
68
                null
69
            );
70
            definition.addDynFieldString("name")
71
                .setMandatory(true)
72
                .setPersistent(true);
73
            definition.addDynFieldInt("type")
74
                .setMandatory(true)
75
                .setPersistent(true);
76
            definition.addDynFieldInt("featureTypeIndex")
77
                .setMandatory(true)
78
                .setPersistent(true);
79
            definition.addDynFieldObject("emulator")
80
                .setClassOfValue(FeatureAttributeEmulator.class)
81
                .setMandatory(false)
82
                .setPersistent(true);
83
            definition.addDynFieldLong("IntervalStart")
84
                .setMandatory(false)
85
                .setPersistent(true);
86
            definition.addDynFieldLong("IntervalEnd")
87
                .setMandatory(false)
88
                .setPersistent(true);
89
            definition.addDynFieldString("dataProfile")
90
                .setMandatory(false)
91
                .setPersistent(true);
92
        }
93 56
    }
94 57

  
95 58
    
96
    public static class Attribute implements Persistent {
97

  
98
        private String name;
99
        private int type;
100
        private FeatureAttributeEmulator emulator;
101
        private int featureTypeIndex;
102
        private RelativeInterval interval;
103
        private String dataProfile;
104
        
105
        public Attribute() {
106
            
107
        }
108

  
109
        private Attribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) {
110
            this.featureTypeIndex = featureTypeIndex;
111
            this.name = attrdesc.getName();
112
            this.type = attrdesc.getType();
113
            this.emulator = attrdesc.getFeatureAttributeEmulator();
114
            if( attrdesc.getInterval()!=null && attrdesc.getInterval().isRelative() ) {
115
                this.interval = (RelativeInterval) attrdesc.getInterval();
116
            } else {
117
                this.interval = null;
118
            }
119
            this.dataProfile = attrdesc.getDataProfileName();
120
        }
121

  
122
        public boolean needPersist() {
123
            if( emulator!= null && emulator instanceof Persistent ) {
124
                return true;
125
            }
126
            if( this.interval!=null ) {
127
                return true;
128
            }
129
            if( !StringUtils.isBlank(this.dataProfile) ) {
130
                return true;
131
            }
132
            return false;
133
        }
134
        
135
        @Override
136
        public void saveToState(PersistentState state) throws PersistenceException {
137
            state.set("name", name);
138
            state.set("type", type);
139
            state.set("dataProfile", dataProfile);
140
            state.set("featureTypeIndex", featureTypeIndex);
141
            state.set("emulator", emulator);
142
            if( this.interval!=null ) {
143
                state.set("intervalStart", interval.getStart().toMillis());
144
                state.set("intervalEnd", interval.getEnd().toMillis());
145
            } else {
146
                state.setNull("intervalStart");
147
                state.setNull("intervalEnd");
148
            }
149
        }
150

  
151
        @Override
152
        public void loadFromState(PersistentState state) throws PersistenceException {
153
            this.name = state.getString("name");
154
            this.type = state.getInt("type");
155
            this.dataProfile = state.getString("dataProfile");
156
            this.featureTypeIndex = state.getInt("featureTypeIndex");
157
            this.emulator = (FeatureAttributeEmulator) state.get("emulator");
158
            if( state.get("intervalStart")!=null ) {
159
                this.interval = TimeSupportLocator.getManager().createRelativeInterval(
160
                        state.getLong("intervalStart"), 
161
                        state.getLong("intervalEnd")
162
                );
163
            }
164
        }
165

  
166
        private void fetch(DefaultFeatureAttributeDescriptor attrdesc) {
167
            attrdesc.setDataType(this.type);
168
            attrdesc.setName(this.name);
169
            attrdesc.setFeatureAttributeEmulator(this.emulator);
170
            attrdesc.setInterval(this.interval);
171
            attrdesc.setDataProfileName(this.dataProfile);
172
        }
173
        
174
        
175
    }
59
    private final List<FeatureType> featureTypes;
60
    private String defaultFeatureTypeId;
176 61
    
177
    private final List<Attribute> attributes;
178
    
179 62
    public DALFile() {
180
        this.attributes = new ArrayList<>();
63
        this.featureTypes = new ArrayList<>();
181 64
    }
182 65

  
183 66
    public boolean isEmpty() {
184
        if( CollectionUtils.isEmpty(this.attributes) ) {
67
        if( CollectionUtils.isEmpty(this.featureTypes) ) {
185 68
            return true;
186 69
        }
187 70
        return false;
......
193 76
            PersistentState state = manager.getState(this);
194 77
            OutputStream out = resource.asOutputStream();
195 78
            manager.saveState(state, out);
196
        } catch(Exception ex) {
79
        } catch(Throwable ex) {
197 80
            throw new RuntimeException("Can't write DAL resource.",ex);
198
        } finally {
199
            IOUtils.closeQuietly(resource);
200 81
        }
201 82
    }
202 83

  
......
205 86
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
206 87
            InputStream in = resource.asInputStream();
207 88
            DALFile x = (DALFile) manager.getObject(in);
208
            this.attributes.clear();
209
            this.attributes.addAll(x.attributes);
210
        } catch(Exception ex) {
211
            throw new RuntimeException("Can't read DAL file.",ex);
212
        } finally {
213
            IOUtils.closeQuietly(resource);
89
            this.featureTypes.clear();
90
            this.featureTypes.addAll(x.featureTypes);
91
        } catch(Throwable ex) {
92
            throw new RuntimeException("Can't read DAL resource.",ex);
214 93
        }
215 94
    }
216 95

  
217
    public void setStore(FeatureStore store) throws DataException {
218
        this.attributes.clear();
96
    public void setStore(DefaultFeatureStore store) throws DataException {
97
        this.featureTypes.clear();
219 98
        
220
        List<FeatureType> types = store.getFeatureTypes();
221
        for( int n=0; n<types.size(); n++ ) {
222
            FeatureType type = types.get(n);
223
                for (FeatureAttributeDescriptor attrdesc : type) {
224
                    Attribute attribute = new Attribute(n,attrdesc);
225
                    if( attribute.needPersist() ) {
226
                        this.attributes.add(attribute);
227
                    }
228
            }
99
        List<FeatureType> theTypes = store.getFeatureTypes();
100
        for (FeatureType theType : theTypes) {
101
            this.featureTypes.add(theType.getCopy());
229 102
        }
103
        this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
230 104
    }
231 105

  
232
    public void updateStore(FeatureStore store) throws DataException {
233
        List<FeatureType> types = store.getFeatureTypes();
234
        Set<DefaultFeatureType> needFixTypes = new HashSet<>();
235
        
236
        for (Attribute attribute : this.attributes) {
237
            DefaultFeatureType ft = (DefaultFeatureType) types.get(attribute.featureTypeIndex);
238
            DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) ft.get(attribute.name);
239
            if( ft.get(attribute.name)==null ) {
240
                attrdesc = new DefaultFeatureAttributeDescriptor(ft);
241
                attribute.fetch(attrdesc);
242
                ft.add(attrdesc);
243
            } else {
244
                attribute.fetch(attrdesc);
106
    public void updateStore(DefaultFeatureStore store) throws DataException {
107
        List<FeatureType> theTypes = new ArrayList<>();
108
        FeatureType defaultFeatureType = null;
109
        for (FeatureType type : this.featureTypes) {
110
            ((DefaultFeatureType)type).setStore(store);
111
            theTypes.add(type);
112
            if( StringUtils.equals(defaultFeatureTypeId, type.getId()) ) {
113
                defaultFeatureType = type;
245 114
            }
246
            needFixTypes.add(ft);
247 115
        }
248
        for (DefaultFeatureType type : needFixTypes) {
249
            type.fixAll();
116
        if( theTypes.isEmpty() ) {
117
            return;
250 118
        }
119
        if( defaultFeatureType==null ) {
120
            defaultFeatureType = theTypes.get(0);
121
        }
122
        store.setFeatureTypes(theTypes, defaultFeatureType);
251 123
    }
252 124

  
253 125
    @Override
254 126
    public void saveToState(PersistentState state) throws PersistenceException {
255
        state.set("attributes", attributes);
127
        state.set("defaultFeatureType", defaultFeatureTypeId);
128
        state.set("featureTypes", featureTypes);
256 129
    }
257 130

  
258 131
    @Override
259 132
    public void loadFromState(PersistentState state) throws PersistenceException {
260
        this.attributes.clear();
261
        Iterator<Attribute> it = state.getIterator("attributes");
262
        while( it.hasNext() ) {
263
            Attribute attribute = it.next();
264
            this.attributes.add(attribute);
133
        try {
134
            defaultFeatureTypeId = state.getString("defaultFeatureType");
135
            List<FeatureType> theTypes = state.getList("featureTypes");
136
            featureTypes.clear();
137
            for (FeatureType theType : theTypes) {
138
                this.featureTypes.add(theType);
139
            }
140
        } catch(Throwable th) {
141
            LOGGER.warn("Can't load DAL resource.", th);
142
            throw new PersistenceException(th);
265 143
        }
266 144
    }
267 145

  
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
47 47
import org.gvsig.fmap.dal.feature.FeatureRules;
48 48
import org.gvsig.fmap.dal.feature.FeatureStore;
49 49
import org.gvsig.fmap.dal.feature.FeatureType;
50
import org.gvsig.tools.ToolsLocator;
50 51
import org.gvsig.tools.dynobject.DynClass;
51 52
import org.gvsig.tools.dynobject.DynField;
52 53
import org.gvsig.tools.dynobject.DynMethod;
......
55 56
import org.gvsig.tools.dynobject.DynStruct;
56 57
import org.gvsig.tools.dynobject.exception.DynMethodException;
57 58
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
59
import org.gvsig.tools.persistence.PersistenceManager;
60
import org.gvsig.tools.persistence.Persistent;
61
import org.gvsig.tools.persistence.PersistentState;
62
import org.gvsig.tools.persistence.exception.PersistenceException;
58 63

  
59
public class DefaultFeatureType extends ArrayList<FeatureAttributeDescriptor> implements FeatureType,
60
		DynClass, org.gvsig.tools.lang.Cloneable {
64
public class DefaultFeatureType 
65
        extends ArrayList<FeatureAttributeDescriptor> 
66
        implements 
67
            FeatureType,
68
            Persistent,
69
            DynClass, 
70
            org.gvsig.tools.lang.Cloneable 
71
    {
61 72

  
62 73
	/**
63 74
	 *
......
81 92
        private WeakReference storeRef;
82 93
        private boolean requiredFields;
83 94

  
84
	protected DefaultFeatureType(FeatureStore store, String id) {
85
            if (StringUtils.isEmpty(id)) {
86
                id = "default";
87
            }
88
            if( store == null ) {
89
                this.storeRef = null; 
90
            } else {
91
                this.storeRef = new WeakReference(store);
92
            }
95
	public DefaultFeatureType() {
96
            // Usado en la persistencia.
93 97
            this.internalID = Integer.toHexString((int) (Math.random() * 100000)).toUpperCase();
94
            this.id = id;
98
            this.id = id = "default";
95 99
            this.rules = new DefaultFeatureRules();
96 100
            this.hasEvaluators = false;
97 101
            this.hasEmulators = false;
......
100 104
            this.defaultGeometryAttributeIndex = -1;
101 105
            this.defaultTimeAttributeIndex = -1;
102 106
            this.allowAtomaticValues = false;
107
        }
108
        
109
	protected DefaultFeatureType(FeatureStore store, String id) {
110
            this();
111
            if (StringUtils.isEmpty(id)) {
112
                id = "default";
113
            }
114
            this.id = id;
115
            setStore(store);
103 116
	}
104 117

  
105 118
	protected DefaultFeatureType(FeatureStore store) {
106
		this(store, (String)null);
119
            this(store, (String)null);
107 120
	}
108 121

  
109 122
	protected DefaultFeatureType(DefaultFeatureType other) {
110
		this(other.getStore(), (String)null);
111
		initialize(other, true);
123
            this(other.getStore(), (String)null);
124
            initialize(other, true);
112 125
	}
113 126

  
114 127
	protected DefaultFeatureType(DefaultFeatureType other,
115 128
			boolean copyAttributes) {
116
		this(other.getStore(), (String)null);
117
		initialize(other, copyAttributes);
129
            this(other.getStore(), (String)null);
130
            initialize(other, copyAttributes);
118 131
	}
119 132

  
120 133
	protected void initialize(DefaultFeatureType other, boolean copyAttributes) {
......
882 895
        }
883 896
        return (FeatureStore) this.storeRef.get();
884 897
    }
898
    
899
    public void setStore(FeatureStore store) {
900
        if( store == null ) {
901
            this.storeRef = null; 
902
        } else {
903
            this.storeRef = new WeakReference(store);
904
        }
905
    }
885 906

  
907
    @Override
908
    public void loadFromState(PersistentState state)
909
            throws PersistenceException {
910

  
911
//        FIXME: rules
912

  
913
        hasEvaluators = state.getBoolean("hasEvaluators");
914
        hasEmulators = state.getBoolean("hasEmulators");
915
        defaultGeometryAttributeName = state.getString("defaultGeometryAttributeName");
916
        defaultTimeAttributeName = state.getString("defaultTimeAttributeName");
917
        defaultGeometryAttributeIndex = state.getInt("defaultGeometryAttributeIndex");
918
        defaultTimeAttributeIndex = state.getInt("defaultTimeAttributeIndex");
919
        id = state.getString("id");
920
        hasOID = state.getBoolean("hasOID");
921
        allowAtomaticValues = state.getBoolean("allowAtomaticValues");
922

  
923
        requiredFields = state.getBoolean("requiredFields");
924
        internalID = state.getString("internalID");
925
        
926
        List<FeatureAttributeDescriptor> elements = state.getList("elements");
927
        for (FeatureAttributeDescriptor element : elements) {
928
            ((DefaultFeatureAttributeDescriptor)element).setFeatureType(this);
929
            super.add(element);
930
        }
931
        
932
        List<String> thePk = state.getList("pk");
933
        if( thePk==null ) {
934
            this.pk = null;
935
        } else {
936
            this.pk = new FeatureAttributeDescriptor[thePk.size()];
937
            for (int i = 0; i < pk.length; i++) {
938
                pk[i] = this.getAttributeDescriptor(thePk.get(i));
939
            }
940
        }
941
        this.fixAll();
942
    }
943

  
944
    @Override
945
    public void saveToState(PersistentState state) throws PersistenceException {
946

  
947
//        FIXME: rules
948

  
949
        state.set("hasEvaluators", hasEvaluators);
950
        state.set("hasEmulators", hasEmulators);
951
        state.set("defaultGeometryAttributeName", defaultGeometryAttributeName);
952
        state.set("defaultTimeAttributeName", defaultTimeAttributeName);
953
        state.set("defaultGeometryAttributeIndex", defaultGeometryAttributeIndex);
954
        state.set("defaultTimeAttributeIndex", defaultTimeAttributeIndex);
955
        state.set("id", id);
956
        state.set("hasOID", hasOID);
957
        state.set("allowAtomaticValues", allowAtomaticValues);
958

  
959
        List<String> thePk = null;
960
        if( this.pk!=null ) {
961
            thePk = new ArrayList<>();
962
            for (FeatureAttributeDescriptor attrdesc : this.pk) {
963
                thePk.add(attrdesc.getName());
964
            }
965
        }
966
        state.set("pk",thePk);
967

  
968
        state.set("requiredFields", requiredFields);
969
        state.set("internalID", internalID);
970
        
971
        List<FeatureAttributeDescriptor> elements = new ArrayList<>();
972
        elements.addAll(this);
973
        state.set("elements", elements);
974
        
975
        
976
    }
977
    
978
    private static final String FEATTYPE_PERSISTENCE_DEFINITION_NAME = "FeatureType";
979

  
980
    public static void registerPersistenceDefinition() {
981
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
982
        
983
        if (manager.getDefinition(FEATTYPE_PERSISTENCE_DEFINITION_NAME)
984
                == null) {
985
            DynStruct definition = manager.addDefinition(DefaultFeatureType.class,
986
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME,
987
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME
988
                        + " persistent definition",
989
                    null,
990
                    null
991
            );
992
//            definition.addDynFieldObject("rules");
993
            definition.addDynFieldBoolean("hasEvaluators");
994
            definition.addDynFieldBoolean("hasEmulators");
995
            definition.addDynFieldString("defaultGeometryAttributeName");
996
            definition.addDynFieldString("defaultTimeAttributeName");
997
            definition.addDynFieldInt("defaultGeometryAttributeIndex");
998
            definition.addDynFieldInt("defaultTimeAttributeIndex");
999
            definition.addDynFieldString("id");
1000
            definition.addDynFieldBoolean("hasOID");
1001
            definition.addDynFieldBoolean("allowAtomaticValues");
1002
            
1003
            definition.addDynFieldArray("pk")
1004
                    .setClassOfItems(String.class);
1005
            
1006
            definition.addDynFieldBoolean("requiredFields");
1007
            definition.addDynFieldString("internalID"); 
1008

  
1009
            definition.addDynFieldList("elements")
1010
                    .setClassOfItems(FeatureAttributeDescriptor.class); 
1011
        }
1012
    }
1013

  
1014

  
886 1015
}
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/impl/expressionevaluator/DefaultFeatureAttributeEmulatorExpression.java
209 209
            DynStruct definition = manager.addDefinition(DefaultFeatureAttributeEmulatorExpression.class,
210 210
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME,
211 211
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME
212
                    + " Persistent definition",
212
                    + " persistent definition",
213 213
                    null,
214 214
                    null
215 215
            );
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/impl/DALDefaultImplLibrary.java
33 33
import org.gvsig.fmap.dal.DataManager;
34 34
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
35 35
import org.gvsig.fmap.dal.feature.impl.DALFile;
36
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureAttributeDescriptor;
36 37
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQuery;
37 38
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
38 39
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReferenceSelection;
39 40
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection;
40 41
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff