View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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  /*
35   * To change this license header, choose License Headers in Project Properties.
36   * To change this template file, choose Tools | Templates
37   * and open the template in the editor.
38   */
39  package fr.paris.lutece.plugins.transparency.service;
40  
41  import com.fasterxml.jackson.databind.JsonNode;
42  import com.fasterxml.jackson.databind.ObjectMapper;
43  import fr.paris.lutece.plugins.transparency.business.Lobby;
44  import fr.paris.lutece.plugins.transparency.business.LobbyHome;
45  import fr.paris.lutece.portal.service.i18n.I18nService;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.util.httpaccess.HttpAccess;
49  import fr.paris.lutece.util.httpaccess.HttpAccessException;
50  import java.io.IOException;
51  import java.sql.Date;
52  import java.text.MessageFormat;
53  import java.util.HashMap;
54  import java.util.Iterator;
55  import java.util.Locale;
56  import java.util.Map;
57  
58  /**
59   * Lobby tools
60   */
61  public final class LobbyService
62  {
63  
64      // Plugin properties
65      private static final String PROPERTY_URL_LOBBY_LIST_REFERENCE = "lobby.json.list.url";
66  
67      // Msg
68      private static final String MSG_SYNCHRO_KEY = "transparency.message.synchro";
69      private static final String MSG_ERROR_GET_JSON = "transparency.message.synchro.error";
70  
71      // constants (for synchro)
72      private static final String CONSTANT_KEY_PUBLICATIONS = "publications";
73      private static final String CONSTANT_KEY_DENOMINATION = "denomination";
74      private static final String CONSTANT_KEY_IDENTIFIANTNATIONAL = "identifiantNational";
75      private static final String CONSTANT_KEY_TYPEIDENTIFIANTNATIONAL = "typeIdentifiantNational";
76      private static final String CONSTANT_KEY_LIENSITEWEB = "lienSiteWeb";
77  
78      /**
79       * Refresh the lobby data base
80       *
81       * @param locale
82       * @return The Jsp URL of the process result
83       */
84      public static String synchronizeLobbies( Locale locale )
85      {
86  
87          int nbLobby = 0;
88          int nbLobbyCreated = 0;
89  
90          try
91          {
92              String strUri = AppPropertiesService.getProperty( PROPERTY_URL_LOBBY_LIST_REFERENCE );
93  
94              HttpAccess ha = new HttpAccess( );
95  
96              Map<String, String> headersRequest = new HashMap<>( );
97              Map<String, String> headersResponse = new HashMap<>( );
98  
99              String strJson = ha.doGet( strUri, null, null, headersRequest, headersResponse );
100 
101             if ( strJson == null )
102             {
103                 String msg = I18nService.getLocalizedString( MSG_ERROR_GET_JSON, locale );
104                 msg = MessageFormat.format( msg, strUri );
105 
106                 return msg;
107             }
108 
109             ObjectMapper mapper = new ObjectMapper( );
110             JsonNode jsonNode = null;
111 
112             try
113             {
114                 jsonNode = mapper.readTree( strJson );
115             }
116             catch( IOException e )
117             {
118                 String msg = I18nService.getLocalizedString( MSG_ERROR_GET_JSON, locale );
119                 msg = MessageFormat.format( msg, strUri );
120 
121                 AppLogService.error( e );
122                 return msg;
123             }
124 
125             // Parse lobbies
126             Iterator<JsonNode> lobbyList = jsonNode.path( CONSTANT_KEY_PUBLICATIONS ).elements( );
127 
128             while ( lobbyList.hasNext( ) )
129             {
130                 nbLobby++;
131                 Lobby lobby = jsonToLobby( lobbyList.next( ) );
132 
133                 Lobby existingLobby = LobbyHome.getByNationalId( lobby.getNationalId( ) );
134 
135                 if ( existingLobby != null )
136                 {
137                     // update existing lobby
138                     lobby.setId( existingLobby.getId( ) );
139                     LobbyHome.update( lobby );
140                 }
141                 else
142                 {
143                     // insert new lobby
144                     LobbyHome.create( lobby );
145                     nbLobbyCreated++;
146                 }
147             }
148 
149             String msg = I18nService.getLocalizedString( MSG_SYNCHRO_KEY, locale );
150             msg = MessageFormat.format( msg, nbLobby, nbLobbyCreated, nbLobby - nbLobbyCreated );
151 
152             return msg;
153 
154         }
155         catch( HttpAccessException e )
156         {
157             AppLogService.error( e );
158             return e.getLocalizedMessage( );
159         }
160     }
161 
162     /**
163      * Parse Json to populate a Lobby bean
164      * 
165      * @param jsonLobby
166      * @return the lobby bean
167      */
168     private static Lobby jsonToLobby( JsonNode jsonLobby )
169     {
170         Lobby lobby = new Lobby( );
171 
172         lobby.setName( jsonLobby.get( CONSTANT_KEY_DENOMINATION ).asText( ) );
173         lobby.setNationalId( jsonLobby.get( CONSTANT_KEY_IDENTIFIANTNATIONAL ).asText( ) );
174         if ( jsonLobby.has( CONSTANT_KEY_TYPEIDENTIFIANTNATIONAL ) )
175             lobby.setNationalIdType( jsonLobby.get( CONSTANT_KEY_TYPEIDENTIFIANTNATIONAL ).asText( ) );
176         if ( jsonLobby.has( CONSTANT_KEY_LIENSITEWEB ) )
177             lobby.setUrl( jsonLobby.get( CONSTANT_KEY_LIENSITEWEB ).asText( ) );
178 
179         lobby.setVersionDate( new Date( ( new java.util.Date( ) ).getTime( ) ) );
180 
181         lobby.setJsonData( jsonLobby.toString( ) );
182 
183         return lobby;
184     }
185 
186 }