public class SubscriptionServices {
public static final String module = SubscriptionServices.class.getName();
public static Map<String, Object> processExtendSubscription(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException{
GenericDelegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
String partyId = (String) context.get("partyId");
String subscriptionResourceId = (String) context.get("subscriptionResourceId");
String inventoryItemId = (String) context.get("inventoryItemId");
String roleTypeId = (String) context.get("useRoleTypeId");
GenericValue userLogin = (GenericValue) context.get("userLogin");
Integer useTime = (Integer) context.get("useTime");
String useTimeUomId = (String) context.get("useTimeUomId");
String alwaysCreateNewRecordStr = (String) context.get("alwaysCreateNewRecord");
boolean alwaysCreateNewRecord = !"N".equals(alwaysCreateNewRecordStr);
GenericValue lastSubscription = null;
try {
Map<String, String> subscriptionFindMap = UtilMisc.toMap("partyId", partyId, "subscriptionResourceId", subscriptionResourceId);
// if this subscription is attached to something the customer owns, filter by that too
if (UtilValidate.isNotEmpty(inventoryItemId)) subscriptionFindMap.put("inventoryItemId", inventoryItemId);
List<GenericValue> subscriptionList = delegator.findByAnd("Subscription", subscriptionFindMap);
// DEJ20070718 DON'T filter by date, we want to consider all subscriptions: List listFiltered = EntityUtil.filterByDate(subscriptionList, true);
List<GenericValue> listOrdered = EntityUtil.orderBy(subscriptionList, UtilMisc.toList("-fromDate"));
if (listOrdered.size() > 0) {
lastSubscription = listOrdered.get(0);
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.toString());
}
GenericValue newSubscription = null;
if (lastSubscription == null || alwaysCreateNewRecord) {
newSubscription = delegator.makeValue("Subscription");
newSubscription.set("subscriptionResourceId", subscriptionResourceId);
newSubscription.set("partyId", partyId);
newSubscription.set("roleTypeId", roleTypeId);
newSubscription.set("productId", context.get("productId"));
newSubscription.set("orderId", context.get("orderId"));
newSubscription.set("orderItemSeqId", context.get("orderItemSeqId"));
newSubscription.set("automaticExtend", context.get("automaticExtend"));
newSubscription.set("canclAutmExtTimeUomId", context.get("canclAutmExtTimeUomId"));
newSubscription.set("canclAutmExtTime", context.get("canclAutmExtTime"));
} else {
newSubscription = lastSubscription;
}
newSubscription.set("inventoryItemId", inventoryItemId);
Timestamp thruDate = lastSubscription != null ? (Timestamp) lastSubscription.get("thruDate") : null;
// set the fromDate, one way or another
if (thruDate == null) {
// no thruDate? start with NOW
thruDate = nowTimestamp;
newSubscription.set("fromDate", nowTimestamp);
} else {
// there is a thru date... if it is in the past, bring it up to NOW before adding on the time period
// don't want to penalize for skipping time, in other words if they had a subscription last year for a
// month and buy another month, we want that second month to start now and not last year
if (thruDate.before(nowTimestamp)) {
thruDate = nowTimestamp;
}
newSubscription.set("fromDate", thruDate);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(thruDate);
int[] times = UomWorker.uomTimeToCalTime(useTimeUomId);
if (times != null) {
calendar.add(times[0], (useTime.intValue() * times[1]));
} else {
Debug.logWarning("Don't know anything about useTimeUomId [" + useTimeUomId + "], defaulting to month", module);
calendar.add(Calendar.MONTH, (useTime.intValue() * times[1]));
}
thruDate = new Timestamp(calendar.getTimeInMillis());
newSubscription.set("thruDate", thruDate);
Map<String, Object> result = ServiceUtil.returnSuccess();
try {
if (lastSubscription != null && !alwaysCreateNewRecord) {
Map<String, Object> updateSubscriptionMap = dctx.getModelService("updateSubscription").makeValid(newSubscription, ModelService.IN_PARAM);
updateSubscriptionMap.put("userLogin", delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", "system")));
Map<String, Object> updateSubscriptionResult = dispatcher.runSync("updateSubscription", updateSubscriptionMap);
result.put("subscriptionId", updateSubscriptionMap.get("subscriptionId"));
if (ServiceUtil.isError(updateSubscriptionResult)) {
return ServiceUtil.returnError("Error processing subscription update with ID [" + updateSubscriptionMap.get("subscriptionId") + "]", null, null, updateSubscriptionResult);
}
} else {
Map<String, Object> createPartyRoleMap = FastMap.newInstance();
if (UtilValidate.isNotEmpty(roleTypeId)) {
createPartyRoleMap.put("partyId", partyId);
createPartyRoleMap.put("roleTypeId", roleTypeId);
createPartyRoleMap.put("userLogin", userLogin);
Map<String, Object> createPartyRoleResult = dispatcher.runSync("createPartyRole", createPartyRoleMap);
if (ServiceUtil.isError(createPartyRoleResult)) {
return ServiceUtil.returnError("Error creating new PartyRole while processing subscription update with resource ID [" + subscriptionResourceId + "]", null, null, createPartyRoleResult);
}
}
Map<String, Object> createSubscriptionMap = dctx.getModelService("createSubscription").makeValid(newSubscription, ModelService.IN_PARAM);
createSubscriptionMap.put("userLogin", delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", "system")));
Map<String, Object> createSubscriptionResult = dispatcher.runSync("createSubscription", createSubscriptionMap);
if (ServiceUtil.isError(createSubscriptionResult)) {
return ServiceUtil.returnError("Error creating subscription while processing with resource ID [" + subscriptionResourceId + "]", null, null, createSubscriptionResult);
}