@Override protected ActionForward executeAction(final ActionContext context) throws Exception { final RemoveCustomFieldPossibleValueForm form = context.getForm(); final long id = form.getPossibleValueId(); if (id <= 0) { throw new ValidationException(); } final Nature nature = getNature(form); final Map<String, Object> params = new HashMap<String, Object>(); String key; try { final BaseCustomFieldService<CustomField> service = resolveService(nature); final CustomFieldPossibleValue possibleValue = service.loadPossibleValue(id); final CustomField customField = possibleValue.getField(); switch (customField.getNature()) { case PAYMENT: final PaymentCustomField paymentField = (PaymentCustomField) customField; params.put("transferTypeId", paymentField.getTransferType().getId()); break; case MEMBER_RECORD: final MemberRecordCustomField memberRecordField = (MemberRecordCustomField) customField; params.put("memberRecordTypeId", memberRecordField.getMemberRecordType().getId()); default: service.removePossibleValue(id); break; } key = "customField.possibleValue.removed"; } catch (final DaoException e) { key = "customField.possibleValue.error.removing"; } params.put("fieldId", form.getFieldId()); params.put("nature", nature); context.sendMessage(key); return ActionHelper.redirectWithParams( context.getRequest(), context.getSuccessForward(), params); }
private Collection<CustomFieldPossibleValue> resolveAllValues(final ActionContext context) { final EditCustomFieldPossibleValueForm form = context.getForm(); final CustomFieldPossibleValue possibleValue = getDataBinder().readFromString(form.getPossibleValue()); if (possibleValue.isTransient()) { // When inserting, multiple values may be created, one per line final String[] lines = StringUtils.split(form.getMultipleValues(), '\n'); final Collection<CustomFieldPossibleValue> possibleValues = new ArrayList<CustomFieldPossibleValue>(); for (String value : lines) { value = StringUtils.trimToNull(value); if (value == null) { continue; } // Get each possible value final CustomFieldPossibleValue current = (CustomFieldPossibleValue) possibleValue.clone(); current.setValue(value); possibleValues.add(current); } return possibleValues; } else { return Collections.singleton(possibleValue); } }
@Override protected ActionForward handleSubmit(final ActionContext context) throws Exception { final EditCustomFieldPossibleValueForm form = context.getForm(); final Collection<CustomFieldPossibleValue> resolveAllValues = resolveAllValues(context); Boolean isInsert = null; CustomField field = null; CustomFieldPossibleValue parentValue = null; Nature nature = getNature(form); final BaseCustomFieldService<CustomField> service = resolveService(nature); try { for (final CustomFieldPossibleValue possibleValue : resolveAllValues) { if (isInsert == null) { isInsert = possibleValue.getId() == null; field = service.load(possibleValue.getField().getId()); parentValue = possibleValue.getParent(); } service.save(possibleValue); } context.sendMessage( isInsert ? "customField.possibleValue.inserted" : "customField.possibleValue.modified"); final Map<String, Object> params = new HashMap<String, Object>(); params.put("nature", nature); params.put("fieldId", field.getId()); switch (field.getNature()) { case MEMBER_RECORD: final MemberRecordCustomField memberRecordField = (MemberRecordCustomField) field; final Long memberRecordTypeId = memberRecordField.getMemberRecordType().getId(); params.put("memberRecordTypeId", memberRecordTypeId); break; case PAYMENT: final PaymentCustomField paymentField = (PaymentCustomField) field; final TransferType transferType = paymentField.getTransferType(); params.put("transferTypeId", transferType.getId()); params.put("accountTypeId", transferType.getFrom().getId()); break; } if (parentValue != null) { params.put("parentValueId", parentValue.getId()); } return ActionHelper.redirectWithParams( context.getRequest(), context.getSuccessForward(), params); } catch (final DaoException e) { return context.sendError("customField.possibleValue.error.saving"); } }