Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DALFile.java @ 44716

History | View | Annotate | Download (5.96 KB)

1

    
2
package org.gvsig.fmap.dal.feature.impl;
3

    
4
import java.io.File;
5
import java.io.FileOutputStream;
6
import java.io.InputStream;
7
import java.io.OutputStream;
8
import java.util.ArrayList;
9
import java.util.List;
10
import org.apache.commons.collections4.CollectionUtils;
11
import org.apache.commons.io.IOUtils;
12
import org.apache.commons.lang3.StringUtils;
13
import org.gvsig.fmap.dal.exception.DataException;
14
import org.gvsig.fmap.dal.feature.FeatureType;
15
import org.gvsig.tools.ToolsLocator;
16
import org.gvsig.tools.dynobject.DynStruct;
17
import org.gvsig.tools.persistence.PersistenceManager;
18
import org.gvsig.tools.persistence.Persistent;
19
import org.gvsig.tools.persistence.PersistentState;
20
import org.gvsig.tools.persistence.exception.PersistenceException;
21
import org.slf4j.Logger;
22
import org.slf4j.LoggerFactory;
23
import sun.nio.ch.IOUtil;
24

    
25

    
26
@SuppressWarnings("UseSpecificCatch")
27
public class DALFile implements Persistent {
28

    
29
    private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class);
30
    
31
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources";
32
    
33
    public static DALFile getDALFile() {
34
        DALFile f = new DALFile();
35
        return f;
36
    }
37

    
38
    public static DALFile getDALFile(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) {
39
        DALFile df = new DALFile();
40
        df.read(resource);
41
        return df;
42
    }
43
    
44
    public static void registerPersistenceDefinition() {
45
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
46
        if (manager.getDefinition(DALFILE_PERSISTENCE_DEFINITION_NAME) == null) {
47
            DynStruct definition = manager.addDefinition(
48
                DALFile.class,
49
                DALFILE_PERSISTENCE_DEFINITION_NAME, 
50
                DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
51
                null, 
52
                null
53
            );
54
            definition.addDynFieldString("defaultFeatureType");
55
            definition.addDynFieldList("featureTypes")
56
                .setClassOfItems(FeatureType.class)
57
                .setPersistent(true);
58
        }
59
    }
60

    
61
    
62
    private final List<FeatureType> featureTypes;
63
    private String defaultFeatureTypeId;
64
    
65
    public DALFile() {
66
        this.featureTypes = new ArrayList<>();
67
    }
68

    
69
    public boolean isEmpty() {
70
        if( CollectionUtils.isEmpty(this.featureTypes) ) {
71
            return true;
72
        }
73
        return false;
74
    }
75
    
76
    public void write(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) {
77
        try {
78
            OutputStream out = resource.asOutputStream();
79
            this.write(out);
80
            // Don't close the output stream
81
        } catch(Throwable ex) {
82
            throw new RuntimeException("Can't write DAL resource.",ex);
83
        }
84
    }
85

    
86
    public void write(File outfile) {
87
        OutputStream out = null;
88
        try {
89
            out = new FileOutputStream(outfile);
90
            this.write(out);
91
        } catch(Throwable ex) {
92
            throw new RuntimeException("Can't write DAL file.",ex);
93
        } finally {
94
          IOUtils.closeQuietly(out);
95
        }
96
    }
97

    
98
    public void write(OutputStream out) {
99
        try {
100
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
101
            PersistentState state = manager.getState(this);
102
            manager.saveState(state, out);
103
        } catch(Throwable ex) {
104
            throw new RuntimeException("Can't write DAL resource.",ex);
105
        }
106
    }
107

    
108
    public void read(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) {
109
        try {
110
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
111
            InputStream in = resource.asInputStream();
112
            DALFile x = (DALFile) manager.getObject(in);
113
            this.featureTypes.clear();
114
            this.featureTypes.addAll(x.featureTypes);
115
        } catch(Throwable ex) {
116
            throw new RuntimeException("Can't read DAL resource.",ex);
117
        }
118
    }
119
    
120
    public void setFeatureType(FeatureType featureType) throws DataException {
121
        this.featureTypes.clear();
122
        this.featureTypes.add(featureType.getCopy());
123
        this.defaultFeatureTypeId = featureType.getId();
124
    }
125

    
126
    public void setStore(DefaultFeatureStore store) throws DataException {
127
        this.featureTypes.clear();
128
        
129
        List<FeatureType> theTypes = store.getFeatureTypes();
130
        for (FeatureType theType : theTypes) {
131
            this.featureTypes.add(theType.getCopy());
132
        }
133
        this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
134
    }
135

    
136
    public void updateStore(DefaultFeatureStore store) throws DataException {
137
        List<FeatureType> theTypes = new ArrayList<>();
138
        FeatureType defaultFeatureType = null;
139
        for (FeatureType type : this.featureTypes) {
140
            ((DefaultFeatureType)type).setStore(store);
141
            theTypes.add(type);
142
            if( StringUtils.equals(defaultFeatureTypeId, type.getId()) ) {
143
                defaultFeatureType = type;
144
            }
145
        }
146
        if( theTypes.isEmpty() ) {
147
            return;
148
        }
149
        if( defaultFeatureType==null ) {
150
            defaultFeatureType = theTypes.get(0);
151
        }
152
        store.setFeatureTypes(theTypes, defaultFeatureType);
153
    }
154

    
155
    @Override
156
    public void saveToState(PersistentState state) throws PersistenceException {
157
        state.set("defaultFeatureType", defaultFeatureTypeId);
158
        state.set("featureTypes", featureTypes);
159
    }
160

    
161
    @Override
162
    public void loadFromState(PersistentState state) throws PersistenceException {
163
        try {
164
            defaultFeatureTypeId = state.getString("defaultFeatureType");
165
            List<FeatureType> theTypes = state.getList("featureTypes");
166
            featureTypes.clear();
167
            for (FeatureType theType : theTypes) {
168
                this.featureTypes.add(theType);
169
            }
170
        } catch(Throwable th) {
171
            LOGGER.warn("Can't load DAL resource.", th);
172
            throw new PersistenceException(th);
173
        }
174
    }
175

    
176
}