Skip to content

Commit

Permalink
Add OTRS connector
Browse files Browse the repository at this point in the history
For OTRS v6
  • Loading branch information
dolfinus committed May 29, 2019
1 parent b77eb43 commit 3f1cc56
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/org/camunda/latera/bss/connectors/OTRS.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.camunda.latera.bss.connectors

import groovyx.net.http.HttpException
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.camunda.latera.bss.http.HTTPRestProcessor
import org.camunda.latera.bss.logging.SimpleLogger

import org.camunda.latera.bss.connectors.otrs.Main
import org.camunda.latera.bss.connectors.otrs.Entity
import org.camunda.latera.bss.connectors.otrs.types.Ticket

class OTRS implements Main, Entity, Ticket {
String url
String user
private String password
HTTPRestProcessor http
SimpleLogger logger

OTRS(DelegateExecution execution) {
this.logger = new SimpleLogger(execution)
def ENV = System.getenv()

this.url = ENV['OTRS_URL'] ?: execution.getVariable('otrsUrl')
this.user = ENV['OTRS_USER'] ?: execution.getVariable('otrsUser')
this.password = ENV['OTRS_PASSWORD'] ?: execution.getVariable('otrsPassword')
this.http = new HTTPRestProcessor(
baseUrl : url,
execution : execution,
)
}

def sendRequest(LinkedHashMap input, String method = 'get') {
if (input.path) {
input.path = "nph-genericinterface.pl/Webservice/HOMS/${input.path}?UserLogin=${user}&Password=${password}"
}
return http.sendRequest(input, method)
}
}
63 changes: 63 additions & 0 deletions src/org/camunda/latera/bss/connectors/otrs/Entity.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.camunda.latera.bss.connectors.otrs

trait Entity {
LinkedHashMap getEntity(def type, def id) {
def result = null
try {
result = sendRequest(
'get',
path : "${type}Get/${id}"
)?.data
} catch (Exception e) {
logger.error(e)
}
return result
}

LinkedHashMap createEntity(def type, LinkedHashMap params) {
def result = null
try {
logger.info("Creating ${type} with params ${params}")
result = sendRequest(
'post',
path : "${type}Create",
body : params
)?.data
} catch (Exception e) {
logger.error(" Error while creating ${type}")
logger.error(e)
}
return result
}

LinkedHashMap updateEntity(def type, def id, LinkedHashMap params) {
def result = null
try {
logger.info("Updating ${type} id ${id} with params ${params}")
result = sendRequest(
'put',
path : "${type}Update/${id}",
body : params
)?.data
} catch (Exception e) {
logger.error(" Error while updating ${type}")
logger.error(e)
}
return result
}

Boolean deleteEntity(def type, def id) {
try {
logger.info("Deleting ${type} id ${id}")
sendRequest(
'delete',
path : "${type}Delete/${id}"
)
return true
} catch (Exception e) {
logger.error(" Error while deleting ${type}")
logger.error(e)
return false
}
}
}
39 changes: 39 additions & 0 deletions src/org/camunda/latera/bss/connectors/otrs/Main.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.camunda.latera.bss.connectors.otrs

import org.camunda.latera.bss.utils.DateTimeUtil
import org.camunda.latera.bss.utils.StringUtil

trait Main {
LinkedHashMap nvlParams(LinkedHashMap input) {
def params = [:]
input.each { key, value ->
if (value != null) {
if (value == 'null' || value == 'NULL') {
params[key] = null
} else {
params[key] = value
}
}
}

return params
}

LinkedHashMap convertKeys(LinkedHashMap input) {
return StringUtil.camelizeKeys(input, true)
}

def convertValue(def value) {
if (value instanceof Boolean) {
return value ? 1 : 0
}
return value
}

LinkedHashMap convertParams(LinkedHashMap input) {
LinkedHashMap result = [:]
input.each { key, value ->
result[key] = convertValue(value)
}
}
}
209 changes: 209 additions & 0 deletions src/org/camunda/latera/bss/connectors/otrs/types/Ticket.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package org.camunda.latera.bss.connectors.otrs.types

import org.camunda.latera.bss.utils.Base64Converter

trait Ticket {
private static String TICKET_ENTITY_TYPE = 'Ticket'

def getTicketEntityType() {
return TICKET_ENTITY_TYPE
}

LinkedHashMap getTicketDefaultParams() {
return [
title : null,
queueId : null,
lockId : null,
typeId : null,
serviceId : null,
slaId : null,
stateId : null,
priorityId : null,
ownerId : null,
responsibleId : null,
pendingTime : null
]
}

LinkedHashMap getTicketParamsMap(LinkedHashMap params) {
return [
Title : params.title,
QueueID : params.queueId,
Queue : params.queue,
LockID : params.lockId,
Lock : params.lock,
TypeID : params.typeId,
Type : params.type,
ServiceID : params.serviceId,
Service : params.service,
SLAID : params.slaId,
SLA : params.sla,
StateID : params.stateId,
State : params.state,
OwnerID : params.ownerId,
Owner : params.owner,
ResponsibleID : params.responsibleId,
Responsible : params.responsible,
PendingTime : params.pendingTime
]
}

LinkedHashMap getTicketArticleDefaultParams() {
return [
communicationChannelId : null,
isVisibleForCustomer : null,
senderTypeId : null,
autoResponseType : null,
from : null,
subject : null,
body : null,
bodyContentType : 'text/plain; charset=utf8',
bodyCharset : null,
bodyMimeType : null,
noAgentNotify : null,
forceNotificationToUserIds : null,
excludeNotificationToUserIds : null,
excludeMuteNotificationToUserIds : null
]
}

LinkedHashMap getTicketArticleParamsMap(LinkedHashMap params) {
return [
CommunicationChannelID : params.communicationChannelId,
CommunicationChannel : params.communicationChannel,
IsVisibleForCustomer : params.isVisibleForCustomer,
SenderTypeID : params.senderTypeId,
SenderType : params.senderType,
AutoResponseType : params.autoResponseType,
From : params.from,
Subject : params.subject,
Body : params.body,
ContentType : params.bodyContentType,
Charset : params.bodyCharset,
MimeType : params.bodyMimeType,
NoAgentNotify : params.noAgentNotify,
ForceNotificationToUserID : params.forceNotificationToUserIds,
ExcludeNotificationToUserID : params.excludeNotificationToUserIds,
ExcludeMuteNotificationToUserID : params.excludeMuteNotificationToUserIds
]
}

LinkedHashMap getTicketDynamicFieldDefaultParams() {
return [
name : null,
value : null
]
}

LinkedHashMap getTicketDynamicFieldParamsMap(LinkedHashMap params) {
return [
Name : params.name,
Value : params.value
]
}

LinkedHashMap getTicketAttachmentDefaultParams() {
return [
name : '',
content : [] as byte[],
contentType : null
]
}

LinkedHashMap getTicketAttachmentParamsMap(LinkedHashMap params) {
return [
Content : params.content ? Base64Converter.to(params.content) : '',
ContentType : params.contentType,
Filename : params.name
]
}

LinkedHashMap getTicketParams(LinkedHashMap input, List attachments = [], LinkedHashMap dynamicFields = [:], LinkedHashMap additionalParams = [:]) {
def params = getTicketDefaultParams() + input
def ticket = convertParams(nvlParams(getTicketParamsMap(params)) + convertKeys(additionalParams))
def article = convertParams(nvlParams(getTicketArticleParamsMap(params)))

List attachmentList = []
if (attachments) {
attachments.each { it ->
def attachment = getTicketAttachmentDefaultParams() + it
attachmentList += getTicketAttachmentParamsMap(attachment)
}
}

List dynamicFieldsList = []
if (dynamicFields) {
dynamicFields.each { key, value ->
def field = getTicketDynamicFieldDefaultParams() + [
name : key,
value : value
]
dynamicFieldsList += getTicketDynamicFieldParamsMap(field)
}
}

LinkedHashMap result = [:]
if (ticket) {
result.Ticket = ticket
}
if (article) {
result.Article = article
}
if (attachmentsList) {
result.Attachment = attachmentList
}
if (dynamicFieldsList) {
result.DynamicField = dynamicFieldsList
}
return result
}

LinkedHashMap getTicket(def id) {
return getEntity(getTicketEntityType(), id)
}

LinkedHashMap createTicket(LinkedHashMap input, List attachments = [], LinkedHashMap dynamicFields = [:], LinkedHashMap additionalParams = [:]) {
LinkedHashMap params = getTicketParams(input, attachments, dynamicFields, additionalParams)
return createEntity(getTicketEntityType(), params)
}

LinkedHashMap updateTicket(def id, LinkedHashMap input, List attachments = [], LinkedHashMap dynamicFields = [:], LinkedHashMap additionalParams = [:]) {
LinkedHashMap params = getTicketParams(input, attachments, dynamicFields, additionalParams)
return updateEntity(getTicketEntityType(), id, params)
}

LinkedHashMap updateTicket(LinkedHashMap input, def id, List attachments = [], LinkedHashMap dynamicFields = [:], LinkedHashMap additionalParams = [:]) {
return updateTicket(id, input, attachments, dynamicFields, additionalParams)
}

LinkedHashMap addTicketFiles(def id, List attachments = []) {
return updateTicket(id, [:], [:], attachments)
}

LinkedHashMap updateTicketArticle(def id, LinkedHashMap input) {
return updateTicket(id, input)
}

LinkedHashMap updateTicketArticle(LinkedHashMap input, def id) {
return updateTicketArticle(id, input)
}

LinkedHashMap updateTicketDynamicField(def id, def name, def value = null) {
def dynamicFields = [[name: value]]
return updateTicket(id, [:], dynamicFields)
}

LinkedHashMap updateTicketDynamicField(LinkedHashMap input, def id) {
updateTicketDynamicField(id, input.name, input.value)
}

LinkedHashMap updateTicketDynamicFields(LinkedHashMap input, def id) {
input.each { key, value ->
updateTicketDynamicField(id, key, value)
}
}

Boolean deleteTicket(def id) {
return deleteEntity(getTicketEntityType(), id)
}
}

0 comments on commit 3f1cc56

Please sign in to comment.