From 1f40b311b0691e418c2d7a1a62d5e121d3ba3370 Mon Sep 17 00:00:00 2001 From: dolfinus Date: Wed, 8 May 2019 02:37:47 +0300 Subject: [PATCH] Add Person methods to Hoper class --- .../latera/bss/connectors/Hoper.groovy | 20 ++-- .../latera/bss/connectors/hoper/Entity.groovy | 82 +++++++++++++++ .../latera/bss/connectors/hoper/Hydra.groovy | 5 +- .../latera/bss/connectors/hoper/Main.groovy | 14 +++ .../bss/connectors/hoper/hydra/Person.groovy | 99 +++++++++++++++++++ 5 files changed, 209 insertions(+), 11 deletions(-) create mode 100644 src/org/camunda/latera/bss/connectors/hoper/Entity.groovy create mode 100644 src/org/camunda/latera/bss/connectors/hoper/Main.groovy create mode 100644 src/org/camunda/latera/bss/connectors/hoper/hydra/Person.groovy diff --git a/src/org/camunda/latera/bss/connectors/Hoper.groovy b/src/org/camunda/latera/bss/connectors/Hoper.groovy index 5d183111..ab76b908 100644 --- a/src/org/camunda/latera/bss/connectors/Hoper.groovy +++ b/src/org/camunda/latera/bss/connectors/Hoper.groovy @@ -9,7 +9,7 @@ class Hoper { String url private String user private String password - String version + Integer version HTTPRestProcessor http DelegateExecution execution SimpleLogger logger @@ -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, @@ -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 [] } @@ -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) } } \ No newline at end of file diff --git a/src/org/camunda/latera/bss/connectors/hoper/Entity.groovy b/src/org/camunda/latera/bss/connectors/hoper/Entity.groovy new file mode 100644 index 00000000..3df68772 --- /dev/null +++ b/src/org/camunda/latera/bss/connectors/hoper/Entity.groovy @@ -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 + } + } +} \ No newline at end of file diff --git a/src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy b/src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy index e0940593..95b61dda 100644 --- a/src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy +++ b/src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy @@ -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 diff --git a/src/org/camunda/latera/bss/connectors/hoper/Main.groovy b/src/org/camunda/latera/bss/connectors/hoper/Main.groovy new file mode 100644 index 00000000..4bab9223 --- /dev/null +++ b/src/org/camunda/latera/bss/connectors/hoper/Main.groovy @@ -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 + } +} \ No newline at end of file diff --git a/src/org/camunda/latera/bss/connectors/hoper/hydra/Person.groovy b/src/org/camunda/latera/bss/connectors/hoper/hydra/Person.groovy new file mode 100644 index 00000000..068dc1d3 --- /dev/null +++ b/src/org/camunda/latera/bss/connectors/hoper/hydra/Person.groovy @@ -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) + } +} \ No newline at end of file