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