Skip to content

Commit

Permalink
Add utils class for Oracle, Order, IO, JSON, String and DateUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed Dec 27, 2018
1 parent 9a78333 commit 08e46b2
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/org/camunda/latera/bss/logging.groovy
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/org/camunda/latera/bss/utils/Base64Converter.groovy
Original file line number Diff line number Diff line change
@@ -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)
}
}
24 changes: 24 additions & 0 deletions src/org/camunda/latera/bss/utils/DateTimeUtil.groovy
Original file line number Diff line number Diff line change
@@ -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)
}
}

22 changes: 22 additions & 0 deletions src/org/camunda/latera/bss/utils/IO.groovy
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 13 additions & 0 deletions src/org/camunda/latera/bss/utils/JSON.groovy
Original file line number Diff line number Diff line change
@@ -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)
}
}
44 changes: 44 additions & 0 deletions src/org/camunda/latera/bss/utils/Oracle.groovy
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
16 changes: 16 additions & 0 deletions src/org/camunda/latera/bss/utils/Order.groovy
Original file line number Diff line number Diff line change
@@ -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
}
}
52 changes: 52 additions & 0 deletions src/org/camunda/latera/bss/utils/StringUtil.groovy
Original file line number Diff line number Diff line change
@@ -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[])
}
}

0 comments on commit 08e46b2

Please sign in to comment.