From 95f0eda361ffe730cb8e960ea30e2a4fed720d36 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 17 Apr 2020 12:04:25 +0200 Subject: [PATCH] Improved: Convert LeadServices.xml mini lang to groovy (OFBIZ-11572) --- .../sfa/lead/LeadServices.groovy | 201 ++++++++++++++++ .../minilang/sfa/lead/LeadServices.xml | 221 ------------------ .../marketing/servicedef/services.xml | 4 +- .../minilang/party/PartySimpleMethods.xml | 1 + applications/party/servicedef/services.xml | 12 +- 5 files changed, 215 insertions(+), 224 deletions(-) create mode 100644 applications/marketing/groovyScripts/sfa/lead/LeadServices.groovy delete mode 100644 applications/marketing/minilang/sfa/lead/LeadServices.xml diff --git a/applications/marketing/groovyScripts/sfa/lead/LeadServices.groovy b/applications/marketing/groovyScripts/sfa/lead/LeadServices.groovy new file mode 100644 index 0000000000..9480ecf15e --- /dev/null +++ b/applications/marketing/groovyScripts/sfa/lead/LeadServices.groovy @@ -0,0 +1,201 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.sql.Timestamp + +import org.apache.ofbiz.base.util.StringUtil +import org.apache.ofbiz.base.util.UtilDateTime +import org.apache.ofbiz.base.util.UtilProperties +import org.apache.ofbiz.entity.GenericValue +import org.apache.ofbiz.minilang.SimpleMapProcessor +import org.apache.ofbiz.service.GenericServiceException +import org.apache.ofbiz.service.ServiceUtil + +def createLead() { + String leadContactPartyId + String partyGroupPartyId + // Check if Person or PartyGroup name is supplied + if ((!parameters.firstName || !parameters.lastName) && !parameters.groupName) { + return error(UtilProperties.getMessage("MarketingUiLabels", + "SfaFirstNameLastNameAndCompanyNameMissingError", locale)) + } + run service: "ensurePartyRole", with: [partyId: userLogin.partyId, + roleTypeId: "OWNER"] + // PartyRole check end + parameters.roleTypeId = "LEAD" + + Map serviceResult = run service: "createPersonRoleAndContactMechs", with: parameters + if (ServiceUtil.isError(serviceResult)) { + return serviceResult + } + leadContactPartyId = serviceResult.partyId + serviceResult = run service: "createPartyRelationship", with: [partyIdFrom: userLogin.partyId, + partyIdTo: leadContactPartyId, + roleTypeIdFrom: "OWNER", + roleTypeIdTo: "LEAD", + partyRelationshipTypeId: "LEAD_OWNER"] + if (ServiceUtil.isError(serviceResult)) { + return serviceResult + } + run service: "setPartyStatus", with: [partyId: leadContactPartyId, + statusId: "LEAD_ASSIGNED"] + + // Now create PartyGroup corresponding to the companyName, if its not null and then set up + // relationship of Person and PartyGroup as Employee and title + parameters.partyTypeId = "PARTY_GROUP" + if (!leadContactPartyId) { + parameters.roleTypeId = "ACCOUNT_LEAD" + // In case we have any contact mech data then associate with party group + serviceResult = run service: "createPartyGroupRoleAndContactMechs", with: parameters + if (ServiceUtil.isError(serviceResult)) { + return serviceResult + } + partyGroupPartyId = serviceResult.partyId + run service: "setPartyStatus", with: [partyId: partyGroupPartyId, + statusId: "LEAD_ASSIGNED"] + } else { + serviceResult = run service: "createPartyGroup", with: resolvePartyProcessMap() + if (ServiceUtil.isError(serviceResult)) { + return serviceResult + } + partyGroupPartyId = serviceResult.partyId + serviceResult = run service: "createPartyRole", with: [partyId: partyGroupPartyId, + roleTypeId: "ACCOUNT_LEAD"] + if (ServiceUtil.isError(serviceResult)) { + return serviceResult + } + } + if (leadContactPartyId) { + run service: "createPartyRelationship", with: [partyIdFrom: partyGroupPartyId, + partyIdTo: leadContactPartyId, + roleTypeIdFrom: "ACCOUNT_LEAD", + roleTypeIdTo: "LEAD", + positionTitle: parameters.title, + partyRelationshipTypeId: "EMPLOYMENT"] + } + run service: "createPartyRelationship", with: [partyIdFrom: userLogin.partyId, + partyIdTo: partyGroupPartyId, + roleTypeIdFrom: "OWNER", + roleTypeIdTo: "ACCOUNT_LEAD", + partyRelationshipTypeId: "LEAD_OWNER"] + + if (parameters.dataSourceId) { + serviceResult = run service: "createPartyDataSource", with: [partyId: leadContactPartyId, + dataSourceId: parameters.dataSourceId] + if (ServiceUtil.isError(serviceResult)) { + return serviceResult + } + } + Map result = success() + result.partyId = leadContactPartyId + result.partyGroupPartyId = partyGroupPartyId + result.roleTypeId = parameters.roleTypeId + result.successMessage = UtilProperties.getMessage("MarketingUiLabels", "SfaLeadCreatedSuccessfully", locale) + return result +} + +/** + * Convert a lead person into a contact and associated lead group to an account + * @return + */ +def convertLeadToContact() { + String partyId = parameters.partyId + String partyGroupId = parameters.partyGroupId + Timestamp nowTimestamp = UtilDateTime.nowTimestamp() + + GenericValue partyRelationship = from("PartyRelationship") + .where(partyIdTo: partyId, + roleTypeIdFrom: "OWNER", + roleTypeIdTo: "LEAD", + partyRelationshipTypeId: "LEAD_OWNER") + .filterByDate() + .orderBy("-fromDate") + .queryFirst() + if (partyRelationship) { + partyRelationship.thruDate = nowTimestamp + run service: "updatePartyRelationship", with: partyRelationship.getAllFields() + logInfo("Expiring relationship ${partyRelationship}") + } + + // Expire relation between lead company and lead person + partyRelationship = from("PartyRelationship") + .where(partyIdFrom: partyGroupId, roleTypeIdTo: "LEAD", roleTypeIdFrom: "ACCOUNT_LEAD", partyRelationshipTypeId: "EMPLOYMENT") + .filterByDate() + .orderBy("-fromDate") + .queryFirst() + if (partyRelationship) { + partyRelationship.thruDate = nowTimestamp + run service: "updatePartyRelationship", with: partyRelationship.getAllFields() + } + + // Expire relation between lead company and its owner + partyRelationship = from("PartyRelationship") + .where(partyIdFrom: userLogin.partyId, partyIdTo: partyGroupId, roleTypeIdTo: "ACCOUNT_LEAD", roleTypeIdFrom: "OWNER") + .filterByDate() + .orderBy("-fromDate") + .queryFirst() + if (partyRelationship) { + partyRelationship.thruDate = nowTimestamp + run service: "updatePartyRelationship", with: partyRelationship.getAllFields() + } + + run service: "ensurePartyRole", with: [partyId: partyGroupId, + roleTypeId: "ACCOUNT"] + + run service: "createPartyRelationship", with: [partyIdFrom: userLogin.partyId, + partyIdTo: partyGroupId, + roleTypeIdFrom: "OWNER", + roleTypeIdTo: "ACCOUNT", + partyRelationshipTypeId: "ACCOUNT"] + + run service: "setPartyStatus", with: [partyId: partyGroupId, + statusId: "LEAD_CONVERTED"] + + run service: "createPartyRole", with: [partyId: partyId, + roleTypeId: "CONTACT"] + + // create new relationship between new account and contact person there + run service: "createPartyRelationship", with: [partyIdFrom: partyGroupId, + roleTypeIdFrom: "ACCOUNT", + partyIdTo: partyId, + roleTypeIdTo: "CONTACT", + partyRelationshipTypeId: "EMPLOYMENT"] + + run service: "setPartyStatus", with: [partyId: partyId, + statusId: "LEAD_CONVERTED"] + + Map result = success() + result.partyId = partyId + result.partyGroupId = partyGroupId + result.successMessage = "Lead ${partyGroupId} ${partyId} succesfully converted to Account/Contact" + return result +} + +def resolvePartyProcessMap() { + List messages = [] + Map resultMap = [:] + //TODO convert map processor + SimpleMapProcessor.runSimpleMapProcessor('component://party/minilang/party/PartyMapProcs.xml', + 'partyGroup', parameters, resultMap, messages, context.locale) + // Check errors + if (messages) { + throw new GenericServiceException(messages.join(',')) + } + return resultMap +} \ No newline at end of file diff --git a/applications/marketing/minilang/sfa/lead/LeadServices.xml b/applications/marketing/minilang/sfa/lead/LeadServices.xml deleted file mode 100644 index d235fa30aa..0000000000 --- a/applications/marketing/minilang/sfa/lead/LeadServices.xml +++ /dev/null @@ -1,221 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/applications/marketing/servicedef/services.xml b/applications/marketing/servicedef/services.xml index ec5d244f5c..28a646b3bb 100644 --- a/applications/marketing/servicedef/services.xml +++ b/applications/marketing/servicedef/services.xml @@ -481,7 +481,7 @@ under the License. - + Sales Lead can be just a person or a person representing a company or a company (party group). createLead works 1) If person information is passed. 2) If company (party group) information is passed. 3) If Person and company (party group) information is passed. @@ -563,7 +563,7 @@ under the License. - + diff --git a/applications/party/minilang/party/PartySimpleMethods.xml b/applications/party/minilang/party/PartySimpleMethods.xml index a874f11dfb..ed7922a923 100644 --- a/applications/party/minilang/party/PartySimpleMethods.xml +++ b/applications/party/minilang/party/PartySimpleMethods.xml @@ -148,6 +148,7 @@ under the License. + diff --git a/applications/party/servicedef/services.xml b/applications/party/servicedef/services.xml index 07337ec876..0226f83f68 100644 --- a/applications/party/servicedef/services.xml +++ b/applications/party/servicedef/services.xml @@ -127,7 +127,17 @@ under the License. - + + Creates a person, role and contactMechs + + + + + + + + Update a Person