Search in sources :

Example 56 with Security

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

the class ContactMechServices method deleteContactMech.

/**
 * Deletes a ContactMech
 * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_DELETE 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> deleteContactMech(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");
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_DELETE");
    if (result.size() > 0) {
        return result;
    }
    // never delete a contact mechanism, just put a to date on the link to the party
    String contactMechId = (String) context.get("contactMechId");
    GenericValue partyContactMech = null;
    try {
        // try to find a PartyContactMech with a valid date range
        partyContactMech = EntityQuery.use(delegator).from("PartyContactMech").where("partyId", partyId, "contactMechId", contactMechId).orderBy("fromDate").filterByDate().queryFirst();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_delete_contact_info_read", UtilMisc.toMap("errMessage", e.getMessage()), locale));
    }
    if (partyContactMech == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_delete_contact_info_no_contact_found", locale));
    }
    partyContactMech.set("thruDate", UtilDateTime.nowTimestamp());
    try {
        partyContactMech.store();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_delete_contact_info_write", locale));
    }
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Security(org.apache.ofbiz.security.Security)

Example 57 with Security

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

the class ContactMechServices method createContactMech.

/**
 * Creates a ContactMech
 * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_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> createContactMech(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();
    List<GenericValue> toBeStored = new LinkedList<>();
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_CREATE");
    if (result.size() > 0) {
        return result;
    }
    String contactMechTypeId = (String) context.get("contactMechTypeId");
    String newCmId = null;
    try {
        newCmId = delegator.getNextSeqId("ContactMech");
    } catch (IllegalArgumentException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_create_contact_info_id_generation_failure", locale));
    }
    GenericValue tempContactMech = delegator.makeValue("ContactMech", UtilMisc.toMap("contactMechId", newCmId, "contactMechTypeId", contactMechTypeId));
    toBeStored.add(tempContactMech);
    if (!"_NA_".equals(partyId)) {
        toBeStored.add(delegator.makeValue("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", newCmId, "fromDate", now, "roleTypeId", context.get("roleTypeId"), "allowSolicitation", context.get("allowSolicitation"), "extension", context.get("extension"))));
    }
    if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.service_createContactMech_not_be_used_for_POSTAL_ADDRESS", locale));
    } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.service_createContactMech_not_be_used_for_TELECOM_NUMBER", locale));
    } else {
        tempContactMech.set("infoString", context.get("infoString"));
    }
    try {
        delegator.storeAll(toBeStored);
    } catch (GenericEntityException e) {
        Debug.logWarning(e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_create_contact_info_write", UtilMisc.toMap("errMessage", e.getMessage()), locale));
    }
    result.put("contactMechId", newCmId);
    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 58 with Security

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

the class ContactMechServices method updateTelecomNumber.

/**
 * Updates a TelecomNumber
 * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_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> updateTelecomNumber(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();
    List<GenericValue> toBeStored = new LinkedList<>();
    boolean isModified = false;
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_UPDATE");
    if (result.size() > 0) {
        return result;
    }
    String newCmId = null;
    try {
        newCmId = delegator.getNextSeqId("ContactMech");
    } catch (IllegalArgumentException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_change_contact_info_id_generation_failure", locale));
    }
    String contactMechId = (String) context.get("contactMechId");
    GenericValue contactMech = null;
    GenericValue partyContactMech = null;
    try {
        contactMech = EntityQuery.use(delegator).from("ContactMech").where("contactMechId", contactMechId).queryOne();
        // try to find a PartyContactMech with a valid date range
        partyContactMech = EntityQuery.use(delegator).from("PartyContactMech").where("partyId", partyId, "contactMechId", contactMechId).orderBy("fromDate").filterByDate().queryFirst();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        contactMech = null;
        partyContactMech = null;
    }
    if (contactMech == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_find_specified_contact_info_read", locale));
    }
    if (partyContactMech == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.cannot_update_specified_contact_info_not_corresponds", locale));
    }
    toBeStored.add(partyContactMech);
    // never change a contact mech, just create a new one with the changes
    GenericValue newContactMech = GenericValue.create(contactMech);
    GenericValue newPartyContactMech = GenericValue.create(partyContactMech);
    GenericValue relatedEntityToSet = null;
    if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) {
        GenericValue telNum = null;
        try {
            telNum = EntityQuery.use(delegator).from("TelecomNumber").where("contactMechId", contactMechId).queryOne();
        } catch (GenericEntityException e) {
            Debug.logWarning(e.toString(), module);
            telNum = null;
        }
        relatedEntityToSet = GenericValue.create(telNum);
        relatedEntityToSet.set("countryCode", context.get("countryCode"));
        relatedEntityToSet.set("areaCode", context.get("areaCode"));
        relatedEntityToSet.set("contactNumber", context.get("contactNumber"));
        if (telNum == null || !relatedEntityToSet.equals(telNum)) {
            isModified = true;
        }
        relatedEntityToSet.set("contactMechId", newCmId);
        newPartyContactMech.set("extension", context.get("extension"));
    } else {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_update_contact_as_TELECOM_NUMBER_specified", UtilMisc.toMap("contactMechTypeId", contactMech.getString("contactMechTypeId")), locale));
    }
    newPartyContactMech.set("roleTypeId", context.get("roleTypeId"));
    newPartyContactMech.set("allowSolicitation", context.get("allowSolicitation"));
    if (!newContactMech.equals(contactMech)) {
        isModified = true;
    }
    if (!newPartyContactMech.equals(partyContactMech)) {
        isModified = true;
    }
    toBeStored.add(newContactMech);
    toBeStored.add(newPartyContactMech);
    if (isModified) {
        toBeStored.add(relatedEntityToSet);
        newContactMech.set("contactMechId", newCmId);
        newPartyContactMech.set("contactMechId", newCmId);
        newPartyContactMech.set("fromDate", now);
        newPartyContactMech.set("thruDate", null);
        try {
            Iterator<GenericValue> partyContactMechPurposes = UtilMisc.toIterator(partyContactMech.getRelated("PartyContactMechPurpose", null, null, false));
            while (partyContactMechPurposes != null && partyContactMechPurposes.hasNext()) {
                GenericValue tempVal = GenericValue.create(partyContactMechPurposes.next());
                tempVal.set("contactMechId", newCmId);
                toBeStored.add(tempVal);
            }
        } catch (GenericEntityException e) {
            Debug.logWarning(e.toString(), module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_change_contact_info_read", UtilMisc.toMap("errMessage", e.getMessage()), locale));
        }
        partyContactMech.set("thruDate", now);
        try {
            delegator.storeAll(toBeStored);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.toString(), module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_change_contact_info_write", UtilMisc.toMap("errMessage", e.getMessage()), locale));
        }
    } else {
        result.put("newContactMechId", contactMechId);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
        result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resourceError, "contactmechservices.no_changes_made_not_updating", locale));
        return result;
    }
    result.put("newContactMechId", newCmId);
    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 59 with Security

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

the class ContactMechServices method createPartyContactMechPurpose.

// ============================================================================
// ============================================================================
/**
 * Creates a PartyContactMechPurpose
 * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_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> createPartyContactMechPurpose(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");
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_CREATE");
    String errMsg = null;
    Locale locale = (Locale) context.get("locale");
    if (result.size() > 0) {
        return result;
    }
    // required parameters
    String contactMechId = (String) context.get("contactMechId");
    String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId");
    Timestamp fromDate = (Timestamp) context.get("fromDate");
    GenericValue tempVal = null;
    try {
        tempVal = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate("contactFromDate", "contactThruDate", "purposeFromDate", "purposeThruDate").queryFirst();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        tempVal = null;
    }
    if (UtilValidate.isEmpty(fromDate)) {
        fromDate = UtilDateTime.nowTimestamp();
    }
    if (tempVal != null) {
        // exists already with valid date, show warning
        errMsg = UtilProperties.getMessage(resourceError, "contactmechservices.could_not_create_new_purpose_already_exists", locale);
        errMsg += ": " + tempVal.getPrimaryKey().toString();
        return ServiceUtil.returnError(errMsg);
    }
    // no entry with a valid date range exists, create new with open thruDate
    GenericValue newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", fromDate));
    try {
        delegator.create(newPartyContactMechPurpose);
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_add_purpose_write", UtilMisc.toMap("errMessage", e.getMessage()), locale));
    }
    result.put("fromDate", fromDate);
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp)

Example 60 with Security

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

the class ContactMechServices method deletePartyContactMechPurposeIfExists.

public static Map<String, Object> deletePartyContactMechPurposeIfExists(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");
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_DELETE");
    if (result.size() > 0) {
        return result;
    }
    // required parameters
    String contactMechId = (String) context.get("contactMechId");
    String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId");
    GenericValue tempVal = null;
    try {
        tempVal = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate("contactFromDate", "contactThruDate", "purposeFromDate", "purposeThruDate").queryFirst();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        tempVal = null;
    }
    if (tempVal != null) {
        Map<String, Object> deletePcmCtx = UtilMisc.toMap("contactMechId", context.get("contactMechId"));
        deletePcmCtx.put("contactMechPurposeTypeId", context.get("contactMechPurposeTypeId"));
        deletePcmCtx.put("fromDate", tempVal.get("purposeFromDate"));
        deletePcmCtx.put("userLogin", context.get("userLogin"));
        deletePcmCtx.put("partyId", partyId);
        try {
            Map<String, Object> deletePcmResult = ctx.getDispatcher().runSync("deletePartyContactMechPurpose", deletePcmCtx);
            if (ServiceUtil.isError(deletePcmResult)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(deletePcmResult));
            }
        } catch (GenericServiceException e) {
            Debug.logWarning(e.getMessage(), module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_delete_purpose_from_contact_mechanism_read", UtilMisc.toMap("errMessage", e.getMessage()), locale));
        }
    }
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) Security(org.apache.ofbiz.security.Security)

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