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
35
36
37
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
60
61 public final class LobbyService
62 {
63
64
65 private static final String PROPERTY_URL_LOBBY_LIST_REFERENCE = "lobby.json.list.url";
66
67
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
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
80
81
82
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
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
138 lobby.setId( existingLobby.getId( ) );
139 LobbyHome.update( lobby );
140 }
141 else
142 {
143
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
164
165
166
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 }