View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * ComponentService
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       * Save a component into the Datastore as JSON format
63       * 
64       * @param component
65       *            The component
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       * Load a component from the Datastore
84       * 
85       * @param strArtifactId
86       *            The Artifact ID
87       * @return The component
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      * Clear cache by removing datastore data
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      * Convert a component into JSON
123      * 
124      * @param component
125      *            The component
126      * @return JSON
127      * @throws IOException
128      *             if an error occurs
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      * Convert JSON into Component
138      * 
139      * @param strJson
140      *            The JSON
141      * @return The component
142      * @throws IOException
143      *             if an error occurs
144      */
145     private static Component loadFromJSON( String strJson ) throws IOException
146     {
147         return _mapper.readValue( strJson, Component.class );
148     }
149 }