Search in sources :

Example 11 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class UtilCacheEvents method clearAllEvent.

/**
 * An HTTP WebEvent handler that clears all caches
 * @param request The HTTP request object for the current JSP or Servlet request.
 * @param response The HTTP response object for the current JSP or Servlet request.
 * @return return an HTTP WebEvent handler that clears all caches
 */
public static String clearAllEvent(HttpServletRequest request, HttpServletResponse response) {
    String errMsg = "";
    Locale locale = UtilHttp.getLocale(request);
    Security security = (Security) request.getAttribute("security");
    if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
        errMsg = UtilProperties.getMessage(err_resource, "utilCacheEvents.permissionEdit", locale) + ".";
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    UtilCache.clearAllCaches();
    errMsg = UtilProperties.getMessage(err_resource, "utilCache.clearAllCaches", locale);
    request.setAttribute("_EVENT_MESSAGE_", errMsg + " (" + UtilDateTime.nowDateString("yyyy-MM-dd HH:mm:ss") + ").");
    return "success";
}
Also used : Locale(java.util.Locale) Security(org.apache.ofbiz.security.Security)

Example 12 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentMethodServices method updateCreditCard.

/**
 * Updates CreditCard and PaymentMethod entities according to the parameters passed in the context
 * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_UPDATE permission
 * @param ctx The DispatchContext that this service is operating in
 * @param context Map containing the input parameters
 * @return Map with the result of the service, the output parameters
 */
public static Map<String, Object> updateCreditCard(DispatchContext ctx, Map<String, Object> context) {
    Map<String, Object> result = new HashMap<>();
    Delegator delegator = ctx.getDelegator();
    Security security = ctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Timestamp now = UtilDateTime.nowTimestamp();
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE", "ACCOUNTING", "_UPDATE");
    if (result.size() > 0) {
        return result;
    }
    List<GenericValue> toBeStored = new LinkedList<>();
    boolean isModified = false;
    GenericValue paymentMethod = null;
    GenericValue newPm = null;
    GenericValue creditCard = null;
    GenericValue newCc = null;
    String paymentMethodId = (String) context.get("paymentMethodId");
    try {
        creditCard = EntityQuery.use(delegator).from("CreditCard").where("paymentMethodId", paymentMethodId).queryOne();
        paymentMethod = EntityQuery.use(delegator).from("PaymentMethod").where("paymentMethodId", paymentMethodId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingCreditCardUpdateReadFailure", locale) + e.getMessage());
    }
    if (creditCard == null || paymentMethod == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingCreditCardUpdateWithPaymentMethodId", locale) + paymentMethodId);
    }
    if (!paymentMethod.getString("partyId").equals(partyId) && !security.hasEntityPermission("PAY_INFO", "_UPDATE", userLogin) && !security.hasEntityPermission("ACCOUNTING", "_UPDATE", userLogin)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingCreditCardUpdateWithoutPermission", UtilMisc.toMap("partyId", partyId, "paymentMethodId", paymentMethodId), locale));
    }
    // do some more complicated/critical validation...
    List<String> messages = new LinkedList<>();
    // first remove all spaces from the credit card number
    String updatedCardNumber = StringUtil.removeSpaces((String) context.get("cardNumber"));
    if (updatedCardNumber.startsWith("*")) {
        // get the masked card number from the db
        String origCardNumber = creditCard.getString("cardNumber");
        int cardLength = origCardNumber.length() - 4;
        // use builder for better performance
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < cardLength; i++) {
            builder.append("*");
        }
        String origMaskedNumber = builder.append(origCardNumber.substring(cardLength)).toString();
        // compare the two masked numbers
        if (updatedCardNumber.equals(origMaskedNumber)) {
            updatedCardNumber = origCardNumber;
        }
    }
    context.put("cardNumber", updatedCardNumber);
    if (!UtilValidate.isCardMatch((String) context.get("cardType"), (String) context.get("cardNumber"))) {
        messages.add(UtilProperties.getMessage(resource, "AccountingCreditCardNumberInvalid", UtilMisc.toMap("cardType", (String) context.get("cardType"), "validCardType", UtilValidate.getCardType((String) context.get("cardNumber"))), locale));
    }
    if (!UtilValidate.isDateAfterToday((String) context.get("expireDate"))) {
        messages.add(UtilProperties.getMessage(resource, "AccountingCreditCardExpireDateBeforeToday", UtilMisc.toMap("expireDate", (String) context.get("expireDate")), locale));
    }
    if (messages.size() > 0) {
        return ServiceUtil.returnError(messages);
    }
    newPm = GenericValue.create(paymentMethod);
    toBeStored.add(newPm);
    newCc = GenericValue.create(creditCard);
    toBeStored.add(newCc);
    String newPmId = null;
    try {
        newPmId = delegator.getNextSeqId("PaymentMethod");
    } catch (IllegalArgumentException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingCreditCardUpdateIdGenerationFailure", locale));
    }
    newPm.set("partyId", partyId);
    newPm.set("fromDate", context.get("fromDate"), false);
    newPm.set("description", context.get("description"));
    // The following check is needed to avoid to reactivate an expired pm
    if (newPm.get("thruDate") == null) {
        newPm.set("thruDate", context.get("thruDate"));
    }
    newCc.set("companyNameOnCard", context.get("companyNameOnCard"));
    newCc.set("titleOnCard", context.get("titleOnCard"));
    newCc.set("firstNameOnCard", context.get("firstNameOnCard"));
    newCc.set("middleNameOnCard", context.get("middleNameOnCard"));
    newCc.set("lastNameOnCard", context.get("lastNameOnCard"));
    newCc.set("suffixOnCard", context.get("suffixOnCard"));
    newCc.set("cardType", context.get("cardType"));
    newCc.set("cardNumber", context.get("cardNumber"));
    newCc.set("expireDate", context.get("expireDate"));
    GenericValue newPartyContactMechPurpose = null;
    String contactMechId = (String) context.get("contactMechId");
    if (UtilValidate.isNotEmpty(contactMechId) && !"_NEW_".equals(contactMechId)) {
        // set the contactMechId on the credit card
        newCc.set("contactMechId", contactMechId);
    }
    if (!newCc.equals(creditCard) || !newPm.equals(paymentMethod)) {
        newPm.set("paymentMethodId", newPmId);
        newCc.set("paymentMethodId", newPmId);
        newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now));
        isModified = true;
    }
    if (UtilValidate.isNotEmpty(contactMechId) && !"_NEW_".equals(contactMechId)) {
        // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
        String contactMechPurposeTypeId = "BILLING_LOCATION";
        GenericValue tempVal = null;
        try {
            List<GenericValue> allPCWPs = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).queryList();
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "contactFromDate", "contactThruDate", true);
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "purposeFromDate", "purposeThruDate", true);
            tempVal = EntityUtil.getFirst(allPCWPs);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
            tempVal = null;
        }
        if (tempVal == null) {
            // no value found, create a new one
            newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now));
        }
    }
    if (isModified) {
        if (newPartyContactMechPurpose != null) {
            toBeStored.add(newPartyContactMechPurpose);
        }
        // set thru date on old paymentMethod
        paymentMethod.set("thruDate", now);
        toBeStored.add(paymentMethod);
        try {
            delegator.storeAll(toBeStored);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingCreditCardUpdateWriteFailure", locale) + e.getMessage());
        }
    } else {
        result.put("paymentMethodId", paymentMethodId);
        result.put("oldPaymentMethodId", paymentMethodId);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
        if (contactMechId == null || !"_NEW_".equals(contactMechId)) {
            result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "AccountingNoChangesMadeNotUpdatingCreditCard", locale));
        }
        return result;
    }
    result.put("oldPaymentMethodId", paymentMethodId);
    result.put("paymentMethodId", newCc.getString("paymentMethodId"));
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 13 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentMethodServices method updateCheckAccount.

public static Map<String, Object> updateCheckAccount(DispatchContext ctx, Map<String, ? extends Object> context) {
    Map<String, Object> result = new HashMap<>();
    Delegator delegator = ctx.getDelegator();
    Security security = ctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Timestamp now = UtilDateTime.nowTimestamp();
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE", "ACCOUNTING", "_UPDATE");
    if (result.size() > 0) {
        return result;
    }
    List<GenericValue> toBeStored = new LinkedList<>();
    boolean isModified = false;
    GenericValue paymentMethod = null;
    GenericValue newPm = null;
    GenericValue checkAccount = null;
    GenericValue newCa = null;
    String paymentMethodId = (String) context.get("paymentMethodId");
    try {
        checkAccount = EntityQuery.use(delegator).from("CheckAccount").where("paymentMethodId", paymentMethodId).queryOne();
        paymentMethod = EntityQuery.use(delegator).from("PaymentMethod").where("paymentMethodId", paymentMethodId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingCheckAccountCannotBeUpdated", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    if (checkAccount == null || paymentMethod == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingCheckAccountCannotBeUpdated", UtilMisc.toMap("errorString", paymentMethodId), locale));
    }
    if (!paymentMethod.getString("partyId").equals(partyId) && !security.hasEntityPermission("PAY_INFO", "_UPDATE", userLogin) && !security.hasEntityPermission("ACCOUNTING", "_UPDATE", userLogin)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingCheckAccountCannotBeUpdated", UtilMisc.toMap("partyId", partyId, "paymentMethodId", paymentMethodId), locale));
    }
    newPm = GenericValue.create(paymentMethod);
    toBeStored.add(newPm);
    newCa = GenericValue.create(checkAccount);
    toBeStored.add(newCa);
    String newPmId = null;
    try {
        newPmId = delegator.getNextSeqId("PaymentMethod");
    } catch (IllegalArgumentException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingCheckAccountCannotBeUpdated", locale));
    }
    newPm.set("partyId", partyId);
    newPm.set("paymentMethodTypeId", context.get("paymentMethodTypeId"));
    newPm.set("fromDate", context.get("fromDate"), false);
    newPm.set("description", context.get("description"));
    newCa.set("bankName", context.get("bankName"));
    newCa.set("routingNumber", context.get("routingNumber"));
    newCa.set("accountType", context.get("accountType"));
    newCa.set("accountNumber", context.get("accountNumber"));
    newCa.set("nameOnAccount", context.get("nameOnAccount"));
    newCa.set("companyNameOnAccount", context.get("companyNameOnAccount"));
    newCa.set("contactMechId", context.get("contactMechId"));
    if (!newCa.equals(checkAccount) || !newPm.equals(paymentMethod)) {
        newPm.set("paymentMethodId", newPmId);
        newCa.set("paymentMethodId", newPmId);
        newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now));
        isModified = true;
    }
    GenericValue newPartyContactMechPurpose = null;
    String contactMechId = (String) context.get("contactMechId");
    if (UtilValidate.isNotEmpty(contactMechId)) {
        // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
        String contactMechPurposeTypeId = "BILLING_LOCATION";
        GenericValue tempVal = null;
        try {
            List<GenericValue> allPCWPs = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).queryList();
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "contactFromDate", "contactThruDate", true);
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "purposeFromDate", "purposeThruDate", true);
            tempVal = EntityUtil.getFirst(allPCWPs);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
            tempVal = null;
        }
        if (tempVal == null) {
            // no value found, create a new one
            newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now));
        }
    }
    if (isModified) {
        // Debug.logInfo("yes, is modified", module);
        if (newPartyContactMechPurpose != null) {
            toBeStored.add(newPartyContactMechPurpose);
        }
        // set thru date on old paymentMethod
        paymentMethod.set("thruDate", now);
        toBeStored.add(paymentMethod);
        try {
            delegator.storeAll(toBeStored);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingCheckAccountCannotBeUpdated", UtilMisc.toMap("errorString", e.getMessage()), locale));
        }
    } else {
        result.put("paymentMethodId", paymentMethodId);
        result.put("oldPaymentMethodId", paymentMethodId);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
        result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "AccountingCheckAccountCannotBeUpdated", locale));
        return result;
    }
    result.put("paymentMethodId", newCa.getString("paymentMethodId"));
    result.put("oldPaymentMethodId", paymentMethodId);
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 14 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentMethodServices method createCreditCard.

/**
 * Creates CreditCard and PaymentMethod entities according to the parameters passed in the context
 * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_CREATE permission
 * @param ctx The DispatchContext that this service is operating in
 * @param context Map containing the input parameters
 * @return Map with the result of the service, the output parameters
 */
public static Map<String, Object> createCreditCard(DispatchContext ctx, Map<String, Object> context) {
    Map<String, Object> result = new HashMap<>();
    Delegator delegator = ctx.getDelegator();
    Security security = ctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Timestamp now = UtilDateTime.nowTimestamp();
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE", "ACCOUNTING", "_CREATE");
    if (result.size() > 0) {
        return result;
    }
    // do some more complicated/critical validation...
    List<String> messages = new LinkedList<>();
    // first remove all spaces from the credit card number
    context.put("cardNumber", StringUtil.removeSpaces((String) context.get("cardNumber")));
    if (!UtilValidate.isCardMatch((String) context.get("cardType"), (String) context.get("cardNumber"))) {
        messages.add(UtilProperties.getMessage(resource, "AccountingCreditCardNumberInvalid", UtilMisc.toMap("cardType", (String) context.get("cardType"), "validCardType", UtilValidate.getCardType((String) context.get("cardNumber"))), locale));
    }
    if (!UtilValidate.isDateAfterToday((String) context.get("expireDate"))) {
        messages.add(UtilProperties.getMessage(resource, "AccountingCreditCardExpireDateBeforeToday", UtilMisc.toMap("expireDate", (String) context.get("expireDate")), locale));
    }
    if (messages.size() > 0) {
        return ServiceUtil.returnError(messages);
    }
    List<GenericValue> toBeStored = new LinkedList<>();
    GenericValue newPm = delegator.makeValue("PaymentMethod");
    toBeStored.add(newPm);
    GenericValue newCc = delegator.makeValue("CreditCard");
    toBeStored.add(newCc);
    String newPmId = (String) context.get("paymentMethodId");
    if (UtilValidate.isEmpty(newPmId)) {
        try {
            newPmId = delegator.getNextSeqId("PaymentMethod");
        } catch (IllegalArgumentException e) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingCreditCardCreateIdGenerationFailure", locale));
        }
    }
    newPm.set("partyId", partyId);
    newPm.set("description", context.get("description"));
    newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now));
    newPm.set("thruDate", context.get("thruDate"));
    newCc.set("companyNameOnCard", context.get("companyNameOnCard"));
    newCc.set("titleOnCard", context.get("titleOnCard"));
    newCc.set("firstNameOnCard", context.get("firstNameOnCard"));
    newCc.set("middleNameOnCard", context.get("middleNameOnCard"));
    newCc.set("lastNameOnCard", context.get("lastNameOnCard"));
    newCc.set("suffixOnCard", context.get("suffixOnCard"));
    newCc.set("cardType", context.get("cardType"));
    newCc.set("cardNumber", context.get("cardNumber"));
    newCc.set("expireDate", context.get("expireDate"));
    newPm.set("paymentMethodId", newPmId);
    newPm.set("paymentMethodTypeId", "CREDIT_CARD");
    newCc.set("paymentMethodId", newPmId);
    GenericValue newPartyContactMechPurpose = null;
    String contactMechId = (String) context.get("contactMechId");
    if (UtilValidate.isNotEmpty(contactMechId) && !"_NEW_".equals(contactMechId)) {
        // set the contactMechId on the credit card
        newCc.set("contactMechId", context.get("contactMechId"));
        // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
        String contactMechPurposeTypeId = "BILLING_LOCATION";
        GenericValue tempVal = null;
        try {
            List<GenericValue> allPCWPs = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).queryList();
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "contactFromDate", "contactThruDate", true);
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "purposeFromDate", "purposeThruDate", true);
            tempVal = EntityUtil.getFirst(allPCWPs);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
            tempVal = null;
        }
        if (tempVal == null) {
            // no value found, create a new one
            newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now));
        }
    }
    if (newPartyContactMechPurpose != null) {
        toBeStored.add(newPartyContactMechPurpose);
    }
    try {
        delegator.storeAll(toBeStored);
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingCreditCardCreateWriteFailure", locale) + e.getMessage());
    }
    result.put("paymentMethodId", newCc.getString("paymentMethodId"));
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 15 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentMethodServices method createCheckAccount.

public static Map<String, Object> createCheckAccount(DispatchContext ctx, Map<String, ? extends Object> context) {
    Map<String, Object> result = new HashMap<>();
    Delegator delegator = ctx.getDelegator();
    Security security = ctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Timestamp now = UtilDateTime.nowTimestamp();
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE", "ACCOUNTING", "_CREATE");
    if (result.size() > 0) {
        return result;
    }
    List<GenericValue> toBeStored = new LinkedList<>();
    GenericValue newPm = delegator.makeValue("PaymentMethod");
    toBeStored.add(newPm);
    GenericValue newCa = delegator.makeValue("CheckAccount");
    toBeStored.add(newCa);
    String newPmId = (String) context.get("paymentMethodId");
    if (UtilValidate.isEmpty(newPmId)) {
        try {
            newPmId = delegator.getNextSeqId("PaymentMethod");
        } catch (IllegalArgumentException e) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingCheckNotAdded", locale));
        }
    }
    newPm.set("partyId", partyId);
    newPm.set("description", context.get("description"));
    newPm.set("paymentMethodTypeId", context.get("paymentMethodTypeId"));
    newPm.set("fromDate", now);
    newPm.set("paymentMethodId", newPmId);
    newCa.set("bankName", context.get("bankName"));
    newCa.set("routingNumber", context.get("routingNumber"));
    newCa.set("accountType", context.get("accountType"));
    newCa.set("accountNumber", context.get("accountNumber"));
    newCa.set("nameOnAccount", context.get("nameOnAccount"));
    newCa.set("companyNameOnAccount", context.get("companyNameOnAccount"));
    newCa.set("contactMechId", context.get("contactMechId"));
    newCa.set("paymentMethodId", newPmId);
    GenericValue newPartyContactMechPurpose = null;
    String contactMechId = (String) context.get("contactMechId");
    if (UtilValidate.isNotEmpty(contactMechId)) {
        // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
        String contactMechPurposeTypeId = "BILLING_LOCATION";
        GenericValue tempVal = null;
        try {
            List<GenericValue> allPCWPs = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).queryList();
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "contactFromDate", "contactThruDate", true);
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "purposeFromDate", "purposeThruDate", true);
            tempVal = EntityUtil.getFirst(allPCWPs);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
            tempVal = null;
        }
        if (tempVal == null) {
            // no value found, create a new one
            newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now));
        }
    }
    if (newPartyContactMechPurpose != null) {
        toBeStored.add(newPartyContactMechPurpose);
    }
    try {
        delegator.storeAll(toBeStored);
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingCheckNotAdded", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    result.put("paymentMethodId", newPm.getString("paymentMethodId"));
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Aggregations

Security (org.apache.ofbiz.security.Security)79 GenericValue (org.apache.ofbiz.entity.GenericValue)69 Delegator (org.apache.ofbiz.entity.Delegator)60 Locale (java.util.Locale)56 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)54 HashMap (java.util.HashMap)36 Timestamp (java.sql.Timestamp)27 LinkedList (java.util.LinkedList)27 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)20 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)18 Map (java.util.Map)12 HttpSession (javax.servlet.http.HttpSession)7 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 BigDecimal (java.math.BigDecimal)6 List (java.util.List)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)4 File (java.io.File)3