From 8d38d30b5cd93dd14859e2c44b5d49cbc7104334 Mon Sep 17 00:00:00 2001 From: dolfinus Date: Thu, 9 May 2019 23:09:32 +0300 Subject: [PATCH] Some code beautify Get rid of execution fields in connectors class Get rid of 4-spaces identation Shorten lines with sendRequest method calls Only tokens and passwords are private fields --- .../camunda/latera/bss/connectors/HID.groovy | 15 +- .../camunda/latera/bss/connectors/HOMS.groovy | 75 +++++--- .../latera/bss/connectors/Hoper.groovy | 26 +-- .../latera/bss/connectors/Imprint.groovy | 35 ++-- .../camunda/latera/bss/connectors/Mail.groovy | 6 +- .../camunda/latera/bss/connectors/Odoo.groovy | 13 +- .../latera/bss/connectors/Planado.groovy | 171 +++++++++--------- .../latera/bss/connectors/hid/Hydra.groovy | 16 +- .../bss/connectors/hid/hydra/Company.groovy | 2 +- .../latera/bss/connectors/hoper/Hydra.groovy | 4 +- 10 files changed, 199 insertions(+), 164 deletions(-) diff --git a/src/org/camunda/latera/bss/connectors/HID.groovy b/src/org/camunda/latera/bss/connectors/HID.groovy index 02e9e90a..ef2644b7 100644 --- a/src/org/camunda/latera/bss/connectors/HID.groovy +++ b/src/org/camunda/latera/bss/connectors/HID.groovy @@ -10,15 +10,18 @@ import org.camunda.latera.bss.connectors.hid.Table import org.camunda.bpm.engine.delegate.DelegateExecution class HID implements Table { + String url + String user + private String password XMLRPCServerProxy proxy HID(DelegateExecution execution) { - def url = execution.getVariable('hidUrl') ?: 'http://hid:10080' - def user = execution.getVariable('hidUser') ?: 'hydra' - def password = execution.getVariable('hidPassword') + this.url = execution.getVariable('hidUrl') ?: 'http://hid:10080' + this.user = execution.getVariable('hidUser') ?: 'hydra' + this.password = execution.getVariable('hidPassword') - this.proxy = new XMLRPCServerProxy(url) - this.proxy.setBasicAuth(user, password) + this.proxy = new XMLRPCServerProxy(this.url) + this.proxy.setBasicAuth(this.user, this.password) } Object queryDatabase(String query, Boolean asMap = false, Boolean noLimit = false) { @@ -50,7 +53,7 @@ class HID implements Table { } } } - + return result } diff --git a/src/org/camunda/latera/bss/connectors/HOMS.groovy b/src/org/camunda/latera/bss/connectors/HOMS.groovy index ee7d7a21..758df611 100644 --- a/src/org/camunda/latera/bss/connectors/HOMS.groovy +++ b/src/org/camunda/latera/bss/connectors/HOMS.groovy @@ -8,7 +8,10 @@ import org.camunda.latera.bss.utils.JSON import org.camunda.latera.bss.utils.Base64Converter class HOMS { - HTTPRestProcessor processor + String url + String user + private String password + HTTPRestProcessor http DelegateExecution execution String homsOrderCode String homsOrderId @@ -18,17 +21,19 @@ class HOMS { this.execution = execution this.logger = new SimpleLogger(this.execution) - def url = execution.getVariable("homsUrl") - def user = execution.getVariable("homsUser") - def password = execution.getVariable("homsPassword") + this.url = execution.getVariable("homsUrl") + this.user = execution.getVariable("homsUser") + this.password = execution.getVariable("homsPassword") def supress = execution.getVariable('homsOrderSupress') ?: false - this.processor = new HTTPRestProcessor(baseUrl : url, - user : user, - password : password, - supressRequestBodyLog: supress, - supressResponseBodyLog: supress, - execution : execution) + this.http = new HTTPRestProcessor( + baseUrl : this.url, + user : this.user, + password : this.password, + supressRequestBodyLog: supress, + supressResponseBodyLog: supress, + execution : this.execution + ) this.homsOrderCode = execution.getVariable('homsOrderCode') this.homsOrderId = execution.getVariable('homsOrderId') } @@ -41,11 +46,13 @@ class HOMS { ] ] logger.info("/ Creating new order ...") - def result = processor.sendRequest(path: '/api/orders', - supressRequestBodyLog: false, - supressResponseBodyLog: false, - body: body, - 'post') + def result = http.sendRequest( + 'post', + path: '/api/orders', + supressRequestBodyLog: false, + supressResponseBodyLog: false, + body: body + ) LinkedHashMap order = result.order homsOrderCode = order.code homsOrderId = order.id @@ -67,9 +74,11 @@ class HOMS { ] ] logger.info("/ Starting order ...") - def result = this.processor.sendRequest(path: "/api/orders/${homsOrderCode}", - body: body, - 'put') + def result = this.http.sendRequest( + 'put', + path: "/api/orders/${homsOrderCode}", + body: body + ) logger.info('\\ Order started') } @@ -81,16 +90,20 @@ class HOMS { ] ] logger.info('/ Saving order data...') - def result = this.processor.sendRequest(path: "/api/orders/${homsOrderCode}", - body: body, - 'put') + def result = this.http.sendRequest( + 'put', + path: "/api/orders/${homsOrderCode}", + body: body + ) logger.info('\\ Order data saved') } void getOrderData() { logger.info('/ Receiving order data...') - def result = this.processor.sendRequest(path: "/api/orders/${homsOrderCode}", - 'get') + def result = this.http.sendRequest( + 'get', + path: "/api/orders/${homsOrderCode}" + ) homsOrderId = result.order.id execution.setVariable('homsOrderId', homsOrderId) @@ -112,9 +125,11 @@ class HOMS { ] ] logger.info("/ Finishing order ...") - def result = this.processor.sendRequest(path: "/api/orders/${homsOrderCode}", - body: body, - 'put') + def result = this.http.sendRequest( + 'put', + path: "/api/orders/${homsOrderCode}", + body: body + ) logger.info('\\ Order finished') } @@ -128,9 +143,11 @@ class HOMS { files: JSON.to(files) ] logger.info("Attaching files to order ${homsOrderId}") - def newFiles = processor.sendRequest(path: '/widget/file_upload', - body: body, - 'post') + def newFiles = this.http.sendRequest( + 'post', + path: '/widget/file_upload', + body: body + ) if (save) { def existingFiles = JSON.from(execution.getVariable('homsOrderDataFileList')) def newList = existingFiles + newFiles diff --git a/src/org/camunda/latera/bss/connectors/Hoper.groovy b/src/org/camunda/latera/bss/connectors/Hoper.groovy index ab76b908..cc789956 100644 --- a/src/org/camunda/latera/bss/connectors/Hoper.groovy +++ b/src/org/camunda/latera/bss/connectors/Hoper.groovy @@ -7,37 +7,39 @@ import org.camunda.bpm.engine.delegate.DelegateExecution class Hoper { String url - private String user + String user private String password Integer version HTTPRestProcessor http - DelegateExecution execution SimpleLogger logger Hoper(DelegateExecution execution) { - this.execution = execution this.logger = new SimpleLogger(execution) 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, - execution : execution) + this.http = new HTTPRestProcessor( + baseUrl : this.url, + execution : execution + ) } private def authToken() { def auth = [ session: [ - login : user, - password : password + login : this.user, + password : this.password ] ] - return http.sendRequest('get', - path : "/rest/v${this.version}/login", - body : auth, - supressRequestBodyLog : true, - supressResponseBodyLog : true)?.session?.token + return http.sendRequest( + 'get', + path : "/rest/v${this.version}/login", + body : auth, + supressRequestBodyLog : true, + supressResponseBodyLog : true + )?.session?.token } private def authBasic() { diff --git a/src/org/camunda/latera/bss/connectors/Imprint.groovy b/src/org/camunda/latera/bss/connectors/Imprint.groovy index 880c4468..e692c888 100644 --- a/src/org/camunda/latera/bss/connectors/Imprint.groovy +++ b/src/org/camunda/latera/bss/connectors/Imprint.groovy @@ -6,27 +6,30 @@ import org.camunda.latera.bss.utils.DateTimeUtil import org.camunda.bpm.engine.delegate.DelegateExecution class Imprint { - HTTPRestProcessor processor + String url + String version + private String token + HTTPRestProcessor http LinkedHashMap headers - DelegateExecution execution SimpleLogger logger String locale Imprint(DelegateExecution execution) { - this.execution = execution this.logger = new SimpleLogger(execution) this.locale = execution.getVariable("locale") ?: 'en' - def url = execution.getVariable("imprintUrl") - def version = execution.getVariable("imprintVersion") - def token = execution.getVariable("imprintToken") + this.url = execution.getVariable("imprintUrl") + this.version = execution.getVariable("imprintVersion") + this.token = execution.getVariable("imprintToken") def headers = [ - 'X_IMPRINT_API_VERSION' : version, - 'X_IMPRINT_API_TOKEN' : token + 'X_IMPRINT_API_VERSION' : this.version, + 'X_IMPRINT_API_TOKEN' : this.token ] - this.processor = new HTTPRestProcessor(baseUrl : url, - headers : headers, - execution : execution) + this.http = new HTTPRestProcessor( + baseUrl : url, + headers : headers, + execution : execution + ) } def print(String template, LinkedHashMap data) { @@ -39,10 +42,12 @@ class Imprint { todayFull : DateTimeUtil.format(DateTimeUtil.local(), DateTimeUtil.FULL_DATE_FORMAT, this.locale) ] + data ] - def result = this.processor.sendRequest(path : '/api/print', - body : body, - contentType : 'application/json', - 'post') + def result = this.http.sendRequest( + 'post', + path : '/api/print', + body : body, + contentType : 'application/json' + ) def file = result return file } diff --git a/src/org/camunda/latera/bss/connectors/Mail.groovy b/src/org/camunda/latera/bss/connectors/Mail.groovy index 726e3ef3..8f32f4ce 100644 --- a/src/org/camunda/latera/bss/connectors/Mail.groovy +++ b/src/org/camunda/latera/bss/connectors/Mail.groovy @@ -8,9 +8,9 @@ import javax.mail.Message class MailSender { - private String host - private Integer port - private String user + String host + Integer port + String user private String password private Session session private Message message diff --git a/src/org/camunda/latera/bss/connectors/Odoo.groovy b/src/org/camunda/latera/bss/connectors/Odoo.groovy index 3dfea2c2..eab54423 100644 --- a/src/org/camunda/latera/bss/connectors/Odoo.groovy +++ b/src/org/camunda/latera/bss/connectors/Odoo.groovy @@ -13,7 +13,7 @@ import org.camunda.latera.bss.connectors.odoo.types.Country class Odoo implements Main, Entity, Lead, Customer, Country { String url - private String user + String user private String password private String token String db @@ -30,9 +30,11 @@ class Odoo implements Main, Entity, Lead, Customer, Country { this.password = execution.getVariable('crmPassword') this.token = execution.getVariable('crmToken') this.db = execution.getVariable('crmDatabase') ?: 'odoo' - this.http = new HTTPRestProcessor(baseUrl : url, - contentType : 'application/x-www-form-urlencoded', - execution : execution) + this.http = new HTTPRestProcessor( + baseUrl : url, + contentType : 'application/x-www-form-urlencoded', + execution : execution + ) } private String authToken() { @@ -49,7 +51,8 @@ class Odoo implements Main, Entity, Lead, Customer, Country { return http.sendRequest( 'get', path : '/api/auth/token', - query : query)?.access_token?.toString() + query : query + )?.access_token?.toString() } private LinkedHashMap authHeader() { diff --git a/src/org/camunda/latera/bss/connectors/Planado.groovy b/src/org/camunda/latera/bss/connectors/Planado.groovy index acf162e2..ac471501 100644 --- a/src/org/camunda/latera/bss/connectors/Planado.groovy +++ b/src/org/camunda/latera/bss/connectors/Planado.groovy @@ -9,17 +9,22 @@ import org.apache.http.impl.client.LaxRedirectStrategy import java.security.MessageDigest class Planado { - private String planadoApiKey - private HTTPRestProcessor http - private SimpleLogger logger + String url + private String planadoToken + HTTPRestProcessor http + SimpleLogger logger Planado(DelegateExecution execution) { this.logger = new SimpleLogger(execution) - this.planadoApiKey = execution.getVariable('planadoApiKey') - def headers = ["X-Planado-Api-Token": planadoApiKey] - def url = 'https://api.planadoapp.com/api/v1/' - http = new HTTPRestProcessor(baseUrl: url, - headers: headers) + + this.url = 'https://api.planadoapp.com/api/v1/' + this.planadoToken = execution.getVariable('planadoApiKey') + + def headers = ["X-Planado-Api-Token": planadoToken] + http = new HTTPRestProcessor( + baseUrl: url, + headers: headers + ) } private String __makeExtID(String s) { @@ -32,9 +37,8 @@ class Planado { Object getUser(String extID) { try { return http.sendRequest( - 'get', - path: "clients/${extID}.json", - + 'get', + path: "clients/${extID}.json", ) } catch (HttpException ex) { @@ -45,8 +49,8 @@ class Planado { Object getUsers() { try { return http.sendRequest( - 'get', - path: "clients.json" + 'get', + path: "clients.json" ) } catch (HttpException ex) { @@ -57,8 +61,8 @@ class Planado { void deleteUser(String extID) { try { http.sendRequest( - "delete", - path: "clients/${extID}.json" + "delete", + path: "clients/${extID}.json" ) } catch (HttpException ex) { @@ -68,16 +72,16 @@ class Planado { String createUser(Map userData) { String extID = __makeExtID( - [ - userData.firstName, - userData.middleName, - userData.lastName, - userData.addressStreet, - userData.addressEntrance, - userData.addressFloor, - userData.addressApartment, - userData.phone - ].findAll { it -> !it?.isEmpty() }.join(';') + [ + userData.firstName, + userData.middleName, + userData.lastName, + userData.addressStreet, + userData.addressEntrance, + userData.addressFloor, + userData.addressApartment, + userData.phone + ].findAll { it -> !it?.isEmpty() }.join(';') ) if (getUser(extID)) { @@ -86,32 +90,33 @@ class Planado { } HashMap payload = [ - external_id : extID, - organization: false, - first_name : userData.firstName, - middle_name : userData.middleName, - last_name : userData.lastName, - name : [userData.lastName, userData.firstName].findAll { it -> !it?.isEmpty() }.join(' '), - site_address : [ - formatted : userData.addressStreet, - entrance_no: userData.addressEntrance, - floor : userData.addressFloor, - apartment : userData.addressApartment, - description: userData.addressDescription?:"" - ], - email : userData.email, - cell_phone : userData.phone + external_id : extID, + organization : false, + first_name : userData.firstName, + middle_name : userData.middleName, + last_name : userData.lastName, + name : [userData.lastName, userData.firstName].findAll { it -> !it?.isEmpty() }.join(' '), + site_address : [ + formatted : userData.addressStreet, + entrance_no : userData.addressEntrance, + floor : userData.addressFloor, + apartment : userData.addressApartment, + description : userData.addressDescription?:"" + ], + email : userData.email, + cell_phone : userData.phone ] if (userData.addressLat && userData.addressLon) payload.site_address.geolocation = [ - latitude : userData.addressLat, - longitude: userData.addressLon + latitude : userData.addressLat, + longitude: userData.addressLon ] http.sendRequest( - 'post', - path: 'clients.json', - body: payload) + 'post', + path: 'clients.json', + body: payload + ) return extID } @@ -120,14 +125,14 @@ class Planado { Map companyData ) { String extID = __makeExtID( - [ - companyData.companyName, - companyData.addressStreet, - companyData.addressEntrance, - companyData.addressFloor, - companyData.addressApartment, - companyData.phone - ].findAll { it -> !it?.isEmpty() }.join(';') + [ + companyData.companyName, + companyData.addressStreet, + companyData.addressEntrance, + companyData.addressFloor, + companyData.addressApartment, + companyData.phone + ].findAll { it -> !it?.isEmpty() }.join(';') ) if (getUser(extID)) { @@ -135,23 +140,23 @@ class Planado { return extID } HashMap payload = [ - external_id : extID, - organization : true, - organization_name: companyData.companyName, - site_address: [ - formatted : companyData.addressStreet, - entrance_no: companyData.addressEntrance, - floor : companyData.addressFloor, - apartment : companyData.addressApartment, - description: companyData.addressDescription?:"" - ], - email : companyData.email, - contacts : [[ - type : "phone", - name : companyData.companyName, - value: companyData.phone, - value_normalized: companyData.phone - ]] + external_id : extID, + organization : true, + organization_name : companyData.companyName, + site_address : [ + formatted : companyData.addressStreet, + entrance_no : companyData.addressEntrance, + floor : companyData.addressFloor, + apartment : companyData.addressApartment, + description : companyData.addressDescription?:"" + ], + email : companyData.email, + contacts : [[ + type : "phone", + name : companyData.companyName, + value: companyData.phone, + value_normalized: companyData.phone + ]] ] if (companyData.addressLat && companyData.addressLon) payload.site_address.geolocation = [ @@ -160,9 +165,10 @@ class Planado { ] http.sendRequest( - 'post', - path: 'clients.json', - body: payload) + 'post', + path: 'clients.json', + body: payload + ) return extID } @@ -170,8 +176,8 @@ class Planado { void deleteJob(String jobID) { try { http.sendRequest( - "delete", - path: "jobs/${extID}.json" + "delete", + path: "jobs/${extID}.json" ) } catch (HttpException ex) { @@ -187,9 +193,10 @@ class Planado { ] def res = http.sendRequest( - 'post', - path: 'jobs.json', - body: payload) + 'post', + path: 'jobs.json', + body: payload + ) return res?.job_id?:null } @@ -197,8 +204,8 @@ class Planado { Object getJob(String jobID) { try { return http.sendRequest( - 'get', - path: "jobs/${jobID}.json" + 'get', + path: "jobs/${jobID}.json" ) } catch (HttpException ex) { @@ -209,8 +216,8 @@ class Planado { Object getJobTemplate(String templateID) { try { return http.sendRequest( - 'get', - path: "templates/${templateID}.json" + 'get', + path: "templates/${templateID}.json" ) } catch (HttpException ex) { diff --git a/src/org/camunda/latera/bss/connectors/hid/Hydra.groovy b/src/org/camunda/latera/bss/connectors/hid/Hydra.groovy index ebd3b3f8..1fb8897b 100644 --- a/src/org/camunda/latera/bss/connectors/hid/Hydra.groovy +++ b/src/org/camunda/latera/bss/connectors/hid/Hydra.groovy @@ -25,25 +25,25 @@ import org.camunda.latera.bss.connectors.hid.hydra.Address class Hydra implements Ref, Good, Document, Contract, PriceOrder, Invoice, Subject, Company, Person, Reseller, Group, Customer, Account, Subscription, Equipment, Region, Address { private static Integer DEFAULT_FIRM = 100 HID hid + String user + private String password def firmId def resellerId - DelegateExecution execution SimpleLogger logger Hydra(DelegateExecution execution) { - this.execution = execution - this.logger = new SimpleLogger(this.execution) - this.hid = new HID(execution) + this.logger = new SimpleLogger(execution) + this.hid = new HID(execution) - def user = execution.getVariable('hydraUser') ?: 'hydra' - def password = execution.getVariable('hydraPassword') + this.user = execution.getVariable('hydraUser') ?: 'hydra' + this.password = execution.getVariable('hydraPassword') this.firmId = execution.getVariable('hydraFirmId') ?: (execution.getVariable('homsOrderDataFirmId') ?: getDefaultFirmId()) this.resellerId = execution.getVariable('hydraResellerId') ?: execution.getVariable('homsOrderDataResellerId') this.hid.execute('MAIN.INIT', [ vch_VC_IP : '127.0.0.1', - vch_VC_USER : user, - vch_VC_PASS : password, + vch_VC_USER : this.user, + vch_VC_PASS : this.password, vch_VC_APP_CODE : 'NETSERV_HID', vch_VC_CLN_APPID: 'HydraOMS' ]) diff --git a/src/org/camunda/latera/bss/connectors/hid/hydra/Company.groovy b/src/org/camunda/latera/bss/connectors/hid/hydra/Company.groovy index 63c19dc9..7ff46582 100644 --- a/src/org/camunda/latera/bss/connectors/hid/hydra/Company.groovy +++ b/src/org/camunda/latera/bss/connectors/hid/hydra/Company.groovy @@ -15,7 +15,7 @@ trait Company { def getCompaniesTable() { return COMPANIES_TABLE } - + LinkedHashMap getCompany(def companyId) { LinkedHashMap where = [ n_subject_id: companyId diff --git a/src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy b/src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy index 8181efef..c57c78e1 100644 --- a/src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy +++ b/src/org/camunda/latera/bss/connectors/hoper/Hydra.groovy @@ -22,12 +22,10 @@ class Hydra implements Main, Entity, Subject, Person, Company, Address, Customer Hoper hoper def firmId def resellerId - DelegateExecution execution SimpleLogger logger Hydra(DelegateExecution execution) { - this.execution = execution - this.logger = new SimpleLogger(this.execution) + this.logger = new SimpleLogger(execution) this.hoper = new Hoper(execution) this.firmId = execution.getVariable('hydraFirmId') ?: (execution.getVariable('homsOrderDataFirmId') ?: getDefaultFirmId())