git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube...
authorSweidan Omar <TFS\sweidan.omar>
Thu, 28 Apr 2022 08:13:25 +0000 (08:13 +0000)
committerSweidan Omar <TFS\sweidan.omar>
Thu, 28 Apr 2022 08:13:25 +0000 (08:13 +0000)
server/user.jobengine.osgi.server/src/user/jobengine/server/JobEngineConfiguration.java

index cbbb7b09db717557217046bdcd6318adbae868ee..af69ea3cc61f5f391a862914043cf1e034dc0d59 100644 (file)
@@ -1,5 +1,6 @@
 package user.jobengine.server;\r
 \r
+import java.io.FileInputStream;\r
 import java.io.FileNotFoundException;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
@@ -12,6 +13,7 @@ import java.nio.file.FileSystems;
 import java.nio.file.Files;\r
 import java.nio.file.Path;\r
 import java.nio.file.Paths;\r
+import java.nio.file.attribute.DosFileAttributeView;\r
 import java.util.AbstractMap.SimpleEntry;\r
 import java.util.ArrayList;\r
 import java.util.Collections;\r
@@ -31,6 +33,7 @@ import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;\r
 import javax.xml.transform.stream.StreamResult;\r
 \r
+import org.apache.commons.io.FilenameUtils;\r
 import org.apache.commons.lang.RandomStringUtils;\r
 import org.apache.commons.lang.StringUtils;\r
 import org.apache.logging.log4j.LogManager;\r
@@ -56,7 +59,6 @@ public class JobEngineConfiguration implements IJobEngineConfiguration {
        public static final String CONF_MAESTRO = "settings/maestro.yaml";\r
        public static final String CONF_EXECUTORS = "jobs/executors.xml";\r
        public static final String CONF_SCHEDULES = "jobs/schedules.json";\r
-       public static final String CONF_SCHEDULES2 = "jobs/schedules2.json";\r
        public static final String DIR_STEPS = "jobs/steps";\r
        public static final String DIR_CLASSES = "jobs/classes";\r
        public static final String DIR_TEMPLATES = "jobs/templates";\r
@@ -378,53 +380,86 @@ public class JobEngineConfiguration implements IJobEngineConfiguration {
        }\r
 \r
        @Override\r
-       public void saveSchedule(BasicDBObject processToSave) throws Exception {\r
-               logger.info("saveSchedule()");\r
-               schedules.add(new SimpleEntry<String, BasicDBObject>("", processToSave));\r
+       public void saveSchedulesJson() throws Exception {\r
+               logger.info("saveSchedulesJson()");\r
 \r
                Path schedulesPath = null;\r
                try {\r
-                       schedulesPath = Paths.get(systemConfig.getConfig(CONF_SCHEDULES2));\r
+                       schedulesPath = Paths.get(systemConfig.getConfig(CONF_SCHEDULES));\r
                        if (!Files.exists(schedulesPath)) {\r
                                Files.createFile(schedulesPath);\r
                        }\r
                        if (Files.isDirectory(schedulesPath))\r
                                throw new FileNotFoundException(schedulesPath + " is a directory!");\r
                        logger.info("Saving schedules.json to {}", schedulesPath);\r
-                       Files.write(schedulesPath, processToSave.toPrettyString("").getBytes());\r
+                       BasicDBObject dbo = new BasicDBObject();\r
+                       BasicDBList jobList = new BasicDBList();\r
+                       for (int i = 0; i < schedules.size(); i++) {\r
+                               BasicDBObject temp = schedules.get(i).getValue();\r
+                               temp.remove("xml");\r
+                               jobList.add(temp);\r
+                       }\r
+                       dbo.put("joblist", jobList);\r
+                       if (Files.isWritable(schedulesPath)) {\r
+                               logger.info("{} is writable");\r
+                       } else {\r
+                               logger.info("{} was not writable");\r
+                               DosFileAttributeView dos = Files.getFileAttributeView(schedulesPath, DosFileAttributeView.class);\r
+                               if (dos != null) {\r
+                                       try {\r
+                                               dos.setReadOnly(false);\r
+                                               logger.info("{} is writable");\r
+                                       } catch (IOException e) {\r
+                                               logger.info("IOException: {}", e.getCause());\r
+                                       }\r
+                               }\r
+                       }\r
+                       Files.write(schedulesPath, dbo.toPrettyString("").getBytes());\r
                } catch (FileNotFoundException e) {\r
-                       logger.error("File not found: {}", CONF_SCHEDULES2);\r
+                       logger.error("File not found: {}", CONF_SCHEDULES);\r
                } catch (IOException e) {\r
                        logger.error("File is not writeable/not created: {}", schedulesPath.toString());\r
                }\r
        }\r
 \r
        @Override\r
-       public void saveTemplateXml(String oldSelectedJob, JobTemplate templateToSave) {\r
+       public void saveTemplateXml(JobTemplate templateToSave) {\r
                logger.info("saveTemplateXml()");\r
                if (templateToSave == null) {\r
                        throw new NullPointerException("templateToSave == null");\r
                }\r
-               for (int i = 0; i < templates.size(); i++) {\r
-                       if (templates.get(i).getName().equals(oldSelectedJob)) {\r
-                               templates.get(i).setName(templateToSave.getName());\r
-                               Path templatesPath = null;\r
-                               try {\r
-                                       templatesPath = Paths.get(systemConfig.getConfig(DIR_TEMPLATES));\r
-                                       if (!Files.isDirectory(templatesPath))\r
-                                               throw new FileNotFoundException(templatesPath + " is not a directory!");\r
-                                       String filePath = templatesPath.toString() + FileSystems.getDefault().getSeparator()\r
-                                                       + templates.get(i).getFileName();\r
-                                       FileOutputStream fos = new FileOutputStream(filePath);\r
-                                       logger.info("Saving job template to {}", filePath);\r
-//                                     if (templateToSave.toXmlDocument() != null) {\r
-                                       if (templates.get(i).toXmlDocument() != null) {\r
-                                               writeXml(templates.get(i).toXmlDocument(), fos);\r
+\r
+               Path templatesPath = null;\r
+               String filePath = null;\r
+               try {\r
+                       templatesPath = Paths.get(systemConfig.getConfig(DIR_TEMPLATES));\r
+                       if (!Files.isDirectory(templatesPath))\r
+                               throw new FileNotFoundException(templatesPath + " is not a directory!");\r
+                       if (templateToSave.getFileName() != null) {\r
+                               filePath = templatesPath.toString() + FileSystems.getDefault().getSeparator()\r
+                                               + templateToSave.getFileName();\r
+                               if (Files.isWritable(Paths.get(filePath))) {\r
+                                       logger.info("{} is writable");\r
+                               } else {\r
+                                       logger.info("{} is not writable");\r
+                                       DosFileAttributeView dos = Files.getFileAttributeView(Paths.get(filePath),\r
+                                                       DosFileAttributeView.class);\r
+                                       if (dos != null) {\r
+                                               try {\r
+                                                       dos.setReadOnly(false);\r
+                                               } catch (IOException e) {\r
+                                                       logger.info("IOException: {}", e.getCause());\r
+                                               }\r
                                        }\r
-                               } catch (FileNotFoundException e) {\r
-                                       logger.error("Folder not found: {}", DIR_TEMPLATES);\r
+                               }\r
+                               FileOutputStream fos = new FileOutputStream(filePath);\r
+                               logger.info("Saving job template to {}", filePath);\r
+                               if (templateToSave.toXmlDocument() != null) {\r
+                                       writeXml(templateToSave.toXmlDocument(), fos);\r
                                }\r
                        }\r
+               } catch (FileNotFoundException e) {\r
+                       logger.error("File not found: {}", filePath);\r
                }\r
        }\r
 \r
@@ -450,30 +485,45 @@ public class JobEngineConfiguration implements IJobEngineConfiguration {
        }\r
 \r
        @Override\r
-       public void duplicateProcess(JobTemplate templateToDuplicate) {\r
-               logger.info("duplicateProcess()");\r
-               JobTemplate templateWithNewName = new JobTemplate(templateToDuplicate);\r
-               templateWithNewName.setName(templateToDuplicate.getName().concat("-copy"));\r
-               templates.add(templateWithNewName);\r
-               saveTemplateXml("", templateWithNewName);\r
-       }\r
+       public void duplicateTemplate(String originalTemplateName) {\r
+               logger.info("duplicateTemplate()");\r
+               if (originalTemplateName == null) {\r
+                       throw new NullPointerException("originalTemplateName == null");\r
+               }\r
 \r
-       @Override\r
-       public void copyProcessEntry(BasicDBObject scheduleToCopy) {\r
-               logger.info("copyProcessEntry()");\r
-               BasicDBObject dbo = new BasicDBObject();\r
-               dbo.append("active", scheduleToCopy.getBoolean("active"));\r
-               dbo.append("executeimmediate", scheduleToCopy.getBoolean("executeimmediate"));\r
-               dbo.append("name", scheduleToCopy.get("name").toString().concat("-copy"));\r
-               dbo.append("template", scheduleToCopy.get("template").toString());\r
-               if (scheduleToCopy.get("cronexpression") != null) {\r
-                       dbo.append("cronexpression", scheduleToCopy.get("cronexpression").toString());\r
+               Path templatesPath = null;\r
+               String filePath = null;\r
+               String duplicateFileName = null;\r
+               try {\r
+                       templatesPath = Paths.get(systemConfig.getConfig(DIR_TEMPLATES));\r
+                       if (!Files.isDirectory(templatesPath))\r
+                               throw new FileNotFoundException(templatesPath + " is not a directory!");\r
+                       duplicateFileName = FilenameUtils.getName(originalTemplateName).replace(".xml", "").concat("-copy.xml");\r
+                       filePath = templatesPath.toString() + FileSystems.getDefault().getSeparator() + duplicateFileName;\r
+                       while (Files.exists(Paths.get(filePath))) {\r
+                               duplicateFileName = FilenameUtils.getName(duplicateFileName).replace(".xml", "").concat("-copy.xml");\r
+                               filePath = templatesPath.toString() + FileSystems.getDefault().getSeparator() + duplicateFileName;\r
+                       }\r
+                       InputStream is = new FileInputStream(\r
+                                       templatesPath.toString() + FileSystems.getDefault().getSeparator() + originalTemplateName);\r
+                       Files.copy(is, Paths.get(filePath));\r
+               } catch (FileNotFoundException e) {\r
+                       logger.error("File not found: {}", filePath);\r
+               } catch (IOException e) {\r
+                       logger.error("IOException: {}", e.getCause());\r
                }\r
-               dbo.append("parameters", scheduleToCopy.get("parameters"));\r
 \r
-               SimpleEntry<String, BasicDBObject> newScheduledJob = new SimpleEntry<String, BasicDBObject>("", dbo);\r
-               schedules.add(newScheduledJob);\r
+       }\r
 \r
+       @Override\r
+       public boolean scheduleExists(String name) {\r
+               boolean doesExist = false;\r
+               for (int i = 0; i < schedules.size(); i++) {\r
+                       if (schedules.get(i).getValue().getString("name").equals(name)) {\r
+                               doesExist = true;\r
+                       }\r
+               }\r
+               return doesExist;\r
        }\r
 \r
        @Override\r