svn-gvsig-desktop / 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 / actions / ReportUtils.java @ 47752
History | View | Annotate | Download (5.38 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.fmap.dal.swing.impl.actions; |
7 |
|
8 |
import java.awt.event.ActionEvent; |
9 |
import java.io.InputStream; |
10 |
import java.util.ArrayList; |
11 |
import java.util.Collections; |
12 |
import java.util.List; |
13 |
import java.util.function.Predicate; |
14 |
import javax.json.Json; |
15 |
import javax.json.JsonObject; |
16 |
import javax.swing.Action; |
17 |
import org.apache.commons.io.IOUtils; |
18 |
import org.apache.commons.lang3.StringUtils; |
19 |
import org.gvsig.fmap.dal.DataStore; |
20 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
21 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
22 |
import org.gvsig.fmap.dal.swing.DALActionFactory; |
23 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
24 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
25 |
import org.gvsig.fmap.dal.swing.report.ReportAction; |
26 |
import org.gvsig.fmap.dal.swing.report.ReportActionFactory; |
27 |
import org.gvsig.tools.dispose.DisposeUtils; |
28 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
/**
|
33 |
*
|
34 |
* @author fdiaz
|
35 |
*/
|
36 |
public class ReportUtils { |
37 |
|
38 |
private static final Logger LOGGER = LoggerFactory.getLogger(ReportUtils.class); |
39 |
|
40 |
private ReportUtils(){
|
41 |
|
42 |
} |
43 |
|
44 |
public static List<JsonObject> getReports(FeatureStore store) { |
45 |
List<JsonObject> reports = new ArrayList<>(); |
46 |
ResourcesStorage resources = null;
|
47 |
try {
|
48 |
resources = store.getResourcesStorage(); |
49 |
if (resources != null) { |
50 |
List<ResourcesStorage.Resource> reportsResources = resources.getResources("report"); |
51 |
if (reportsResources != null && !reportsResources.isEmpty()) { |
52 |
for (ResourcesStorage.Resource resource : reportsResources) {
|
53 |
InputStream is = null; |
54 |
try {
|
55 |
is = resource.asInputStream(); |
56 |
JsonObject json = Json.createReader(is).readObject(); |
57 |
reports.add(json); |
58 |
} catch (Exception ex) { |
59 |
LOGGER.warn("Can't load report form resource (" + resource.getURL() + ")", ex); |
60 |
} finally {
|
61 |
IOUtils.closeQuietly(is); |
62 |
} |
63 |
DisposeUtils.disposeQuietly(resource); |
64 |
} |
65 |
} |
66 |
} |
67 |
} finally {
|
68 |
DisposeUtils.disposeQuietly(resources); |
69 |
} |
70 |
if (reports.isEmpty()) {
|
71 |
return null; |
72 |
} |
73 |
return reports;
|
74 |
} |
75 |
|
76 |
public static List<ReportAction> getReportActions(DataStore store) { |
77 |
return getReportActions(store, null, null, null); |
78 |
} |
79 |
|
80 |
public static List<ReportAction> getReportActions(DataStore store, Predicate filter) { |
81 |
return getReportActions(store, filter, null, null); |
82 |
} |
83 |
|
84 |
public static List<ReportAction> getReportActions(DataStore store, Predicate<JsonObject> filter, DALActionFactory.DALActionContext context, ActionEvent e) { |
85 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
86 |
List<ReportAction> actions = new ArrayList<>(); |
87 |
List<JsonObject> reports = getReports((FeatureStore) store);
|
88 |
if (reports != null && !reports.isEmpty()) { |
89 |
FeatureSelection selecteds = null;
|
90 |
if(context == null){ |
91 |
selecteds = context.getSelecteds(); |
92 |
} |
93 |
for (JsonObject json : reports) {
|
94 |
if(filter == null || filter.test(json)){ |
95 |
ReportAction action = null;
|
96 |
for (ReportActionFactory theFactory : dataSwingManager.getReportActionFactories()) {
|
97 |
if( theFactory.isApplicable(json,context, e)) {
|
98 |
if(context == null){ |
99 |
action = theFactory.createReportAction( |
100 |
(FeatureStore) store, |
101 |
null,
|
102 |
null,
|
103 |
json |
104 |
); |
105 |
} else {
|
106 |
action = theFactory.createReportAction( |
107 |
(FeatureStore) store, |
108 |
context.getQuery(), |
109 |
selecteds, |
110 |
json |
111 |
); |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
if( action != null ) { |
117 |
String name = action.getReportName();
|
118 |
if( StringUtils.isBlank(name) ) {
|
119 |
action.setReportName(store.getName()); |
120 |
} |
121 |
String label = action.getReportLabel();
|
122 |
if( StringUtils.isBlank(label) ) {
|
123 |
action.setReportLabel(action.getReportName()); |
124 |
} |
125 |
actions.add(action); |
126 |
} |
127 |
} |
128 |
} |
129 |
} |
130 |
Collections.sort(
|
131 |
actions, |
132 |
(ReportAction o1, ReportAction o2) -> StringUtils.compare(o1.getReportLabel(), o2.getReportLabel()) |
133 |
); |
134 |
return actions;
|
135 |
} |
136 |
|
137 |
|
138 |
|
139 |
} |