View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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  
35  package fr.paris.lutece.plugins.referentielbo.v1.rs;
36  
37  import com.google.gson.reflect.TypeToken;
38  import fr.paris.lutece.plugins.referentielbo.v1.model.IndividuDto;
39  import fr.paris.lutece.plugins.referentielbo.v1.model.exception.ReferentielException;
40  import fr.paris.lutece.plugins.referentielbo.v1.model.profil.ProfilIndividuDto;
41  import fr.paris.lutece.plugins.utilitairesfo.utils.StringUtils;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.util.httpaccess.HttpAccess;
44  import fr.paris.lutece.util.httpaccess.HttpAccessException;
45  
46  import java.util.ArrayList;
47  import java.util.List;
48  import java.util.Map;
49  import java.util.Optional;
50  
51  public class ReferentielIndividuService extends AbstractWSReferentiel implements IReferentielIndividuService
52  {
53  
54      @Deprecated
55      public ReferentielIndividuService( String referentielBaseUrl )
56      {
57          super( referentielBaseUrl );
58      }
59      public ReferentielIndividuService( String referentielBaseUrl, String xGraviteeApiKey )
60      {
61          super( referentielBaseUrl, xGraviteeApiKey );
62      }
63  
64      @Override
65      @Deprecated
66      public Optional<IndividuDto> recupererIndividuAvecGuid( String guid ) throws ReferentielException
67      {
68          if ( StringUtils.isBlank( guid ) )
69          {
70              throw new NullPointerException( "GUID null ou vide" );
71          }
72          HttpAccess httpAccess = new HttpAccess( );
73          try
74          {
75              String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_INDIVIDU_AVEC_GUID_V1 + guid;
76              Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
77              String response = httpAccess.doGet( apiUrl, null, null, mapHeadersRequest );
78  
79              gererErreur( response );
80  
81              IndividuDto individuDto = getGson( ).fromJson( response, IndividuDto.class );
82              return Optional.ofNullable( individuDto );
83          }
84          catch( HttpAccessException e )
85          {
86              AppLogService.error("error during mapping response", e);
87              throw new ReferentielException( e );
88          }
89      }
90  
91      @Override
92      @Deprecated
93      public Optional<IndividuDto> recupererIndividuAvecCuid( String cuid ) throws ReferentielException
94      {
95          if ( StringUtils.isBlank( cuid ) )
96          {
97              throw new NullPointerException( "CUID null ou vide" );
98          }
99          HttpAccess httpAccess = new HttpAccess( );
100         try
101         {
102             String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_INDIVIDU_AVEC_CUID_V1 + cuid;
103             Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
104             String response = httpAccess.doGet(apiUrl, null ,null, mapHeadersRequest );
105 
106             gererErreur( response );
107 
108             IndividuDto individuDto = getGson( ).fromJson( response, IndividuDto.class );
109             return Optional.ofNullable( individuDto );
110         }
111         catch( HttpAccessException e )
112         {
113             AppLogService.error("erreur de mapping de la réponse", e);
114             throw new ReferentielException( e );
115         }
116     }
117 
118     @Override
119     @Deprecated
120     public List<IndividuDto> recupererIndividusParEtablissement( Long idEtablissement ) throws ReferentielException
121     {
122         if ( idEtablissement == null )
123         {
124             throw new NullPointerException( "idEtablissement null" );
125         }
126         HttpAccess httpAccess = new HttpAccess( );
127         try
128         {
129             String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_INDIVIDU_AVEC_ID_ETABLISSEMENT_V1 + idEtablissement;
130             Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
131             String response = httpAccess.doGet(apiUrl, null, null, mapHeadersRequest );
132 
133             gererErreur( response );
134 
135             return ( List<IndividuDto> ) Optional.ofNullable( getGson( ).fromJson( response, new TypeToken<ArrayList<IndividuDto>>( ) {}.getType( ) ) ).orElse( new ArrayList<>(  ) );
136         }
137         catch( HttpAccessException e )
138         {
139             AppLogService.error("erreur de mapping de la réponse", e);
140             throw new ReferentielException( e );
141         }
142     }
143 
144     @Override
145     @Deprecated
146     public Optional<IndividuDto> creerIndividu( ProfilIndividuDto profilIndividuDto ) throws ReferentielException
147     {
148         HttpAccess httpAccess = new HttpAccess( );
149         try
150         {
151             String apiUrl = getReferentielBaseUrl( ) + Constants.ENREGISTRER_INDIVIDU_V1;
152             String json = getGson( ).toJson( profilIndividuDto );
153             Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
154             String response = httpAccess.doPostJSON(apiUrl, json, mapHeadersRequest, null);
155 
156             gererErreur( response );
157 
158             IndividuDto individuDto = getGson( ).fromJson( response, IndividuDto.class );
159             return Optional.ofNullable( individuDto );
160         }
161         catch( HttpAccessException e )
162         {
163             AppLogService.error("erreur de mapping de la réponse", e);
164             throw new ReferentielException( e );
165         }
166     }
167 
168     @Override
169     @Deprecated
170     public Optional<IndividuDto> modifierIndividu( ProfilIndividuDto profilIndividuDto ) throws ReferentielException
171     {
172         return creerIndividu( profilIndividuDto );
173     }
174 }