Advertisement
Guest User

Untitled

a guest
May 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.44 KB | None | 0 0
  1. package it.exprivia.eni.pdl.estrazione;
  2.  
  3. import it.exprivia.eni.pdl.PDLAPP_nomi.Siti;
  4. import it.exprivia.eni.pdl.db.DatabaseManager;
  5. import it.exprivia.eni.pdl.db.DatabaseManagerFactory;
  6. import it.exprivia.eni.pdl.db.QueryRow;
  7. import it.exprivia.eni.pdl.entity.PdlUser;
  8. import it.exprivia.eni.pdl.ldap.PdlLDAPHelper;
  9. import it.exprivia.eni.pdl.utility.PDLConstants;
  10.  
  11. import java.io.PrintWriter;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Arrays;
  14. import java.util.Collection;
  15. import java.util.Collections;
  16. import java.util.Comparator;
  17. import java.util.Date;
  18. import java.util.HashMap;
  19. import java.util.HashSet;
  20. import java.util.Iterator;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Properties;
  25.  
  26. public class EstrazioneUtenze {
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         SimpleDateFormat format = new SimpleDateFormat(
  30.                 "ddMMyyyyHHmmss");
  31.         String config = "config_PROD.properties";
  32.  
  33.         Properties props = new Properties();
  34.         props.load(EstrazioneUtenze.class.getResourceAsStream(config));
  35.  
  36.         PdlLDAPHelper.setup(props.getProperty("ldapUser"), props.getProperty("ldapPassword"),
  37.                 props.getProperty("ldapDomain"), props.getProperty("ldapURL"));
  38.         PdlLDAPHelper ldap = new PdlLDAPHelper();
  39.  
  40.         PrintWriter output = new PrintWriter(
  41.                 "Utenti eWP " + format.format(new Date()) + "_siti.csv");
  42.         //by Ros 21/05/2019 ->
  43.         //String header = "Matricola;Profilo;Nome;Cognome;BusinessUnit;Sito;Mail";
  44.         String header = "Matricola;Profilo;BusinessUnit";
  45.         //<-
  46.        
  47.         String queryUtentiInterni = "SELECT U.MATRICOLA AS USERID, U.NOME AS NOME, U.COGNOME AS COGNOME, "
  48.                 + "UPPER(LISTAGG(R.DESCRIZIONE, ',') WITHIN GROUP(ORDER BY R.DESCRIZIONE)) AS PROFILO, "
  49.                 + "UPPER(LISTAGG(S.DESCRIZIONE, ',') WITHIN GROUP(ORDER BY S.DESCRIZIONE)) AS SITI,"
  50.                 + "UPPER(LISTAGG(TB.CODICE, ',') WITHIN GROUP(ORDER BY TB.CODICE)) AS BUSINESSUNIT "
  51.                 + "FROM TB_UTENTI U, TB_RUOLI_UTENTE TRU, TB_RUOLI R, TB_SITI S, TB_SITI_UTENTE TSU, TB_BU_UTENTE TBU, TB_BU TB "
  52.                 + "WHERE U.ID = TRU.UTENTEID "
  53.                 + "AND TRU.RUOLOID = R.ID "
  54.                 + "AND U.ID = TSU.UTENTEID "
  55.                 + "AND TSU.SITOID = S.ID "
  56.                 + "AND U.ID = TBU.UTENTEID "
  57.                 + "AND TB.ID = TBU.BUID "
  58.                 + "GROUP BY U.MATRICOLA, U.NOME, U.COGNOME ";
  59.  
  60.         String queryUtentiEsterni = "SELECT USER_NAME AS USERID, NOME, COGNOME, "
  61.                 + "LISTAGG(BU, ',') WITHIN GROUP(ORDER BY BU) AS BUSINESSUNIT, "
  62.                 + "LISTAGG(SITO, ',') WITHIN GROUP(ORDER BY SITO) AS SITI, "
  63.                 + "LISTAGG(RUOLI, ',') WITHIN GROUP(ORDER BY RUOLI) AS PROFILO "
  64.                 + "FROM TB_UTENTI_AIE_CONTRATTI "
  65.                 + "WHERE CANCELLATO = 0 "
  66.                 + "GROUP BY USER_NAME, NOME, COGNOME ";
  67.  
  68.         List<QueryRow> utenti = new LinkedList<QueryRow>();
  69.  
  70.         DatabaseManager database = DatabaseManagerFactory.get(props);
  71.         database.connect();
  72.         try {
  73.             output.println(header);
  74.             try {
  75.                 utenti.addAll(database.query(queryUtentiInterni, new Object[] {}));
  76.                 utenti.addAll(database.query(queryUtentiEsterni, new Object[] {}));
  77.  
  78.                 Collections.sort(utenti, new Comparator<QueryRow>() {
  79.  
  80.                     @Override
  81.                     public int compare(QueryRow o1, QueryRow o2) {
  82.                         String matricola1 = (String) o1.get("USERID");
  83.                         String matricola2 = (String) o2.get("USERID");
  84.                         return compareStrings(matricola1, matricola2, true);
  85.                     }
  86.                 });
  87.  
  88.                 //by Ros 21/05/2019->
  89.                 Map<String,String[]> userMap = new HashMap<String,String[]>();
  90.                 //<-
  91.                 for (QueryRow utente : utenti) {
  92.                     String matricola = (String) utente.get("USERID");
  93.  
  94.                     String profilo = utente.get("PROFILO") == null ? "" : utente.get("PROFILO").toString();
  95.  
  96.                     String nome = utente.get("NOME") == null ? "" : utente.get("NOME").toString();
  97.                     String cognome = utente.get("COGNOME") == null ? "" : utente.get("COGNOME").toString();
  98.  
  99.                     String businessUnit = utente.get("BUSINESSUNIT") == null ? "" : utente.get("BUSINESSUNIT").toString();
  100.                     String siti = utente.get("SITI") == null ? "" : utente.get("SITI").toString();
  101.  
  102.                     String mail = "";
  103.                     //aggiunta query ldap per mail solo per utenti interni
  104.                     if(!matricola.contains("@")){
  105.                         PdlUser user = ldap.getPdlUser(matricola);
  106.                         if(user!=null){
  107.                             mail=user.getMail()==null?"":user.getMail();
  108.                         }else{
  109.                             System.err.println("user "+ matricola + " not found on ldap");
  110.                         }
  111.  
  112.                     }else{
  113.                         mail=matricola;
  114.                     }
  115.  
  116.  
  117.                     if (matricola != null) {
  118.                         //by Ros 21/05/2019 ->
  119.                         /*
  120.                                        String input = matricola + ";";
  121.                                        input += asString(Arrays.asList(profilo.split(",")), ", ") + ";";
  122.                                        input += nome + ";";
  123.                                        input += cognome + ";";
  124.                                        input += asString(replaceBusinessUnits(Arrays.asList(businessUnit.split(","))), ", ") + ";";
  125.                                        input += cleanAndList(siti) + ";";
  126.                                        input += mail + ";";
  127.                                        output.println(input);
  128.                          */
  129.                         if (!userMap.containsKey(mail))
  130.                             userMap.put(mail, new String[] {matricola, profilo, businessUnit});
  131.                         else{
  132.                             if (!mail.equals(matricola))
  133.                                 userMap.get(mail)[0] = new String(matricola);
  134.                             userMap.get(mail)[1] += "," + profilo;
  135.                             userMap.get(mail)[2] += "," + businessUnit;
  136.                         }
  137.                         // <-
  138.                     }
  139.                 }
  140.                 //by Ros 21/05/2019 ->
  141.                 for (Map.Entry<String, String[]> pair : userMap.entrySet()){
  142.                     String input = pair.getValue()[0] + ";";
  143.                     input += asString(Arrays.asList(pair.getValue()[1].split(",")), ", ") + ";";
  144.                     input += (pair.getValue()[0].contains("@"))
  145.                             ? asString(replaceBusinessUnits(Arrays.asList(pair.getValue()[2].split(","))), ", ") + ";"
  146.                                     : "" ;
  147.                     output.println(input);
  148.                 }
  149.                 //<-
  150.             } finally {
  151.                 output.flush();
  152.                 output.close();
  153.             }
  154.         } finally {
  155.             database.disconnect();
  156.         }
  157.     }
  158.  
  159.     private static List<String> replaceBusinessUnits(List<String> asList) {
  160.         List<String> replaced = new LinkedList<String>();
  161.         if (asList != null) {
  162.             for (String bu : asList) {
  163.  
  164.                 if (bu == null || bu.trim().isEmpty()) {
  165.                     replaced.add("");
  166.                 } else {
  167.                     if (bu.trim().equalsIgnoreCase("R&M")) {
  168.                         replaced.add("R&M and Chemicals");
  169.                     } else if (bu.trim().equalsIgnoreCase("PW")) {
  170.                         replaced.add("EniPower S.p.A.");
  171.                     } else if (bu.trim().equalsIgnoreCase("VS")) {
  172.                         replaced.add("Versalis");
  173.                     } else {
  174.                         replaced.add("Upstream");
  175.                     }
  176.                 }
  177.  
  178.             }
  179.         }
  180.         return replaced;
  181.     }
  182.  
  183.     private static String cleanAndList(String siti) {
  184.         List<String> result = new LinkedList<String>();
  185.         String[] asArray = siti.split(",");
  186.         for (String a : asArray) {
  187.             Siti s = Siti.fromString(a);
  188.             if (s != null) {
  189.                 result.add(s.getFullName());
  190.             } else {
  191.                 System.err.println(a + " was not recognized");
  192.                 result.add(a);
  193.             }
  194.         }
  195.  
  196.         return asString(result, ", ");
  197.     }
  198.  
  199.     /**
  200.      * Converte in stringa una Collection,
  201.      * utilizzando la stringa passata come secondo parametro
  202.      * per separare i vari elementi.
  203.      * @param removeDuplicates se bisogna rimuovere eventuali duplicati.
  204.      */
  205.     public static <T> String asString(Collection<T> collection, String separator, boolean removeDuplicates) {
  206.         String result = "";
  207.         if (collection != null) {
  208.             Collection<T> coll;
  209.             if (removeDuplicates) {
  210.                 coll = new HashSet<T>(
  211.                         collection);
  212.             } else {
  213.                 coll = new LinkedList<T>(
  214.                         collection);
  215.             }
  216.  
  217.             Iterator<T> iterator = coll.iterator();
  218.             while (iterator.hasNext()) {
  219.                 T next = iterator.next();
  220.                 result += separator + (next == null ? "" : next);
  221.             }
  222.         }
  223.  
  224.         return result.replaceFirst(separator, "");
  225.     }
  226.  
  227.     /**
  228.      * Converte in stringa (eliminando i duplicati) una Collection,
  229.      * utilizzando la stringa passata come secondo parametro
  230.      * per separare i vari elementi.
  231.      */
  232.     public static <T> String asString(Collection<T> collection, String separator) {
  233.         return asString(collection, separator, true);
  234.     }
  235.  
  236.     public static <T> String asString(T[] collection, String separator) {
  237.         return asString(Arrays.asList(collection), separator);
  238.     }
  239.  
  240.     public static int compareStrings(final String str1, final String str2, final boolean nullIsLess) {
  241.         if (str1 == str2) {
  242.             return 0;
  243.         }
  244.         if (str1 == null) {
  245.             return nullIsLess ? -1 : 1;
  246.         }
  247.         if (str2 == null) {
  248.             return nullIsLess ? 1 : -1;
  249.         }
  250.         return str1.compareTo(str2);
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement