001/*
002 *  Copyright 2022 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.odfsync.pegase.ws;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.activity.Initializable;
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.runtime.config.Config;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028
029import fr.pcscol.pegase.cof.ApiClient;
030import fr.pcscol.pegase.cof.api.ArbresApi;
031import fr.pcscol.pegase.cof.api.FormationsApi;
032import fr.pcscol.pegase.cof.api.GroupementsApi;
033import fr.pcscol.pegase.cof.api.ObjetsFormationApi;
034import fr.pcscol.pegase.cof.api.ObjetsMaquetteApi;
035
036/**
037 * Manager for Pégase API.
038 * Get or create API objects and configure it.
039 */
040public class PegaseApiManager extends AbstractLogEnabled implements Component, Serviceable, Initializable
041{
042    /** Role */
043    public static final String ROLE = PegaseApiManager.class.getName();
044
045    /** The Pégase token manager */
046    protected PegaseTokenManager _pegaseTokenManager;
047    
048    /* Pégase APIs */
049    private ObjetsMaquetteApi _objetsMaquetteApi;
050    private FormationsApi _formationsApi;
051    private GroupementsApi _groupementsApi;
052    private ObjetsFormationApi _objetsFormationApi;
053    private ArbresApi _arbresApi;
054    
055    /* Configuration */
056    private boolean _isActive;
057    private String _cofUrl;
058
059    public void service(ServiceManager manager) throws ServiceException
060    {
061        _pegaseTokenManager = (PegaseTokenManager) manager.lookup(PegaseTokenManager.ROLE);
062    }
063    
064    public void initialize() throws Exception
065    {
066        _isActive = Config.getInstance().getValue("pegase.activate", true, false);
067        if (_isActive)
068        {
069            /* Configuration for API */
070            _cofUrl = Config.getInstance().getValue("pegase.cof.url");
071        }
072    }
073    
074    /**
075     * Update the API client to set server URL and token.
076     * @param apiClient The API client to update
077     * @throws IOException if an error occurs
078     */
079    protected void updateApiClient(ApiClient apiClient) throws IOException
080    {
081        apiClient.setBearerToken(_pegaseTokenManager.getToken());
082        apiClient.setServerUrl(_cofUrl);
083    }
084    
085    /**
086     * Getter for objetsMaquetteApi
087     * @return objetsMaquetteApi
088     * @throws IOException If an error occurs while retrieving the token
089     */
090    public ObjetsMaquetteApi getObjetsMaquetteApi() throws IOException
091    {
092        if (_objetsMaquetteApi == null)
093        {
094            _objetsMaquetteApi = new ObjetsMaquetteApi();
095        }
096        
097        updateApiClient(_objetsMaquetteApi.getApiClient());
098        
099        return _objetsMaquetteApi;
100    }
101
102    /**
103     * Getter for FormationsApi
104     * @return FormationsApi
105     * @throws IOException If an error occurs while retrieving the token
106     */
107    public FormationsApi getFormationsApi() throws IOException
108    {
109        if (_formationsApi == null)
110        {
111            _formationsApi = new FormationsApi();
112        }
113        
114        updateApiClient(_formationsApi.getApiClient());
115        
116        return _formationsApi;
117    }
118
119    /**
120     * Getter for GroupementsApi
121     * @return GroupementsApi
122     * @throws IOException If an error occurs while retrieving the token
123     */
124    public GroupementsApi getGroupementsApi() throws IOException
125    {
126        if (_groupementsApi == null)
127        {
128            _groupementsApi = new GroupementsApi();
129        }
130        
131        updateApiClient(_groupementsApi.getApiClient());
132        
133        return _groupementsApi;
134    }
135
136    /**
137     * Getter for ObjetsFormationApi
138     * @return ObjetsFormationApi
139     * @throws IOException If an error occurs while retrieving the token
140     */
141    public ObjetsFormationApi getObjetsFormationApi() throws IOException
142    {
143        if (_objetsFormationApi == null)
144        {
145            _objetsFormationApi = new ObjetsFormationApi();
146        }
147        
148        updateApiClient(_objetsFormationApi.getApiClient());
149        
150        return _objetsFormationApi;
151    }
152    
153    /**
154     * Getter for ArbresApi
155     * @return arbresApi
156     * @throws IOException If an error occurs while retrieving the token
157     */
158    public ArbresApi getArbresApi() throws IOException
159    {
160        if (_arbresApi == null)
161        {
162            _arbresApi = new ArbresApi();
163        }
164        
165        updateApiClient(_arbresApi.getApiClient());
166        
167        return _arbresApi;
168    }
169}