001/*
002 *  Copyright 2017 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.apogee;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.ibatis.mapping.Environment;
028import org.apache.ibatis.session.SqlSession;
029import org.apache.ibatis.type.JdbcType;
030import org.apache.ibatis.type.TypeHandlerRegistry;
031
032import org.ametys.core.datasource.AbstractMyBatisDAO;
033import org.ametys.plugins.contentio.synchronize.impl.typehandler.ZonedDateTimeTypeHandler;
034import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsCollection;
035
036/**
037 * Data access object to the remote DB Apogee
038 */
039public class ApogeeDAO extends AbstractMyBatisDAO
040{
041    /** Avalon ROLE */
042    public static final String ROLE = ApogeeDAO.class.getName();
043
044    /** The name of request attribute for the datasource id */
045    protected static final String DATASOURCE_REQUEST_ATTR = ApogeeDAO.class.getName() + "$dataSourceId";
046    
047    private Context _context;
048    
049    @Override
050    public void contextualize(Context context) throws ContextException
051    {
052        super.contextualize(context);
053        _context = context;
054    }
055
056    @Override
057    protected org.apache.ibatis.session.Configuration _getMyBatisConfiguration(Environment env)
058    {
059        org.apache.ibatis.session.Configuration config = super._getMyBatisConfiguration(env);
060        TypeHandlerRegistry typeHandlerRegistry = config.getTypeHandlerRegistry();
061        
062        // Transform date types to ZonedDateTime
063        typeHandlerRegistry.register(Object.class, JdbcType.DATE, ZonedDateTimeTypeHandler.class);
064        typeHandlerRegistry.register(Object.class, JdbcType.TIMESTAMP, ZonedDateTimeTypeHandler.class);
065        
066        return config;
067    }
068    
069    @Override
070    protected void _configureDatasource(Configuration configuration) throws ConfigurationException
071    {
072        // Do nothing
073    }
074    
075    @Override
076    protected String _getDataSourceId()
077    {
078        // Get data source id from request attributes
079        return (String) _getRequest().getAttribute(DATASOURCE_REQUEST_ATTR);
080    }
081    
082    /**
083     * Get the request
084     * @return the request
085     */
086    protected Request _getRequest()
087    {
088        Request request = ContextHelper.getRequest(_context);
089        if (request == null)
090        {
091            throw new IllegalStateException("Apogee DAO can not be used outside a request");
092        }
093        return request;
094    }
095    
096    /**
097     * Returns Apogee programs matching the search criteria
098     * @param dataSourceId The id of data source. Can not be null.
099     * @param sccParams the parameters values of the Apogee synchronizable collection
100     * @param params The parameters for search criteria: DIP code, VDI code, title
101     * @return the list Apogee course lists matching the search criteria
102     */
103    public List<Map<String, Object>> searchPrograms(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
104    {
105        return _executeSearch(dataSourceId, sccParams, params, "Apogee.searchPrograms");
106    }
107    
108    /**
109     * Returns joint org units linked to a program
110     * @param dataSourceId The id of data source. Can not be null.
111     * @param sccParams the parameters values of the Apogee synchronizable collection
112     * @param params The parameters for search criteria: Program ID
113     * @return The list of Apogee joint org units matching the program
114     */
115    public List<Map<String, Object>> getJointOrgUnits(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
116    {
117        return _executeSearch(dataSourceId, sccParams, params, "Apogee.getJointOrgUnits");
118    }
119
120    /**
121     * Returns form of teaching linked to a program
122     * @param dataSourceId The id of data source. Can not be null.
123     * @param sccParams the parameters values of the Apogee synchronizable collection
124     * @param params The parameters for search criteria: Program ID
125     * @return The list of Apogee form of teaching matching the program
126     */
127    public List<Map<String, Object>> getFormOfTeachingOrg(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
128    {
129        return _executeSearch(dataSourceId, sccParams, params, "Apogee.getFormOfTeachingOrg");
130    }
131    
132    /**
133     * Returns the add elements (rich texts) to a program
134     * @param dataSourceId The id of data source. Can not be null.
135     * @param sccParams the parameters values of the Apogee synchronizable collection
136     * @param params The parameters for search criteria: Program ID
137     * @return The list of Apogee add elements matching the program
138     */
139    public List<Map<String, Object>> getAddElements(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
140    {
141        return _executeSearch(dataSourceId, sccParams, params, "Apogee.getAddElements");
142    }
143    
144    /**
145     * Returns the domains to a program
146     * @param dataSourceId The id of data source. Can not be null.
147     * @param sccParams the parameters values of the Apogee synchronizable collection
148     * @param params The parameters for search criteria: Program ID
149     * @return The list of Apogee domains matching the program
150     */
151    public List<Map<String, Object>> getDomains(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
152    {
153        return _executeSearch(dataSourceId, sccParams, params, "Apogee.getDomains");
154    }
155    
156    /**
157     * Returns Apogee organizational units matching the search criteria
158     * @param dataSourceId The id of data source. Can not be null.
159     * @param sccParams the parameters values of the Apogee synchronizable collection
160     * @param params The parameters for search criteria: cod_cmp, cod_tpc, title
161     * @return The list of Apogee organizational units matching the search criteria
162     */
163    public List<Map<String, Object>> searchOrgUnits(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
164    {
165        return _executeSearch(dataSourceId, sccParams, params, "Apogee.searchOrgUnits");
166    }
167    
168    /**
169     * Returns Apogee containers matching the search criteria
170     * @param dataSourceId The id of data source. Can not be null.
171     * @param sccParams the parameters values of the Apogee synchronizable collection
172     * @param params The parameters for search criteria: code, type, title
173     * @return The list of Apogee containers matching the search criteria
174     */
175    public List<Map<String, Object>> searchContainers(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
176    {
177        return _executeSearch(dataSourceId, sccParams, params, "Apogee.searchContainers");
178    }
179    
180    /**
181     * Returns Apogee subprograms matching the search criteria
182     * @param dataSourceId The id of data source. Can not be null.
183     * @param sccParams the parameters values of the Apogee synchronizable collection
184     * @param params The parameters for search criteria: "dip", "vdi", "vdi-ter", "etp-code", "vet-code", "vet-title"
185     * @return The list of Apogee subprograms matching the search criteria
186     */
187    public List<Map<String, Object>> searchSubPrograms(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
188    {
189        return _executeSearch(dataSourceId, sccParams, params, "Apogee.searchSubPrograms");
190    }
191    
192    /**
193     * Returns the Apogee educational elements matching the search criteria
194     * @param dataSourceId The id of data source. Can not be null.
195     * @param sccParams the parameters values of the Apogee synchronizable collection
196     * @param params The parameters for search criteria: ELP code (code), ELP type (type), title (title)
197     * @return the list of Apogee educational elements matching the search criteria
198     */
199    public List<Map<String, Object>> searchCourses(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
200    {
201        return _executeSearch(dataSourceId, sccParams, params, "Apogee.searchCourses");
202    }
203    
204    /**
205     * Returns the course parts (hours) to a course
206     * @param dataSourceId The id of data source. Can not be null.
207     * @param sccParams the parameters values of the Apogee synchronizable collection
208     * @param params The parameters for search criteria: Course ID
209     * @return The list of Apogee course parts matching the course
210     */
211    public List<Map<String, Object>> getCourseParts(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
212    {
213        return _executeSearch(dataSourceId, sccParams, params, "Apogee.getCourseParts");
214    }
215    
216    /**
217     * Returns the Apogee course lists matching the search criteria
218     * @param dataSourceId The id of data source. Can not be null.
219     * @param sccParams the parameters values of the Apogee synchronizable collection
220     * @param params The parameters for search criteria: ELP code (code), NEL code (cod_nel), title (title)
221     * @return the list Apogee course lists matching the search criteria
222     */
223    public List<Map<String, Object>> searchCourseLists(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
224    {
225        return _executeSearch(dataSourceId, sccParams, params, "Apogee.searchCourseLists");
226    }
227    
228    /**
229     * Returns the Apogee objects matching the search criteria on the asked statement.
230     * @param dataSourceId The id of data source. Can not be null.
231     * @param sccParams the parameters values of the Apogee synchronizable collection
232     * @param params The parameters for search criteria
233     * @param query The query identifier to execute
234     * @return the list Apogee objects matching the search criteria
235     */
236    protected List<Map<String, Object>> _executeSearch(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params, String query)
237    {
238        _prepareSqlRequest(dataSourceId, sccParams, params);
239        
240        try (SqlSession session = getSession())
241        {
242            return session.selectList(query, params);
243        }
244    }
245    
246    /**
247     * Common method to prepare the SQL request to execute
248     * @param dataSourceId The id of data source. Can not be null.
249     * @param sccParams the parameters values of the used Apogee synchronizable collection
250     * @param params the SQL parameters
251     */
252    protected void _prepareSqlRequest(String dataSourceId, Map<String, Object> sccParams, Map<String, Object> params)
253    {
254        // Set data source id in request attributes
255        _getRequest().setAttribute(DATASOURCE_REQUEST_ATTR, dataSourceId);
256        
257        // Add administrative year to SQL parameters if exists
258        if (sccParams.containsKey(AbstractApogeeSynchronizableContentsCollection.PARAM_YEAR))
259        {
260            params.put("year", sccParams.get(AbstractApogeeSynchronizableContentsCollection.PARAM_YEAR));
261        }
262    }
263}