Batch, Remove unneeded Classes, refine, etc
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / reports / Notify.java
index 7cddea2..e6ce084 100644 (file)
@@ -69,11 +69,13 @@ import org.onap.aaf.misc.env.util.Chrono;
         private PropAccess access;
         private AuthzTrans noAvg;
         private CQLBatch cqlBatch;
+       private LastNotified  lastN;
+       private CQLBatchLoop cbl;
 
         public Notify(AuthzTrans trans) throws APIException, IOException, OrganizationException {
                 super(trans.env());
                 access = env.access();
-                session = super.cluster.connect();
+                session = cluster.connect();
 
                 String mailerCls = env.getProperty("MAILER");
                 String mailFrom = env.getProperty("MAIL_FROM");
@@ -95,7 +97,11 @@ import org.onap.aaf.misc.env.util.Chrono;
 
                 String line;
                 StringBuilder sb = new StringBuilder();
-                BufferedReader br = new BufferedReader(new FileReader(header_html));
+                File fhh = new File(header_html);
+                if(!fhh.exists()) {
+                        throw new APIException(header_html + " does not exist");
+                }
+                BufferedReader br = new BufferedReader(new FileReader(fhh));
                 try {
                         while((line=br.readLine())!=null) {
                                 sb.append(line);
@@ -130,7 +136,12 @@ import org.onap.aaf.misc.env.util.Chrono;
                 urgent = false;
                 
                 sb.setLength(0);
-                br = new BufferedReader(new FileReader(footer_html));
+                fhh = new File(footer_html);
+                if(!fhh.exists()) {
+                        throw new APIException(footer_html + " does not exist");
+                }
+
+                br = new BufferedReader(new FileReader(fhh));
                 try {
                         while((line=br.readLine())!=null) {
                                 sb.append(line);
@@ -142,7 +153,10 @@ import org.onap.aaf.misc.env.util.Chrono;
                 }
 
                 noAvg = trans.env().newTransNoAvg();
-                cqlBatch = new CQLBatch(noAvg.info(),session); 
+                cqlBatch = new CQLBatch(noAvg.debug(),session); 
+                cbl = new CQLBatchLoop(cqlBatch,50,dryRun);
+                
+                lastN = new LastNotified(session);
         }
 
         /*
@@ -209,7 +223,8 @@ import org.onap.aaf.misc.env.util.Chrono;
 
                         // now create Notification
                         for(NotifyBody nb : NotifyBody.getAll()) {
-                                notify(noAvg, nb);
+                                int count = notify(noAvg, nb);
+                                trans.info().printf("Emailed %d for %s",count,nb.name());
                         }
                         
                         //
@@ -251,8 +266,7 @@ import org.onap.aaf.misc.env.util.Chrono;
                                                }
                                        });
                                } catch (IOException | CadiException e) {
-                                       e.printStackTrace();
-                                       // .... but continue with next row
+                                       trans.error().log(e);
                        } finally {
                                tt.done();
                        }
@@ -267,44 +281,47 @@ import org.onap.aaf.misc.env.util.Chrono;
                CSV.Saver rs = new CSV.Saver();
                
                TimeTaken tt = trans.start("Obtain Last Notifications for Approvers", Trans.SUB);
-               LastNotified lastN;
                try {
-                       lastN = new LastNotified(session);
                        lastN.add(mpending.keySet());
                } finally {
                        tt.done();
                }
                
                Pending p;
-               final CQLBatchLoop cbl = new CQLBatchLoop(cqlBatch,50,dryRun);
+               
                tt = trans.start("Notify for Pending", Trans.SUB);
+               List<String> idList = new ArrayList<String>();
+               String id;
                try {
                        for(Entry<String, Pending> es : mpending.entrySet()) {
+                               id = es.getKey();
+                               idList.clear();
+                               idList.add(id);
                                p = es.getValue();
                                boolean nap = p.newApprovals();
                                if(!nap) {
-                               Date dateLastNotified = lastN.lastNotified(es.getKey(),"pending","");
+                               Date dateLastNotified = lastN.lastNotified(id,"pending","");
                                if(dateLastNotified==null || dateLastNotified.after(oneWeek) ) {
                                        nap=true;
                                }
                                }
                                if(nap) {
-                                       rs.row("appr", es.getKey(),p.qty(),batchEnv);
+                                       rs.row("appr", id,p.qty(),batchEnv);
                                        npab.store(rs.asList());
                                        if(notify(noAvg, npab)>0) {
                                                // Update
                                                cbl.preLoop();
-                                               lastN.update(cbl.inc(),es.getKey(),"pending","");
+//                                             lastN.update(cbl.inc(),key,"pending","");
+                                               npab.record(trans,cbl.inc(), id, idList, lastN);
+                                               npab.inc();
                                        }
                                }
                        }
                } finally {
                        cbl.flush();
                        tt.done();
+                trans.info().printf("Notified %d persons of Pending Approvals", npab.count());
                }
-            trans.info().printf("Created %d Notifications", count.get());
-
-
 
                } catch (APIException | IOException e1) {
                        trans.error().log(e1);
@@ -315,78 +332,65 @@ import org.onap.aaf.misc.env.util.Chrono;
                 }
         }
 
-        public int notify(AuthzTrans trans, NotifyBody nb) {
+        private int notify(AuthzTrans trans, NotifyBody nb) {
                 List<String> toList = new ArrayList<>();
                 List<String> ccList = new ArrayList<>();
+                List<String> idList = new ArrayList<>();
 
                 String run = nb.type()+nb.name();
                 String test = dryRun?run:null;
-                String last = null;
                 
                 ONE_EMAIL:
                 for(String id : nb.users()) {
-                        last = id;
                         toList.clear();
                         ccList.clear();
+                        idList.clear();
                         try {
-                                Identity identity = trans.org().getIdentity(trans, id);
-                                if(identity==null) {
+                                List<Identity> identities = trans.org().getIDs(trans, id, nb.escalation());
+                                if(identities.isEmpty()) {
                                         trans.warn().printf("%s is invalid for this Organization. Skipping notification.",id);
                                 } else {
-                                        if(!identity.isPerson()) {
-                                                identity = identity.responsibleTo();
-                                        }
-                                        if(identity==null) {
-                                                trans.warn().printf("Responsible Identity %s is invalid for this Organization. Skipping notification.",id);
-                                        } else {
-                                                for(int i=1;i<=nb.escalation();++i) {
-                                                        if(identity != null) {
-                                                                if(i==1) { // self and Delegates
-                                                                        toList.add(identity.email());
-                                                                        List<String> dels = identity.delegate();
-                                                                        if(dels!=null) {
-                                                                                for(String d : dels) {
-                                                                                        toList.add(d);
-                                                                                }
-                                                                        }
-                                                                } else {
-                                                                        Identity s = identity.responsibleTo();
-                                                                        if(s==null) {
-                                                                                trans.error().printf("Identity %s has no %s", identity.fullID(),
-                                                                                                identity.isPerson()?"supervisor":"sponsor");
-                                                                        } else {
-                                                                                ccList.add(s.email());
-                                                                        }
-                                                                }
-                                                        }
+                                        Identity identity = null;
+                                        for(Identity ident : identities) {
+                                                if(identity==null) {
+                                                        identity = ident;
+                                                        toList.add(ident.email());
+                                                } else {
+                                                        ccList.add(ident.email());
                                                 }
+                                                idList.add(ident.fullID());
+                                        }
+                                        StringBuilder content = new StringBuilder();
+                                        content.append(String.format(header,version,Identity.mixedCase(identity.firstName())));
 
-                                                StringBuilder content = new StringBuilder();
-                                                content.append(String.format(header,version,Identity.mixedCase(identity.firstName())));
-
-                                                nb.body(trans, content, indent, this, id);
-                                                content.append(footer);
+                                        nb.body(trans, content, indent, this, id);
+                                        content.append(footer);
 
-                                                if(mailer.sendEmail(trans, test, toList, ccList, nb.subject(),content.toString(), urgent)) {
-                                                        nb.inc();
-                                                } else {
-                                                        trans.error().log("Mailer failed to send Mail");
-                                                }
-                                                if(maxEmails>0 && nb.count()>=maxEmails) {
-                                                        break ONE_EMAIL;
-                                                }
+                                        if(mailer.sendEmail(trans, test, toList, ccList, nb.subject(),content.toString(), urgent)) {
+                                               cbl.preLoop();
+                                               nb.record(trans,cbl.inc(), id, idList, lastN);
+                                               nb.inc();
+                                        } else {
+                                                trans.error().log("Mailer failed to send Mail");
+                                        }
+                                        if(maxEmails>0 && nb.count()>=maxEmails) {
+                                                break ONE_EMAIL;
                                         }
                                 }
                         } catch (OrganizationException e) {
                                 trans.error().log(e);
                         }
                 }
-                if(nb.count()<=1) {
-                        trans.info().printf("Notified %s for %s",last,run);
-                } else {
-                        trans.info().printf("Emailed %d for %s",nb.count(),run);
-                }
+                cbl.flush();
                 return nb.count();
         }
 
+       /* (non-Javadoc)
+        * @see org.onap.aaf.auth.batch.Batch#_close(org.onap.aaf.auth.env.AuthzTrans)
+        */
+       @Override
+       protected void _close(AuthzTrans trans) {
+               cbl.flush();
+       }
+
  }