- @Override
- protected void run(AuthzTrans trans) {
- List<String> toList = new ArrayList<>();
- List<String> ccList = new ArrayList<>();
- AuthzTrans noAvg = trans.env().newTransNoAvg();
- String subject = "Test Notify";
- boolean urgent = false;
-
-
-
- final Notify notify = this;
- final Holder<List<String>> info = new Holder<>(null);
- final Set<String> errorSet = new HashSet<>();
-
- try {
- for(File f : notifyFile) {
- CSV csv = new CSV(f);
- try {
- csv.visit(new CSV.Visitor() {
- @Override
- public void visit(List<String> row) throws IOException, CadiException {
- if("info".equals(row.get(0))) {
- info.set(row);
- }
- if(info.get()==null) {
- throw new CadiException("First line of Feed MUST contain 'info' record");
- }
- String key = row.get(0)+'|'+info.get().get(1);
- NotifyBody body = NotifyBody.get(key);
- if(body==null) {
- errorSet.add("No NotifyBody defined for " + key);
- } else {
- body.store(row);
- }
- }
- });
- } catch (IOException | CadiException e) {
- e.printStackTrace();
- }
-
- // now create Notification
- for(NotifyBody nb : NotifyBody.getAll()) {
- for(String id : nb.users()) {
- toList.clear();
- ccList.clear();
- try {
- String bodyS = nb.body(noAvg, notify, id);
- Identity identity = trans.org().getIdentity(noAvg, id);
- if(!identity.isPerson()) {
- identity = identity.responsibleTo();
- }
- for(int i=1;i<nb.escalation();++i) {
- if(identity != null) {
- if(i==1) {
- toList.add(identity.email());
- } else {
- identity=identity.responsibleTo();
- ccList.add(identity.email());
- }
- }
- }
-
- mailer.sendEmail(noAvg, dryRun, mailFrom, toList, ccList, subject,
- String.format(header,"2.1.9",Identity.mixedCase(identity.firstName()))+
- bodyS +
- footer, urgent);
- } catch (OrganizationException e) {
- trans.error().log(e);
- }
- }
- }
-
- }
+ public Notify(AuthzTrans trans) throws APIException, IOException, OrganizationException {
+ super(trans.env());
+ String mailerCls = env.getProperty("MAILER");
+ String mailFrom = env.getProperty("MAIL_FROM");
+ String header_html = env.getProperty("HEADER_HTML");
+ String footer_html = env.getProperty("FOOTER_HTML");
+ String str = env.getProperty("MAX_EMAIL");
+ guiURL = env.getProperty("GUI_URL");
+ maxEmails = str==null||str.isEmpty()?Integer.MAX_VALUE:Integer.parseInt(str);
+ if(mailerCls==null || mailFrom==null || guiURL==null || header_html==null || footer_html==null) {
+ throw new APIException("Notify requires MAILER, MAILER_FROM, GUI_URL, HEADER_HTML and FOOTER_HTML properties");
+ }
+ try {
+ Class<?> mailc = Class.forName(mailerCls);
+ Constructor<?> mailcst = mailc.getConstructor(Access.class);
+ mailer = (Mailer)mailcst.newInstance(env.access());
+ } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+ throw new APIException("Unable to construct " + mailerCls,e);
+ }
+
+ String line;
+ StringBuilder sb = new StringBuilder();
+ BufferedReader br = new BufferedReader(new FileReader(header_html));
+ try {
+ while((line=br.readLine())!=null) {
+ sb.append(line);
+ sb.append('\n');
+ }
+ String html_css = env.getProperty(HTML_CSS);
+ int hc = sb.indexOf(HTML_CSS);
+ if(hc!=0 && html_css!=null) {
+ header = sb.replace(hc,hc+HTML_CSS.length(), html_css).toString();
+ } else {
+ header = sb.toString();
+ }
+ } finally {
+ br.close();
+ }
+
+ // Establish index from header
+ int lastTag = header.lastIndexOf('<');
+ if(lastTag>0) {
+ int prevCR = header.lastIndexOf('\n',lastTag);
+ if(prevCR>0) {
+ indent = lastTag-prevCR;
+ } else {
+ indent = 6; //arbitrary
+ }
+ } else {
+ indent = 6;
+ }
+
+ urgent = false;
+
+ sb.setLength(0);
+ br = new BufferedReader(new FileReader(footer_html));
+ try {
+ while((line=br.readLine())!=null) {
+ sb.append(line);
+ sb.append('\n');
+ }
+ footer = sb.toString();
+ } finally {
+ br.close();
+ }
+
+ }
+
+ /*
+ * Note: We try to put things related to Notify as Main Class in Run, where we might have put in
+ * Constructor, so that we can have other Classes call just the "notify" method.
+ */
+ @Override
+ protected void run(AuthzTrans trans) {
+ AuthzTrans noAvg = trans.env().newTransNoAvg();
+
+ final Holder<List<String>> info = new Holder<>(null);
+ final Set<String> errorSet = new HashSet<>();
+
+ try {
+ // Class Load possible data
+ NotifyBody.load(env.access());
+
+
+ // Create Intermediate Output
+ File logDir = logDir();
+ Set<File> notifyFile = new HashSet<>();
+ if(args().length>0) {
+ for(int i=0;i<args().length;++i) {
+ notifyFile.add(new File(logDir, args()[i]));
+ }
+ } else {
+ String fmt = "%s"+Chrono.dateOnlyStamp()+".csv";
+ File file;
+ for(NotifyBody nb : NotifyBody.getAll()) {
+ file = new File(logDir,String.format(fmt, nb.name()));
+ if(file.exists()) {
+ trans.info().printf("Processing '%s' in %s",nb.type(),file.getCanonicalPath());
+ notifyFile.add(file);
+ } else {
+ trans.info().printf("No Files found for %s",nb.name());
+ }
+ }
+ }
+
+ for(File f : notifyFile) {
+ CSV csv = new CSV(env.access(),f);
+ try {
+ csv.visit(new CSV.Visitor() {
+ @Override
+ public void visit(List<String> row) throws IOException, CadiException {
+ if("info".equals(row.get(0))) {
+ info.set(row);
+ }
+ if(info.get()==null) {
+ throw new CadiException("First line of Feed MUST contain 'info' record");
+ } String key = row.get(0)+'|'+info.get().get(1);
+ NotifyBody body = NotifyBody.get(key);
+ if(body==null) {
+ errorSet.add("No NotifyBody defined for " + key);
+ } else {
+ body.store(row);
+ }
+ }
+ });
+ } catch (IOException | CadiException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ // now create Notification
+ for(NotifyBody nb : NotifyBody.getAll()) {
+ notify(noAvg, nb);
+ }
+
+ } catch (APIException | IOException e1) {
+ trans.error().log(e1);