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.gipse.v1.rs;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Optional;
41  
42  import com.google.gson.reflect.TypeToken;
43  import fr.paris.lutece.plugins.gipse.v1.model.dto.TypeEnseignementRestDto;
44  import fr.paris.lutece.plugins.gipse.v1.model.exception.ReferentielException;
45  import fr.paris.lutece.plugins.gipse.v1.model.utils.Constants;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  import fr.paris.lutece.util.httpaccess.HttpAccess;
48  import fr.paris.lutece.util.httpaccess.HttpAccessException;
49  
50  public class TypeEnseignementService extends AbstractWSReferentiel implements ITypeEnseignementService
51  {
52  
53      public TypeEnseignementService( String referentielBaseUrl, String xGraviteeApiKey )
54      {
55          super( referentielBaseUrl, xGraviteeApiKey );
56      }
57  
58      @Override
59      public Optional<List<TypeEnseignementRestDto>> getListTypesEnseignement( String strCode, Boolean bIsVisible, String strLibelle ) throws ReferentielException
60      {
61          HttpAccess httpAccess = new HttpAccess( );
62          try
63          {
64              String apiUrl = getReferentielBaseUrl() + Constants.API_PATH_TYPE_ENSEIGNEMENT;
65              Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
66              if( ( strCode !=  null ) || ( bIsVisible != null ) || ( strLibelle != null ) )
67              {
68                  apiUrl += "?";
69                  if(bIsVisible != null)
70                  {
71                      apiUrl += Constants.PARAM_VISIBLE+"="+bIsVisible+"&";
72                  }
73                  if(strCode != null)
74                  {
75                      apiUrl += Constants.PARAM_CODE+"="+strCode+"&";
76                  }
77                  if(strLibelle != null)
78                  {
79                      apiUrl += Constants.PARAM_LIBELLE+"="+strLibelle+"&";
80                  }
81              }
82              String response = appelDoGet( httpAccess, Constants.API_PATH_TYPE_ENSEIGNEMENT, apiUrl, mapHeadersRequest );
83  
84              gererErreur( response );
85  
86              List<TypeEnseignementRestDto> listTypesEnseignementDto = getGson( ).fromJson( response, new TypeToken<ArrayList<TypeEnseignementRestDto>>( ) {}.getType( ) );
87              return Optional.ofNullable( listTypesEnseignementDto );
88          }
89          catch( HttpAccessException e )
90          {
91              AppLogService.error(e);
92              throw new ReferentielException( e );
93          }
94      }
95  
96      @Override
97      public Optional<TypeEnseignementRestDto> getTypeEnseignementById( int nId ) throws ReferentielException
98      {
99          HttpAccess httpAccess = new HttpAccess( );
100         try
101         {
102             String apiUrl = getReferentielBaseUrl() + Constants.API_PATH_TYPE_ENSEIGNEMENT + "/" + nId;
103             Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
104             String response = appelDoGet( httpAccess, Constants.API_PATH_TYPE_ENSEIGNEMENT, apiUrl, mapHeadersRequest );
105 
106             gererErreur( response );
107 
108             TypeEnseignementRestDto typeEnseignementDto = getGson( ).fromJson( response, TypeEnseignementRestDto.class );
109             return Optional.ofNullable( typeEnseignementDto );
110         }
111         catch( HttpAccessException e )
112         {
113             AppLogService.error(e);
114             throw new ReferentielException( e );
115         }
116     }
117 
118 }