1
2
3
4
5
6 package fr.paris.lutece.plugins.dataviz.service;
7
8 import fr.paris.lutece.plugins.dataviz.business.ISqlStat;
9 import fr.paris.lutece.plugins.dataviz.business.IStat;
10 import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
11 import fr.paris.lutece.portal.service.spring.SpringContextService;
12 import fr.paris.lutece.util.json.JsonResponse;
13 import fr.paris.lutece.util.json.JsonUtil;
14 import java.util.List;
15
16
17
18
19
20 public class StatsService extends AbstractCacheableService
21 {
22 private static final String CACHE_SERVICE_NAME = "Dataviz Stats Cache";
23 private static StatsService _singleton;
24 private static List<ISqlStat> _listStat;
25
26 private StatsService( )
27 {
28 }
29
30
31
32 public static synchronized StatsService instance( )
33 {
34 if ( _singleton == null )
35 {
36 _singleton = new StatsService( );
37 _singleton.init( );
38 }
39
40 return _singleton;
41 }
42
43
44
45
46 private void init( )
47 {
48 initCache();
49
50 _listStat = SpringContextService.getBeansOfType( ISqlStat.class );
51 }
52
53
54
55
56
57
58
59 public String getStat( String strStatName ) throws StatNotFoundException
60 {
61 _listStat = SpringContextService.getBeansOfType( ISqlStat.class );
62 String strResult = (String) getFromCache( strStatName.toLowerCase( ) );
63
64 if ( strResult == null )
65 {
66 boolean bFound = false;
67
68 for ( ISqlStat stat : _listStat )
69 {
70 if ( strStatName.equalsIgnoreCase( stat.getName( ) ) )
71 {
72 JsonResponse json = new JsonResponse( stat.getResult( ) );
73 strResult = JsonUtil.buildJsonResponse( json );
74 putInCache( strStatName, strResult );
75 bFound = true;
76
77 break;
78 }
79 }
80
81 if ( !bFound )
82 {
83 throw new StatNotFoundException( strStatName );
84 }
85 }
86
87 return strResult;
88 }
89
90 @Override
91 public String getName( )
92 {
93 return CACHE_SERVICE_NAME;
94 }
95 }