Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / report / LayoutReportActionParametersImpl.java @ 1608

History | View | Annotate | Download (15 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.app.project.documents.layout.report;
7

    
8
import java.io.ByteArrayInputStream;
9
import java.io.ByteArrayOutputStream;
10
import java.io.IOException;
11
import java.io.InputStream;
12
import java.nio.charset.StandardCharsets;
13
import java.util.List;
14
import javax.json.JsonNumber;
15
import javax.json.JsonObject;
16
import org.apache.commons.codec.DecoderException;
17
import org.apache.commons.codec.binary.Hex;
18
import org.apache.commons.io.IOUtils;
19
import org.apache.commons.lang3.StringUtils;
20
import org.gvsig.app.project.documents.Document;
21
import org.gvsig.app.project.documents.layout.LayoutDocument;
22
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
23
import org.gvsig.fmap.dal.feature.FeatureStore;
24
import org.gvsig.fmap.mapcontext.MapContextLocator;
25
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
26
import org.gvsig.json.Json;
27
import org.gvsig.json.JsonObjectBuilder;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dispose.DisposeUtils;
30
import org.gvsig.tools.persistence.PersistenceManager;
31
import org.gvsig.tools.persistence.PersistentState;
32
import org.gvsig.tools.persistence.exception.PersistenceException;
33
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
/**
38
 *
39
 * @author fdiaz
40
 */
41
public class LayoutReportActionParametersImpl implements LayoutReportActionParameters {
42
    private static final Logger LOGGER = LoggerFactory.getLogger(LayoutReportActionParametersImpl.class);
43

    
44
    private final String LAYOUT_REPORT_BUFFER_SIZE = "bufferSize";
45
    private final String LAYOUT_REPORT_CENTER_TO_CURRENT_ROW = "centerToCurrentRow";
46
    private final String LAYOUT_REPORT_DEFAULT_TEMPLATE_NAME = "layout";
47
    private final String LAYOUT_REPORT_LABEL = "label";
48
    private final String LAYOUT_REPORT_NAME = "name";
49
    private final String LAYOUT_REPORT_TEMPLATE_NAME = "templateName";
50
    private final String LAYOUT_REPORT_HIGHLIGHT_RECORD = "highlightRecord";
51
    private final String LAYOUT_REPORT_HIGHLIGHT_SYMBOL = "highlightSymbol";
52
    private final String LAYOUT_REPORT_SYMBOL_TO_RECORD = "applySymbolToRecord";
53
    private final String LAYOUT_REPORT_SYMBOL_TO_EVERYTHING_EXCEPT_RECORD = "applySymbolToEverythinfExceptRecord";
54
    private final String LAYOUT_REPORT_TYPE = "type";
55
    private final String LAYOUT_REPORT_VIEW_TAG = "viewTag";
56
    private final String LAYOUT_REPORT_USE_INTERNAL_PDF_VIEWER = "useInternalPdfViewer";
57
    private final String LAYOUT_REPORT_ZOOM_TO_CURRENT_ROW = "zoomToCurrentRow";
58
    
59
    private String identifier;
60
    private String label;
61
    private boolean centerToCurrentRow;
62
    private boolean zoomToCurrentRow;
63
    private double bufferSize;
64
    private boolean useInternalPdfViewer;
65
    private boolean highlightRecord;
66
    private String viewTag;
67
    private ISymbol highlightSymbol;
68
    private String highlightSymbolSerialized;
69
    private boolean applySymbolToRecord;
70
    private boolean applySymbolToEverythingExceptRecord;
71
    private String templateName;
72

    
73
    LayoutReportActionParametersImpl() {
74
    }
75

    
76
    LayoutReportActionParametersImpl(String name) {
77
        this.identifier = name;
78
        this.label = name;
79
    }
80
    
81
    
82
    @Override
83
    public String getIdentifier() {
84
        return identifier;
85
    }
86

    
87
    @Override
88
    public void setIdentifier(String identifier) {
89
        this.identifier = identifier;
90
    }
91

    
92
    @Override
93
    public String getLabel() {
94
        return this.label;
95
    }
96

    
97
    @Override
98
    public void setLabel(String label) {
99
        this.label = label;
100
    }
101

    
102
    @Override
103
    public boolean isCenterToCurrentRow() {
104
        return this.centerToCurrentRow;
105
    }
106

    
107
    @Override
108
    public void setCenterToCurrentRow(boolean centerToCurrentRow) {
109
        this.centerToCurrentRow = centerToCurrentRow;
110
    }
111

    
112
    @Override
113
    public boolean isZoomToCurrentRow() {
114
        return this.zoomToCurrentRow;
115
    }
116

    
117
    @Override
118
    public void setZoomToCurrentRow(boolean zoomToCurrentRow) {
119
        this.zoomToCurrentRow = zoomToCurrentRow;
120
    }
121

    
122
    @Override
123
    public double getBufferSize() {
124
        return this.bufferSize;
125
    }
126

    
127
    @Override
128
    public void setBufferSize(double bufferSize) {
129
        this.bufferSize = bufferSize;
130
    }
131

    
132
    @Override
133
    public boolean isHighlightRecord() {
134
        return this.highlightRecord;
135
    }
136

    
137
    @Override
138
    public void setHighlightRecord(boolean b) {
139
        this.highlightRecord = b;
140
    }
141
    
142
    @Override
143
    public boolean isApplySymbolToRecord() {
144
        return this.applySymbolToRecord;
145
    }
146

    
147
    @Override
148
    public void setApplySymbolToRecord(boolean b) {
149
        this.applySymbolToRecord = b;
150
    }
151

    
152
    @Override
153
    public boolean isApplySymbolToEverythingExceptRecord() {
154
        return this.applySymbolToEverythingExceptRecord;
155
    }
156

    
157
    @Override
158
    public void setApplySymbolToEverythingExceptRecord(boolean b) {
159
        this.applySymbolToEverythingExceptRecord = b;
160
    }
161

    
162
    @Override
163
    public boolean isUseInternalPdfViewer() {
164
        return useInternalPdfViewer;
165
    }
166

    
167
    @Override
168
    public void setUseInternalPdfViewer(boolean useIntervalPdfViewer) {
169
        this.useInternalPdfViewer = useIntervalPdfViewer;
170
    }
171
    
172
    @Override
173
    public String getViewTag() {
174
        return this.viewTag;
175
    }
176

    
177
    @Override
178
    public void setViewTag(String viewTag) {
179
        this.viewTag = viewTag;
180
    }
181

    
182
    @Override
183
    public ISymbol getHighlightSymbol(FeatureStore featureStore) {
184
        if(this.highlightSymbol == null){
185
            this.highlightSymbol = createSymbol(featureStore, this.highlightSymbolSerialized);
186
        }
187
        return this.highlightSymbol;
188
    }
189

    
190
    @Override
191
    public void setHighlightSymbol(ISymbol symbol) {
192
        this.highlightSymbol = symbol;
193
        this.highlightSymbolSerialized = null;
194
    }
195
    
196
    @Override
197
    public String getHighlightSymbolSerialized() {
198
        if(this.highlightSymbolSerialized == null){
199
            this.highlightSymbolSerialized = createSymbolSerialized(this.highlightSymbol);
200
        }
201
        return this.highlightSymbolSerialized;
202
    }
203

    
204
    @Override
205
    public void setHighlightSymbolSerialized(String symbol) {
206
        this.highlightSymbolSerialized = symbol;
207
        this.highlightSymbol = null;
208
    }
209
    
210
    @Override
211
    public String getTemplateName() {
212
        return this.templateName;
213
    }
214
    
215
    @Override
216
    public void setTemplateName(String templateName) {
217
        this.templateName = templateName;
218
    }
219

    
220
    private static ISymbol createSymbol(FeatureStore featureStore, String s) {
221
        FeatureAttributeDescriptor geomAttr = featureStore.getDefaultFeatureTypeQuietly().getDefaultGeometryAttribute();
222
        if (geomAttr == null) {
223
            return null;
224
        }
225
        int geomType = geomAttr.getGeomType().getType();
226

    
227
        try {
228
            if (StringUtils.isBlank(s)) {
229
                return MapContextLocator.getMapContextManager().createSymbol(geomType);
230
            }
231

    
232
            byte[] bytes = Hex.decodeHex(s.toCharArray());
233
            PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
234
            InputStream in = new ByteArrayInputStream(bytes);
235
            ISymbol symbol = (ISymbol) persistenceManager.getObject(in);
236
            return symbol;
237
        } catch (DecoderException ex) {
238
            return MapContextLocator.getMapContextManager().createSymbol(geomType);
239
        }
240

    
241
    }
242
    
243
    private static String createSymbolSerialized(ISymbol symbol){
244
        try {
245
            if(symbol == null){
246
                return null;
247
            }
248
            PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
249
            PersistentState state = persistenceManager.getState(symbol);
250
            ByteArrayOutputStream out = new ByteArrayOutputStream();
251
            persistenceManager.saveState(state, out);
252
            return Hex.encodeHexString(out.toByteArray());
253
        } catch (Exception ex) {
254
            throw new RuntimeException("Can't persist symbol", ex);
255
        }
256
    }
257
    
258

    
259
    @Override
260
    public void fromJson(JsonObject json) {
261
    this.templateName = json.getString(LAYOUT_REPORT_TEMPLATE_NAME, LAYOUT_REPORT_DEFAULT_TEMPLATE_NAME); // "1.layout"...
262
    
263
    this.identifier = json.getString(LAYOUT_REPORT_NAME, null);
264
    this.label = json.getString(LAYOUT_REPORT_LABEL, this.identifier);
265
    this.highlightRecord = json.getBoolean(LAYOUT_REPORT_HIGHLIGHT_RECORD, true);
266
    this.highlightSymbol = null;
267
    this.highlightSymbolSerialized = json.getString(LAYOUT_REPORT_HIGHLIGHT_SYMBOL, null);
268
    this.applySymbolToRecord = json.getBoolean(LAYOUT_REPORT_SYMBOL_TO_RECORD, false);
269
    this.applySymbolToEverythingExceptRecord = json.getBoolean(LAYOUT_REPORT_SYMBOL_TO_EVERYTHING_EXCEPT_RECORD, false);
270

    
271
    this.centerToCurrentRow = json.getBoolean(LAYOUT_REPORT_CENTER_TO_CURRENT_ROW, true);
272
    this.zoomToCurrentRow = json.getBoolean(LAYOUT_REPORT_ZOOM_TO_CURRENT_ROW, true);
273
    this.bufferSize = 0.0;
274
    if(json.containsKey(LAYOUT_REPORT_BUFFER_SIZE)){
275
        JsonNumber theSize = json.getJsonNumber(LAYOUT_REPORT_BUFFER_SIZE);
276
        if(theSize != null){
277
            this.bufferSize = theSize.doubleValue();
278
        }
279
    } 
280
    this.setViewTag(json.getString(LAYOUT_REPORT_VIEW_TAG, null));
281
    this.useInternalPdfViewer = json.getBoolean(LAYOUT_REPORT_USE_INTERNAL_PDF_VIEWER, true);
282
    }
283

    
284
    @Override
285
    public JsonObjectBuilder toJsonBuilder() {
286
        JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
287
        jsonBuilder.add(LAYOUT_REPORT_NAME, this.identifier);
288
        jsonBuilder.add(LAYOUT_REPORT_TYPE, LayoutReportActionFactory.LAYOUT_REPORT_ACTION_NAME);
289
        jsonBuilder.add(LAYOUT_REPORT_LABEL, this.label);
290
        jsonBuilder.add(LAYOUT_REPORT_TEMPLATE_NAME, this.templateName);
291
        if (highlightSymbol != null) {
292
            jsonBuilder.add(LAYOUT_REPORT_HIGHLIGHT_SYMBOL, this.getHighlightSymbolSerialized());
293
        }
294
        jsonBuilder.add(LAYOUT_REPORT_CENTER_TO_CURRENT_ROW, this.centerToCurrentRow);
295
        jsonBuilder.add(LAYOUT_REPORT_ZOOM_TO_CURRENT_ROW, this.zoomToCurrentRow);
296
        jsonBuilder.add(LAYOUT_REPORT_BUFFER_SIZE, this.bufferSize);
297
        jsonBuilder.add(LAYOUT_REPORT_HIGHLIGHT_RECORD, this.highlightRecord);
298
        jsonBuilder.add(LAYOUT_REPORT_SYMBOL_TO_RECORD, this.applySymbolToRecord);
299
        jsonBuilder.add(LAYOUT_REPORT_SYMBOL_TO_EVERYTHING_EXCEPT_RECORD, this.applySymbolToEverythingExceptRecord);
300
        jsonBuilder.add(LAYOUT_REPORT_VIEW_TAG, this.viewTag);
301
        jsonBuilder.add(LAYOUT_REPORT_USE_INTERNAL_PDF_VIEWER, this.useInternalPdfViewer);
302
        
303
        return jsonBuilder;
304
    }
305

    
306
    @Override
307
    public void save(FeatureStore store, LayoutDocument layout) throws IOException {
308
        ResourcesStorage.Resource resource = null;
309
        ResourcesStorage.Resource resourceLayout = null;
310
        boolean persistentIndependentSave = false;
311
        try {
312
            ResourcesStorage resources = store.getResourcesStorage();
313
            
314
            String resourceName = getResourceName(resources, this.identifier);
315
            if(resourceName == null){
316
                resourceName = getNextResourceName(resources, "report");
317
            }
318
            String theTemplateName = this.templateName;
319
            if(StringUtils.isBlank(this.templateName)){
320
                theTemplateName = LayoutReportActionFactory.LAYOUT_REPORT_ACTION_NAME;
321
                if(StringUtils.contains(resourceName, resources.getSeparator())){
322
                    theTemplateName = StringUtils.replaceIgnoreCase(
323
                            resourceName, 
324
                            resources.getSeparator()+"report", 
325
                            resources.getSeparator()+LayoutReportActionFactory.LAYOUT_REPORT_ACTION_NAME);
326
                }
327
                this.templateName = theTemplateName;
328
            }
329
            
330
            resource = resources.getResource(resourceName);
331
            IOUtils.write(this.toJson().toString(), resource.asOutputStream(), StandardCharsets.UTF_8);
332
            
333
            if(layout != null) {
334
                persistentIndependentSave = layout.isPersistIndependent();
335
                resourceLayout = resources.getResource(theTemplateName);
336
                PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
337
                layout.setPersistIndependent(true);
338
                PersistentState persistentState = persistenceManager.getState(layout.getMainWindow());
339
                persistenceManager.saveState(persistentState, resourceLayout.asOutputStream());
340
            }
341
        } catch (PersistenceException ex) {
342
            throw new IOException("Can't persist LayoutReportActionParameters", ex);
343
        } finally {
344
            if(layout != null){
345
                layout.setPersistIndependent(persistentIndependentSave);
346
            }
347
            IOUtils.closeQuietly(resource);
348
            IOUtils.closeQuietly(resourceLayout);
349
        }
350
    }
351
    
352
    private String getNextResourceName(ResourcesStorage resources, String resourceName){
353
        List<ResourcesStorage.Resource> reports = resources.getResources(resourceName);
354
        if(reports == null || reports.isEmpty()){
355
            return resourceName;
356
        }
357
        int n = -1;
358
        for (ResourcesStorage.Resource report : reports) {
359
            if(StringUtils.contains(report.getName(), resources.getSeparator())){
360
                String[] s = StringUtils.split(report.getName(), resources.getSeparator());
361
                n = Math.max(n, Integer.parseInt(s[0]));
362
            } else {
363
                n=0;
364
            }
365
        }
366
        
367
        if (n < 0) {
368
            return resourceName;
369
        }
370
        
371
        n++;
372
        return String.valueOf(n) + resources.getSeparator() + resourceName;
373

    
374
        
375
    }
376
    
377
    private String getResourceName(ResourcesStorage resources, String reportName) {
378
        try {
379
            if (resources != null) {
380
                List<ResourcesStorage.Resource> reportsResources = resources.getResources("report");
381
                if (reportsResources != null && !reportsResources.isEmpty()) {
382
                    for (ResourcesStorage.Resource resource : reportsResources) {
383
                        InputStream is = null;
384
                        try {
385
                            is = resource.asInputStream();
386
                            JsonObject json = javax.json.Json.createReader(is).readObject();
387
                            String name = json.getString(LAYOUT_REPORT_NAME, null);
388
                            if(StringUtils.equalsIgnoreCase(name, reportName)){
389
                                return resource.getName();
390
                            }
391
                        } catch (Exception ex) {
392
                            LOGGER.warn("Can't load report form resource (" + resource.getURL() + ")", ex);
393
                        } finally {
394
                            IOUtils.closeQuietly(is);
395
                        }
396
                        DisposeUtils.disposeQuietly(resource);
397
                    }
398
                }
399
            }
400
        } finally {
401
            DisposeUtils.disposeQuietly(resources);
402
        }
403
        return null;
404
    }
405
    
406
    
407
    
408
}