[Policy-52, Policy-92, Policy-93] Policy Enhancements and bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / admin / PolicyNotificationMail.java
index 3aee634..1a535dc 100644 (file)
@@ -46,15 +46,15 @@ import org.springframework.mail.javamail.MimeMessageHelper;
 
 @Configurable
 public class PolicyNotificationMail{
-       private static Logger LOGGER    = FlexLogger.getLogger(PolicyNotificationMail.class);
+       private static Logger policyLogger      = FlexLogger.getLogger(PolicyNotificationMail.class);
        
        @Bean
        public JavaMailSenderImpl javaMailSenderImpl(){
                JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
-               mailSender.setHost(PolicyController.smtpHost);
-               mailSender.setPort(Integer.parseInt(PolicyController.smtpPort));
-               mailSender.setUsername(PolicyController.smtpUsername);
-               mailSender.setPassword(PolicyController.smtpPassword);
+               mailSender.setHost(PolicyController.getSmtpHost());
+               mailSender.setPort(Integer.parseInt(PolicyController.getSmtpPort()));
+               mailSender.setUsername(PolicyController.getSmtpUsername());
+               mailSender.setPassword(PolicyController.getSmtpPassword());
                Properties prop = mailSender.getJavaMailProperties();
                prop.put("mail.transport.protocol", "smtp");
                prop.put("mail.smtp.auth", "true");
@@ -63,51 +63,50 @@ public class PolicyNotificationMail{
                return mailSender;
        }
 
-       @SuppressWarnings("resource")
        public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) throws MessagingException {  
-               String from = PolicyController.smtpUsername;
+               String from = PolicyController.getSmtpUsername();
                String to = "";
                String subject = "";
                String message = "";
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date date = new Date();
-               if(mode.equalsIgnoreCase("EditPolicy")){
+               if("EditPolicy".equalsIgnoreCase(mode)){
                        subject = "Policy has been Updated : "+entityItem.getPolicyName();
-                       message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Updated" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
+                       message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Updated" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
                                         + '\n'  + '\n' + "Modified By : " +entityItem.getModifiedBy() + '\n' + "Modified Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
                }
-               if(mode.equalsIgnoreCase("Rename")){
+               if("Rename".equalsIgnoreCase(mode)){
                        subject = "Policy has been Renamed : "+entityItem.getPolicyName();
-                       message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Renamed" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
+                       message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Renamed" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
                                         + '\n'  + '\n' + "Renamed By : " +entityItem.getModifiedBy() + '\n' + "Renamed Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
                }
-               if(mode.equalsIgnoreCase("DeleteAll")){
+               if("DeleteAll".equalsIgnoreCase(mode)){
                        subject = "Policy has been Deleted : "+entityItem.getPolicyName();
-                       message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Deleted with All Versions" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  
+                       message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Deleted with All Versions" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  
                                         + '\n'  + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
                }
-               if(mode.equalsIgnoreCase("DeleteOne")){
+               if("DeleteOne".equalsIgnoreCase(mode)){
                        subject = "Policy has been Deleted : "+entityItem.getPolicyName();
-                       message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  +"Policy Version : " +entityItem.getActiveVersion()
+                       message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  +"Policy Version : " +entityItem.getActiveVersion()
                                         + '\n'  + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
                }
-               if(mode.equalsIgnoreCase("DeleteScope")){
+               if("DeleteScope".equalsIgnoreCase(mode)){
                        subject = "Scope has been Deleted : "+entityItem.getPolicyName();
-                       message = "The Scope Which you are watching in  " + PolicyController.smtpApplicationName + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Scope Name  : "  + policyName + '\n'  
+                       message = "The Scope Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Scope Name  : "  + policyName + '\n'  
                                         + '\n'  + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
                }
-               if(mode.equalsIgnoreCase("SwitchVersion")){
+               if("SwitchVersion".equalsIgnoreCase(mode)){
                        subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
-                       message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been SwitchedVersion" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
+                       message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been SwitchedVersion" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
                                         + '\n'  + '\n' + "Switched By : " +entityItem.getModifiedBy() + '\n' + "Switched Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
                }
-               if(mode.equalsIgnoreCase("Move")){
+               if("Move".equalsIgnoreCase(mode)){
                        subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
-                       message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Moved to Other Scope" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
+                       message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Moved to Other Scope" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
                                         + '\n'  + '\n' + "Moved By : " +entityItem.getModifiedBy() + '\n' + "Moved Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
                }
                String policyFileName = entityItem.getPolicyName();
-               String checkPolicyName = policyFileName;
+               String checkPolicyName = policyName;
                if(policyFileName.contains("/")){
                        policyFileName = policyFileName.substring(0, policyFileName.indexOf("/"));
                        policyFileName = policyFileName.replace("/", File.separator);
@@ -120,44 +119,42 @@ public class PolicyNotificationMail{
                String query = "from WatchPolicyNotificationTable where policyName like'" +policyFileName+"%'";
                boolean sendFlag = false;
                List<Object> watchList = policyNotificationDao.getDataByQuery(query);
-               if(watchList != null){
-                       if(watchList.size() > 0){
-                               for(Object watch : watchList){
-                                       WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
-                                       String watchPolicyName = list.getPolicyName();
-                                       if(watchPolicyName.contains("Config_")){
-                                               if(watchPolicyName.equals(checkPolicyName)){
-                                                       sendFlag = true;
-                                               }
-                                       }else if(watchPolicyName.contains("Action_")){
-                                               if(watchPolicyName.equals(checkPolicyName)){
-                                                       sendFlag = true;
-                                               }
-                                       }else if(watchPolicyName.contains("Decision_")){
-                                               if(watchPolicyName.equals(checkPolicyName)){
-                                                       sendFlag = true;
-                                               }
-                                       }else{
+               if(watchList != null && !watchList.isEmpty()){
+                       for(Object watch : watchList){
+                               WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
+                               String watchPolicyName = list.getPolicyName();
+                               if(watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_")){
+                                       if(watchPolicyName.equals(checkPolicyName)){
                                                sendFlag = true;
+                                       }else{
+                                               sendFlag = false;
                                        }
-                                       if(sendFlag){
-                                               to = list.getLoginIds()+"@"+PolicyController.smtpEmailExtension;
+                               }
+                               if(sendFlag){
+                                       AnnotationConfigApplicationContext ctx = null;
+                                       try {
+                                               to = list.getLoginIds()+"@"+PolicyController.getSmtpEmailExtension();
                                                to = to.trim();
-                                               AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
+                                               ctx = new AnnotationConfigApplicationContext();
                                                ctx.register(PolicyNotificationMail.class);
                                                ctx.refresh();
                                                JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
                                                MimeMessage mimeMessage = mailSender.createMimeMessage();
                                                MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
-                                               try {
-                                                       mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
-                                               } catch (Exception e) {
-                                                       LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
-                                               }
+                                               mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
                                                mailMsg.setTo(to);
                                                mailMsg.setSubject(subject);
                                                mailMsg.setText(message);
                                                mailSender.send(mimeMessage);
+                                               if(mode.equalsIgnoreCase("Rename") || mode.contains("Delete") || mode.contains("Move")){
+                                                       policyNotificationDao.delete(watch);
+                                               }
+                                       } catch (Exception e) {
+                                               policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
+                                       }finally{
+                                               if(ctx != null){
+                                                       ctx.close();
+                                               }
                                        }
                                }
                        }