3030import com .arcadeanalytics .provider .DataSourceInfo ;
3131import com .arcadeanalytics .provider .DataSourceMetadataProvider ;
3232import com .arcadeanalytics .provider .DataSourceProviderFactory ;
33- import com .arcadeanalytics .provider .FileSystemDataProvider ;
33+ import com .arcadeanalytics .provider .DataSourceTableDataProvider ;
3434import com .arcadeanalytics .provider .GraphData ;
3535import com .arcadeanalytics .repository .ArcadeUserRepository ;
3636import com .arcadeanalytics .repository .DataSetRepository ;
3737import com .arcadeanalytics .repository .DataSourceRepository ;
38- import com .arcadeanalytics .repository .FileSystemRepository ;
38+ import com .arcadeanalytics .repository .FileSystemWidgetSnapshotsRepository ;
3939import com .arcadeanalytics .repository .WidgetRepository ;
4040import com .arcadeanalytics .repository .search .WidgetSearchRepository ;
4141import com .arcadeanalytics .security .AuthoritiesConstants ;
6363import org .springframework .stereotype .Service ;
6464import org .springframework .transaction .annotation .Transactional ;
6565
66- import java .time .LocalDateTime ;
67- import java .time .format .DateTimeFormatter ;
68- import java .util .Collections ;
6966import java .util .LinkedHashMap ;
7067import java .util .LinkedHashSet ;
7168import java .util .List ;
7673import java .util .stream .Collectors ;
7774
7875import static com .arcadeanalytics .service .util .DataSourceUtil .toDataSourceInfo ;
76+ import static java .util .Collections .emptyList ;
7977import static org .elasticsearch .index .query .QueryBuilders .queryStringQuery ;
8078
8179/**
@@ -101,8 +99,6 @@ public class WidgetService {
10199
102100 private final ArcadeUserRepository arcadeUserRepository ;
103101
104- private final FileSystemRepository fsRepository ;
105-
106102 private final CacheManager cacheManager ;
107103
108104 private final DataSourceProviderFactory <DataSourceMetadataProvider > dataSourceMetadataProviderFactory ;
@@ -111,29 +107,36 @@ public class WidgetService {
111107
112108 private final DataSourceProviderFactory <DataSourceGraphProvider > dataSourceGraphProviderFactory ;
113109
110+ private final FileSystemWidgetSnapshotsRepository widgetSnapshotsRepository ;
111+
112+ private final DataSourceProviderFactory <DataSourceTableDataProvider > dataSourceTableDataProviderFactory ;
113+
114114 public WidgetService (WidgetRepository widgetRepository ,
115115 WidgetMapper widgetMapper ,
116116 WidgetSearchRepository widgetSearchRepository ,
117117 DataSetRepository dataSetRepository ,
118118 DataSourceRepository dataSourceRepository ,
119119 ArcadeUserRepository arcadeUserRepository ,
120- FileSystemRepository fsRepository ,
120+ FileSystemWidgetSnapshotsRepository widgetSnapshotsRepository ,
121121 CacheManager cacheManager ,
122122 DataSourceProviderFactory <DataSourceMetadataProvider > dataSourceMetadataProviderFactory ,
123123 DataSourceProviderFactory <DataSourceGraphDataProvider > dataSourceGraphDataProviderFactory ,
124+ DataSourceProviderFactory <DataSourceTableDataProvider > dataSourceTableDataProviderFactory ,
124125 DataSourceProviderFactory <DataSourceGraphProvider > dataSourceGraphProviderFactory ) {
125126 this .widgetRepository = widgetRepository ;
126127 this .widgetMapper = widgetMapper ;
127128 this .widgetSearchRepository = widgetSearchRepository ;
128129 this .dataSetRepository = dataSetRepository ;
129130 this .dataSourceRepository = dataSourceRepository ;
130131 this .arcadeUserRepository = arcadeUserRepository ;
131- this .fsRepository = fsRepository ;
132+ this .widgetSnapshotsRepository = widgetSnapshotsRepository ;
132133 this .cacheManager = cacheManager ;
133134
134135 this .dataSourceMetadataProviderFactory = dataSourceMetadataProviderFactory ;
135136 this .dataSourceGraphDataProviderFactory = dataSourceGraphDataProviderFactory ;
137+ this .dataSourceTableDataProviderFactory = dataSourceTableDataProviderFactory ;
136138 this .dataSourceGraphProviderFactory = dataSourceGraphProviderFactory ;
139+
137140 }
138141
139142
@@ -275,14 +278,39 @@ public Page<WidgetDTO> search(String query, Pageable pageable) {
275278 }
276279
277280 @ Transactional (readOnly = true )
278- public GraphData getData (Long id , QueryDTO query ) {
281+ public GraphData getTableData (Long id , QueryDTO query ) {
279282 return getWidgetIfAllowed (id )
280283 .map (widget -> {
281284
282285 final Contract contract = contract ();
283286
284287 if (query .getDatasetCardinality () > contract .getMaxElements ()) return GraphData .getEMPTY ();
285288
289+ final DataSourceInfo ds = toDataSourceInfo (widget .getDataSource ());
290+
291+ final int limit = contract .getMaxElements () - query .getDatasetCardinality ();
292+
293+ final GraphData graphData = dataSourceTableDataProviderFactory .create (ds )
294+ .fetchData (ds , query .getQuery (), query .getParams (), limit );
295+ return graphData ;
296+
297+ }
298+
299+ )
300+ .orElse (GraphData .getEMPTY ());
301+
302+
303+ }
304+
305+
306+ @ Transactional (readOnly = true )
307+ public GraphData getData (Long id , QueryDTO query ) {
308+ return getWidgetIfAllowed (id )
309+ .map (widget -> {
310+
311+ final Contract contract = contract ();
312+
313+ if (query .getDatasetCardinality () > contract .getMaxElements ()) return GraphData .getEMPTY ();
286314
287315 final DataSourceInfo ds = toDataSourceInfo (widget .getDataSource ());
288316
@@ -309,8 +337,8 @@ public GraphData getData(Long id, QueryDTO query) {
309337 public Optional <String > getSnapshot (Long id ) {
310338
311339 return getWidgetIfAllowed (id )
312- .map (widget -> new FileSystemDataProvider ( fsRepository )
313- .fetchData (widget )
340+ .map (widget -> widgetSnapshotsRepository
341+ .loadLatestSnapshot (widget )
314342 .map (name -> name .replace (SNAPSHOT_PREFIX , "" )))
315343 .orElse (Optional .empty ());
316344 }
@@ -325,8 +353,8 @@ public Optional<String> getSnapshotForEmbed(UUID uuid) {
325353
326354 return widgetRepository .findOneByUuid (uuid )
327355 .filter (widget -> widget .isShared ())
328- .map (widget -> new FileSystemDataProvider ( fsRepository )
329- .fetchData (widget )
356+ .map (widget -> widgetSnapshotsRepository
357+ .loadLatestSnapshot (widget )
330358 .map (name -> name .replace (SNAPSHOT_PREFIX , "" )))
331359 .orElse (Optional .empty ());
332360 }
@@ -344,21 +372,21 @@ public Optional<String> getSnapshot(Long id, String fileName) {
344372 if (fileName .equalsIgnoreCase ("last" )) return getSnapshot (id );
345373
346374 return getWidgetIfAllowed (id )
347- .map (widget -> new FileSystemDataProvider ( fsRepository )
348- .fetchData (widget , SNAPSHOT_PREFIX + fileName )
375+ .map (widget -> widgetSnapshotsRepository
376+ .loadSnapshot (widget , SNAPSHOT_PREFIX + fileName )
349377 .map (name -> name .replace (SNAPSHOT_PREFIX , "" )))
350378 .orElse (Optional .empty ());
351379 }
352380
353381
354382 public List <String > getSnapshots (Long id ) {
355383 return getWidgetIfAllowed (id )
356- .map (widget -> new FileSystemDataProvider ( fsRepository )
384+ .map (widget -> widgetSnapshotsRepository
357385 .getAllSnapshots (widget ).stream ()
358386 .map (name -> name .replace (SNAPSHOT_PREFIX , "" ))
359387 .collect (Collectors .toList ()))
360388
361- .orElse (Collections . emptyList ());
389+ .orElse (emptyList ());
362390
363391 }
364392
@@ -368,13 +396,8 @@ public boolean saveSnapshot(Long id, String snapshotData) {
368396
369397 return getWidgetIfAllowed (id )
370398 .map (widget -> {
371- final String file = "widgets/"
372- + widget .getId ().toString ()
373- + "/"
374- + SNAPSHOT_PREFIX
375- + DateTimeFormatter .ISO_LOCAL_DATE_TIME .format (LocalDateTime .now ());
376399
377- final boolean stored = fsRepository . store ( file , snapshotData . getBytes () );
400+ final boolean stored = widgetSnapshotsRepository . storeSnapshot ( widget , snapshotData );
378401
379402 widgetRepository .save (widget .hasSnapshot (stored ));
380403 return stored ;
@@ -386,7 +409,7 @@ public boolean saveSnapshot(Long id, String snapshotData) {
386409 public boolean deleteSnapshot (Long id , String fileName ) {
387410
388411 return getWidgetIfAllowed (id )
389- .map (widget -> new FileSystemDataProvider ( fsRepository )
412+ .map (widget -> widgetSnapshotsRepository
390413 .deleteSnapshot (widget , SNAPSHOT_PREFIX + fileName ))
391414 .orElse (false );
392415 }
0 commit comments