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