Author: jacopoc
Date: Sat Nov  8 09:38:09 2014
New Revision: 1637535

URL: http://svn.apache.org/r1637535
Log:
OFBIZ-5793 Enhanced logic to run product subscription expiry service only once 
when the subscription expires:
a) augment the OFBiz data model with the new field: 
Subscription.expirationCompletedDate
b) modify the algorithm of runServiceUponSubscriptionExpiry to also check 
whether the expiry service has already run, by checking that 
expirationCompletedDate is null.
if expirationCompletedDate is null (and the other conditions are satisfied), 
run the service in SubscriptionResource.serviceNameOnExpiry and update the 
date/time into expirationCompletedDate
if expirationCompletedDate is not null, skip the expired subscription and move 
to the next

Thanks to Ivan Cauchi for the patch (slightly modified by me).

Modified:
    ofbiz/trunk/applications/product/entitydef/entitymodel.xml
    
ofbiz/trunk/applications/product/src/org/ofbiz/product/subscription/SubscriptionServices.java
    ofbiz/trunk/applications/product/widget/catalog/SubscriptionForms.xml

Modified: ofbiz/trunk/applications/product/entitydef/entitymodel.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/entitydef/entitymodel.xml?rev=1637535&r1=1637534&r2=1637535&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/entitydef/entitymodel.xml (original)
+++ ofbiz/trunk/applications/product/entitydef/entitymodel.xml Sat Nov  8 
09:38:09 2014
@@ -4462,7 +4462,7 @@ under the License.
         <field name="canclAutmExtTimeUomId" type="id"><description>Unit Of 
Measure used for the automatic extension of the 
subscription.</description></field>
         <field name="gracePeriodOnExpiry" type="numeric"><description>The time 
period (before the end of the thruDate) after which the automatic extension of 
the subscription will be executed.</description></field>
         <field name="gracePeriodOnExpiryUomId" type="id"><description>Unit Of 
Measure used for the automatic extension of the 
subscription.</description></field>
-        <field name="serviceNameOnExpiry" 
type="long-varchar"><description>Name of service which will run on subscription 
expiration.</description></field>
+        <field name="expirationCompletedDate" 
type="date-time"><description>The date when expiration 
completed.</description></field>
         <prim-key field="subscriptionId"/>
         <relation type="one" fk-name="SUBSC_SRESRC" 
rel-entity-name="SubscriptionResource">
             <key-map field-name="subscriptionResourceId"/>

Modified: 
ofbiz/trunk/applications/product/src/org/ofbiz/product/subscription/SubscriptionServices.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/subscription/SubscriptionServices.java?rev=1637535&r1=1637534&r2=1637535&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/product/src/org/ofbiz/product/subscription/SubscriptionServices.java
 (original)
+++ 
ofbiz/trunk/applications/product/src/org/ofbiz/product/subscription/SubscriptionServices.java
 Sat Nov  8 09:38:09 2014
@@ -315,6 +315,7 @@ public class SubscriptionServices {
         String gracePeriodOnExpiry = null;
         String gracePeriodOnExpiryUomId = null;
         String subscriptionId = null;
+        Timestamp expirationCompletedDate = null;
         
         try {
             EntityCondition cond1 = 
EntityCondition.makeCondition("automaticExtend", EntityOperator.EQUALS, "N");
@@ -325,57 +326,61 @@ public class SubscriptionServices {
             
             if (subscriptionList != null) {
                 for (GenericValue subscription : subscriptionList) {
-                    Calendar currentDate = Calendar.getInstance();
-                    currentDate.setTime(UtilDateTime.nowTimestamp());
-                    // check if the thruDate + grace period (if provided) is 
earlier than today's date
-                    Calendar endDateSubscription = Calendar.getInstance();
-                    int field = Calendar.MONTH;
-                    String subscriptionResourceId = 
subscription.getString("subscriptionResourceId");
-                    GenericValue subscriptionResource = null;
-                    subscriptionResource = 
EntityQuery.use(delegator).from("SubscriptionResource").where("subscriptionResourceId",
 subscriptionResourceId).queryOne();
-                    subscriptionId = subscription.getString("subscriptionId");
-                    gracePeriodOnExpiry = 
subscription.getString("gracePeriodOnExpiry");
-                    gracePeriodOnExpiryUomId = 
subscription.getString("gracePeriodOnExpiryUomId");
-                    String serviceNameOnExpiry = 
subscriptionResource.getString("serviceNameOnExpiry");
-                    
endDateSubscription.setTime(subscription.getTimestamp("thruDate"));
-                    
-                    if (gracePeriodOnExpiry != null && 
gracePeriodOnExpiryUomId != null) {
-                        if ("TF_day".equals(gracePeriodOnExpiryUomId)) {
-                            field = Calendar.DAY_OF_YEAR;
-                        } else if ("TF_wk".equals(gracePeriodOnExpiryUomId)) {
-                            field = Calendar.WEEK_OF_YEAR;
-                        } else if ("TF_mon".equals(gracePeriodOnExpiryUomId)) {
-                            field = Calendar.MONTH;
-                        } else if ("TF_yr".equals(gracePeriodOnExpiryUomId)) {
-                            field = Calendar.YEAR;
-                        } else {
-                            Debug.logWarning("Don't know anything about 
gracePeriodOnExpiryUomId [" + gracePeriodOnExpiryUomId + "], defaulting to 
month", module);
-                        }
-                        endDateSubscription.add(field, 
Integer.valueOf(gracePeriodOnExpiry).intValue());
-                    }
-                    
-                    if ((currentDate.after(endDateSubscription) || 
currentDate.equals(endDateSubscription)) && serviceNameOnExpiry != null) {
-                        if (userLogin != null) {
-                            expiryMap.put("userLogin", userLogin);
-                        }
-                        if (subscriptionId != null) {
-                            expiryMap.put("subscriptionId", subscriptionId);
-                        }
-                        result = dispatcher.runSync(serviceNameOnExpiry, 
expiryMap);
-                        if (ServiceUtil.isSuccess(result)) {
-                            Debug.logInfo("Subscription expired successfully 
for subscription ID:" + subscriptionId, module);
-                        } else if (ServiceUtil.isError(result)) {
-                            result = null;
-                            Debug.logError("Error expiring subscription while 
processing with subscriptionId: " + subscriptionId, module);
+                       expirationCompletedDate = 
subscription.getTimestamp("expirationCompletedDate");
+                       if (expirationCompletedDate == null) {
+                               Calendar currentDate = Calendar.getInstance();
+                        currentDate.setTime(UtilDateTime.nowTimestamp());
+                        // check if the thruDate + grace period (if provided) 
is earlier than today's date
+                        Calendar endDateSubscription = Calendar.getInstance();
+                        int field = Calendar.MONTH;
+                        String subscriptionResourceId = 
subscription.getString("subscriptionResourceId");
+                        GenericValue subscriptionResource = null;
+                        subscriptionResource = 
EntityQuery.use(delegator).from("SubscriptionResource").where("subscriptionResourceId",
 subscriptionResourceId).queryOne();
+                        subscriptionId = 
subscription.getString("subscriptionId");
+                        gracePeriodOnExpiry = 
subscription.getString("gracePeriodOnExpiry");
+                        gracePeriodOnExpiryUomId = 
subscription.getString("gracePeriodOnExpiryUomId");
+                        String serviceNameOnExpiry = 
subscriptionResource.getString("serviceNameOnExpiry");
+                        
endDateSubscription.setTime(subscription.getTimestamp("thruDate"));
+                        
+                        if (gracePeriodOnExpiry != null && 
gracePeriodOnExpiryUomId != null) {
+                            if ("TF_day".equals(gracePeriodOnExpiryUomId)) {
+                                field = Calendar.DAY_OF_YEAR;
+                            } else if 
("TF_wk".equals(gracePeriodOnExpiryUomId)) {
+                                field = Calendar.WEEK_OF_YEAR;
+                            } else if 
("TF_mon".equals(gracePeriodOnExpiryUomId)) {
+                                field = Calendar.MONTH;
+                            } else if 
("TF_yr".equals(gracePeriodOnExpiryUomId)) {
+                                field = Calendar.YEAR;
+                            } else {
+                                Debug.logWarning("Don't know anything about 
gracePeriodOnExpiryUomId [" + gracePeriodOnExpiryUomId + "], defaulting to 
month", module);
+                            }
+                            endDateSubscription.add(field, 
Integer.valueOf(gracePeriodOnExpiry).intValue());
                         }
+                        if ((currentDate.after(endDateSubscription) || 
currentDate.equals(endDateSubscription)) && serviceNameOnExpiry != null) {
+                            if (userLogin != null) {
+                                expiryMap.put("userLogin", userLogin);
+                            }
+                            if (subscriptionId != null) {
+                                expiryMap.put("subscriptionId", 
subscriptionId);
+                            }
+                            result = dispatcher.runSync(serviceNameOnExpiry, 
expiryMap);
+                            if (ServiceUtil.isSuccess(result)) {
+                                subscription.set("expirationCompletedDate", 
UtilDateTime.nowTimestamp());
+                                delegator.store(subscription);
+                                Debug.logInfo("Subscription expired 
successfully for subscription ID:" + subscriptionId, module);
+                            } else if (ServiceUtil.isError(result)) {
+                                result = null;
+                                Debug.logError("Error expiring subscription 
while processing with subscriptionId: " + subscriptionId, module);
+                            }
 
-                        if (result != null && subscriptionId != null) {
-                            Debug.logInfo("Service mentioned in 
serviceNameOnExpiry called with result: " + result.get("successMessage"), 
module);
-                        } else if (result == null && subscriptionId != null) {
-                            Debug.logError("Subscription couldn't be expired 
for subscriptionId: " + subscriptionId, module);
-                            return ServiceUtil.returnError("Subscription 
couldn't be expired for subscriptionId: " + subscriptionId);
+                            if (result != null && subscriptionId != null) {
+                                Debug.logInfo("Service mentioned in 
serviceNameOnExpiry called with result: " + result.get("successMessage"), 
module);
+                            } else if (result == null && subscriptionId != 
null) {
+                                Debug.logError("Subscription couldn't be 
expired for subscriptionId: " + subscriptionId, module);
+                                return ServiceUtil.returnError("Subscription 
couldn't be expired for subscriptionId: " + subscriptionId);
+                            }
                         }
-                    }
+                       }
                 }
             }
         } catch (GenericServiceException e) {

Modified: ofbiz/trunk/applications/product/widget/catalog/SubscriptionForms.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/widget/catalog/SubscriptionForms.xml?rev=1637535&r1=1637534&r2=1637535&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/widget/catalog/SubscriptionForms.xml 
(original)
+++ ofbiz/trunk/applications/product/widget/catalog/SubscriptionForms.xml Sat 
Nov  8 09:38:09 2014
@@ -304,7 +304,6 @@ under the License.
                 </entity-options>
             </drop-down>
         </field>
-        <field name="serviceNameOnExpiry" ><ignored/></field>
         <field name="submitButton" title="${uiLabelMap.CommonUpdate}" 
widget-style="smallSubmit"><submit button-type="button"/></field>
     </form>
 


Reply via email to