ofbiz-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jler...@apache.org
Subject [ofbiz-framework] 09/26: Fixed: Specified key was too long; max key length is 767 bytes for ProductPromoCodeEmail entity.(OFBIZ-5426) (#44)
Date Sat, 04 Apr 2020 15:59:29 GMT
This is an automated email from the ASF dual-hosted git repository.

jleroux pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git

commit 4d2e5d3e26dfc5011084f8f34c430a52c9497df1
Author: Pawan Verma <verma-pawan@users.noreply.github.com>
AuthorDate: Mon Mar 23 17:08:58 2020 +0530

    Fixed: Specified key was too long; max key length is 767 bytes for ProductPromoCodeEmail
entity.(OFBIZ-5426) (#44)
    
    * Fixed: Specified key was too long; max key length is 767 bytes for ProductPromoCodeEmail
entity.
    (OFBIZ-5426)
    
    The problem is in the entity model. An email address should not be used in the primary
key - mainly because an email address is case-insensitive. A better design would be to use
the email address contact mechanism ID in the primary key.
    
    Done Following:
    1. Changed Entity Name from ProductPromoCodeEmail to ProductPromoCodeContMech
    2. Related Changes for the entity name change
    3. Migration service to migrate old data
    
    Thanks, Leon for the report and Adrian Crum, Jacques Le Roux, Ingo Wolfmayr, Deepak Dixit,
Pierre Smits and Gil Portenseigne for the discussion and review.
    
    * Improved: Added new line in service definition file and lincence in MigrationServices
file.
    (OFBIZ-5426)
    
    Thanks, Jacopo for the review.
---
 .../datamodel/entitydef/product-entitymodel.xml    | 24 ++++--
 .../catalog/promo/EditProductPromoCode.groovy      |  2 +-
 applications/product/ofbiz-component.xml           |  1 +
 .../product/servicedef/services_migration.xml      | 32 ++++++++
 .../product/servicedef/services_pricepromo.xml     |  8 +-
 .../ofbiz/product/migrate/MigrationServices.java   | 92 ++++++++++++++++++++++
 .../apache/ofbiz/product/promo/PromoServices.java  | 34 +++++++-
 .../template/promo/EditProductPromoCode.ftl        |  5 +-
 .../product/webapp/catalog/WEB-INF/controller.xml  |  2 +-
 .../webtools/groovyScripts/entity/XmlDsDump.groovy |  4 +-
 10 files changed, 186 insertions(+), 18 deletions(-)

diff --git a/applications/datamodel/entitydef/product-entitymodel.xml b/applications/datamodel/entitydef/product-entitymodel.xml
index 2ee09ec..e34749d 100644
--- a/applications/datamodel/entitydef/product-entitymodel.xml
+++ b/applications/datamodel/entitydef/product-entitymodel.xml
@@ -3640,7 +3640,7 @@ under the License.
         <key-map field-name="lastModifiedByUserLogin" rel-field-name="userLoginId"/>
       </relation>
     </entity>
-    <entity entity-name="ProductPromoCodeEmail"
+    <entity entity-name="OldProductPromoCodeEmail" table-name="PRODUCT_PROMO_CODE_EMAIL"
             package-name="org.apache.ofbiz.product.promo"
             title="Product Promotion Email">
       <field name="productPromoCodeId" type="id"></field>
@@ -3651,19 +3651,33 @@ under the License.
         <key-map field-name="productPromoCodeId"/>
       </relation>
     </entity>
+    <entity entity-name="ProdPromoCodeContactMech"
+            package-name="org.apache.ofbiz.product.promo"
+            title="Product Promotion ContactMech">
+        <field name="productPromoCodeId" type="id"></field>
+        <field name="contactMechId" type="id"></field>
+        <prim-key field="productPromoCodeId"/>
+        <prim-key field="contactMechId"/>
+        <relation type="one" fk-name="PROD_PRCDE_PCD" rel-entity-name="ProductPromoCode">
+            <key-map field-name="productPromoCodeId"/>
+        </relation>
+        <relation type="one" fk-name="PROD_PRCDE_CM" rel-entity-name="ContactMech">
+            <key-map field-name="contactMechId"/>
+        </relation>
+    </entity>
     <view-entity entity-name="ProductPromoCodeEmailParty"
             package-name="org.apache.ofbiz.product.promo"
             title="Product Promotion Email and Party View">
-      <member-entity entity-alias="PPCE" entity-name="ProductPromoCodeEmail"/>
+      <member-entity entity-alias="PPCCM" entity-name="ProdPromoCodeContactMech"/>
       <member-entity entity-alias="CM" entity-name="ContactMech"/>
       <member-entity entity-alias="PCM" entity-name="PartyContactMech"/>
-      <alias entity-alias="PPCE" name="productPromoCodeId"/>
+      <alias entity-alias="PPCCM" name="productPromoCodeId"/>
       <alias entity-alias="CM" name="infoString"/>
       <alias entity-alias="PCM" name="partyId"/>
       <alias entity-alias="PCM" name="fromDate"/>
       <alias entity-alias="PCM" name="thruDate"/>
-      <view-link entity-alias="PPCE" rel-entity-alias="CM">
-          <key-map field-name="emailAddress" rel-field-name="infoString"/>
+      <view-link entity-alias="PPCCM" rel-entity-alias="CM">
+          <key-map field-name="contactMechId"/>
       </view-link>
       <view-link entity-alias="CM" rel-entity-alias="PCM">
           <key-map field-name="contactMechId"/>
diff --git a/applications/product/groovyScripts/catalog/promo/EditProductPromoCode.groovy
b/applications/product/groovyScripts/catalog/promo/EditProductPromoCode.groovy
index ee9403c..085580a 100644
--- a/applications/product/groovyScripts/catalog/promo/EditProductPromoCode.groovy
+++ b/applications/product/groovyScripts/catalog/promo/EditProductPromoCode.groovy
@@ -38,7 +38,7 @@ if (productPromoId) {
 productPromoCodeEmails = null
 productPromoCodeParties = null
 if (productPromoCode) {
-    productPromoCodeEmails = productPromoCode.getRelated("ProductPromoCodeEmail", null, null,
false)
+    productPromoCodeEmails = productPromoCode.getRelated("ProdPromoCodeContactMech", null,
null, false)
     productPromoCodeParties = productPromoCode.getRelated("ProductPromoCodeParty", null,
null, false)
 }
 
diff --git a/applications/product/ofbiz-component.xml b/applications/product/ofbiz-component.xml
index 9a203d0..1cc86f0 100644
--- a/applications/product/ofbiz-component.xml
+++ b/applications/product/ofbiz-component.xml
@@ -42,6 +42,7 @@ under the License.
     <service-resource type="model" loader="main" location="servicedef/services_feature.xml"/>
     <service-resource type="model" loader="main" location="servicedef/services_inventory.xml"/>
     <service-resource type="model" loader="main" location="servicedef/services_maint.xml"/>
+    <service-resource type="model" loader="main" location="servicedef/services_migration.xml"/>
     <service-resource type="model" loader="main" location="servicedef/services_picklist.xml"/>
     <service-resource type="model" loader="main" location="servicedef/services_price.xml"/>
     <service-resource type="model" loader="main" location="servicedef/services_pricepromo.xml"/>
diff --git a/applications/product/servicedef/services_migration.xml b/applications/product/servicedef/services_migration.xml
new file mode 100644
index 0000000..56255d3
--- /dev/null
+++ b/applications/product/servicedef/services_migration.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+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.
+-->
+
+<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/services.xsd">
+    <description>Product Component Services</description>
+    <vendor>OFBiz</vendor>
+    <version>1.0</version>
+
+    <service name="migrateProductPromoCodeEmail" engine="java"
+             location="org.apache.ofbiz.product.migrate.MigrationServices" invoke="migrateProductPromoCodeEmail"
auth="true">
+        <description>Copy the ProductPromoCodeEmail entity to ProductPromoCodeContactMech</description>
+        <permission-service service-name="productPriceGenericPermission" main-action="CREATE"/>
+    </service>
+</services>
diff --git a/applications/product/servicedef/services_pricepromo.xml b/applications/product/servicedef/services_pricepromo.xml
index 86bbdb8..df0ee3d 100644
--- a/applications/product/servicedef/services_pricepromo.xml
+++ b/applications/product/servicedef/services_pricepromo.xml
@@ -212,14 +212,14 @@ under the License.
         <auto-attributes mode="IN" entity-name="ProductPromoCode" include="nonpk" optional="true"/>
     </service>
 
-    <service name="createProductPromoCodeEmail" default-entity-name="ProductPromoCodeEmail"
engine="entity-auto" invoke="create" auth="true">
-        <description>Create a ProductPromoCodeEmail</description>
+    <service name="createProductPromoCodeContactMech" default-entity-name="ProdPromoCodeContactMech"
engine="entity-auto" invoke="create" auth="true">
+        <description>Create a ProdPromoCodeContactMech</description>
         <permission-service service-name="productPriceGenericPermission" main-action="CREATE"/>
         <auto-attributes include="pk" mode="IN" optional="false"/>
         <auto-attributes include="nonpk" mode="IN" optional="true"/>
     </service>
-    <service name="deleteProductPromoCodeEmail" default-entity-name="ProductPromoCodeEmail"
engine="entity-auto" invoke="delete" auth="true">
-        <description>Delete a ProductPromoCodeEmail</description>
+    <service name="deleteProductPromoCodeContactMech" default-entity-name="ProdPromoCodeContactMech"
engine="entity-auto" invoke="delete" auth="true">
+        <description>Delete a ProdPromoCodeContactMech</description>
         <permission-service service-name="productPriceGenericPermission" main-action="DELETE"/>
         <auto-attributes include="pk" mode="IN" optional="false"/>
     </service>
diff --git a/applications/product/src/main/java/org/apache/ofbiz/product/migrate/MigrationServices.java
b/applications/product/src/main/java/org/apache/ofbiz/product/migrate/MigrationServices.java
new file mode 100644
index 0000000..9a50a9c
--- /dev/null
+++ b/applications/product/src/main/java/org/apache/ofbiz/product/migrate/MigrationServices.java
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * 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.
+ *******************************************************************************/
+package org.apache.ofbiz.product.migrate;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.UtilValidate;
+import org.apache.ofbiz.entity.Delegator;
+import org.apache.ofbiz.entity.GenericEntityException;
+import org.apache.ofbiz.entity.GenericValue;
+import org.apache.ofbiz.entity.util.EntityListIterator;
+import org.apache.ofbiz.entity.util.EntityQuery;
+import org.apache.ofbiz.service.DispatchContext;
+import org.apache.ofbiz.service.ServiceUtil;
+
+public class MigrationServices {
+    public static final String module = MigrationServices.class.getName();
+
+    public static Map<String, Object> migrateProductPromoCodeEmail(DispatchContext
dctx, Map<String, Object> context) {
+        Delegator delegator = dctx.getDelegator();
+        List<Object> errors = new LinkedList<>();
+        EntityQuery eq = EntityQuery.use(delegator).from("OldProductPromoCodeEmail");
+
+        try (EntityListIterator eli = eq.queryIterator()) {
+            GenericValue productPromoCodeEmail = null;
+            while ((productPromoCodeEmail = eli.next()) != null) {
+                String contactMechId;
+
+                String emailAddress = productPromoCodeEmail.getString("emailAddress");
+                if (!UtilValidate.isEmail(emailAddress)) {
+                    Debug.logError(emailAddress + ": is not a valid email address", module);
+                    errors.add(emailAddress + ": is not a valid email address ");
+                    continue;
+                }
+
+                long contactMechs = EntityQuery.use(delegator)
+                        .from("ContactMech")
+                        .where("infoString", emailAddress)
+                        .queryCount();
+                if (contactMechs > 1) {
+                    errors.add(emailAddress + ": Too many contactMechIds found ");
+                    continue;
+                }
+
+                GenericValue contactMech = EntityQuery.use(delegator)
+                        .from("ContactMech")
+                        .where("infoString", emailAddress)
+                        .queryOne();
+                if (contactMech == null) {
+                    //If no contactMech found create new
+                    GenericValue newContactMech = delegator.makeValue("ContactMech");
+                    contactMechId = delegator.getNextSeqId("ContactMech");
+                    newContactMech.set("contactMechId", contactMechId);
+                    newContactMech.set("contactMechTypeId", "EMAIL_ADDRESS");
+                    newContactMech.set("infoString", emailAddress);
+                    delegator.create(newContactMech);
+                } else {
+                    contactMechId = contactMech.getString("contactMechId");
+                }
+
+                GenericValue prodPromoCodeContMech = delegator.makeValue("ProdPromoCodeContactMech");
+                prodPromoCodeContMech.set("productPromoCodeId", productPromoCodeEmail.getString("productPromoCodeId"));
+                prodPromoCodeContMech.set("contactMechId", contactMechId);
+                //createOrStore to avoid duplicate data for same email.
+                delegator.createOrStore(prodPromoCodeContMech);
+            }
+
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+        }
+        return ServiceUtil.returnSuccess("Data has been migrated with following errors: "
+ errors);
+    }
+}
diff --git a/applications/product/src/main/java/org/apache/ofbiz/product/promo/PromoServices.java
b/applications/product/src/main/java/org/apache/ofbiz/product/promo/PromoServices.java
index f162e3b..c601844 100644
--- a/applications/product/src/main/java/org/apache/ofbiz/product/promo/PromoServices.java
+++ b/applications/product/src/main/java/org/apache/ofbiz/product/promo/PromoServices.java
@@ -255,9 +255,37 @@ public class PromoServices {
                 if (line.length() > 0 && !line.startsWith("#")) {
                     if (UtilValidate.isEmail(line)) {
                         // valid email address
-                        Map<String, Object> result = dispatcher.runSync("createProductPromoCodeEmail",
UtilMisc.<String, Object>toMap("productPromoCodeId",
-                                productPromoCodeId, "emailAddress", line, "userLogin", userLogin));
-                        if (result != null && ServiceUtil.isError(result)) {
+                        GenericValue contactMech;
+                        String contactMechId;
+                        try {
+                            //check for existing contactMechId
+                            contactMech = EntityQuery.use(dctx.getDelegator()).from("ContactMech")
+                                    .where("infoString", line).queryOne();
+                        } catch (GenericEntityException e) {
+                            Debug.logError(e, module);
+                            errors.add(line + ": Too many contactMechIds found ");
+                            continue;
+                        }
+                        Map<String, Object> result = new HashMap<>();
+                        if (contactMech == null) {
+                            //If no contactMech found create new
+                            result = dispatcher.runSync("createContactMech",
+                                    UtilMisc.toMap("contactMechTypeId", "EMAIL_ADDRESS",
"infoString", line,
+                                            "userLogin", userLogin));
+                            if (ServiceUtil.isError(result)) {
+                                errors.add(line + ": " + ServiceUtil.getErrorMessage(result));
+                                continue;
+                            } else {
+                                contactMechId = (String) result.get("contactMechId");
+                            }
+                        } else {
+                            contactMechId = contactMech.getString("contactMechId");
+                        }
+                        result.clear();
+                        result = dispatcher.runSync("createProductPromoCodeContactMech",
+                                UtilMisc.<String, Object>toMap("productPromoCodeId",
+                                productPromoCodeId, "contactMechId", contactMechId, "userLogin",
userLogin));
+                        if (ServiceUtil.isError(result)) {
                             errors.add(line + ": " + ServiceUtil.getErrorMessage(result));
                         }
                     } else {
diff --git a/applications/product/template/promo/EditProductPromoCode.ftl b/applications/product/template/promo/EditProductPromoCode.ftl
index ba791c1..40eb6a8 100644
--- a/applications/product/template/promo/EditProductPromoCode.ftl
+++ b/applications/product/template/promo/EditProductPromoCode.ftl
@@ -24,11 +24,12 @@ under the License.
         <div class="screenlet-body">
             <#list productPromoCodeEmails as productPromoCodeEmail>
               <div>
+                <#assign contactMech = EntityQuery.use(delegator).from("ContactMech").where("contactMechId",
productPromoCodeEmail.contactMechId).queryOne()!/>
                 <form name="deleteProductPromoCodeEmail_${productPromoCodeEmail_index}"
method="post" action="<@ofbizUrl>deleteProductPromoCodeEmail</@ofbizUrl>">
                   <input type="hidden" name="productPromoCodeId" value="${productPromoCodeEmail.productPromoCodeId}"/>
               
-                  <input type="hidden" name="emailAddress" value="${productPromoCodeEmail.emailAddress}"/>
               
+                  <input type="hidden" name="contactMechId" value="${productPromoCodeEmail.contactMechId}"/>
                   <input type="hidden" name="productPromoId" value="${productPromoId}"/>
               
-                  <a href='javascript:document.deleteProductPromoCodeEmail_${productPromoCodeEmail_index}.submit()'
class='buttontext'>${uiLabelMap.CommonRemove}</a>&nbsp;${productPromoCodeEmail.emailAddress}
+                  <a href='javascript:document.deleteProductPromoCodeEmail_${productPromoCodeEmail_index}.submit()'
class='buttontext'>${uiLabelMap.CommonRemove}</a>&nbsp;${contactMech.infoString!}
                 </form>
               </div>                
             </#list>
diff --git a/applications/product/webapp/catalog/WEB-INF/controller.xml b/applications/product/webapp/catalog/WEB-INF/controller.xml
index 496f99b..5ba2873 100644
--- a/applications/product/webapp/catalog/WEB-INF/controller.xml
+++ b/applications/product/webapp/catalog/WEB-INF/controller.xml
@@ -1965,7 +1965,7 @@ under the License.
     </request-map>
     <request-map uri="deleteProductPromoCodeEmail">
         <security https="true" auth="true"/>
-        <event type="service" path="" invoke="deleteProductPromoCodeEmail"/>
+        <event type="service" path="" invoke="deleteProductPromoCodeContactMech"/>
         <response name="success" type="view" value="EditProductPromoCode"/>
         <response name="error" type="view" value="EditProductPromoCode"/>
     </request-map>
diff --git a/framework/webtools/groovyScripts/entity/XmlDsDump.groovy b/framework/webtools/groovyScripts/entity/XmlDsDump.groovy
index 54844a8..c0a7fa0 100644
--- a/framework/webtools/groovyScripts/entity/XmlDsDump.groovy
+++ b/framework/webtools/groovyScripts/entity/XmlDsDump.groovy
@@ -86,7 +86,7 @@ if ("Product1".equals(preConfiguredSetName)) {
     passedEntityNames.add("ProductFeatureGroup")
     passedEntityNames.add("ProductPriceChange")
     passedEntityNames.add("ProductPromoAction")
-    passedEntityNames.add("ProductPromoCodeEmail")
+    passedEntityNames.add("ProdPromoCodeContactMech")
     passedEntityNames.add("ProductPromoCodeParty")
     passedEntityNames.add("ProductPromoCond")
 } else if ("Product4".equals(preConfiguredSetName)) {
@@ -128,7 +128,7 @@ if ("Product1".equals(preConfiguredSetName)) {
     passedEntityNames.add("ProductPromoProduct")
     passedEntityNames.add("ProductPromoRule")
     passedEntityNames.add("ProductPromoAction")
-    passedEntityNames.add("ProductPromoCodeEmail")
+    passedEntityNames.add("ProdPromoCodeContactMech")
     passedEntityNames.add("ProductPromoCodeParty")
     passedEntityNames.add("ProductPromoCond")
 


Mime
View raw message