Revision 44190 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
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(); |
Also available in: Unified diff