use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class ContactMechServices method updatePostalAddress.
/**
* Updates a PostalAddress
* <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> updatePostalAddress(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();
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
contactMech = null;
}
if (!"_NA_".equals(partyId)) {
// try to find a PartyContactMech with a valid date range
try {
partyContactMech = EntityQuery.use(delegator).from("PartyContactMech").where("partyId", partyId, "contactMechId", contactMechId).orderBy("fromDate").filterByDate().queryFirst();
if (partyContactMech == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.cannot_update_specified_contact_info_not_corresponds", locale));
}
toBeStored.add(partyContactMech);
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
contactMech = null;
}
}
if (contactMech == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_find_specified_contact_info_read", locale));
}
// never change a contact mech, just create a new one with the changes
GenericValue newContactMech = GenericValue.create(contactMech);
GenericValue newPartyContactMech = null;
if (partyContactMech != null) {
newPartyContactMech = GenericValue.create(partyContactMech);
}
GenericValue relatedEntityToSet = null;
if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) {
GenericValue addr = null;
try {
addr = EntityQuery.use(delegator).from("PostalAddress").where("contactMechId", contactMechId).queryOne();
} catch (GenericEntityException e) {
Debug.logWarning(e.toString(), module);
addr = null;
}
relatedEntityToSet = GenericValue.create(addr);
relatedEntityToSet.set("toName", context.get("toName"));
relatedEntityToSet.set("attnName", context.get("attnName"));
relatedEntityToSet.set("address1", context.get("address1"));
relatedEntityToSet.set("address2", context.get("address2"));
relatedEntityToSet.set("directions", context.get("directions"));
relatedEntityToSet.set("city", context.get("city"));
relatedEntityToSet.set("postalCode", context.get("postalCode"));
relatedEntityToSet.set("postalCodeExt", context.get("postalCodeExt"));
relatedEntityToSet.set("stateProvinceGeoId", context.get("stateProvinceGeoId"));
relatedEntityToSet.set("countryGeoId", context.get("countryGeoId"));
relatedEntityToSet.set("postalCodeGeoId", context.get("postalCodeGeoId"));
if (addr == null || !relatedEntityToSet.equals(addr)) {
isModified = true;
}
relatedEntityToSet.set("contactMechId", newCmId);
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_update_contact_as_POSTAL_ADDRESS_specified", UtilMisc.toMap("contactMechTypeId", contactMech.getString("contactMechTypeId")), locale));
}
if (newPartyContactMech != null) {
newPartyContactMech.set("roleTypeId", context.get("roleTypeId"));
newPartyContactMech.set("allowSolicitation", context.get("allowSolicitation"));
}
if (!newContactMech.equals(contactMech)) {
isModified = true;
}
if (newPartyContactMech != null && !newPartyContactMech.equals(partyContactMech)) {
isModified = true;
}
toBeStored.add(newContactMech);
if (newPartyContactMech != null) {
toBeStored.add(newPartyContactMech);
}
if (isModified) {
toBeStored.add(relatedEntityToSet);
newContactMech.set("contactMechId", newCmId);
if (newPartyContactMech != null) {
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;
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class ContactMechServices method updateContactMech.
/**
* Updates a ContactMech
* <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> updateContactMech(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();
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
contactMech = null;
}
if (!"_NA_".equals(partyId)) {
// try to find a PartyContactMech with a valid date range
try {
partyContactMech = EntityQuery.use(delegator).from("PartyContactMech").where("partyId", partyId, "contactMechId", contactMechId).orderBy("fromDate").filterByDate().queryFirst();
if (partyContactMech == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.cannot_update_specified_contact_info_not_corresponds", locale));
}
toBeStored.add(partyContactMech);
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
contactMech = null;
}
}
if (contactMech == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_find_specified_contact_info_read", locale));
}
String contactMechTypeId = contactMech.getString("contactMechTypeId");
// never change a contact mech, just create a new one with the changes
GenericValue newContactMech = GenericValue.create(contactMech);
GenericValue newPartyContactMech = GenericValue.create(partyContactMech);
if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.service_updateContactMech_not_be_used_for_POSTAL_ADDRESS", locale));
} else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.service_updateContactMech_not_be_used_for_TELECOM_NUMBER", locale));
} else {
newContactMech.set("infoString", context.get("infoString"));
}
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) {
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;
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class ContactMechServices method deletePartyContactMechPurpose.
/**
* Deletes the PartyContactMechPurpose corresponding to the parameters in the context
* <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> deletePartyContactMechPurpose(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");
Timestamp fromDate = (Timestamp) context.get("fromDate");
GenericValue pcmp = null;
try {
pcmp = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", fromDate).queryOne();
if (pcmp == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_delete_purpose_from_contact_mechanism_not_found", locale));
}
} catch (GenericEntityException 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));
}
pcmp.set("thruDate", UtilDateTime.nowTimestamp());
try {
pcmp.store();
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "contactmechservices.could_not_delete_purpose_from_contact_mechanism_write", UtilMisc.toMap("errMessage", e.getMessage()), locale));
}
result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "PartyContactMechanismSuccessfullyDeleted", locale));
return result;
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class ShoppingCartEvents method modifyCart.
/**
* Update the items in the shopping cart.
*/
public static String modifyCart(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
ShoppingCart cart = getCartObject(request);
Locale locale = UtilHttp.getLocale(request);
GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
Security security = (Security) request.getAttribute("security");
ShoppingCartHelper cartHelper = new ShoppingCartHelper(null, dispatcher, cart);
String controlDirective;
Map<String, Object> result;
Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
String removeSelectedFlag = request.getParameter("removeSelected");
String[] selectedItems = request.getParameterValues("selectedItem");
boolean removeSelected = ("true".equals(removeSelectedFlag) && selectedItems != null && selectedItems.length > 0);
result = cartHelper.modifyCart(security, userLogin, paramMap, removeSelected, selectedItems, locale);
controlDirective = processResult(result, request);
// Determine where to send the browser
if (controlDirective.equals(ERROR)) {
return "error";
}
return "success";
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class PartyRelationshipServices method createPartyRelationshipType.
/**
* Creates a PartyRelationshipType
*@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> createPartyRelationshipType(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");
ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_CREATE");
if (result.size() > 0) {
return result;
}
GenericValue partyRelationshipType = delegator.makeValue("PartyRelationshipType", UtilMisc.toMap("partyRelationshipTypeId", context.get("partyRelationshipTypeId")));
partyRelationshipType.set("parentTypeId", context.get("parentTypeId"), false);
partyRelationshipType.set("hasTable", context.get("hasTable"), false);
partyRelationshipType.set("roleTypeIdValidFrom", context.get("roleTypeIdValidFrom"), false);
partyRelationshipType.set("roleTypeIdValidTo", context.get("roleTypeIdValidTo"), false);
partyRelationshipType.set("description", context.get("description"), false);
partyRelationshipType.set("partyRelationshipName", context.get("partyRelationshipName"), false);
try {
if ((EntityQuery.use(delegator).from(partyRelationshipType.getEntityName()).where(partyRelationshipType.getPrimaryKey()).queryOne()) != null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyRelationshipTypeAlreadyExists", locale));
}
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyRelationshipTypeReadFailure", UtilMisc.toMap("errorString", e.getMessage()), locale));
}
try {
partyRelationshipType.create();
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyRelationshipTypeWriteFailure", UtilMisc.toMap("errorString", e.getMessage()), locale));
}
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
return result;
}
Aggregations