Skip to content

Commit

Permalink
Add Person methods to Hoper class
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed May 7, 2019
1 parent afefddb commit 1f40b31
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/org/camunda/latera/bss/connectors/Hoper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Hoper {
String url
private String user
private String password
String version
Integer version
HTTPRestProcessor http
DelegateExecution execution
SimpleLogger logger
Expand All @@ -18,8 +18,8 @@ class Hoper {
this.execution = execution
this.logger = new SimpleLogger(execution)

this.url = execution.getVariable("hoperUrl")
this.version = execution.getVariable("hoperVersion") ?: '2'
this.url = execution.getVariable("hoperUrl") ?: 'http://hoper:3000'
this.version = execution.getVariable("hoperVersion") ?: 2
this.user = execution.getVariable("hydraUser")
this.password = execution.getVariable("hydraPassword")
this.http = new HTTPRestProcessor(baseUrl : this.url,
Expand All @@ -41,16 +41,16 @@ class Hoper {
}

private def authBasic() {
def auth = "${user}:${password}"
def auth = "${this.user}:${this.password}"
return Base64Converter.to(auth)
}

private def authHeader() {
if (version.toString() == '1') {
return ['Authorization': "Basic ${authBasic}"]
if (this.version == 1) {
return ['Authorization': "Basic ${this.authBasic()}"]
}
if (version.toString() == '2') {
return ['Authorization': "Token token=\"${authToken}\""]
if (this.version == 2) {
return ['Authorization': "Token token=\"${this.authToken()}\""]
}
return []
}
Expand All @@ -59,8 +59,8 @@ class Hoper {
if (!input.headers) {
input.headers = [:]
}
input.headers += authHeader
input.path = "/rest/v${version}/${input.path}".toString()
input.headers += this.authHeader()
input.path = "/rest/v${this.version}/${input.path}".toString()
return http.sendRequest(input, method)
}
}
82 changes: 82 additions & 0 deletions src/org/camunda/latera/bss/connectors/hoper/Entity.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.camunda.latera.bss.connectors.hoper.hydra

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

trait Entity {
LinkedHashMap getEntityDefaultParams() {
return [:]
}

LinkedHashMap getEntityParamsMap(LinkedHashMap params) {
return [:]
}

LinkedHashMap getEntityParams(LinkedHashMap input) {
def params = getEntityDefaultParams() + input
def where = getEntityParamsMap(params)
return getEntityParamsMap(params)
}

LinkedHashMap getEntity(def type, def id) {
def result = null
try {
result = hoper.sendRequest(
'get',
path : "${type.parent}/${type.plural}/${id}"
)?."${type.one}"
} catch (Exception e) {
logger.error(e)
}
return result
}

LinkedHashMap createEntity(def type, LinkedHashMap params) {
def result = null
try {
logger.info("Creating ${type.one} with params ${params}")
result = hoper.sendRequest(
'post',
path : "${type.parent}/${type.plural}",
body : ["${type.one}": params]
)?."${type.one}"
logger.info(" ${StringUtil.capitalize(type.one.toUppercase} was created successfully!")
} catch (Exception e) {
logger.error(" Error while creating ${type.one}")
logger.error(e)
}
return result
}

LinkedHashMap updateEntity(def type, def id, LinkedHashMap params) {
def result = null
try {
logger.info("Updating ${type.one} id ${id} with params ${params}")
result = hoper.sendRequest(
'put',
path : "${type.parent}/${type.plural}/${id}",
body : ["${type.one}": params]
)?."${type.one}"
logger.info(" ${StringUtil.capitalize(type.one.toUppercase} was updated successfully!")
} catch (Exception e) {
logger.error(" Error while updating ${type.one}")
logger.error(e)
}
return result
}

Boolean deleteEntity(def type, def id) {
try {
logger.info("Deleting ${type.one} id ${id}")
hoper.sendRequest(
'delete',
path : "${type.parent}/${type.plural}/${id}"
)
logger.info(" ${StringUtil.capitalize(type.one.toUppercase} was deleted successfully!")
return true
} catch (Exception e) {
logger.error(" Error while deleting ${type.one}")
logger.error(e)
return false
}
}
}
5 changes: 4 additions & 1 deletion src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import groovy.net.xmlrpc.*
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.camunda.latera.bss.logging.SimpleLogger
import org.camunda.latera.bss.connectors.Hoper
import org.camunda.latera.bss.connectors.hoper.hydra.Main
import org.camunda.latera.bss.connectors.hoper.hydra.Entity
import org.camunda.latera.bss.connectors.hoper.hydra.Subject
import org.camunda.latera.bss.connectors.hoper.hydra.Person

class Hydra implements Subject {
class Hydra implements Main, Entity, Subject, Person {
private static Integer DEFAULT_FIRM = 100
Hoper hoper
def firmId
Expand Down
14 changes: 14 additions & 0 deletions src/org/camunda/latera/bss/connectors/hoper/Main.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.camunda.latera.bss.connectors.hoper.hydra

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

return params
}
}
99 changes: 99 additions & 0 deletions src/org/camunda/latera/bss/connectors/hoper/hydra/Person.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.camunda.latera.bss.connectors.hoper.hydra

trait Person {
private static LinkedHashMap PERSON_ENTITY_TYPE = [
parent : 'subjects',
one : 'person',
plural : 'persons'
]

def getPersonEntityType() {
return PERSON_ENTITY_TYPE
}

LinkedHashMap getPersonDefaultParams() {
return [
firstName : null,
secondName : null,
lastName : null,
opfId : null,
sexId : null,
inn : null,
docTypeId : null,
docSerial : null,
docNumber : null,
docDate : null,
docDepartment : null,
docAuthor : null,
birthDate : null,
birthPlace : null,
rem : null,
groupId : null,
stateId : null
//firmId : getFirmId()
]
}

LinkedHashMap getPersonParamsMap(LinkedHashMap params, LinkedHashMap additionalParams = [:]) {
def result = [
vc_first_name : params.firstName,
vc_second_name : params.secondName,
vc_surname : params.lastName,
n_opf_id : params.opfId,
n_sex_id : params.sexId,
vc_inn : params.inn,
vc_doc_serial : params.docSerial,
vc_doc_no : params.docNumber,
d_doc : params.docDate,
vc_doc_department : params.docDepartment,
vc_document : params.docAuthor,
d_birth : params.birthDate,
vc_birth_place : params.birthPlace,
vc_pens_insurance : params.pensInsurance,
vc_med_insurance : params.medInsurance,
vc_rem : params.rem,
n_subj_state_id : params.stateId,
n_firm_id : params.firmId,
t_tags : params.tags
]
if (additionalParams) {
result.additional_values = params.additionalParams
}
return result
}

LinkedHashMap getPersonParams(LinkedHashMap input, LinkedHashMap additionalParams = [:]) {
def params = getPersonDefaultParams() + input
def data = getPersonParamsMap(params + additionalParams)
return nvlParams(data)
}

LinkedHashMap getPerson(def id) {
return getEntity(getPersonEntityType(), id)
}

LinkedHashMap createPerson(LinkedHashMap input, LinkedHashMap additionalParams = [:]) {
LinkedHashMap params = getPersonParams(input, additionalParams)
return createEntity(getPersonEntityType(), params)
}

LinkedHashMap updatePerson(def id, LinkedHashMap input, LinkedHashMap additionalParams = [:]) {
LinkedHashMap params = getPersonParams(input, additionalParams)
return updateEntity(getPersonEntityType(), id, params)
}

LinkedHashMap putPerson(LinkedHashMap input, LinkedHashMap additionalParams = [:]) {
def personId = input.personId
input.remove('personId')

if (personId) {
return updatePerson(personId, input, additionalParams)
} else {
return createPerson(input, additionalParams)
}
}

Boolean deletePerson(def id) {
return deleteEntity(getPersonEntityType(), id)
}
}

0 comments on commit 1f40b31

Please sign in to comment.