@Transactional @RegisterActivity(type = UserActivityType.BILLING_MODEL_RAPPEL_MERGE) public Message<BillingModel> mergeRappelDetail(Rappel entity) { try { // TODO deberiamos tener un constructor de mensajes a partir de errores de validacion en lugar // de tomar solo el primer mensaje Set<ConstraintViolation<Rappel>> violations = validator.validate(entity); if (!violations.isEmpty()) { String messageKey = violations.iterator().next().getMessage(); return new Message<>(Message.CODE_GENERIC_ERROR, i18nService.getMessage(messageKey)); } EntityManager entityManager = entityManagerProvider.get(); BillingModel model = entityManager.find(BillingModel.class, entity.getModel().getId()); Rappel current; String message; if (entity.getId() == null) { current = new Rappel(); current.merge(entity); current.setModel(model); entityManager.persist(current); message = i18nService.getMessage("billingModel.rappel.persist"); } else { current = entityManager.find(Rappel.class, entity.getId()); current.merge(entity); entityManager.merge(current); message = i18nService.getMessage("billingModel.rappel.merge"); } auditService.processModified(model); return new Message<>(Message.CODE_SUCCESS, message, model); } catch (Exception ex) { return new Message<>( Message.CODE_GENERIC_ERROR, i18nService.getMessage("billingModel.rappel.merge")); } }
@Transactional @RegisterActivity(type = UserActivityType.BILLING_MODEL_RAPPEL_REMOVE) public Message<BillingModel> removeRappelDetail(Long primaryKey) { try { Validate.notNull(primaryKey); EntityManager entityManager = entityManagerProvider.get(); Rappel rappel = entityManager.find(Rappel.class, primaryKey); Validate.notNull(rappel); BillingModel model = rappel.getModel(); entityManager.remove(rappel); entityManager.flush(); entityManager.refresh(model); auditService.processModified(model); entityManager.merge(model); entityManager.flush(); String message = i18nService.getMessage("billingModel.rappel.remove"); return new Message<>(Message.CODE_SUCCESS, message, model); } catch (Exception ex) { return new Message<>( Message.CODE_GENERIC_ERROR, i18nService.getMessage("billingModel.rappel.error.remove")); } }