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

index 1bd5c162ef8fbcdbe8d10834a8757f4c08a07d5e..c13f8b6c5c8855f86742b4125b02e945318aec96 100644 (file)
@@ -1,11 +1,14 @@
 package user.jobengine.server;\r
 \r
 import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.InputStream;\r
+import java.io.OutputStream;\r
 import java.net.MalformedURLException;\r
 import java.net.URL;\r
 import java.net.URLClassLoader;\r
+import java.nio.file.FileSystems;\r
 import java.nio.file.Files;\r
 import java.nio.file.Path;\r
 import java.nio.file.Paths;\r
@@ -20,9 +23,18 @@ import java.util.function.Consumer;
 import java.util.function.Predicate;\r
 import java.util.stream.Stream;\r
 \r
+import javax.xml.transform.OutputKeys;\r
+import javax.xml.transform.Transformer;\r
+import javax.xml.transform.TransformerConfigurationException;\r
+import javax.xml.transform.TransformerException;\r
+import javax.xml.transform.TransformerFactory;\r
+import javax.xml.transform.dom.DOMSource;\r
+import javax.xml.transform.stream.StreamResult;\r
+\r
 import org.apache.commons.lang.StringUtils;\r
 import org.apache.logging.log4j.LogManager;\r
 import org.apache.logging.log4j.Logger;\r
+import org.w3c.dom.Document;\r
 \r
 import com.ibm.nosql.json.JSONUtil;\r
 import com.ibm.nosql.json.api.BasicDBList;\r
@@ -43,6 +55,7 @@ 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
@@ -289,9 +302,9 @@ public class JobEngineConfiguration implements IJobEngineConfiguration {
                logger.info("Loading scheduler configuration file {}", configFilePath);\r
                String jsonConfig = new String(Files.readAllBytes(Paths.get(configFilePath)));\r
                BasicDBObject dbo = (BasicDBObject) JSONUtil.jsonToDbObject(jsonConfig);\r
-               BasicDBList scheduleJobs = NoSQLUtils.asDBList(dbo, "joblist");\r
+               BasicDBList scheduledJobs = NoSQLUtils.asDBList(dbo, "joblist");\r
 \r
-               scheduleJobs.forEach(o -> {\r
+               scheduledJobs.forEach(o -> {\r
                        try {\r
                                BasicDBObject j = (BasicDBObject) o;\r
                                String template = j.getString("template");\r
@@ -363,8 +376,108 @@ public class JobEngineConfiguration implements IJobEngineConfiguration {
                return result;\r
        }\r
 \r
+       @Override\r
+       public void saveSchedule(BasicDBObject processToSave) throws Exception {\r
+               logger.info("saveSchedule()");\r
+               schedules.add(new SimpleEntry<String, BasicDBObject>("", processToSave));\r
+\r
+               Path schedulesPath = null;\r
+               try {\r
+                       schedulesPath = Paths.get(systemConfig.getConfig(CONF_SCHEDULES2));\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
+               } catch (FileNotFoundException e) {\r
+                       logger.error("File not found: {}", CONF_SCHEDULES2);\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
+               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
+                               } catch (FileNotFoundException e) {\r
+                                       logger.error("Folder not found: {}", DIR_TEMPLATES);\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       private void writeXml(Document document, OutputStream os) {\r
+               document.setXmlVersion("1.1");\r
+               TransformerFactory transformerFactory = TransformerFactory.newInstance();\r
+               Transformer transformer;\r
+               try {\r
+                       transformer = transformerFactory.newTransformer();\r
+                       transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");\r
+                       transformer.setOutputProperty(OutputKeys.INDENT, "yes");\r
+                       transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");\r
+                       transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");\r
+                       transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "xml");\r
+                       DOMSource source = new DOMSource(document);\r
+                       StreamResult result = new StreamResult(os);\r
+                       transformer.transform(source, result);\r
+               } catch (TransformerConfigurationException e) {\r
+                       e.printStackTrace();\r
+               } catch (TransformerException e) {\r
+                       e.printStackTrace();\r
+               }\r
+       }\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
+\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
+               }\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
+\r
        @Override\r
        public void resetStepClassLoader() {\r
                resetStepClassLoader = true;\r
        }\r
+\r
 }\r