Java tutorial
package fr.univlorraine.ecandidat.controllers; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigDecimal; import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.List; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Query; import org.apache.commons.lang.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import fr.univlorraine.ecandidat.entities.siscol.WSAdresse; import fr.univlorraine.ecandidat.entities.siscol.WSCursusInterne; import fr.univlorraine.ecandidat.entities.siscol.WSBac; import fr.univlorraine.ecandidat.entities.siscol.WSIndividu; import fr.univlorraine.ecandidat.services.file.FileManager; import fr.univlorraine.ecandidat.services.ldap.PeopleLdap; import fr.univlorraine.ecandidat.services.siscol.SiScolException; import fr.univlorraine.ecandidat.utils.ConstanteUtils; /** * Gestion de la version de dmo de l'application * @author Kevin Hergalant */ @Component public class DemoController { private Logger logger = LoggerFactory.getLogger(DemoController.class); /* Injections */ @Resource private transient Environment environment; @Resource private transient ApplicationContext applicationContext; @Resource private transient EntityManagerFactory entityManagerFactoryEcandidat; @Resource private transient FileManager fileManager; @Resource private transient NomenclatureController nomenclatureController; @Resource private transient DroitProfilController droitProfilController; @Value("${file.filesystem.candidat.path}") private String folderCandidat; @Value("${file.filesystem.gestionnaire.path}") private String folderGestionnaire; private Boolean isDemoMode = null; public Boolean getDemoMode() { if (isDemoMode == null) { isDemoMode = Boolean.valueOf(environment.getProperty("demoMode")); if (isDemoMode == null) { isDemoMode = false; } } return isDemoMode; } /** Le batch de dmo * @throws SiScolException */ public void launchDemoBatch() throws SiScolException { if (getDemoMode()) { logger.debug("Lancement du batch demo"); cleanData(); nomenclatureController.majNomenclature(true); cleanFolderFiles(); addAdmins(); populateData(); logger.debug("Fin du batch demo"); } } /** * Lance le script pour effacer les donnes */ @Transactional private void cleanData() { logger.debug("Demo : debut clean des datas"); launchSqlScript("cleanData.sql"); logger.debug("Demo : fin clean des datas"); } /** * Lance le script pour ajouter les donnes */ @Transactional private void populateData() { logger.debug("Demo : debut clean des datas"); launchSqlScript("populateData.sql"); logger.debug("Demo : fin clean des datas"); } /** * Ajoute les administrateurs */ private void addAdmins() { droitProfilController.addDroitProfilIndForAdmin("ziller5", "Olivier Ziller", "olivier.ziller@univ-lorraine.fr"); droitProfilController.addDroitProfilIndForAdmin("champmar5", "Cedric Champmartin", "cedric.champmartin@univ-lorraine.fr"); droitProfilController.addDroitProfilIndForAdmin("eCandidat", "eCandidat", "kevin.hergalant@univ-lorraine.fr"); droitProfilController.addDroitProfilIndForAdmin("andre7", "Severine Klipfel", "severine.klipfel@univ-lorraine.fr"); } /** * Vide les repertoires */ public void cleanFolderFiles() { if (fileManager.getType().equals(ConstanteUtils.TYPE_FICHIER_STOCK_FILE_SYSTEM)) { cleanFiles(folderCandidat); cleanFiles(folderGestionnaire); } } /** * Vide les fichiers d'un repertoire */ private void cleanFiles(String folderPath) { if (folderPath == null) { return; } File folder = new File(folderPath); if (!folder.isDirectory()) { return; } for (File file : folder.listFiles()) { file.delete(); } } /** Lance un script sql * @param script */ @Transactional private void launchSqlScript(String script) { EntityManager em = entityManagerFactoryEcandidat.createEntityManager(); em.getTransaction().begin(); try { final InputStream inputStream = this.getClass().getResourceAsStream("/db/demo/" + script); final InputStreamReader inputStreamReader = new InputStreamReader(inputStream); final BufferedReader bufferedReader = new BufferedReader(inputStreamReader); while (bufferedReader.ready()) { Query query = em.createNativeQuery(bufferedReader.readLine()); query.executeUpdate(); } } catch (Exception e) { em.getTransaction().rollback(); em.close(); } em.getTransaction().commit(); em.close(); } /** * @return une liste de peopleLdap anonyme pour la recherche Ldap */ public List<PeopleLdap> findListIndividuLdapDemo() { List<PeopleLdap> liste = new ArrayList<PeopleLdap>(); for (int i = 0; i < 6; i++) { String login = RandomStringUtils.randomAlphabetic(8).toLowerCase() + RandomStringUtils.randomNumeric(1); PeopleLdap people = new PeopleLdap(login, "displayName-" + login, "sn-" + login, "cn-" + login, "mail-" + login, null, "M.", "givenName-" + login); liste.add(people); } return liste; } /** * @param ine * @return un individu Apogee anonyme */ public WSIndividu recupInfoEtudiant(String ine) { WSIndividu ind = null; GregorianCalendar cal = new GregorianCalendar(); cal.set(1992, 2, 12); if (ine != null && ine.equals("000000000") || ine.equals("1111111111") || ine.equals("2222222222") || ine.equals("3333333333") || ine.equals("4444444444") || ine.equals("5555555555") || ine.equals("6666666666") || ine.equals("7777777777") || ine.equals("8888888888") || ine.equals("9999999999")) { String cpt = ine.substring(0, 1); ind = new WSIndividu(1, "1", "057", new BigDecimal(ine), ine, "A", "D", cal.getTime(), "NomPat-" + cpt, "NomUsu-" + cpt, "Prenom1-" + cpt, "Prenom2-" + cpt, "Metz", "100"); ind.setAdresse(new WSAdresse(1, "57000", "57463", "100", "15 rue de Nancy", "Etage 1", "Porte droite", "0383542120", "0612356421")); ind.setBac(new WSBac(new Long(ine), "S", "057", "0573227Y", null, "2009", "O")); List<WSCursusInterne> listCursusInterne = new ArrayList<WSCursusInterne>(); listCursusInterne.add(new WSCursusInterne("VET001-001", "License 1 - Droit", "2010", "AB", "1")); listCursusInterne.add(new WSCursusInterne("VET001-002", "License 2 - Droit", "2011", "P", "1")); listCursusInterne.add(new WSCursusInterne("VET001-003", "License 2 - Droit", "2012", "P", "1")); listCursusInterne.add(new WSCursusInterne("VET001-004", "Master 1 - Droit", "2013", "B", "1")); listCursusInterne.add(new WSCursusInterne("VET001-005", "Master 2 - Droit", "2014", "P", "1")); ind.setListCursusInterne(listCursusInterne); } return ind; } }