Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package nc.noumea.mairie.alfresco.webScript; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.transaction.SystemException; import javax.transaction.UserTransaction; import nc.noumea.mairie.abs.dto.AgentWithServiceDto; import nc.noumea.mairie.ads.dto.LightUserDto; import nc.noumea.mairie.ws.IRadiWsConsumer; import nc.noumea.mairie.ws.ISirhWsConsumer; import org.alfresco.model.ContentModel; import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.security.PersonService; import org.alfresco.service.cmr.site.SiteInfo; import org.alfresco.service.cmr.site.SiteService; import org.alfresco.service.namespace.QName; import org.apache.log4j.Logger; import org.springframework.extensions.webscripts.Cache; import org.springframework.extensions.webscripts.DeclarativeWebScript; import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.WebScriptRequest; /** * * @author rebjo84 */ public class SynchroniseAgentWebScript extends DeclarativeWebScript { private Logger logger = Logger.getLogger(SynchroniseAgentWebScript.class); private static final String NAMESPACE = "http://www.noumea.nc/model/sirhmodel/1.0"; public static final QName ID_AGENT = QName.createQName(NAMESPACE, "mairie:idAgent"); private NodeService nodeService; private PersonService personService; private SiteService siteService; private ServiceRegistry serviceRegistry; private ISirhWsConsumer sirhWsConsumer; private IRadiWsConsumer radiWsConsumer; @Override public Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) { logger.info("DEBUT Web Script SynchroniseAgentWebScript"); Map<String, Object> model = new HashMap<String, Object>(); model.put("nomWebScript", "SynchroniseAgentWebScript"); long startTime = System.currentTimeMillis(); if (!isSiteSirhExist()) { logger.debug("Site SIRH not exist"); long endTime = System.currentTimeMillis(); model.put("nbrAgentCree", "Site SIRH not exist"); model.put("tempsExecution", endTime - startTime); return model; } List<AgentWithServiceDto> listAgentsSIRH = sirhWsConsumer.getListeAgentsMairie(); List<String> listEmployeeNumber = new ArrayList<String>(); for (AgentWithServiceDto agent : listAgentsSIRH) { if (!listEmployeeNumber.contains(getEmployeeNumberFromIdAgent(agent.getIdAgent()))) { listEmployeeNumber.add(getEmployeeNumberFromIdAgent(agent.getIdAgent())); } } logger.info("Nombre d'agents retournes par SIRH : " + listAgentsSIRH.size()); List<LightUserDto> listUsersRadi = radiWsConsumer.getListAgentCompteAD(listEmployeeNumber); logger.info("Nombre d'agents retournes par RADI : " + listUsersRadi.size()); int nbrAgentCree = 0; for (AgentWithServiceDto agent : listAgentsSIRH) { LightUserDto userRadi = getLightUserRadiDto(listUsersRadi, agent); if (null == userRadi) { logger.debug("User not exist in RADI : " + agent.getIdAgent()); continue; } logger.debug("User find in RADI : " + agent.getIdAgent()); // nous gerons nous meme les transactions // car nous avons eu "TransactionalCache' is full" // cela ralentit fortement Alfresco UserTransaction trx = serviceRegistry.getTransactionService().getNonPropagatingUserTransaction(false); try { if (!personService.personExists(userRadi.getsAMAccountName())) { trx.begin(); personService.setCreateMissingPeople(true); NodeRef person = personService .createPerson(createDefaultProperties(userRadi.getsAMAccountName(), agent.getPrenom(), agent.getNom(), userRadi.getMail(), agent.getIdAgent())); trx.commit(); if (null != person) { nbrAgentCree++; } } else { logger.debug("User already exist in alfresco : " + agent.getIdAgent() + " " + userRadi.getsAMAccountName()); } } catch (Throwable e) { try { trx.rollback(); logger.error(e.getMessage()); } catch (IllegalStateException | SecurityException | SystemException e1) { logger.error(e1.getMessage()); } } } long endTime = System.currentTimeMillis(); model.put("nbrAgentCree", nbrAgentCree); model.put("tempsExecution", endTime - startTime); logger.debug(nbrAgentCree + " users crs."); logger.info("FIN Web Script SynchroniseAgentWebScript"); return model; } public LightUserDto getLightUserRadiDto(List<LightUserDto> listUsersRadi, AgentWithServiceDto agent) { if (null != listUsersRadi && null != agent && null != agent.getIdAgent()) { for (LightUserDto userRadi : listUsersRadi) { if (userRadi.getEmployeeNumber().toString() .equals(getEmployeeNumberFromIdAgent(agent.getIdAgent()))) { return userRadi; } } } return null; } public String getEmployeeNumberFromIdAgent(Integer idAgent) { return idAgent.toString().substring(0, 1) + idAgent.toString().substring(2, idAgent.toString().length()); } public Map<QName, Serializable> createDefaultProperties(String userName, String firstName, String lastName, String email, Integer idAgent) { HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>(); properties.put(ContentModel.TYPE_CONTENT, "mairie:customPersonModel"); properties.put(ContentModel.PROP_USERNAME, userName); properties.put(ContentModel.PROP_FIRSTNAME, firstName); properties.put(ContentModel.PROP_LASTNAME, lastName); properties.put(ContentModel.PROP_EMAIL, email); properties.put(ID_AGENT, idAgent); return properties; } public boolean isSiteSirhExist() { SiteInfo site = siteService.getSite(CreateSiteWebScript.NOM_SITE); if (null == site) return false; return true; } public NodeService getNodeService() { return nodeService; } public void setNodeService(NodeService nodeService) { this.nodeService = nodeService; } public ISirhWsConsumer getSirhWsConsumer() { return sirhWsConsumer; } public void setSirhWsConsumer(ISirhWsConsumer sirhWsConsumer) { this.sirhWsConsumer = sirhWsConsumer; } public PersonService getPersonService() { return personService; } public void setPersonService(PersonService personService) { this.personService = personService; } public IRadiWsConsumer getRadiWsConsumer() { return radiWsConsumer; } public void setRadiWsConsumer(IRadiWsConsumer radiWsConsumer) { this.radiWsConsumer = radiWsConsumer; } public SiteService getSiteService() { return siteService; } public void setSiteService(SiteService siteService) { this.siteService = siteService; } public ServiceRegistry getServiceRegistry() { return serviceRegistry; } public void setServiceRegistry(ServiceRegistry serviceRegistry) { this.serviceRegistry = serviceRegistry; } }