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  package fr.paris.lutece.plugins.referentielbo.v2.rs;
35  
36  import com.google.gson.reflect.TypeToken;
37  import fr.paris.lutece.plugins.referentielbo.v2.model.exception.ReferentielException;
38  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielCategorieSocioProfessionnelle;
39  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielCivilite;
40  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielGenre;
41  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielMethodeCertification;
42  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielPieceJustificative;
43  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielQualite;
44  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielRegimeSocioProfessionnel;
45  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielStatutMarital;
46  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielTypeAdresse;
47  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielTypeLienIndividus;
48  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielTypePersonneMorale;
49  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielTypeRessourceFinanciere;
50  import fr.paris.lutece.plugins.referentielbo.v2.model.referentiel.ReferentielTypeTelephone;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  import fr.paris.lutece.util.httpaccess.HttpAccess;
53  import fr.paris.lutece.util.httpaccess.HttpAccessException;
54  
55  import java.lang.reflect.Type;
56  import java.util.ArrayList;
57  import java.util.List;
58  import java.util.Map;
59  import java.util.Optional;
60  
61  public class ReferentielTypesService extends AbstractWSReferentiel implements IReferentielTypesService
62  {
63  
64  
65      @Deprecated
66      public ReferentielTypesService( String referentielBaseUrl )
67      {
68          super( referentielBaseUrl );
69      }
70      public ReferentielTypesService( String referentielBaseUrl, String xGraviteeApiKey )
71      {
72          super( referentielBaseUrl, xGraviteeApiKey );
73      }
74  
75      public List<ReferentielPieceJustificative> recupererReferentielPieceJustificative( ) throws ReferentielException
76      {
77          return ( List<ReferentielPieceJustificative> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_PIECES_JUSTIFICATIVE, new TypeToken<ArrayList<ReferentielPieceJustificative>>( ) {}.getType( ) );
78      }
79  
80      public List<ReferentielTypeLienIndividus> recupererReferentielTypeLienIndividus( ) throws ReferentielException
81      {
82          return ( List<ReferentielTypeLienIndividus> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_TYPE_LIENS_INDIVIDUS, new TypeToken<ArrayList<ReferentielTypeLienIndividus>>( ) {}.getType( ) );
83      }
84  
85      @Override
86      public List<ReferentielCategorieSocioProfessionnelle> recupererReferentielCategorieSocioProfessionnelle( ) throws ReferentielException
87      {
88          return ( List<ReferentielCategorieSocioProfessionnelle> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_CATEGORIE_SOCIO_PRO_PATH, new TypeToken<ArrayList<ReferentielCategorieSocioProfessionnelle>>( ) {}.getType( ) );
89      }
90  
91      @Override
92      public List<ReferentielCivilite> recupererReferentielCivilite( ) throws ReferentielException
93      {
94          return ( List<ReferentielCivilite> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_CIVILITE_PATH, new TypeToken<ArrayList<ReferentielCivilite>>( ) {}.getType( ) );
95      }
96  
97      @Override
98      public List<ReferentielGenre> recupererReferentielGenre( ) throws ReferentielException
99      {
100         return ( List<ReferentielGenre> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_GENRE_PATH, new TypeToken<ArrayList<ReferentielGenre>>( ) {}.getType( ) );
101     }
102 
103     @Override
104     public List<ReferentielMethodeCertification> recupererReferentielMethodeCertification( ) throws ReferentielException
105     {
106         return ( List<ReferentielMethodeCertification> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_METHODE_CERTIFICATION_PATH, new TypeToken<ArrayList<ReferentielMethodeCertification>>( ) {}.getType( ) );
107     }
108 
109     @Override
110     public List<ReferentielQualite> recupererReferentielQualite( ) throws ReferentielException
111     {
112         return ( List<ReferentielQualite> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_QUALITE_PATH, new TypeToken<ArrayList<ReferentielQualite>>( ) {}.getType( ) );
113     }
114 
115     @Override
116     public List<ReferentielRegimeSocioProfessionnel> recupererReferentielRegimeSocioProfessionnel( ) throws ReferentielException
117     {
118         return ( List<ReferentielRegimeSocioProfessionnel> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_REGIME_SOCIO_PRO_PATH, new TypeToken<ArrayList<ReferentielRegimeSocioProfessionnel>>( ) {}.getType( ) );
119     }
120 
121     @Override
122     public List<ReferentielStatutMarital> recupererReferentielStatutMarital( ) throws ReferentielException
123     {
124         return ( List<ReferentielStatutMarital> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_STATUT_MARITAL_PATH, new TypeToken<ArrayList<ReferentielStatutMarital>>( ) {}.getType( ) );
125     }
126 
127     @Override
128     public List<ReferentielTypeAdresse> recupererReferentielTypeAdresse( ) throws ReferentielException
129     {
130         return ( List<ReferentielTypeAdresse> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_TYPE_ADRESSE_PATH, new TypeToken<ArrayList<ReferentielTypeAdresse>>( ) {}.getType( ) );
131     }
132 
133     @Override
134     public List<ReferentielTypePersonneMorale> recupererReferentielTypePersonneMorale( ) throws ReferentielException
135     {
136         return ( List<ReferentielTypePersonneMorale> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_TYPE_PERSONNE_MORALE_PATH, new TypeToken<ArrayList<ReferentielTypePersonneMorale>>( ) {}.getType( ) );
137     }
138 
139     @Override
140     public List<ReferentielTypeRessourceFinanciere> recupererReferentielTypeRessourceFinanciere( ) throws ReferentielException
141     {
142         return ( List<ReferentielTypeRessourceFinanciere> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_TYPE_RESSOURCE_FINANCIERE_PATH, new TypeToken<ArrayList<ReferentielTypeRessourceFinanciere>>( ) {}.getType( ) );
143     }
144 
145     @Override
146     public List<ReferentielTypeTelephone> recupererReferentielTypeTelephone( ) throws ReferentielException
147     {
148         return ( List<ReferentielTypeTelephone> ) recupererReferentiel( Constants.RECUPERATION_REFERENTIEL_TYPE_TELEPHONE_PATH, new TypeToken<ArrayList<ReferentielTypeTelephone>>( ) {}.getType( ) );
149     }
150 
151     private List<?> recupererReferentiel( String pathSpecifique, Type typeRetour ) throws ReferentielException
152     {
153         HttpAccess httpAccess = new HttpAccess( );
154         try
155         {
156             String apiUrl = getReferentielBaseUrl( ) + pathSpecifique;
157             Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
158             String response = appelDoGet( httpAccess, pathSpecifique, apiUrl, mapHeadersRequest );
159 
160             gererErreur( response );
161 
162             return ( List<?> ) Optional.ofNullable( getGson( ).fromJson( response, typeRetour ) ).orElse( new ArrayList<>(  ) );
163         }
164         catch( HttpAccessException e )
165         {
166             AppLogService.error("erreur de mapping de la réponse", e);
167             throw new ReferentielException( e );
168         }
169     }
170 }