svn commit: r586555 - /ofbiz/trunk/applications/party/src/org/ofbiz/party/party/PartyServices.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r586555 - /ofbiz/trunk/applications/party/src/org/ofbiz/party/party/PartyServices.java

apatel-2
Author: apatel
Date: Fri Oct 19 10:37:12 2007
New Revision: 586555

URL: http://svn.apache.org/viewvc?rev=586555&view=rev
Log:
fix link party. Now fin account and userlogin information is migrated when parties are linked.

Modified:
    ofbiz/trunk/applications/party/src/org/ofbiz/party/party/PartyServices.java

Modified: ofbiz/trunk/applications/party/src/org/ofbiz/party/party/PartyServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/party/src/org/ofbiz/party/party/PartyServices.java?rev=586555&r1=586554&r2=586555&view=diff
==============================================================================
--- ofbiz/trunk/applications/party/src/org/ofbiz/party/party/PartyServices.java (original)
+++ ofbiz/trunk/applications/party/src/org/ofbiz/party/party/PartyServices.java Fri Oct 19 10:37:12 2007
@@ -1390,6 +1390,36 @@
         String partyId = (String) context.get("partyId");
         Timestamp now = UtilDateTime.nowTimestamp();
 
+        if (partyIdTo.equals(partyId)) {
+            return ServiceUtil.returnError("Cannot link the same party with itself");
+        }
+
+        // get the from/to party records
+        GenericValue partyTo;
+        try {
+            partyTo = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyIdTo));
+        } catch (GenericEntityException e) {
+            Debug.log(e, module);
+            return ServiceUtil.returnError(e.getMessage());
+        }
+        if (partyTo == null) {
+            return ServiceUtil.returnError("Party To does not exist!");
+        }
+        if ("PARTY_DISABLED".equals(partyTo.get("statusId"))) {
+            return ServiceUtil.returnError("Cannot merge records into a disabled party!");
+        }
+
+        GenericValue party;
+        try {
+            party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
+        } catch (GenericEntityException e) {
+            Debug.log(e, module);
+            return ServiceUtil.returnError(e.getMessage());
+        }
+        if (party == null) {
+            return ServiceUtil.returnError("Party FROM does not exist!");
+        }
+
         // update the contact mech records
         try {
             delegator.storeByCondition("PartyContactMech", UtilMisc.<String, Object>toMap("partyId", partyIdTo, "thruDate", now),
@@ -1417,6 +1447,33 @@
             return ServiceUtil.returnError(e.getMessage());
         }
 
+        // update the inventory item(s)
+        try {
+            delegator.storeByCondition("InventoryItem", UtilMisc.toMap("ownerPartyId", partyIdTo),
+                    new EntityExpr("ownerPartyId", EntityOperator.EQUALS, partyId));
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+            return ServiceUtil.returnError(e.getMessage());
+        }
+
+        // update the subscription
+        try {
+            delegator.storeByCondition("Subscription", UtilMisc.toMap("partyId", partyIdTo),
+                    new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+            return ServiceUtil.returnError(e.getMessage());
+        }
+
+        // update the userLogin records
+        try {
+            delegator.storeByCondition("UserLogin", UtilMisc.toMap("partyId", partyIdTo),
+                    new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+            return ServiceUtil.returnError(e.getMessage());
+        }
+
         // update the non-existing party roles
         List rolesToMove;
         try {
@@ -1439,30 +1496,62 @@
                 return ServiceUtil.returnError(e.getMessage());
             }
         }
+
+        // update the order role records
+        try {
+            delegator.storeByCondition("OrderRole", UtilMisc.toMap("partyId", partyIdTo),
+                    new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+            return ServiceUtil.returnError(e.getMessage());
+        }
+
+        // invoice role
         try {
-            delegator.removeByAnd("PartyRole", UtilMisc.toMap("partyId", partyId));
+            delegator.storeByCondition("InvoiceRole", UtilMisc.toMap("partyId", partyIdTo),
+                    new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
         } catch (GenericEntityException e) {
             Debug.logError(e, module);
             return ServiceUtil.returnError(e.getMessage());
         }
 
+        // data resource role
         try {
-            delegator.storeByCondition("PartyRole", UtilMisc.toMap("partyId", partyIdTo),
+            delegator.storeByCondition("DataResourceRole", UtilMisc.toMap("partyId", partyIdTo),
                     new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
         } catch (GenericEntityException e) {
             Debug.logError(e, module);
             return ServiceUtil.returnError(e.getMessage());
         }
 
-        // update the order role records
+        // content role
         try {
-            delegator.storeByCondition("OrderRole", UtilMisc.toMap("partyId", partyIdTo),
+            delegator.storeByCondition("ContentRole", UtilMisc.toMap("partyId", partyIdTo),
+                    new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+            return ServiceUtil.returnError(e.getMessage());
+        }
+
+        // update the fin account
+        try {
+            delegator.storeByCondition("FinAccountRole", UtilMisc.toMap("partyId", partyIdTo),
                     new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
         } catch (GenericEntityException e) {
             Debug.logError(e, module);
             return ServiceUtil.returnError(e.getMessage());
         }
 
+        // TODO: there are a number of other places which may need to be updated
+
+        // remove all previous party roles
+        try {
+            delegator.removeByAnd("PartyRole", UtilMisc.toMap("partyId", partyId));
+        } catch (GenericEntityException e) {
+            Debug.logWarning(e, module);
+            // if this fails no problem
+        }
+
         // update the non-existing attributes
         List attrsToMove;
         try {
@@ -1492,18 +1581,31 @@
             return ServiceUtil.returnError(e.getMessage());
         }
 
-        // disable the party
-        Map disableResp = null;
+        // create a party link attribute
+        GenericValue linkAttr = delegator.makeValue("PartyAttribute");
+        linkAttr.set("partyId", partyId);
+        linkAttr.set("attrName", "LINKED_TO");
+        linkAttr.set("attrValue", partyIdTo);
         try {
-            disableResp = dispatcher.runSync("setPartyStatus", UtilMisc.toMap("partyId", partyId, "statusId", "PARTY_DISABLED", "userLogin", userLogin));
-        } catch (GenericServiceException e) {
+            delegator.create(linkAttr);
+        } catch (GenericEntityException e) {
             Debug.logError(e, module);
             return ServiceUtil.returnError(e.getMessage());
         }
-        if (disableResp != null && ServiceUtil.isError(disableResp)) {
-            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(disableResp));
-        }
 
+        // disable the party
+        String currentStatus = party.getString("statusId");
+        if (currentStatus == null || !"PARTY_DISABLED".equals(currentStatus)) {
+            party.set("statusId", "PARTY_DISABLED");
+
+            try {
+                party.store();
+            } catch (GenericEntityException e) {
+                Debug.logError(e, "Error setting disable mode on partyId: " + partyId, module);
+                return ServiceUtil.returnError(e.getMessage());
+            }
+        }
+                
         Map resp = ServiceUtil.returnSuccess();
         resp.put("partyId", partyIdTo);
         return resp;