[OFBiz] SVN: r6485 - in trunk/applications/party: servicedef src/org/ofbiz/party/contact

sichen@svn.ofbiz.org sichen at svn.ofbiz.org
Mon Jan 9 16:45:34 CST 2006


Author: sichen
Date: 2006-01-09 16:45:28 -0600 (Mon, 09 Jan 2006)
New Revision: 6485

Modified:
   trunk/applications/party/servicedef/services.xml
   trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java
Log:
Added service to copy all contact mechs of one party to another. This method will not delete or overwite any contact mechs. The copied ones are brand new contact mechs with a new contactMechId.

Modified: trunk/applications/party/servicedef/services.xml
===================================================================
--- trunk/applications/party/servicedef/services.xml	2006-01-09 22:35:20 UTC (rev 6484)
+++ trunk/applications/party/servicedef/services.xml	2006-01-09 22:45:28 UTC (rev 6485)
@@ -396,6 +396,12 @@
         <attribute name="contactMechId" type="String" mode="INOUT" optional="false"/>
         <attribute name="emailAddress" type="String" mode="IN" optional="false"/>
     </service>
+    <service name="copyPartyContactMechs" engine="java"
+        location="org.ofbiz.party.contact.ContactMechServices" invoke="copyPartyContactMechs" auth="true">
+        <description>Copies all contact mechs from the partyIdFrom to the partyIdTo. Does not delete or overwrite any contact mechs.</description>
+        <attribute name="partyIdFrom" type="String" mode="IN" optional="false"/>
+        <attribute name="partyIdTo" type="String" mode="IN" optional="false"/>
+    </service>
 
     <!-- contact mech attribute services -->
     <service name="createContactMechAttribute" engine="simple" default-entity-name="ContactMechAttribute"

Modified: trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java
===================================================================
--- trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java	2006-01-09 22:35:20 UTC (rev 6484)
+++ trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java	2006-01-09 22:45:28 UTC (rev 6485)
@@ -934,4 +934,77 @@
         result.put("valueMaps", valueMaps );
         return result;
     }
+
+    /**
+     * Copies all contact mechs from one party to another. Does not delete or overwrite any contact mechs.
+     */
+    public static Map copyPartyContactMechs(DispatchContext dctx, Map context) {
+        GenericDelegator delegator = dctx.getDelegator();
+        LocalDispatcher dispatcher = dctx.getDispatcher();
+        Security security = dctx.getSecurity();
+        GenericValue userLogin = (GenericValue) context.get("userLogin");
+        Locale locale = (Locale) context.get("locale");
+
+        String partyIdFrom = (String) context.get("partyIdFrom");
+        String partyIdTo = (String) context.get("partyIdTo");
+
+        try {
+            // grab all of the non-expired contact mechs using this party worker method
+            List valueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, partyIdFrom, false);
+
+            // loop through results 
+            for (Iterator iter = valueMaps.iterator(); iter.hasNext(); ) {
+                Map thisMap = (Map) iter.next();
+                GenericValue contactMech = (GenericValue) thisMap.get("contactMech");
+                GenericValue partyContactMech = (GenericValue) thisMap.get("partyContactMech");
+                List partyContactMechPurposes = (List) thisMap.get("partyContactMechPurposes");
+                List toBeStored = new LinkedList();
+
+                // only one of these will be set
+                GenericValue postalAddress = (GenericValue) thisMap.get("postalAddress");
+                GenericValue telecomNumber = (GenericValue) thisMap.get("telecomNumber");
+
+                // generate the new contactMechId
+                String contactMechId = delegator.getNextSeqId("ContactMech");
+
+                // copy the contact mech
+                GenericValue contactMechNew = delegator.makeValue("ContactMech", UtilMisc.toMap("contactMechId", contactMechId));
+                contactMechNew.setNonPKFields(contactMech.getAllFields());
+                toBeStored.add(contactMechNew);
+
+                // copy the party contact mech for the partyIdTo effective now
+                GenericValue partyContactMechNew = delegator.makeValue("PartyContactMech", UtilMisc.toMap("partyId", partyIdTo, 
+                            "contactMechId", contactMechId, "fromDate", UtilDateTime.nowTimestamp()));
+                partyContactMechNew.setNonPKFields(partyContactMech.getAllFields());
+                toBeStored.add(partyContactMechNew);
+
+                // copy postal address or telecom number if exists
+                if (postalAddress != null) {
+                    GenericValue newPostalAddress = delegator.makeValue("PostalAddress", UtilMisc.toMap("contactMechId", contactMechId));
+                    newPostalAddress.setNonPKFields(postalAddress.getAllFields());
+                    toBeStored.add(newPostalAddress);
+                } else if (telecomNumber != null) {
+                    GenericValue newTelecomNumber = delegator.makeValue("TelecomNumber", UtilMisc.toMap("contactMechId", contactMechId));
+                    newTelecomNumber.setNonPKFields(telecomNumber.getAllFields());
+                    toBeStored.add(newTelecomNumber);
+                }
+
+                // loop through purposes and copy each 
+                for (Iterator piter = partyContactMechPurposes.iterator(); piter.hasNext(); ) {
+                    GenericValue purpose = (GenericValue) piter.next();
+                    Map input = UtilMisc.toMap("partyId", partyIdTo, "contactMechId", contactMechId, "fromDate", UtilDateTime.nowTimestamp());
+                    input.put("contactMechPurposeTypeId", purpose.getString("contactMechPurposeTypeId"));
+                    GenericValue newPurpose = delegator.makeValue("PartyContactMechPurpose", input); 
+                    toBeStored.add(newPurpose);
+                }
+
+                // store them all
+                delegator.storeAll(toBeStored);
+            }
+        } catch (GenericEntityException e) {
+            Debug.logError(e, e.getMessage(), module);
+            return ServiceUtil.returnError("Failed to copy contact mechs. Error: " + e.getMessage());
+        }
+        return ServiceUtil.returnSuccess();
+    }
 }



More information about the Svn mailing list