Return-Path: Delivered-To: apmail-ofbiz-commits-archive@www.apache.org Received: (qmail 18701 invoked from network); 15 Feb 2010 18:01:39 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Feb 2010 18:01:39 -0000 Received: (qmail 92132 invoked by uid 500); 15 Feb 2010 15:14:58 -0000 Delivered-To: apmail-ofbiz-commits-archive@ofbiz.apache.org Received: (qmail 92053 invoked by uid 500); 15 Feb 2010 15:14:58 -0000 Mailing-List: contact commits-help@ofbiz.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ofbiz.apache.org Delivered-To: mailing list commits@ofbiz.apache.org Received: (qmail 92043 invoked by uid 99); 15 Feb 2010 15:14:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Feb 2010 15:14:58 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Feb 2010 15:14:57 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 9453623889ED; Mon, 15 Feb 2010 15:14:37 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r910235 - in /ofbiz/trunk/framework/common: servicedef/services_test.xml src/org/ofbiz/common/CommonServices.java Date: Mon, 15 Feb 2010 15:14:36 -0000 To: commits@ofbiz.apache.org From: bibryam@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100215151437.9453623889ED@eris.apache.org> Author: bibryam Date: Mon Feb 15 15:14:32 2010 New Revision: 910235 URL: http://svn.apache.org/viewvc?rev=910235&view=rev Log: Added a generic cascadeDelete service which can be used for data deletion. It uses recursion to delete all the related data entries. Modified: ofbiz/trunk/framework/common/servicedef/services_test.xml ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java Modified: ofbiz/trunk/framework/common/servicedef/services_test.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/servicedef/services_test.xml?rev=910235&r1=910234&r2=910235&view=diff ============================================================================== --- ofbiz/trunk/framework/common/servicedef/services_test.xml (original) +++ ofbiz/trunk/framework/common/servicedef/services_test.xml Mon Feb 15 15:14:32 2010 @@ -210,4 +210,11 @@ Test Ping Service + + + Remove generic value and its related values + + + Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java?rev=910235&r1=910234&r2=910235&view=diff ============================================================================== --- ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java (original) +++ ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java Mon Feb 15 15:14:32 2010 @@ -45,6 +45,7 @@ import org.apache.log4j.Logger; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilDateTime; +import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilValidate; import static org.ofbiz.base.util.UtilGenerics.checkList; @@ -54,6 +55,8 @@ import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.model.ModelEntity; +import org.ofbiz.entity.model.ModelRelation; +import org.ofbiz.entity.model.ModelViewEntity; import org.ofbiz.entity.transaction.TransactionUtil; import org.ofbiz.service.DispatchContext; import org.ofbiz.service.GenericServiceException; @@ -534,5 +537,42 @@ return ServiceUtil.returnError("Invalid count returned from database"); } } + + public static Map cascadeDelete(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + String entityName = (String) context.get("entityName"); + Map pkFields = UtilGenerics.checkMap(context.get("pkFields")); + + try { + GenericValue value = delegator.findByPrimaryKey(entityName, pkFields); + ModelEntity modelEntity = delegator.getModelEntity(entityName); + List relations = modelEntity.getRelationsManyList(); + + if (value == null || modelEntity instanceof ModelViewEntity) { + return ServiceUtil.returnSuccess(); + } + + for (ModelRelation relation : relations) { + String combinedName = relation.getCombinedName(); + List relatedValues = value.getRelated(combinedName); + for (GenericValue relatedValue : relatedValues) { + pkFields = relatedValue.getPrimaryKey().getAllFields(); + entityName = relatedValue.getEntityName(); + Map newContext = UtilMisc.toMap("entityName", entityName, "pkFields", pkFields); + Mapresult = CommonServices.cascadeDelete(dctx, newContext); + if (ServiceUtil.isError(result)) { + return result; + } + } + } + + Debug.logInfo("Removing value: " + value , module); + delegator.removeValue(value); + } catch (GenericEntityException e) { + return ServiceUtil.returnError(e.getMessage()); + } + + return ServiceUtil.returnSuccess(); + } }