1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.lutecetools.service;
35
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.fasterxml.jackson.databind.SerializationFeature;
38
39 import fr.paris.lutece.plugins.lutecetools.business.Component;
40 import fr.paris.lutece.portal.service.daemon.AppDaemonService;
41 import fr.paris.lutece.portal.service.datastore.DatastoreService;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43
44 import java.io.IOException;
45
46 import java.util.Date;
47
48
49
50
51 public class ComponentService
52 {
53 private static final String DSKEY_PREFIX = "lutecetools.database.";
54 private static final String DAEMON_KEY = "lutecetoolsCacheUpdater";
55 private static final ObjectMapper _mapper = new ObjectMapper( );
56
57 private ComponentService( )
58 {
59 }
60
61
62
63
64
65
66
67 public static void save( Component component )
68 {
69 try
70 {
71 component.setLastUpdate( new Date( ).getTime( ) );
72
73 String strJSON = getAsJSON( component );
74 DatastoreService.setDataValue( DSKEY_PREFIX + component.getArtifactId( ), strJSON );
75 }
76 catch( IOException ex )
77 {
78 AppLogService.error( "LuteceTools : Error saving component : " + ex.getMessage( ), ex );
79 }
80 }
81
82
83
84
85
86
87
88
89 public static Component load( String strArtifactId )
90 {
91 Component component = null;
92 String strJSON = DatastoreService.getDataValue( DSKEY_PREFIX + strArtifactId, null );
93
94 if ( strJSON != null )
95 {
96 try
97 {
98 component = loadFromJSON( strJSON );
99 }
100 catch( IOException ex )
101 {
102 AppLogService.error( "LuteceTools : Error loading component : " + ex.getMessage( ), ex );
103 }
104 }
105
106 return component;
107 }
108
109
110
111
112 public static void clearCache( )
113 {
114 AppLogService.info( "LuteceTools : clear the cache of the component list ..." );
115 AppDaemonService.stopDaemon( DAEMON_KEY );
116 DatastoreService.removeInstanceDataByPrefix( DSKEY_PREFIX );
117 AppDaemonService.startDaemon( DAEMON_KEY );
118 AppLogService.info( "LuteceTools : cache cleared." );
119 }
120
121
122
123
124
125
126
127
128
129
130 private static String getAsJSON( Component component ) throws IOException
131 {
132 _mapper.enable( SerializationFeature.INDENT_OUTPUT );
133 return _mapper.writeValueAsString( component );
134 }
135
136
137
138
139
140
141
142
143
144
145 private static Component loadFromJSON( String strJson ) throws IOException
146 {
147 return _mapper.readValue( strJson, Component.class );
148 }
149 }