diff --git a/src/org/camunda/latera/bss/logging.groovy b/src/org/camunda/latera/bss/logging.groovy index 970a2f19..5165c125 100644 --- a/src/org/camunda/latera/bss/logging.groovy +++ b/src/org/camunda/latera/bss/logging.groovy @@ -1,6 +1,8 @@ package org.camunda.latera.bss.logging import org.camunda.bpm.engine.delegate.DelegateExecution +import org.camunda.latera.bss.utils.DateTimeUtil +import java.time.format.DateTimeFormatter import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -29,28 +31,23 @@ class Logging { class SimpleLogger { String processInstanceID String homsOrderCode - String dateFormat + DateTimeFormatter dateFormat SimpleLogger(DelegateExecution execution) { this.processInstanceID = execution.getProcessInstanceId() - this.homsOrderCode = execution.getVariable('homsOrderCode') - this.dateFormat = execution.getVariable('loggingDateFormat')?:"yyyy-MM-dd HH:mm:ss" + this.homsOrderCode = execution.getVariable('homsOrderCode') ?: 'ORD-NONE' + this.dateFormat = execution.getVariable('loggingDateFormat') ? DateTimeFormatter.ofPattern(execution.getVariable('loggingDateFormat')) : DateTimeUtil.dateTimeFormat } void log(Object message, String level = "info") { - String timestamp = new Date().format(this.dateFormat) + String timestamp = DateTimeUtil.now().format(this.dateFormat) String logPrefix - if (homsOrderCode) { - logPrefix = "${timestamp} ${processInstanceID} [${homsOrderCode}] ${level.toUpperCase().padRight(5, ' ')} - ".toString() - } else { - logPrefix = "${timestamp} ${processInstanceID} ${level.toUpperCase().padRight(5, ' ')} - ".toString() - } + logPrefix = "${timestamp} ${processInstanceID} [${homsOrderCode}] ${level.toUpperCase().padRight(5, ' ')} - ".toString() message.toString().split('\n').each { it -> println(logPrefix + it) } - } void debug(Object message) { diff --git a/src/org/camunda/latera/bss/utils/Base64Converter.groovy b/src/org/camunda/latera/bss/utils/Base64Converter.groovy new file mode 100644 index 00000000..1f529e00 --- /dev/null +++ b/src/org/camunda/latera/bss/utils/Base64Converter.groovy @@ -0,0 +1,12 @@ +package org.camunda.latera.bss.utils +import java.util.Base64 + +class Base64Converter { + static String to(byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes) + } + + static byte[] from(String str) { + return Base64.getDecoder().decode(str) + } +} \ No newline at end of file diff --git a/src/org/camunda/latera/bss/utils/DateTimeUtil.groovy b/src/org/camunda/latera/bss/utils/DateTimeUtil.groovy new file mode 100644 index 00000000..afce9184 --- /dev/null +++ b/src/org/camunda/latera/bss/utils/DateTimeUtil.groovy @@ -0,0 +1,24 @@ +package org.camunda.latera.bss.utils + +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter +import groovy.time.TimeCategory + +class DateTimeUtil { + static DateTimeFormatter ISOFormat = DateTimeFormatter.ISO_OFFSET_DATE_TIME + static DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern('dd.MM.yyyy') + static DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss') + static DateTimeFormatter simpleDateTimeFormat = DateTimeFormatter.ofPattern('dd.MM.yyyy HH:mm:ss') + + static def now() { + return LocalDateTime.now() + } + + static def parse( + String dt, + def format = this.simpleDateTimeFormat + ) { + return LocalDateTime.parse(dt, format) + } +} + diff --git a/src/org/camunda/latera/bss/utils/IO.groovy b/src/org/camunda/latera/bss/utils/IO.groovy new file mode 100644 index 00000000..4b9bd5d7 --- /dev/null +++ b/src/org/camunda/latera/bss/utils/IO.groovy @@ -0,0 +1,22 @@ +package org.camunda.latera.bss.utils + +import java.io.ByteArrayOutputStream +import java.io.InputStreamReader + +class IO { + static def getBytes( + InputStreamReader input + ) { + + ByteArrayOutputStream buffer = new ByteArrayOutputStream() + int nRead; + byte[] data = new byte[1024] + while ((nRead = input.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead) + } + + buffer.flush() + byte[] byteArray = buffer.toByteArray() + return byteArray + } +} \ No newline at end of file diff --git a/src/org/camunda/latera/bss/utils/JSON.groovy b/src/org/camunda/latera/bss/utils/JSON.groovy new file mode 100644 index 00000000..f3f88a75 --- /dev/null +++ b/src/org/camunda/latera/bss/utils/JSON.groovy @@ -0,0 +1,13 @@ +package org.camunda.latera.bss.utils +import groovy.json.JsonSlurper +import groovy.json.JsonOutput + +class JSON { + static String to(Object obj) { + return JsonOutput.toJson(obj) + } + + static Object from(String json) { + return new JsonSlurper().parseText(json) + } +} \ No newline at end of file diff --git a/src/org/camunda/latera/bss/utils/Oracle.groovy b/src/org/camunda/latera/bss/utils/Oracle.groovy new file mode 100644 index 00000000..c61df42e --- /dev/null +++ b/src/org/camunda/latera/bss/utils/Oracle.groovy @@ -0,0 +1,44 @@ +package org.camunda.latera.bss.utils + +class Oracle { + static Object encodeNull(value) { + if (value != 'NULL' && value != null) { + return value + } else { + return 'NULL' + } + } + + static Object decodeNull(value) { + if (encodeNull(value) == 'NULL') { + return null + } + return value + } + + static Object encodeBool(value) { + Object result = encodeNull(value) + if (result != 'NULL') { + return value ? 'Y' : 'N' + } else { + return result + } + } + + static Boolean decodeBool(value) { + return value == 'Y' + } + + static Object nvl(nullable, replacement, args = []) { + Boolean result = decodeNull(nullable) + if (result != null) { + return result + } else { + if (replacement.metaClass.respondsTo(replacement, 'call')) { + return replacement.call(*args) + } else { + return replacement + } + } + } +} \ No newline at end of file diff --git a/src/org/camunda/latera/bss/utils/Order.groovy b/src/org/camunda/latera/bss/utils/Order.groovy new file mode 100644 index 00000000..d4741c85 --- /dev/null +++ b/src/org/camunda/latera/bss/utils/Order.groovy @@ -0,0 +1,16 @@ +package org.camunda.latera.bss.utils +import org.camunda.bpm.engine.delegate.DelegateExecution + +class Order { + static LinkedHashMap getData(DelegateExecution execution) { + LinkedHashMap data = [:] + for (e in execution.getVariables()) { + if (e.key =~ /^homsOrderData/) { + String dataKey = e.key.replaceFirst(/^homsOrderData/, "") + dataKey = (dataKey.getAt(0).toLowerCase() + dataKey.substring(1)).toString() + data[dataKey] = e.value + } + } + return data + } +} \ No newline at end of file diff --git a/src/org/camunda/latera/bss/utils/StringUtil.groovy b/src/org/camunda/latera/bss/utils/StringUtil.groovy new file mode 100644 index 00000000..f7aa366f --- /dev/null +++ b/src/org/camunda/latera/bss/utils/StringUtil.groovy @@ -0,0 +1,52 @@ +package org.camunda.latera.bss.utils +import java.nio.charset.Charset + +class StringUtil { + static byte[] unicodeToVarchar(String input) { + if (input) { + return input.getBytes(Charset.forName("ISO-8859-1")) + } + return null + } + + static String varcharToUnicode(String input) { + if (input) { + return new String(input.getBytes(Charset.forName("ISO-8859-1")), "UTF-8") + } + return null + } + + static byte[] unicodeToCP1251(String input) { + if (input) { + return input.getBytes(Charset.forName("CP1251")) + } + return null + } + + static String cp1251ToUnicode(String input) { + if (input) { + return new String(input.getBytes(Charset.forName("CP1251")), "UTF-8") + } + return null + } + + static Boolean notEmpty(String input) { + if (input) { + return (input?.trim() as boolean) + } + return false + } + + static Boolean isEmpty(String input) { + return !notEmpty(input) + } + + static Boolean isString(def input) { + return (input instanceof CharSequence) + } + + static Boolean isByteArray(def input) { + return (input instanceof byte[]) + } +} +