Information

0
Story Points

Technologies

Decompiled Java File
package com.mpdmal.cloudental.util;

import ch.qos.logback.classic.Logger;
import com.mpdmal.cloudental.entities.Patient;
import com.mpdmal.cloudental.util.exception.InvalidAddressTypeException;
import com.mpdmal.cloudental.util.exception.InvalidContactInfoTypeException;
import com.mpdmal.cloudental.util.exception.InvalidMedEntryAlertException;
import com.mpdmal.cloudental.util.exception.InvalidMedIntakeRouteException;
import com.mpdmal.cloudental.util.exception.InvalidPostitAlertException;
import com.mpdmal.cloudental.util.exception.InvalidTitleFormatTypeException;
import com.mpdmal.cloudental.util.exception.ValidationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.persistence.Query;
import javax.validation.ConstraintViolationException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.util.FileResolver;
import org.hibernate.validator.engine.ConstraintViolationImpl;
import org.slf4j.LoggerFactory;

public class CloudentUtils {
   public static final int DEFAULT_USER_ID = -1;
   public static final int DEFAULT_DISCOUNT_ID = -1;
   public static final int DEFAULT_PRICEABLE_ID = -1;
   public static final int REPORTTYPE_PATIENTS = 1;
   public static final int REPORTTYPE_PHARMACY = 2;
   private static final String DBSTRING = "jdbc:postgresql://localhost:5432/CloudentDB";
   private static final String DBUSER = "aza";
   private static final String DBPWD = "aza";
   private static final String RESOURCES_RELATIVEDIR = "cloudental/jasper/images/";
   private static final String PATIENTS_REPORT_JASPER = "cloudental/jasper/patient_report.jasper";
   private static final String PATIENTS_REPORT_PDF = "cloudental/reporting/patient_report_$.pdf";
   private static final String PRESCRIPTIONS_REPORT_JASPER = "cloudental/jasper/prescriptions_report.jasper";
   private static final String PRESCRIPTIONS_REPORT_PDF = "cloudental/reporting/prescriptions_report_$.pdf";
   public static final String CLOUDENT_ACCOUNT = "cloudental@gmail.com";
   public static final String CLOUDENT_PWD = "cloudental123!";
   private static final Logger logger = (Logger)LoggerFactory.getLogger(CloudentUtils.class);
   private static final Logger servicelogger = (Logger)LoggerFactory.getLogger("com.mpdmal");
   private static final int LOG_TYPE_MSG = 1;
   private static final int LOG_TYPE_WARNING = 2;
   private static final int LOG_TYPE_ERROR = 3;
   private static final int LOG_TYPE_SVCLOG = 4;

   public static boolean isPostitAlertValid(int type) throws InvalidPostitAlertException {
      CloudentUtils.PostitAlertType[] var1 = CloudentUtils.PostitAlertType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.PostitAlertType tp = var1[var3];
         if(type == tp.getValue()) {
            return true;
         }
      }

      return false;
   }

   public static String findPostitAlertDescr(int type) {
      CloudentUtils.PostitAlertType[] var1 = CloudentUtils.PostitAlertType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.PostitAlertType tp = var1[var3];
         if(tp.getValue() == type) {
            return tp.getDescription();
         }
      }

      return "";
   }

   public static boolean isMedEntryAlertValid(int type) throws InvalidMedEntryAlertException {
      CloudentUtils.MedEntryAlertType[] var1 = CloudentUtils.MedEntryAlertType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.MedEntryAlertType tp = var1[var3];
         if(type == tp.getValue()) {
            return true;
         }
      }

      return false;
   }

   public static String findMedEntryAlertDescr(int type) {
      CloudentUtils.MedEntryAlertType[] var1 = CloudentUtils.MedEntryAlertType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.MedEntryAlertType tp = var1[var3];
         if(tp.getValue() == type) {
            return tp.getDescription();
         }
      }

      return "";
   }

   public static boolean isContactInfoTypeValid(int type) throws InvalidContactInfoTypeException {
      CloudentUtils.ContactInfoType[] var1 = CloudentUtils.ContactInfoType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.ContactInfoType tp = var1[var3];
         if(type == tp.getValue()) {
            return true;
         }
      }

      return false;
   }

   public static String findContactInfoTypeDescr(int type) {
      CloudentUtils.ContactInfoType[] var1 = CloudentUtils.ContactInfoType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.ContactInfoType tp = var1[var3];
         if(tp.getValue() == type) {
            return tp.getDescription();
         }
      }

      return "";
   }

   public static boolean isAddressTypeValid(int type) throws InvalidAddressTypeException {
      CloudentUtils.AddressType[] var1 = CloudentUtils.AddressType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.AddressType tp = var1[var3];
         if(type == tp.getValue()) {
            return true;
         }
      }

      return false;
   }

   public static String findAddressTypeDescr(int type) {
      CloudentUtils.AddressType[] var1 = CloudentUtils.AddressType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.AddressType tp = var1[var3];
         if(tp.getValue() == type) {
            return tp.getDescription();
         }
      }

      return "";
   }

   public static boolean isPrescrRowTimeunitValid(int type) {
      CloudentUtils.PrescrRowTimeunit[] var1 = CloudentUtils.PrescrRowTimeunit.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.PrescrRowTimeunit tp = var1[var3];
         if(type == tp.getValue()) {
            return true;
         }
      }

      return false;
   }

   public static String findPrescrRowTimeunitDurDescr(int type) {
      CloudentUtils.PrescrRowTimeunit[] var1 = CloudentUtils.PrescrRowTimeunit.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.PrescrRowTimeunit tp = var1[var3];
         if(tp.getValue() == type) {
            return tp.getDurUnitDescription();
         }
      }

      return "";
   }

   public static String findPrescrRowTimeunitFreqDescr(int type) {
      CloudentUtils.PrescrRowTimeunit[] var1 = CloudentUtils.PrescrRowTimeunit.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.PrescrRowTimeunit tp = var1[var3];
         if(tp.getValue() == type) {
            return tp.getFreqUnitDescription();
         }
      }

      return "";
   }

   public static boolean isTitleFormatTypeValid(int type) throws InvalidTitleFormatTypeException {
      CloudentUtils.EventTitleFormatType[] var1 = CloudentUtils.EventTitleFormatType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.EventTitleFormatType tp = var1[var3];
         if(type == tp.getValue()) {
            return true;
         }
      }

      return false;
   }

   public static String findTitleFormatTypeDescr(int type) {
      CloudentUtils.EventTitleFormatType[] var1 = CloudentUtils.EventTitleFormatType.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.EventTitleFormatType tp = var1[var3];
         if(tp.getValue() == type) {
            return tp.getDescription();
         }
      }

      return "";
   }

   public static String createEventTitle(int type, Patient p) throws InvalidTitleFormatTypeException {
      if(!isTitleFormatTypeValid(type)) {
         throw new InvalidTitleFormatTypeException(type);
      } else {
         return type == CloudentUtils.EventTitleFormatType.FULL.getValue()?p.getSurname() + " " + p.getName():(type == CloudentUtils.EventTitleFormatType.NAME.getValue()?p.getName():(type == CloudentUtils.EventTitleFormatType.SHORT.getValue()?p.getSurname() + " " + p.getName().substring(0, 1):p.getSurname()));
      }
   }

   public static boolean isMedIntakeRouteValid(int type) throws InvalidMedIntakeRouteException {
      CloudentUtils.MedIntakeRoute[] var1 = CloudentUtils.MedIntakeRoute.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.MedIntakeRoute rt = var1[var3];
         if(type == rt.getValue()) {
            return true;
         }
      }

      return false;
   }

   public static String findMedIntakeRouteDescr(int type) {
      CloudentUtils.MedIntakeRoute[] var1 = CloudentUtils.MedIntakeRoute.values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         CloudentUtils.MedIntakeRoute rt = var1[var3];
         if(rt.getValue() == type) {
            return rt.getDescription();
         }
      }

      return "";
   }

   public static void logQueryString(Query q) {
      System.out.println(q.toString());
   }

   public static void logServicecall(String entry) {
      log(entry, 4);
   }

   public static void logMessage(String entry) {
      log(entry, 1);
   }

   public static void logError(String entry) {
      log(entry, 3);
   }

   public static void logWarning(String entry) {
      log(entry, 2);
   }

   public static void log(String entry, int type) {
      switch(type) {
      case 1:
         logger.info(entry);
         break;
      case 2:
         logger.warn(entry);
         break;
      case 3:
         logger.error(entry);
         break;
      case 4:
         servicelogger.debug(entry);
      }

   }

   public static ValidationException createValidationException(ConstraintViolationException e) {
      String msg = "";

      ConstraintViolationImpl impl;
      for(Iterator it = e.getConstraintViolations().iterator(); it.hasNext(); msg = msg.concat(" " + impl.getMessage() + "\n")) {
         impl = (ConstraintViolationImpl)it.next();
         String name = impl.getRootBean().getClass().toString();
         name = name.substring(name.lastIndexOf(".") + 1, name.length());
         msg = msg.concat("Property->" + impl.getPropertyPath().toString().toUpperCase());
         msg = msg.concat(" on Entity->" + name.toUpperCase());
      }

      logError(msg);
      return new ValidationException(msg, e);
   }

   public static void logMethodInfo(Method m, Object[] prms) {
      StringBuilder sb = new StringBuilder("bean service fired:" + m.getName());
      if(prms == null) {
         sb.append("  [no args]");
         logServicecall(sb.toString());
      } else {
         sb.append("  [ ");
         Object[] var3 = prms;
         int var4 = prms.length;

         for(int var5 = 0; var5 < var4; ++var5) {
            Object o = var3[var5];
            if(o != null) {
               sb.append(o.toString()).append(", ");
            }
         }

         sb.delete(sb.length() - 2, sb.length());
         sb.append("]");
         logServicecall(sb.toString());
      }
   }

   public static void logContextData(Map data) {
      Set keys = data.keySet();
      StringBuilder sb = new StringBuilder();
      sb.append("Context Data ------>");
      Iterator var3 = keys.iterator();

      while(var3.hasNext()) {
         String key = (String)var3.next();
         Object val = data.get(key);
         sb.append(key).append(":").append(val.toString()).append("");
      }

      sb.append("<------ End Context Data ");
      logServicecall(sb.toString());
   }

   public static String printPatientReport(int dentistid) throws FileNotFoundException, JRException {
      String outfile = "";
      HashMap parameters = getDefaultReportParameters();
      parameters.put("DENTISTID", new Integer(dentistid));
      String infile = "cloudental/jasper/patient_report.jasper";
      outfile = "cloudental/reporting/patient_report_$.pdf";
      JasperPrint jprint = JasperFillManager.fillReport(new FileInputStream(infile), parameters, getSystemConnection());
      outfile = outfile.replace("$", "" + dentistid);
      JasperExportManager.exportReportToPdfFile(jprint, outfile);
      logMessage("Created PATIENTS REPORT: " + outfile);
      return outfile;
   }

   public static String printPrescriptionReport(int prescid, String headertext, String patientname) throws FileNotFoundException, JRException {
      String outfile = "";
      HashMap parameters = getDefaultReportParameters();
      parameters.put("PRESCRIPTIONID", new Integer(prescid));
      parameters.put("HEADERTEXT", headertext);
      parameters.put("PATIENTNAME", patientname);
      String infile = "cloudental/jasper/prescriptions_report.jasper";
      outfile = "cloudental/reporting/prescriptions_report_$.pdf";
      JasperPrint jprint = JasperFillManager.fillReport(new FileInputStream(infile), parameters, getSystemConnection());
      outfile = outfile.replace("$", "" + prescid);
      JasperExportManager.exportReportToPdfFile(jprint, outfile);
      logMessage("Created PRESCRIPTION REPORT: " + outfile);
      return outfile;
   }

   private static Connection getSystemConnection() {
      try {
         Class.forName("org.postgresql.Driver");
         return DriverManager.getConnection("jdbc:postgresql://localhost:5432/CloudentDB", "aza", "aza");
      } catch (ClassNotFoundException var1) {
         var1.printStackTrace();
      } catch (SQLException var2) {
         var2.printStackTrace();
      }

      return null;
   }

   public static void mailReport(String pdf, String email) throws MessagingException {
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      Session session = Session.getInstance(props);
      if(!verifyEmail(email)) {
         logError("\tinvalid email :" + email);
         throw new MessagingException("invalid email :" + email);
      } else {
         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress("cloudental@gmail.com"));
         message.setRecipients(RecipientType.TO, InternetAddress.parse(email));
         message.setSubject("Cloudental - on demand reporting");
         MimeBodyPart messageBodyPart = new MimeBodyPart();
         FileDataSource ds = new FileDataSource(pdf);
         messageBodyPart.setDataHandler(new DataHandler(ds));
         messageBodyPart.setFileName(ds.getName());
         MimeMultipart mp = new MimeMultipart();
         mp.addBodyPart(messageBodyPart);
         message.setContent(mp);
         message.setSentDate(new Date());
         Transport transport = session.getTransport("smtp");
         transport.connect("smtp.gmail.com", 587, "cloudental@gmail.com", "cloudental123!");
         transport.sendMessage(message, message.getAllRecipients());
         logMessage("\temailed :" + email + " a patient report");
      }
   }

   private static boolean verifyEmail(String email) {
      if(email.length() <= 0) {
         return false;
      } else {
         int atidx = email.indexOf("@");
         int dotidx = email.indexOf(".");
         return atidx >= 1 && dotidx >= 3;
      }
   }

   private static HashMap getDefaultReportParameters() {
      FileResolver fileResolver = new FileResolver() {
         public File resolveFile(String fileName) {
            return new File("cloudental/jasper/images/" + fileName);
         }
      };
      HashMap parameters = new HashMap();
      parameters.put("REPORT_FILE_RESOLVER", fileResolver);
      return parameters;
   }

   public static void contactCloudental(String title, String msg) throws AddressException, MessagingException {
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      Session session = Session.getInstance(props);
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress("cloudental@gmail.com"));
      message.setRecipients(RecipientType.TO, InternetAddress.parse("arilou_npl@hotmail.com"));
      message.setSubject("CD contact:" + title);
      message.setContent(msg, "text/plain");
      message.setSentDate(new Date());
      Transport transport = session.getTransport("smtp");
      transport.connect("smtp.gmail.com", 587, "cloudental@gmail.com", "cloudental123!");
      transport.sendMessage(message, message.getAllRecipients());
      logMessage("\temailed Cloudental! (patco, dimaz)");
   }

   public static enum MedIntakeRoute {
      ORAL(0, "Oral"),
      SUBLINGUAL(1, "Sublingual"),
      RECTAL(2, "Rectal"),
      TRANSDERMAL(3, "Transdermal"),
      TRANSMUCOSAL(4, "Transmucosal");

      private final String desc;
      private final int value;

      private MedIntakeRoute(int type, String desc) {
         this.value = type;
         this.desc = desc;
      }

      public int getValue() {
         return this.value;
      }

      public String getDescription() {
         return this.desc;
      }
   }

   public static enum EventTitleFormatType {
      FULL(1, "Name and Surname"),
      NAME(2, "Name only"),
      SURNAME(3, "Surname only"),
      SHORT(4, "Surname and initial");

      private final String desc;
      private final int value;

      private EventTitleFormatType(int type, String desc) {
         this.value = type;
         this.desc = desc;
      }

      public int getValue() {
         return this.value;
      }

      public String getDescription() {
         return this.desc;
      }
   }

   public static enum PrescrRowTimeunit {
      HOURS(11, "hour(s)", "hour(s)"),
      DAYS(5, "day(s)", "day(s)"),
      WEEK(4, "week(s)", "week(s)"),
      MONTH(2, "month(s)", "month(s)");

      private final String ddesc;
      private final String fdesc;
      private final int value;

      private PrescrRowTimeunit(int type, String fdesc, String ddesc) {
         this.value = type;
         this.fdesc = fdesc;
         this.ddesc = ddesc;
      }

      public int getValue() {
         return this.value;
      }

      public String getFreqUnitDescription() {
         return this.fdesc;
      }

      public String getDurUnitDescription() {
         return this.ddesc;
      }
   }

   public static enum AddressType {
      HOME(0, "Home Address"),
      OFFICE(1, "Office Address"),
      BILLING(2, "Billing Address");

      private final String desc;
      private final int value;

      private AddressType(int type, String desc) {
         this.value = type;
         this.desc = desc;
      }

      public int getValue() {
         return this.value;
      }

      public String getDescription() {
         return this.desc;
      }
   }

   public static enum ContactInfoType {
      EMAIL(0, "E-mail"),
      FAX(1, "Fax"),
      HOME(2, "Home Number "),
      OFFICE(3, "Office Number "),
      MOBILE(4, "Mobile Phone ");

      private final String desc;
      private final int value;

      private ContactInfoType(int type, String desc) {
         this.value = type;
         this.desc = desc;
      }

      public int getValue() {
         return this.value;
      }

      public String getDescription() {
         return this.desc;
      }
   }

   public static enum MedEntryAlertType {
      NOALERT(0, "Normal"),
      LOW(1, "Low"),
      MEDIUM(2, "Medium"),
      HIGH(3, "High");

      private final String desc;
      private final int value;

      private MedEntryAlertType(int type, String desc) {
         this.value = type;
         this.desc = desc;
      }

      public int getValue() {
         return this.value;
      }

      public String getDescription() {
         return this.desc;
      }
   }

   public static enum PostitAlertType {
      NOTE(1, "Note"),
      TODO(2, "To do"),
      ALARM(3, "Alarm");

      private final String desc;
      private final int value;

      private PostitAlertType(int type, String desc) {
         this.value = type;
         this.desc = desc;
      }

      public int getValue() {
         return this.value;
      }

      public String getDescription() {
         return this.desc;
      }
   }
}
Page generated: Oct 19, 2017 2:34:18 PM