Comparaison des versions

Légende

  • Ces lignes ont été ajoutées. Ce mot a été ajouté.
  • Ces lignes ont été supprimées. Ce mot a été supprimé.
  • La mise en forme a été modifiée.

...

Bloc de code
languagejava
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartException;

import fr.univville.siham.dossieragent.DossierAgentDateWebService;
import fr.univville.siham.dossieragent.Individu;
import fr.univville.siham.dossieragent.Matricule;
import fr.univville.siham.dossieragent.ParamAgentDateWS;
import fr.univville.siham.dossieragent.ReferentielService;
import fr.univville.siham.dossieragent.WebserviceException_Exception;
import fr.univville.siham.listeagents.IListeAgentsWebService;
import fr.univville.siham.listeagents.ListeAgentsWS;
import fr.univville.siham.listeagents.ParamListeAgents;
import fr.univville.siham.listeagents.ResultatsListeAgents;
import fr.univville.siham.listeagents.WebServiceException_Exception;
import fr.univville.siham.parametrage.DossierParametrageWebService;
import fr.univville.siham.parametrage.ListeNomenclatures;
import fr.univville.siham.parametrage.ListeUO;
import fr.univville.siham.parametrage.NomenclatureService;
import fr.univville.siham.parametrage.ParamNomenclature;
import fr.univville.siham.parametrage.ParamStructure;
import fr.univville.siham.parametrage.Repertoire;
import fr.univville.siham.parametrage.Structures;

public class SihamService  {
	
	private static final int NB_AGENTS_PER_REQUEST = 20;
	
    protected final Logger log = LoggerFactory.getLogger(this.getClass());
	
	private IListeAgentsWebService listeAgentsWebService;
	
	private DossierAgentDateWebService dossierAgentDateWebService;
	
	private DossierParametrageWebService parametrageWebService;
	
	private String url;
	
	private String username;
	
	private String password;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	private Map<String, List<String>> getWsHeaders() {
		Map<String, List<String>> headers = new HashMap<String, List<String>>();
		headers.put("Username", Collections.singletonList(username));
		headers.put("Password", Collections.singletonList(password));
		return headers;
	}
	
	private IListeAgentsWebService getIListeAgentsWebService() {
		if(listeAgentsWebService==null) {
			try {
				initAuthenticator();
				URL listeAgentsWsUrl = new URL(url + "/ListeAgentsWebService/ListeAgentsWebService?wsdl");
				ListeAgentsWS listeAgentsWS = new ListeAgentsWS(listeAgentsWsUrl);
				listeAgentsWebService = listeAgentsWS.getListeAgentsWebServiceImplPort();
				Map<String, Object> req_ctx = ((BindingProvider)listeAgentsWebService).getRequestContext();
				req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, getWsHeaders());
				log.info("Siham listeAgentsWebService OK");
			} catch(Exception ex) {
				throw new RuntimeException("Exception during initialization of siham ws ListeAgentsWebService", ex); 
			} finally {
				resetAuthenticator();
			}
		}
		return listeAgentsWebService;
	}
	
	private DossierAgentDateWebService getDossierAgentDateWebService() {
		if(dossierAgentDateWebService==null) {
			try {
				initAuthenticator();
				URL dossierAgentWsUrl = new URL(url + "/DossierAgentDateWebService/DossierAgentDateWebService?wsdl");
				ReferentielService referentielService = new ReferentielService(dossierAgentWsUrl);
				dossierAgentDateWebService = referentielService.getDossierAgentDateWebServiceImpPort();
				Map<String, Object> req_ctx_ref = ((BindingProvider)dossierAgentDateWebService).getRequestContext();
				req_ctx_ref.put(MessageContext.HTTP_REQUEST_HEADERS, getWsHeaders());
				log.info("Siham dossierAgentDateWebService OK");
			} catch(Exception ex) {
				throw new RuntimeException("Exception during initialization of siham ws DossierAgentDateWebService", ex); 
			} finally {
				resetAuthenticator();
			}
		}
		return dossierAgentDateWebService;
	}
	
	private DossierParametrageWebService getDossierParametrageWebService() {
		if(parametrageWebService==null) {
			try {
				initAuthenticator();
				URL parametrageWsUrl = new URL(url + "/DossierParametrageWebService/DossierParametrageWebService?wsdl");
				NomenclatureService nomenclatureService = new NomenclatureService(parametrageWsUrl);
				parametrageWebService = nomenclatureService.getDossierParametrageWebServiceImpPort();
				Map<String, Object> req_ctx_par = ((BindingProvider)parametrageWebService).getRequestContext();
				req_ctx_par.put(MessageContext.HTTP_REQUEST_HEADERS, getWsHeaders());
				log.info("Siham parametrageWebService OK");
			} catch(Exception ex) {
				throw new RuntimeException("Exception during initialization of siham ws DossierAgentDateWebService", ex); 
			} finally {
				resetAuthenticator();
			}
		}
		return parametrageWebService;	
	}

	
	private void initAuthenticator() {
		Authenticator.setDefault(new Authenticator() {
			 @Override
			 protected PasswordAuthentication getPasswordAuthentication() {
			   return new PasswordAuthentication(
				 username,
				 password.toCharArray());
			 }
		});
	}
	
	private void resetAuthenticator() {		
		Authenticator.setDefault(null);
	}
	
	protected synchronized List<String> getAgentsMatricules(String nomUsuel) {
		List<String> matricules = new ArrayList<String>();
		ParamListeAgents paramRecupListeAgents = new ParamListeAgents();
		paramRecupListeAgents.setNomUsuel(nomUsuel);
		try {
			 List<ResultatsListeAgents> agents= getIListeAgentsWebService().recupListeAgents(paramRecupListeAgents);
			 for(ResultatsListeAgents agent : agents) {
				 matricules.add(agent.getMatricule());
			 }
			 log.debug(agents.size() + " matricules d'agents récupérés depuis Siham");
		} catch (WebServiceException_Exception e) {
			throw new MultipartException("Erreur lors de la récupération agents depuis web-service siham", e);
		}
		return matricules;
	}
	
	public Individu getSihamIndividu(String matricule) {
		Map<String, Individu> sihamIndividus = getSihamIndividus(Arrays.asList(new String[] {matricule}));
		return sihamIndividus.get(matricule);
	}
	
	public synchronized Map<String, Individu> getSihamIndividus(List<String> matricules) {
		Map<String, Individu> sihamIndividus = new HashMap<String, Individu>();
		ParamAgentDateWS paramAgentDateWS = new ParamAgentDateWS();
		int i = 0;
		int n = 0;
		List<String> matriculesErreurs = new ArrayList<String>();
		for(String matricule: matricules) {
			i++;
			n++;
			Matricule matriculeObj = new Matricule();
			matriculeObj.setMatricule(matricule);
			paramAgentDateWS.getListeMatricules().add(matriculeObj);
			if(i==NB_AGENTS_PER_REQUEST || n==matricules.size()) {
				try {
					List<Individu> agents = getDossierAgentDateWebService().recupDonneesAgents(paramAgentDateWS);
					for(Individu agent: agents) {
						sihamIndividus.put(agent.getDonneesPersonnelles().getMatricule(), agent);
					}
				} catch (WebserviceException_Exception e) {
					log.info("Erreur lors de la récupération agents depuis web-service siham", e);
					for(Matricule agentMatricule : paramAgentDateWS.getListeMatricules()) {
						ParamAgentDateWS agentParamAgentDateWS = new ParamAgentDateWS();
						agentParamAgentDateWS.getListeMatricules().add(agentMatricule);
						try {
							List<Individu> agents = getDossierAgentDateWebService().recupDonneesAgents(agentParamAgentDateWS);
							for(Individu agent: agents) {
								sihamIndividus.put(agent.getDonneesPersonnelles().getMatricule(), agent);
							}
						} catch (WebserviceException_Exception ee) {
							log.warn("Erreur lors de la récupération de cet agent depuis web-service siham : " + agentMatricule.getMatricule(), ee);
							sihamIndividus.put(agentMatricule.getMatricule(), new SihamIndividuAgentInError());
							matriculesErreurs.add(agentMatricule.getMatricule());
						}
					}
				}
				paramAgentDateWS = new ParamAgentDateWS();
				i = 0;
			}
		}
		if(matriculesErreurs.size() > 0) {
			log.error("Matricules en erreur lors de la récupération de ces agents depuis web-service siham : " + matriculesErreurs);
		}
		log.info(sihamIndividus.size() + " agents récupérés depuis Siham dont " + matriculesErreurs.size() + " en erreur.");
		return sihamIndividus;
	}
	
	public Map<String, Individu> getSihamIndividuFromNomUsuel(String nomUsuel) {
		List<String> matricules = getAgentsMatricules(nomUsuel);
		Map<String, Individu> sihamIndividus = getSihamIndividus(matricules);
		return sihamIndividus;
	}
	
	public synchronized List<Structures> getSihamStructures() {
		List<Structures> structures;
		try {
			ParamStructure paramStructure = new ParamStructure();
			paramStructure.setCodeAdministration("");
			paramStructure.setDateObservation("");
			paramStructure.getListeUO().add(new ListeUO());
			structures = getDossierParametrageWebService().recupStructures(paramStructure);
		} catch (fr.univville.siham.parametrage.WebserviceException_Exception e) {
			throw new RuntimeException("Erreur lors de la récupération des structures depuis web-service siham", e);
		}
		log.info(structures.size() + " structures récupérées depuis Siham");
		return structures;
	}
	
	
	public synchronized List<Repertoire> getSihamNomenclaturesRepertoires() {
		List<Repertoire> repertoires;
		try {
			ParamNomenclature paramNomenclature = new ParamNomenclature();
			paramNomenclature.setCodeAdministration("");
			paramNomenclature.setDateObservation("");
			paramNomenclature.getListeNomenclatures().add(new ListeNomenclatures());
			repertoires = getDossierParametrageWebService().recupNomenclaturesRH(paramNomenclature);
		} catch (fr.univville.siham.parametrage.WebserviceException_Exception e) {
			throw new RuntimeException("Erreur lors de la récupération des répertoires de nomenclatures depuis web-service siham", e);
		}
		log.info(repertoires.size() + " répertoires de nomenclatures récupérées depuis Siham");
		return repertoires;
	}


}

...