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 @ 46655

History | View | Annotate | Download (7.52 KB)

1

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

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

    
29

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

    
33
    private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class);
34
    
35
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources";
36
    
37
    public static DALFile getDALFile() {
38
        DALFile f = new DALFile();
39
        return f;
40
    }
41

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

    
65
    
66
    private final List<FeatureType> featureTypes;
67
    private String defaultFeatureTypeId;
68
    
69
    public DALFile() {
70
        this.featureTypes = new ArrayList<>();
71
    }
72

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

    
90
    public void write(File outfile) {
91
        this.write(outfile, "bin");
92
    }
93
    
94
    public void write(File outfile, String format) {
95
        switch(StringUtils.upperCase(format) ) {
96
            case "HEX":
97
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
98
                try {
99
                    this.write(buffer);
100
                    FileUtils.write(
101
                            outfile, 
102
                            Hex.encodeHexString(buffer.toByteArray()), 
103
                            StandardCharsets.UTF_8
104
                    );
105
                } catch(Throwable ex) {
106
                    throw new RuntimeException("Can't write DAL file.",ex);
107
                } finally {
108
                  IOUtils.closeQuietly(buffer);
109
                }
110
                break;
111

    
112
            case "BIN":
113
            default:
114
                OutputStream out = null;
115
                try {
116
                    out = new FileOutputStream(outfile);
117
                    this.write(out);
118
                } catch(Throwable ex) {
119
                    throw new RuntimeException("Can't write DAL file.",ex);
120
                } finally {
121
                  IOUtils.closeQuietly(out);
122
                }
123
                break;
124
        }
125
    }
126

    
127
    public void write(OutputStream out) {
128
        try {
129
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
130
            PersistentState state = manager.getState(this);
131
            manager.saveState(state, out);
132
        } catch(Throwable ex) {
133
            throw new RuntimeException("Can't write DAL resource.",ex);
134
        }
135
    }
136

    
137
    public void read(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) {
138
        if( resource==null ) {
139
            throw new IllegalArgumentException("Resource null is not a valid resource");
140
        }
141
        try {
142
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
143
            InputStream in = resource.asInputStream();
144
            if( in == null ) {
145
                throw new RuntimeException("Can't read DAL resource, input stream is null (resname="+resource.getName()+", resurl="+Objects.toString(resource.getURL())+", restype="+resource.getClass().getName()+").");
146
            }
147
            DALFile x = (DALFile) manager.getObject(in);
148
            this.featureTypes.clear();
149
            this.featureTypes.addAll(x.featureTypes);
150
        } catch(Throwable ex) {
151
            throw new RuntimeException("Can't read DAL resource (resname="+resource.getName()+", resurl="+Objects.toString(resource.getURL())+", restype="+resource.getClass().getName()+").",ex);
152
        }
153
    }
154
    
155
    public void setFeatureType(FeatureType featureType) throws DataException {
156
        this.featureTypes.clear();
157
        this.featureTypes.add(featureType.getCopy());
158
        this.defaultFeatureTypeId = featureType.getId();
159
    }
160

    
161
    public void setStore(DefaultFeatureStore store) throws DataException {
162
        this.featureTypes.clear();
163
        
164
        List<FeatureType> theTypes = store.getFeatureTypes();
165
        for (FeatureType theType : theTypes) {
166
            this.featureTypes.add(theType.getCopy());
167
        }
168
        this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
169
    }
170

    
171
    public void updateStore(DefaultFeatureStore store) throws DataException {
172
        List<FeatureType> theTypes = new ArrayList<>();
173
        FeatureType defaultFeatureType = null;
174
        for (FeatureType type : this.featureTypes) {
175
            ((DefaultFeatureType)type).setStore(store);
176
            theTypes.add(type);
177
            if( StringUtils.equals(defaultFeatureTypeId, type.getId()) ) {
178
                defaultFeatureType = type;
179
            }
180
        }
181
        if( theTypes.isEmpty() ) {
182
            return;
183
        }
184
        if( defaultFeatureType==null ) {
185
            defaultFeatureType = theTypes.get(0);
186
        }
187
        store.setFeatureTypes(theTypes, defaultFeatureType);
188
    }
189

    
190
    @Override
191
    public void saveToState(PersistentState state) throws PersistenceException {
192
        state.set("defaultFeatureType", defaultFeatureTypeId);
193
        state.set("featureTypes", featureTypes);
194
    }
195

    
196
    @Override
197
    public void loadFromState(PersistentState state) throws PersistenceException {
198
        try {
199
            defaultFeatureTypeId = state.getString("defaultFeatureType");
200
            List<FeatureType> theTypes = state.getList("featureTypes");
201
            featureTypes.clear();
202
            for (FeatureType theType : theTypes) {
203
                this.featureTypes.add(theType);
204
            }
205
        } catch(Throwable th) {
206
            LOGGER.warn("Can't load DAL resource.", th);
207
            throw new PersistenceException(th);
208
        }
209
    }
210

    
211
}