blob: beb44a2e82ed9ce601d749f946f584eae45b240b [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2018 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
* generated by Xtext 2.11.0
*
*/
package org.eclipse.osbp.xtext.signal.jvmmodel
import java.nio.file.Path
import java.util.Date
import java.util.HashSet
import javax.inject.Inject
import org.eclipse.osbp.core.api.persistence.IPersistenceService
import org.eclipse.osbp.datainterchange.api.IDataInterchange
import org.eclipse.osbp.dsl.common.xtext.extensions.AnnotationExtension
import org.eclipse.osbp.runtime.common.event.IEventDispatcher
import org.eclipse.osbp.ui.api.customfields.IBlobService
import org.eclipse.osbp.xtext.datainterchange.DataInterchange
import org.eclipse.osbp.xtext.datainterchange.DataInterchangeGroup
import org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable
import org.eclipse.osbp.xtext.datainterchange.jvmmodel.DataDSLJvmModelInferrer
import org.eclipse.osbp.xtext.datainterchange.validation.DataDSLValidator
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryPackage
import org.eclipse.osbp.xtext.signal.CronScheduler
import org.eclipse.osbp.xtext.signal.DailyScheduler
import org.eclipse.osbp.xtext.signal.HourlyScheduler
import org.eclipse.osbp.xtext.signal.MonthlyScheduler
import org.eclipse.osbp.xtext.signal.SignalActionTypeEnum
import org.eclipse.osbp.xtext.signal.SignalDSLPackage
import org.eclipse.osbp.xtext.signal.SignalDatainterchange
import org.eclipse.osbp.xtext.signal.SignalDefinition
import org.eclipse.osbp.xtext.signal.SignalExecutionTypeEnum
import org.eclipse.osbp.xtext.signal.SignalFunction
import org.eclipse.osbp.xtext.signal.SignalPackage
import org.eclipse.osbp.xtext.signal.SignalScheduler
import org.eclipse.osbp.xtext.signal.SignalTask
import org.eclipse.osbp.xtext.signal.SignalWatcher
import org.eclipse.osbp.xtext.signal.WeeklyScheduler
import org.eclipse.osbp.xtext.signal.common.SchedulerImpl
import org.eclipse.osbp.xtext.signal.common.SchedulerJobImpl
import org.eclipse.osbp.xtext.signal.common.WatcherImpl
import org.eclipse.osbp.xtext.signal.common.WatcherJobImpl
import org.eclipse.xtext.common.types.JvmField
import org.eclipse.xtext.common.types.JvmGenericType
import org.eclipse.xtext.common.types.JvmVisibility
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.osgi.service.component.annotations.Activate
import org.osgi.service.component.annotations.Component
import org.osgi.service.component.annotations.Deactivate
import org.osgi.service.component.annotations.Reference
import org.osgi.service.component.annotations.ReferenceCardinality
import org.osgi.service.component.annotations.ReferencePolicy
import org.quartz.Job
import org.quartz.JobDetail
import org.quartz.JobExecutionContext
import org.quartz.JobExecutionException
import org.quartz.Scheduler
import org.quartz.SchedulerException
import org.quartz.Trigger
import org.slf4j.Logger
import java.nio.file.FileSystem
import java.nio.file.FileSystems
/**
* <p>Infers a JVM model from the source model.</p>
*
* <p>The JVM model should contain all elements that would appear in the Java code
* which is generated from the source model. Other models link against the JVM model rather than the source model.</p>
*/
class SignalDSLJvmModelInferrer extends AbstractModelInferrer {
/**
* convenience API to build and initialize JVM types and their members.
*/
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
@Inject extension AnnotationExtension
@Inject extension DataDSLJvmModelInferrer df
@Inject extension DataDSLValidator dv
private var operationlist = <String>newHashSet
/**
* infer model on package base. Will be called for every defined package.
*
* @param dataInterchangePackage
* An instance of {@link SignalDSLPackage}
* @param acceptor
* the xtext acceptor interface
* @param isPreIndexingPhase
* true if in preindexing phase
*/
def dispatch void infer(SignalPackage signalPackage, IJvmDeclaredTypeAcceptor acceptor,
boolean isPreIndexingPhase) {
val pckgName = signalPackage.name;
for (signal : signalPackage.signals) {
// create watchers and schedulers if at least on task has been defined
if(signal !== null && signal.tasks !== null && !signal.tasks.empty) {
var signalClass = signal.toClass(signal.fullyQualifiedName)
if(signal instanceof SignalWatcher) {
signalClass.simpleName = signal.name.toFirstUpper + "Watcher"
// create the watcher tasks file
var signalWatcherJobClass = signal.toClass(signal.fullyQualifiedName)
signalWatcherJobClass.simpleName = signal.name.toFirstUpper + "WatcherJob"
signalWatcherJobClass.packageName = pckgName
acceptor.accept(signalWatcherJobClass, [
superTypes += _typeReferenceBuilder.typeRef(WatcherJobImpl)
it.documentation = "Implements the list of tasks of "+ signal.name.toFirstUpper + "Watcher to be executed."
it.toWatcherJobFields(signal)
it.toWatcherJobOperations(signal)
])
}
else if(signal instanceof SignalScheduler){
signalClass.simpleName = signal.name.toFirstUpper + "Scheduler"
// create the scheduler tasks file
var signalSchedulerJobClass = signal.toClass(signal.fullyQualifiedName)
signalSchedulerJobClass.simpleName = signal.name.toFirstUpper + "SchedulerJob"
signalSchedulerJobClass.packageName = pckgName
acceptor.accept(signalSchedulerJobClass, [
superTypes += _typeReferenceBuilder.typeRef(SchedulerJobImpl)
it.documentation = "Implements the list of tasks of "+ signal.name.toFirstUpper + "Scheduler to be executed."
it.toSchedulerJobFields(signal)
it.toSchedulerJobOperations(signal)
])
}
signalClass.packageName = pckgName
// create the watcher and scheduler files
acceptor.accept(signalClass, [
if(signal instanceof SignalWatcher){
superTypes += _typeReferenceBuilder.typeRef(WatcherImpl)
}
else if(signal instanceof SignalScheduler){
superTypes += _typeReferenceBuilder.typeRef(SchedulerImpl)
superTypes += _typeReferenceBuilder.typeRef(Job)
}
var annotationRef = _annotationTypesBuilder.annotationRef(typeof(Component))
annotationRef.addAnnAttr(signalPackage, "immediate", Boolean.TRUE)
annotations += annotationRef
it.fileHeader = signalPackage.documentation
it.toFields(signal)
it.toConstructor(signal)
it.toOperations(signal)
it.toBinderOperations(signal)
])
}
}
}
def void toSchedulerJobOperations(JvmGenericType type, SignalScheduler scheduler){
//the constructor
type.members += scheduler.toConstructor [
parameters += scheduler.toParameter("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService))
parameters += scheduler.toParameter("dataInterchange", _typeReferenceBuilder.typeRef(IDataInterchange))
parameters += scheduler.toParameter("blobService", _typeReferenceBuilder.typeRef(IBlobService))
parameters += scheduler.toParameter("eventDispatcher", _typeReferenceBuilder.typeRef(IEventDispatcher))
parameters += scheduler.toParameter("schedulerid", _typeReferenceBuilder.typeRef(String))
body = [
append(
'''
this.persistenceService = persistenceService;
this.dataInterchange = dataInterchange;
this.blobService = blobService;
this.eventDispatcher = eventDispatcher;
setSchedulerId(schedulerid);
«IF scheduler.hasFunctionWithFile»paths = new HashMap<String, Path>();
«ENDIF»''')
]
]
//create the execute task operation
type.members += scheduler.toMethod("executeListOfTasks", _typeReferenceBuilder.typeRef(Void::TYPE), [
annotations += _annotationTypesBuilder.annotationRef(Override)
body = [append('''«scheduler.executeSchedulerTasksList»''')]
])
// create all scheduler operations
operationlist.clear
// a list of all available tasks
createTaskOperations(type, scheduler)
}
def void toSchedulerJobFields(JvmGenericType type, SignalScheduler signal) {
var JvmField field = null
// create persistence service field
field = signal.toField("log", _typeReferenceBuilder.typeRef(Logger))
field.visibility = JvmVisibility::PRIVATE
field.documentation = "the log"
field.initializer = '''LoggerFactory.getLogger("signal")'''
type.members += field
// create persistence service field
field = signal.toField("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create datainterchange service field
field = signal.toField("dataInterchange", _typeReferenceBuilder.typeRef(IDataInterchange))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create blob service field
field = signal.toField("blobService", _typeReferenceBuilder.typeRef(IBlobService))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create event dispatcher service field
field = signal.toField("eventDispatcher", _typeReferenceBuilder.typeRef(IEventDispatcher))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create the file path field
if(signal.hasFunctionWithFile){
field = signal.toField("paths", _typeReferenceBuilder.typeRef("HashMap<String,Path>"))
field.visibility = JvmVisibility::PRIVATE
field.static = true
type.members += field
}
}
def void toWatcherJobOperations(JvmGenericType type, SignalWatcher watcher){
//the constructor
type.members += watcher.toConstructor [
parameters += watcher.toParameter("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService))
parameters += watcher.toParameter("dataInterchange", _typeReferenceBuilder.typeRef(IDataInterchange))
parameters += watcher.toParameter("blobService", _typeReferenceBuilder.typeRef(IBlobService))
parameters += watcher.toParameter("eventDispatcher", _typeReferenceBuilder.typeRef(IEventDispatcher))
parameters += watcher.toParameter("file", _typeReferenceBuilder.typeRef(Path))
parameters += watcher.toParameter("watcherid", _typeReferenceBuilder.typeRef(String))
body = [
append(
'''
this.persistenceService = persistenceService;
this.dataInterchange = dataInterchange;
this.blobService = blobService;
this.eventDispatcher = eventDispatcher;
«if(watcher.hasFileMask){
'''setParallelJobExecutionAllowed(true);'''
setTriggerfile(file);
setWatcherId(watcherid);
«IF watcher.hasFunctionWithFile»paths = new HashMap<String, Path>();
«ENDIF»''')
]
]
//create the execute task operation
type.members += watcher.toMethod("executeListOfTasks", _typeReferenceBuilder.typeRef(Void::TYPE), [
annotations += _annotationTypesBuilder.annotationRef(Override)
body = [append('''«watcher.executeWatcherTasksList»''')]
])
// create all watcher operations
operationlist = new HashSet()
createTaskOperations(type, watcher)
}
def void toWatcherJobFields(JvmGenericType type, SignalWatcher signal) {
var JvmField field = null
// create persistence service field
field = signal.toField("log", _typeReferenceBuilder.typeRef(Logger))
field.visibility = JvmVisibility::PRIVATE
field.documentation = "the log"
field.initializer = '''LoggerFactory.getLogger("signal")'''
type.members += field
// create persistence service field
field = signal.toField("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create datainterchange service field
field = signal.toField("dataInterchange", _typeReferenceBuilder.typeRef(IDataInterchange))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create blob service field
field = signal.toField("blobService", _typeReferenceBuilder.typeRef(IBlobService))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create event dispatcher service field
field = signal.toField("eventDispatcher", _typeReferenceBuilder.typeRef(IEventDispatcher))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create the file paths field
if(signal.hasFunctionWithFile){
field = signal.toField("paths", _typeReferenceBuilder.typeRef("HashMap<String,Path>"))
field.visibility = JvmVisibility::PRIVATE
field.static = true
type.members += field
}
}
def void toBinderOperations(JvmGenericType type, SignalDefinition signal) {
// getter sor services
type.members += signal.toMethod("getDataInterchange", _typeReferenceBuilder.typeRef(IDataInterchange), [
visibility = JvmVisibility.PUBLIC
static = true
body = [append('''return dataInterchange;''')]
])
type.members += signal.toMethod("getBlobService", _typeReferenceBuilder.typeRef(IBlobService), [
visibility = JvmVisibility.PUBLIC
static = true
body = [append('''return blobService;''')]
])
type.members += signal.toMethod("getEventDispatcher", _typeReferenceBuilder.typeRef(IEventDispatcher), [
visibility = JvmVisibility.PUBLIC
static = true
body = [append('''return eventDispatcher;''')]
])
type.members += signal.toMethod("getPersistenceService", _typeReferenceBuilder.typeRef(IPersistenceService), [
visibility = JvmVisibility.PUBLIC
static = true
body = [append('''return persistenceService;''')]
])
// bind datainterchange service
type.members += signal.toMethod("bindDataInterchangeMethod", _typeReferenceBuilder.typeRef(Void::TYPE), [
var annotationRef = _annotationTypesBuilder.annotationRef(typeof(Reference))
annotationRef.addAnnAttr(signal, "cardinality", ReferenceCardinality.MANDATORY)
annotationRef.addAnnAttr(signal, "policy", ReferencePolicy.STATIC)
annotations += annotationRef
visibility = JvmVisibility.PUBLIC
synchronized = true
parameters += signal.toParameter("dataInterchange", _typeReferenceBuilder.typeRef(IDataInterchange))
body = [
append('''
this.dataInterchange = dataInterchange;
«if(signal instanceof SignalWatcher){
'''log.info("«signal.name.toFirstUpper + "Watcher"» - DataInterchange bound");'''
}else if (signal instanceof SignalScheduler){
'''log.info("«signal.name.toFirstUpper + "Scheduler"» - DataInterchange bound");'''
}»''')
]
])
// unbind datainterchange service
type.members += signal.toMethod("unbindDataInterchangeMethod", _typeReferenceBuilder.typeRef(Void::TYPE), [
visibility = JvmVisibility.PUBLIC
synchronized = true
parameters += signal.toParameter("dataInterchange", _typeReferenceBuilder.typeRef(IDataInterchange))
body = [
append(
'''
this.dataInterchange = null;
«if(signal instanceof SignalWatcher){
'''log.info("«signal.name.toFirstUpper + "Watcher"» - DataInterchange unbound");'''
}else if (signal instanceof SignalScheduler){
'''log.info("«signal.name.toFirstUpper + "Scheduler"» - DataInterchange unbound");'''
''')
]
])
// bind blob service
type.members += signal.toMethod("bindBlobMethod", _typeReferenceBuilder.typeRef(Void::TYPE), [
var annotationRef = _annotationTypesBuilder.annotationRef(typeof(Reference))
annotationRef.addAnnAttr(signal, "cardinality", ReferenceCardinality.MANDATORY)
annotationRef.addAnnAttr(signal, "policy", ReferencePolicy.STATIC)
annotations += annotationRef
visibility = JvmVisibility.PUBLIC
synchronized = true
parameters += signal.toParameter("blobService", _typeReferenceBuilder.typeRef(IBlobService))
body = [
append(
'''
this.blobService = blobService;
«if(signal instanceof SignalWatcher){
'''log.info("«signal.name.toFirstUpper + "Watcher"» - BlobService bound");'''
}else if (signal instanceof SignalScheduler){
'''log.info("«signal.name.toFirstUpper + "Scheduler"» - BlobService bound");'''
''')
]
])
// unbind blob service
type.members += signal.toMethod("unbindBlobMethod", _typeReferenceBuilder.typeRef(Void::TYPE), [
visibility = JvmVisibility.PUBLIC
synchronized = true
parameters += signal.toParameter("blobService", _typeReferenceBuilder.typeRef(IBlobService))
body = [
append(
'''
this.blobService = null;
«if(signal instanceof SignalWatcher){
'''log.info("«signal.name.toFirstUpper + "Watcher"» - BlobService unbound");'''
}else if (signal instanceof SignalScheduler){
'''log.info("«signal.name.toFirstUpper + "Scheduler"» - BlobService unbound");'''
''')
]
])
// bind eventDispatcher service
type.members += signal.toMethod("bindEventDispatcher", _typeReferenceBuilder.typeRef(Void::TYPE), [
var annotationRef = _annotationTypesBuilder.annotationRef(typeof(Reference))
annotationRef.addAnnAttr(signal, "cardinality", ReferenceCardinality.MANDATORY)
annotationRef.addAnnAttr(signal, "policy", ReferencePolicy.STATIC)
annotations += annotationRef
visibility = JvmVisibility.PUBLIC
synchronized = true
parameters += signal.toParameter("eventDispatcher", _typeReferenceBuilder.typeRef(IEventDispatcher))
body = [
append(
'''
this.eventDispatcher = eventDispatcher;
«if(signal instanceof SignalWatcher){
'''log.info("«signal.name.toFirstUpper + "Watcher"» - EventDispatcher bound");'''
}else if (signal instanceof SignalScheduler){
'''log.info("«signal.name.toFirstUpper + "Scheduler"» - EventDispatcher bound");'''
''')
]
])
// unbind eventDispatcher service
type.members += signal.toMethod("unbindEventDispatcher", _typeReferenceBuilder.typeRef(Void::TYPE), [
visibility = JvmVisibility.PUBLIC
synchronized = true
parameters += signal.toParameter("eventDispatcher", _typeReferenceBuilder.typeRef(IEventDispatcher))
body = [
append(
'''
this.eventDispatcher = null;
«if(signal instanceof SignalWatcher){
'''log.info("«signal.name.toFirstUpper + "Watcher"» - EventDispatcher unbound");'''
}else if (signal instanceof SignalScheduler){
'''log.info("«signal.name.toFirstUpper + "Scheduler"» - EventDispatcher unbound");'''
''')
]
])
// bind persistence service
type.members += signal.toMethod("bindPersistenceMethod", _typeReferenceBuilder.typeRef(Void::TYPE), [
var annotationRef = _annotationTypesBuilder.annotationRef(typeof(Reference))
annotationRef.addAnnAttr(signal, "cardinality", ReferenceCardinality.MANDATORY)
annotationRef.addAnnAttr(signal, "policy", ReferencePolicy.STATIC)
annotations += annotationRef
visibility = JvmVisibility.PUBLIC
synchronized = true
parameters += signal.toParameter("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService))
body = [
append(
'''
this.persistenceService = persistenceService;
«if(signal instanceof SignalWatcher){
'''log.info("«signal.name.toFirstUpper + "Watcher"» - PersistenceService bound");'''
}else if (signal instanceof SignalScheduler){
'''log.info("«signal.name.toFirstUpper + "Scheduler"» - PersistenceService bound");'''
''')
]
])
// unbind persistence service
type.members += signal.toMethod("unbindPersistenceMethod", _typeReferenceBuilder.typeRef(Void::TYPE), [
visibility = JvmVisibility.PUBLIC
synchronized = true
parameters += signal.toParameter("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService))
body = [
append(
'''
this.persistenceService = null;
«if(signal instanceof SignalWatcher){
'''log.info("«signal.name.toFirstUpper + "Watcher"» - PersistenceService unbound");'''
}else if (signal instanceof SignalScheduler){
'''log.info("«signal.name.toFirstUpper + "Scheduler"» - PersistenceService unbound");'''
''')
]
])
}
def void toOperations(JvmGenericType type, SignalDefinition signal) {
if(signal instanceof SignalWatcher){
// creating the register operation, detailing the order
// of execution of each operation after a signal is caught
type.members += signal.toMethod("handleEvent", _typeReferenceBuilder.typeRef(Void::TYPE), [
annotations += _annotationTypesBuilder.annotationRef(Override)
parameters += signal.toParameter("file", _typeReferenceBuilder.typeRef(Path))
body = [append('''«signal.handleEvent»''')]
])
// create watcher executeWatcherService
type.members += signal.toMethod("executeWatcherService", _typeReferenceBuilder.typeRef(Void::TYPE), [
body = [append('''«signal.executeService»''')]
])
}
if(signal instanceof SignalScheduler){
// create startScheduler operation
type.members += signal.toMethod("startScheduler", _typeReferenceBuilder.typeRef(Void::TYPE), [
documentation =
'''
Starts the {@link #scheduler}.'''
exceptions += _typeReferenceBuilder.typeRef(SchedulerException)
body = [append('''«signal.startScheduler»''')]
])
// create shutdownScheduler operation
type.members += signal.toMethod("shutdownScheduler", _typeReferenceBuilder.typeRef(Void::TYPE), [
documentation =
'''
Halts the Scheduler's firing of Triggers, and cleans up all resources associated with the Scheduler.
@param waitForJobsToComplete whether or not the scheduler shuts down immediately or wait for jobs to finish.
@throws SchedulerException'''
exceptions += _typeReferenceBuilder.typeRef(SchedulerException)
parameters += signal.toParameter("waitForJobsToComplete", _typeReferenceBuilder.typeRef(boolean))
body = [append('''«signal.shutdownScheduler»''')]
])
// create scheduleJob operation
type.members += signal.toMethod("scheduleJob", _typeReferenceBuilder.typeRef(Date), [
documentation =
'''
Schedules a given job based on a given trigger.
@param job the {@link JobDetail} to be scheduled
@param trigger the corresponding trigger
@return'''
exceptions += _typeReferenceBuilder.typeRef(SchedulerException)
parameters += signal.toParameter("job", _typeReferenceBuilder.typeRef(JobDetail))
parameters += signal.toParameter("trigger", _typeReferenceBuilder.typeRef(Trigger))
body = [append('''«signal.scheduleJob»''')]
])
// create watcher scheduleAllJobs
type.members += signal.toMethod("scheduleAllJobs", _typeReferenceBuilder.typeRef(Void::TYPE), [
exceptions += _typeReferenceBuilder.typeRef(SchedulerException)
body = [append('''«signal.scheduleAllJobs»''')]
])
// create watcher executeSchedulerService
type.members += signal.toMethod("executeSchedulerService", _typeReferenceBuilder.typeRef(Void::TYPE), [
body = [append('''«signal.executeService»''')]
])
// create job execute method
type.members += signal.toMethod("execute", _typeReferenceBuilder.typeRef(Void::TYPE), [
exceptions += _typeReferenceBuilder.typeRef(JobExecutionException)
annotations += _annotationTypesBuilder.annotationRef(Override)
parameters += signal.toParameter("context", _typeReferenceBuilder.typeRef(JobExecutionContext))
documentation = "Executes the list of tasks as job's logic."
body = [
append(
'''
new «signal.name.toFirstUpper»SchedulerJob(persistenceService, dataInterchange, blobService, eventDispatcher,
"«signal.fullyQualifiedName»Scheduler").executeListOfTasks();''')]
])
}
// creating the getHandlerPropertyFromConfigurationFile operation
type.members += signal.toMethod("getHandlerPropertyFromConfigurationFile", _typeReferenceBuilder.typeRef(String), [
documentation = "Gives the property value of the given property name if existing."
parameters += signal.toParameter("propertyname", _typeReferenceBuilder.typeRef(String))
body = [append('''«signal.handlerPropertyFromConfigurationFile»''')]
])
// create watcher activate
type.members += signal.toMethod("activate", _typeReferenceBuilder.typeRef(Void::TYPE), [
annotations += _annotationTypesBuilder.annotationRef(Activate)
body = [append('''«signal.activate»''')]
])
// create watcher deactivate
type.members += signal.toMethod("deactivate", _typeReferenceBuilder.typeRef(Void::TYPE), [
annotations += _annotationTypesBuilder.annotationRef(Deactivate)
body = [append('''«signal.deactivate»''')]
])
}
def String scheduleJob(SignalScheduler scheduler){
return
'''
try {
return scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
return null;'''
}
def String shutdownScheduler(SignalScheduler scheduler){
return
'''
log.info("Shutdowns "+scheduler.getSchedulerName()+" ...");
scheduler.shutdown(waitForJobsToComplete);'''
}
def String startScheduler(SignalScheduler scheduler){
return
'''
scheduler.start();
log.info(scheduler.getSchedulerName() + " service successfully started!");'''
}
def String handleEvent(SignalWatcher watcher) {
if(watcher !== null) {
return
'''
«if(watcher.hasFileMask){
'''
String maskcfg = getFilemaskValue(getHandlerPropertyFromConfigurationFile("«watcher.fullyQualifiedName»Watcher" + SignalConstants.FILEMASK));
String modelfilemask = "«watcher.identifier»";
if(isFileMaskValid(maskcfg) && isFileMaskValidWithExpectedFileExtension(maskcfg, "«watcher.baseInterchange.fileEndpoint.getValidExtensionToInterchange»")){
modelfilemask = maskcfg;
log.info("«watcher.name»Watcher - The file mask used for file identification is ["+maskcfg+"].");
}else{
log.info("«watcher.name»Watcher - The default file mask [«watcher.identifier»] is used for file identification.");
}
if(isFileNameValidToFileMask(file.getFileName().toString(), modelfilemask)){
addWatcherJob(new «watcher.name.toFirstUpper»WatcherJob(persistenceService, dataInterchange, blobService, eventDispatcher, file, "«watcher.fullyQualifiedName»Watcher"));
}'''
}
else{
'''
String namecfg = getFilemaskValue(getHandlerPropertyFromConfigurationFile("«watcher.fullyQualifiedName»Watcher" + SignalConstants.FILENAME));
String filemask = namecfg != null ? namecfg : "«watcher.identifier»";
if(isFileNameValidToBaseFileName(file.getFileName().toString(), filemask)){
addWatcherJob(new «watcher.name.toFirstUpper»WatcherJob(persistenceService, dataInterchange, blobService, eventDispatcher, file, "«watcher.fullyQualifiedName»Watcher"));
}'''
}
»
else{
log.info("«watcher.name.toFirstUpper»Watcher - No action planed for file ["+file+"].");
}'''
}
return ''''''
}
def void toConstructor(JvmGenericType type, SignalDefinition signal) {
type.members += signal.toConstructor [
if(signal instanceof SignalWatcher){
exceptions += _typeReferenceBuilder.typeRef(Exception)
body = [append('''super("«signal.name.toFirstUpper + "Watcher"»");''')]
}
]
}
def String executeWatcherTasksList(SignalWatcher watcher) {
return
'''
«if(watcher !== null) {
var operationList = ""
var functionWithFile = watcher.hasFunctionWithFile
for (task : watcher.tasks) {
operationList = operationList + getTaskListOperationName(task, functionWithFile)
}
if(watcher.executiontype.equals(SignalExecutionTypeEnum.SEQ)){
'''
log.info("WatcherJobsHandler - START - Sequential execution of «watcher.name.toFirstUpper»WatcherJob ...");
try {
«operationList»
deleteFile(getTriggerfile(), "«watcher.name.toFirstUpper»WatcherJob - Triggerfile");
} catch(Exception e){
log.error("WatcherJobsHandler - ERROR during sequential execution of «watcher.name.toFirstUpper»WatcherJob!", e);
}
setDone(true); // for the WatcherhandlerJob to continue
log.info("WatcherJobsHandler - END - Sequential execution of «watcher.name.toFirstUpper»WatcherJob ...");'''
}
else{
'''
log.info("WatcherJobsHandler - START - Parallel execution of «watcher.name.toFirstUpper»WatcherJob ...");
try { «operationList»
finalizeTaskExecution("«watcher.name.toFirstUpper»WatcherJob");
} catch(Exception e){
log.error("WatcherJobsHandler - ERROR during parallel execution of «watcher.name.toFirstUpper»WatcherJob!", e);
setDone(true); // for the WatcherhandlerJob to continue
log.info("WatcherJobsHandler - END - Parallel execution of «watcher.name.toFirstUpper»WatcherJob ...");
}'''
}
'''
}
def String executeSchedulerTasksList(SignalScheduler scheduler) {
return
'''
«if(scheduler !== null){
var operationList = ""
var functionWithFile = scheduler.hasFunctionWithFile
for (task : scheduler.tasks) {
operationList = operationList + getTaskListOperationName(task, functionWithFile)
}
if(scheduler.executiontype.equals(SignalExecutionTypeEnum.SEQ)){
'''
log.info("START - Sequential execution of «scheduler.name.toFirstUpper»SchedulerJob ...");
try {
«operationList»
} catch(Exception e){
log.error("ERROR during sequential execution of «scheduler.name.toFirstUpper»SchedulerJob!", e);
}
log.info("END - Sequential execution of «scheduler.name.toFirstUpper»SchedulerJob.");'''
}
else{
'''
log.info("START - Parallel execution of «scheduler.name.toFirstUpper»SchedulerJob ...");
try {
«operationList»
finalizeTaskExecution("«scheduler.name.toFirstUpper»SchedulerJob");
} catch(Exception e){
log.error("ERROR during parallel execution of «scheduler.name.toFirstUpper»SchedulerJob!", e);
log.info("END - Parallel execution of «scheduler.name.toFirstUpper»SchedulerJob.");
}'''
}
'''
}
def getTaskListOperationName(SignalTask task, boolean existFunctionWithFile){
if(task instanceof SignalFunction){
if(task !== null && task.doExecuteFunction !== null ) {
if((task.onExportFile || task.onImportFile) && !task.doExecuteFunction.params.empty){
var operationName = ""
if(task.onExportFile){
operationName = "export" + task.supportInterchange.name.toFirstUpper
}
else if(task.onImportFile){
operationName = "import" + task.supportInterchange.name.toFirstUpper
}
return
'''«(task.group.eContainer as FunctionLibraryPackage).fullyQualifiedName».«task.group.name.toString.toFirstUpper».«task.doExecuteFunction.name.toString»(paths.get("«operationName»"));
'''
}
else if(!task.onExportFile && !task.onImportFile){
return
'''«(task.group.eContainer as FunctionLibraryPackage).fullyQualifiedName».«task.group.name.toString.toFirstUpper».«task.doExecuteFunction.name.toString»();
'''
}
}
}
else{
return
'''«IF existFunctionWithFile» paths.put("«task.getAppropriateTaskOperationName»", «task.getAppropriateTaskOperationName»()); «ELSE»«task.getAppropriateTaskOperationName»(); «ENDIF»
'''
}
}
/**
* Returns the operation name of the given task.
*/
def String getAppropriateTaskOperationName(SignalTask task) {
var operation_name = ""
if(task instanceof SignalDatainterchange){
if (task.getActionType == SignalActionTypeEnum.DATAIMPORT && task.dataRef !== null) {
operation_name = "import" + task.dataRef.name.toFirstUpper
} else if (task.dataRef !== null) {
operation_name = "export" + task.dataRef.name.toFirstUpper
}
}
return operation_name
}
/**
* Creates the content of each task operation for scheduler and watcher jobs task files.
*/
def createTaskOperations(JvmGenericType type, SignalDefinition signal) {
for(task : signal.tasks){
var operationname = task.getAppropriateTaskOperationName
// create the operation only if it has not already been created
if (!operationlist.contains(operationname)) {
if(task instanceof SignalDatainterchange){
if(signal instanceof SignalWatcher){
watcherInterchangeOperation(type, signal, task, operationname)
}
else if(signal instanceof SignalScheduler){
schedulerInterchangeOperation(type, signal, task, operationname)
}
}
operationlist.add(operationname)
}
}
}
def schedulerInterchangeOperation(JvmGenericType type, SignalScheduler signal, SignalDatainterchange interchange, String operationname){
if (interchange.getActionType == SignalActionTypeEnum.DATAIMPORT) {
type.members += signal.toMethod(operationname, _typeReferenceBuilder.typeRef(Path), [
exceptions += _typeReferenceBuilder.typeRef(Exception)
body = [append(
'''
«getConfigFileURL((interchange.dataRef.eContainer as DataInterchangeGroup).name)»
«interchange.dataRef.getBasicRunConfiguration(true, WorkerThreadRunnable.Direction.IMPORT.name, null)»
«interchange.dataRef.defaultVariableName».setDirection(WorkerThreadRunnable.Direction.IMPORT);
try {
«if((signal as SignalScheduler).executiontype.equals(SignalExecutionTypeEnum.SEQ)){
'''«signal.name.toFirstUpper + "Scheduler"».getExecutorService().submit(«interchange.dataRef.defaultVariableName»).get();'''
}else{
'''«signal.name.toFirstUpper + "Scheduler"».getExecutorService().execute(«interchange.dataRef.defaultVariableName»);'''
}catch (Exception e) {
log.error("Execution - " + «interchange.dataRef.defaultVariableName».getDirection() + " for " + «interchange.dataRef.defaultVariableName».getName() + " interupted!\n" + e.getMessage());
«if (interchange.getActionType == SignalActionTypeEnum.DATAIMPORT) {
'''
«IF interchange.dataRef.isDeleteFileAfterImport»
renameFile(Paths.get(«interchange.dataRef.defaultVariableName».getFileURL().toString()), "«signal.name.toFirstUpper»SchedulerJob");
«ENDIF»
'''
}
return Paths.get(«interchange.dataRef.defaultVariableName».getFileURL().getPath().substring(1));
''')]
])
} else {
type.members += signal.toMethod(operationname, _typeReferenceBuilder.typeRef(Path), [
body = [
append(
'''
«getConfigFileURL((interchange.dataRef.eContainer as DataInterchangeGroup).name)»
«interchange.dataRef.getBasicRunConfiguration(true, WorkerThreadRunnable.Direction.EXPORT.name, null)»
«interchange.dataRef.defaultVariableName».setDirection(WorkerThreadRunnable.Direction.EXPORT);
try {
«if((signal as SignalScheduler).executiontype.equals(SignalExecutionTypeEnum.SEQ)){
'''«signal.name.toFirstUpper + "Scheduler"».getExecutorService().submit(«interchange.dataRef.defaultVariableName»).get();'''
}else{
'''«signal.name.toFirstUpper + "Scheduler"».getExecutorService().execute(«interchange.dataRef.defaultVariableName»);'''
}catch (Exception e) {
log.error("Execution - " + «interchange.dataRef.defaultVariableName».getDirection() + " for " + «interchange.dataRef.defaultVariableName».getName() + " interupted!\n" + e.getMessage());
}
return «interchange.dataRef.defaultVariableName».getExportPath();
'''
)]
])
}
}
def watcherInterchangeOperation(JvmGenericType type, SignalWatcher watcher, SignalDatainterchange interchange, String operationname){
if(watcher.executiontype.equals(SignalExecutionTypeEnum.SEQ)){
type.members += watcher.toMethod(operationname, _typeReferenceBuilder.typeRef(Path), [
exceptions += _typeReferenceBuilder.typeRef(Exception)
body = [
append(
'''
«if (interchange.getActionType == SignalActionTypeEnum.DATAIMPORT) {
'''
«interchange.dataRef.getBasicRunConfiguration(true, WorkerThreadRunnable.Direction.IMPORT.name, watcher.baseInterchange)»
«interchange.dataRef.defaultVariableName».setDirection(WorkerThreadRunnable.Direction.IMPORT);
'''
} else {
'''
«getConfigFileURL((interchange.dataRef.eContainer as DataInterchangeGroup).name)»
«interchange.dataRef.getBasicRunConfiguration(true, WorkerThreadRunnable.Direction.EXPORT.name, watcher.baseInterchange)»
«interchange.dataRef.defaultVariableName».setDirection(WorkerThreadRunnable.Direction.EXPORT);
'''
«if(interchange == watcher.baseInterchange){ // for the (trigger)interchange marked with the keyword 'applyon'
'''
URI uri = getTriggerfile().toUri();
«interchange.dataRef.defaultVariableName».setFileURL(uri.toString());
'''
}else{
'''«interchange.dataRef.defaultVariableName».setFileURL(getTriggerfile().getParent().toString() + File.separator + "«interchange.dataRef.getDataInterchangeFileName»");'''
try{
WatcherImpl.getExecutorService().submit(«interchange.dataRef.defaultVariableName»).get();
}catch (Exception e) {
log.error("Execution - " + «interchange.dataRef.defaultVariableName».getDirection() + " for " + «interchange.dataRef.defaultVariableName».getName() + " interupted!\n" + e.getMessage());
«interchange.dataRef.defaultVariableName».setExecutionDone(true);
«if (interchange.getActionType == SignalActionTypeEnum.DATAIMPORT) {
if(interchange.dataRef == watcher.baseInterchange && interchange.dataRef.deleteFileAfterImport){
'''renameFile(getTriggerfile(), "«watcher.name.toFirstUpper»WatcherJob");'''
}
else if(interchange.dataRef.deleteFileAfterImport){
'''renameFile(Paths.get(«interchange.dataRef.defaultVariableName».getFileURL().toString()), "«watcher.name.toFirstUpper»WatcherJob");'''
}
}
«if (interchange.getActionType == SignalActionTypeEnum.DATAIMPORT) {
'''return Paths.get(«interchange.dataRef.defaultVariableName».getFileURL().getPath().substring(1));'''
}
else{
'''return «interchange.dataRef.defaultVariableName».getExportPath();'''
''')
]
])
}
else{
type.members += watcher.toMethod(operationname, _typeReferenceBuilder.typeRef(Path), [
exceptions += _typeReferenceBuilder.typeRef(Exception)
body = [
append(
'''
«if (interchange.getActionType == SignalActionTypeEnum.DATAIMPORT) {
'''
«interchange.dataRef.getBasicRunConfiguration(true, WorkerThreadRunnable.Direction.IMPORT.name, watcher.baseInterchange)»
«interchange.dataRef.defaultVariableName».setDirection(WorkerThreadRunnable.Direction.IMPORT);
'''
} else {
'''
«getConfigFileURL((interchange.dataRef.eContainer as DataInterchangeGroup).name)»
«interchange.dataRef.getBasicRunConfiguration(true, WorkerThreadRunnable.Direction.EXPORT.name, watcher.baseInterchange)»
«interchange.dataRef.defaultVariableName».setDirection(WorkerThreadRunnable.Direction.EXPORT);
'''
«if(interchange == watcher.baseInterchange){
'''
URI uri = getTriggerfile().toUri();
«interchange.dataRef.defaultVariableName».setFileURL(uri.toString());'''
}else{
'''«interchange.dataRef.defaultVariableName».setFileURL(getTriggerfile().getParent().toString() + File.separator + "«interchange.dataRef.getDataInterchangeFileName»");'''
checkForCompletion(«interchange.dataRef.defaultVariableName»);
try{
WatcherImpl.getExecutorService().execute(«interchange.dataRef.defaultVariableName»);
}catch (Exception e) {
log.error("Execution - " + «interchange.dataRef.defaultVariableName».getDirection() + " for " + «interchange.dataRef.defaultVariableName».getName() + " interupted!\n" + e.getMessage());
«interchange.dataRef.defaultVariableName».setExecutionDone(true);
«if (interchange.getActionType == SignalActionTypeEnum.DATAIMPORT) {
if(interchange.dataRef == watcher.baseInterchange && interchange.dataRef.deleteFileAfterImport){
'''renameFile(getTriggerfile(), "«watcher.name.toFirstUpper»WatcherJob");'''
}
else if(interchange.dataRef.deleteFileAfterImport){
'''renameFile(Paths.get(«interchange.dataRef.defaultVariableName».getFileURL().toString()), "«watcher.name.toFirstUpper»WatcherJob");'''
}
}
«if (interchange.getActionType == SignalActionTypeEnum.DATAIMPORT) {
'''return Paths.get(«interchange.dataRef.defaultVariableName».getFileURL().getPath().substring(1));'''
}
else{
'''return «interchange.dataRef.defaultVariableName».getExportPath();'''
''')
]
])
}
}
/**
* Creates the fields for all watcher and scheduler files.
*/
def void toFields(JvmGenericType type, SignalDefinition signal) {
var JvmField field = null
// create thread field
field = signal.toField("thread", _typeReferenceBuilder.typeRef(Thread)) [
visibility = JvmVisibility::PRIVATE
]
type.members += field
// create persistence service field
field = signal.toField("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create datainterchange service field
field = signal.toField("dataInterchange", _typeReferenceBuilder.typeRef(IDataInterchange))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create blob service field
field = signal.toField("blobService", _typeReferenceBuilder.typeRef(IBlobService))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create event dispatcher service field
field = signal.toField("eventDispatcher", _typeReferenceBuilder.typeRef(IEventDispatcher))
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
if(signal instanceof SignalScheduler){
// create executor service dispatcher service field
field = signal.toField("scheduler", _typeReferenceBuilder.typeRef(Scheduler))
field.documentation = '''the scheduler instance'''
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
}
}
def String getActivate(SignalDefinition signal) {
var signalName = ""
if(signal instanceof SignalWatcher){
signalName = signal.name.toFirstUpper + "Watcher"
}else if(signal instanceof SignalScheduler){
signalName = signal.name.toFirstUpper + "Scheduler"
}
return
'''
thread = new Thread("«signalName»") {
@Override
public void run() {
«if(signal instanceof SignalWatcher){
'''
setMaxParallelThreadsCount(getHandlerPropertyFromConfigurationFile(SignalConstants.MAXPARALLELTHREADCOUNT_NAME));
initMaxThreadCount();
startWatcherJobsHandling();
executeWatcherService();
'''
}
else if(signal instanceof SignalScheduler){
'''
setMaxParallelThreadsCount(getHandlerPropertyFromConfigurationFile(SignalConstants.MAXPARALLELTHREADCOUNT_NAME));
initMaxThreadCount();
setExecutorService("«signalName»");
executeSchedulerService();
'''
}
};
thread.start();'''
}
def String getExecuteService(SignalDefinition signal) {
var signalName = ""
if(signal instanceof SignalWatcher){
signalName = signal.name.toFirstUpper + "Watcher"
}else if(signal instanceof SignalScheduler){
signalName = signal.name.toFirstUpper + "Scheduler"
}
return
'''
«if(signal instanceof SignalWatcher){
'''
try {
«signal.registerHandlerPathToWatch»
log.info("«signalName» service successfully started.");
processEvents();
} catch (Exception e) {
log.error("«signalName» service interrupted due to: ", e);
}'''
}
else if(signal instanceof SignalScheduler){
'''
try {
scheduler = createScheduler("«signalName»");
scheduleAllJobs();
startScheduler();
} catch (Exception e) {
log.error("«signalName» service interrupted due to: ", e);
}'''}»'''
}
def String getDeactivate(SignalDefinition signal) {
var signalName = ""
if(signal instanceof SignalWatcher){
signalName = signal.name.toFirstUpper + "Watcher"
}else if(signal instanceof SignalScheduler){
signalName = signal.name.toFirstUpper + "Scheduler"
}
return
'''
«if(signal instanceof SignalScheduler){
'''
try {
if(scheduler != null){
shutdownScheduler(false);
}
thread.join();
} catch (Exception e) {
log.error("«signalName» service shortly interrupted ...", e);
}
log.info("«signalName» service shutted down...");
'''
}else{
'''
try {
closeWatcher();
stopWatcherJobsHandling();
thread.join();
} catch (Exception e) {
log.error("«signalName» service shortly interrupted ...", e);
}
log.info("«signalName» service shutted down...");
'''
'''
}
def String scheduleAllJobs(SignalScheduler scheduler) {
var signalName = scheduler.name.toFirstUpper + "Scheduler"
var index = 1
var result = '''
log.info("«signalName» - Starting scheduling all jobs ...");
Trigger trigger; JobDetail job; Date ft; String expr; int hour; int min; int dOw; int dOm; String key;String keyOne;String keyTwo;
'''
if (scheduler.getSchedulertype instanceof CronScheduler) {
var type = scheduler.getSchedulertype as CronScheduler
if (type !== null && type.expression !== null && !type.expression.empty) {
result = result.concat('''
job = createDataTransferJob("«scheduler.fullyQualifiedName»SchedulerJob«index»", "jobs", "-", «scheduler.executiontype.equals(SignalExecutionTypeEnum.SEQ)», "«scheduler.fullyQualifiedName»", «signalName».class);
key = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.CRON_SCHEDULER;
expr = getCronExpressionValue(getHandlerPropertyFromConfigurationFile(key));
trigger = createCronTrigger("«scheduler.fullyQualifiedName»SchedulerTrigger«index»", "triggers", expr != null ? expr : "«type.expression»");
ft = scheduleJob(job, trigger);
log.info("«scheduler.name.toFirstUpper»SchedulerJob«index» has been scheduled to run at: " + ft);
''')
}
} else if (scheduler.getSchedulertype instanceof HourlyScheduler) {
var type = scheduler.getSchedulertype as HourlyScheduler
if (type !== null && type.minute > -1) {
result = result.concat('''
job = createDataTransferJob("«scheduler.fullyQualifiedName»SchedulerJob«index»", "jobs", "-", «scheduler.executiontype.equals(SignalExecutionTypeEnum.SEQ)», "«scheduler.fullyQualifiedName»", «signalName».class);
key = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.HOURLY_SCHEDULER;
min = getMinutesValue(getHandlerPropertyFromConfigurationFile(key));
trigger = createHourlyTrigger("«scheduler.fullyQualifiedName»SchedulerTrigger«index»", "triggers", min>=0? min : «type.minute»);
ft = scheduleJob(job, trigger);
log.info("«scheduler.name.toFirstUpper»SchedulerJob«index» has been scheduled to run at: " + ft);
''')
}
} else if (scheduler.getSchedulertype instanceof DailyScheduler) {
var type = scheduler.getSchedulertype as DailyScheduler
if (type !== null && type.hour > -1 && type.minute > -1) {
result = result.concat('''
job = createDataTransferJob("«scheduler.fullyQualifiedName»SchedulerJob«index»", "jobs", "-", «scheduler.executiontype.equals(SignalExecutionTypeEnum.SEQ)», "«scheduler.fullyQualifiedName»", «signalName».class);
key = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.DAILY_SCHEDULER_HOUR;
hour = getHourValue(getHandlerPropertyFromConfigurationFile(key));
keyOne = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.DAILY_SCHEDULER_MIN;
min = getMinutesValue(getHandlerPropertyFromConfigurationFile(keyOne));
trigger = createDailyTriggerAtHourandMins("«scheduler.fullyQualifiedName»SchedulerTrigger«index»", "triggers", hour>=0 ? hour : «type.hour», min>=0 ? min : «type.minute»);
ft = scheduleJob(job, trigger);
log.info("«scheduler.name.toFirstUpper»SchedulerJob«index» has been scheduled to run at: " + ft);
''')
}
} else if (scheduler.getSchedulertype instanceof WeeklyScheduler) {
var type = scheduler.getSchedulertype as WeeklyScheduler
if (type !== null && type.dayofweek !== null && type.hour > -1 &&
type.minute > -1) {
result = result.concat('''
job = createDataTransferJob("«scheduler.fullyQualifiedName»SchedulerJob«index»", "jobs", "-", «scheduler.executiontype.equals(SignalExecutionTypeEnum.SEQ)», "«scheduler.fullyQualifiedName»", «signalName».class);
key = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.WEEKLY_SCHEDULER_DAYOFWEEK;
dOw = getDayOfWeekValue(getHandlerPropertyFromConfigurationFile(key));
keyOne = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.WEEKLY_SCHEDULER_HOUR;
hour = getHourValue(getHandlerPropertyFromConfigurationFile(keyOne));
keyTwo = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.WEEKLY_SCHEDULER_MIN;
min = getMinutesValue(getHandlerPropertyFromConfigurationFile(keyTwo));
trigger = createWeeklyTriggerOnDayAndHourAndMinute("«scheduler.fullyQualifiedName»SchedulerTrigger«index»", "triggers", dOw>0? dOw : «type.dayofweek.ordinal», hour>=0 ? hour : «type.hour», min>=0 ? min : «type.minute»);
ft = scheduleJob(job, trigger);
log.info("«scheduler.name.toFirstUpper»SchedulerJob«index» has been scheduled to run at: " + ft);
''')
}
} else if (scheduler.getSchedulertype instanceof MonthlyScheduler) {
var type = scheduler.getSchedulertype as MonthlyScheduler
if (type !== null && type.dayofmonth > -1 && type.hour > -1 && type.minute > -1) {
result = result.concat('''
job = createDataTransferJob("«scheduler.fullyQualifiedName»SchedulerJob«index»", "jobs", "-", «scheduler.executiontype.equals(SignalExecutionTypeEnum.SEQ)», "«scheduler.fullyQualifiedName»", «signalName».class);
key = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.MONTHLY_SCHEDULER_DAYOFMONTH;
dOm = getDayOfMonthValue(getHandlerPropertyFromConfigurationFile(key));
keyOne = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.MONTHLY_SCHEDULER_HOUR;
hour = getHourValue(getHandlerPropertyFromConfigurationFile(keyOne));
keyTwo = "«scheduler.fullyQualifiedName»Scheduler" + SignalConstants.MONTHLY_SCHEDULER_MIN;
min = getMinutesValue(getHandlerPropertyFromConfigurationFile(keyTwo));
trigger = createMonthlyTriggerOnDayAndHourAndMinute("«scheduler.fullyQualifiedName»SchedulerTrigger«index»", "triggers", dOm>0? dOm : «type.dayofmonth», hour>=0 ? hour : «type.hour», min>=0 ? min : «type.minute»);
ft = scheduleJob(job, trigger);
log.info("«scheduler.name.toFirstUpper»SchedulerJob«index» has been scheduled to run at: " + ft);
''')
}
}
index++
result = result.concat('''log.info("«signalName» - Scheduling all jobs done.");''')
return result
}
def String registerHandlerPathToWatch(SignalWatcher signal){
var result = "";
var interchange = signal.baseInterchange
if(interchange !== null){
var modelurl = interchange.getDataInterchangeDirectoryUrlPath
result = result.concat('''registerUrl(getHandlerPropertyFromConfigurationFile("«interchange.name»-import"),"«modelurl»");
''')
}
return result
}
/**
* Special function to read signal (watcher or scheduler) properties
* from configuration files.
*/
def String handlerPropertyFromConfigurationFile(SignalDefinition signal){
if(signal !== null) {
var signalName = ""
if(signal instanceof SignalWatcher){
signalName = signal.name.toFirstUpper + "Watcher"
}else if(signal instanceof SignalScheduler){
signalName = signal.name.toFirstUpper + "Scheduler"
}
return
'''
if (propertyname != null) {
«getConfigFileURL(signal.interchangegroup.name)»
File file = new File(url);
if (file.exists()) {
try {
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.loadFromXML(fileInput);
fileInput.close();
String value = properties.getProperty(propertyname);
if (value != null) {
if (log.isDebugEnabled()) {
log.debug("«signalName» - getHandlerPropertyFromConfigurationFile propertyname[" + propertyname
+ "] -> value [" + value + "]");
}
return value;
}
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("«signalName» - Error during getHandlerPropertyFromConfigurationFile of propertyname: [" + propertyname
+ "] ... {}", sw.toString());
}
}
}
if (log.isDebugEnabled()) {
log.debug("«signalName» - getHandlerPropertyFromConfigurationFile propertyname[" + propertyname + "] -> value [null] because the given path"
+ " in 'Window->Preferences->OSBP Application Configuration->External Data Sources->DataInterchange Settings' is non-existing or the propertyname is not defined.");
}
return null;
'''
}
return '''return "";'''
}
def boolean hasSchedulers(SignalPackage pck) {
if (pck !== null && pck.signals!== null && !pck.signals.isEmpty) {
for(signal : pck.signals){
if(signal instanceof SignalScheduler){
return true
}
}
}
return false
}
def boolean hasWatchers(SignalPackage pck) {
if (pck !== null && pck.signals !== null && !pck.signals.isEmpty) {
for(signal : pck.signals){
if(signal instanceof SignalScheduler){
return true
}
}
}
return false;
}
def int getSignalCount(SignalPackage pck, boolean isWatcher) {
if (pck !== null && pck.signals !== null && !pck.signals.isEmpty) {
var count = 0
for(signal : pck.signals){
if(isWatcher && (signal instanceof SignalWatcher)){
count++
}
else if(!isWatcher && signal instanceof SignalScheduler){
count++
}
}
return count
}
return -1
}
def boolean hasFunctionWithFile(SignalDefinition signal){
if(signal !== null && signal.tasks !== null && !signal.tasks.empty){
for(task : signal.tasks){
if(task instanceof SignalFunction){
if(task.onExportFile || task.onImportFile){
return true
}
}
}
}
return false
}
/**
* Gives back the default interchange unit.
*/
def DataInterchange baseInterchange(SignalDefinition signal){
if(signal instanceof SignalWatcher) {
if(signal.defaultInterchange !== null){
return signal.defaultInterchange
}
else if (signal.tasks !== null && !signal.tasks.empty){
for(task : signal.tasks){
if(task instanceof SignalDatainterchange){
if(task.baseinterchange){
return task.dataRef
}
}
}
}
}
return null
}
def int definedBaseInterchangeCount(SignalDefinition signal) {
var count = 0
for (task : signal.tasks) {
if(task instanceof SignalDatainterchange){
if(task.baseinterchange){
count++
}
}
}
return count
}
def SignalDatainterchange definedBaseInterchange(SignalDefinition signal) {
for (task : signal.tasks) {
if(task instanceof SignalDatainterchange){
if(task.baseinterchange){
return task
}
}
}
return null
}
def int taskCount(SignalDefinition signal, boolean interchange) {
var count = 0
for (task : signal.tasks) {
if(interchange){
if(task instanceof SignalDatainterchange){
count++
}
}
else if(task instanceof SignalFunction){
count++
}
}
return count
}
}