Example usage for org.springframework.dao DataIntegrityViolationException getLocalizedMessage

List of usage examples for org.springframework.dao DataIntegrityViolationException getLocalizedMessage

Introduction

In this page you can find the example usage for org.springframework.dao DataIntegrityViolationException getLocalizedMessage.

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:it.geosolutions.geostore.services.ResourceServiceImpl.java

@Override
public long insert(Resource resource)
        throws BadRequestServiceEx, NotFoundServiceEx, DuplicatedResourceNameServiceEx {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Persisting Resource ... ");
    }/* w  w w.j  a  v  a  2 s.co  m*/

    validateResourceName(resource, false);

    Category category = resource.getCategory();
    if (category == null) {
        throw new BadRequestServiceEx("Category type must be specified");
    }

    //
    // Searching the corresponding Category
    //
    Category loadedCategory = null;

    if (category.getId() != null) {
        loadedCategory = categoryDAO.find(category.getId());
        if (loadedCategory == null) {
            throw new NotFoundServiceEx("Resource Category not found [id:" + category.getId() + "]");
        }

    } else if (category.getName() != null) {
        Search searchCriteria = new Search(Category.class);
        searchCriteria.addFilterEqual("name", category.getName());

        List<Category> categories = categoryDAO.search(searchCriteria);
        if (categories.isEmpty()) {
            throw new NotFoundServiceEx("Resource Category not found [name:" + category.getName() + "]");
        }
        loadedCategory = categories.get(0);
    }

    Resource r = new Resource();
    r.setCreation(new Date());
    r.setDescription(resource.getDescription());
    r.setMetadata(resource.getMetadata());
    r.setName(resource.getName());
    r.setCategory(loadedCategory);

    try {
        resourceDAO.persist(r);
    } catch (DataIntegrityViolationException exc) {
        throw new BadRequestServiceEx(exc.getLocalizedMessage());
    }

    //
    // Persisting Attributes
    //
    List<Attribute> attributes = resource.getAttribute();
    if (attributes != null) {
        for (Attribute a : attributes) {
            a.setResource(r);
            attributeDAO.persist(a);
        }
    }

    //
    // Persisting StoredData
    //
    StoredData data = resource.getData();
    if (data != null) {
        data.setId(r.getId());
        data.setResource(r);
        storedDataDAO.persist(data);
    }

    //
    // Persisting SecurityRule
    //
    List<SecurityRule> rules = resource.getSecurity();

    if (rules != null) {
        for (SecurityRule rule : rules) {
            rule.setResource(r);
            securityDAO.persist(rule);
        }
    }

    return r.getId();
}

From source file:org.openmrs.web.controller.person.PersonAttributeTypeFormController.java

/**
 * The onSubmit function receives the form/command object that was modified by the input form
 * and saves it to the db// w w  w.j a  va  2 s.  c o  m
 *
 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
 *      org.springframework.validation.BindException)
 */
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
        BindException errors) throws Exception {

    HttpSession httpSession = request.getSession();

    String view = getFormView();

    if (Context.isAuthenticated()) {
        PersonAttributeType attrType = (PersonAttributeType) obj;
        PersonService ps = Context.getPersonService();
        try {
            if (request.getParameter("save") != null) {
                ps.savePersonAttributeType(attrType);
                view = getSuccessView();
                httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "PersonAttributeType.saved");
            }
            // if the user is retiring out the personAttributeType
            else if (request.getParameter("retire") != null) {
                String retireReason = request.getParameter("retireReason");
                if (attrType.getPersonAttributeTypeId() != null && !(StringUtils.hasText(retireReason))) {
                    errors.reject("retireReason", "general.retiredReason.empty");
                    return showForm(request, response, errors);
                }
                ps.retirePersonAttributeType(attrType, retireReason);
                httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                        "PersonAttributeType.retiredSuccessfully");
                view = getSuccessView();
            }
            // if the user is purging the personAttributeType
            else if (request.getParameter("purge") != null) {
                try {
                    ps.purgePersonAttributeType(attrType);
                    httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                            "PersonAttributeType.purgedSuccessfully");
                    view = getSuccessView();
                } catch (DataIntegrityViolationException e) {
                    httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                            "error.object.inuse.cannot.purge");
                    view = "personAttributeType.form?personAttributeTypeId="
                            + attrType.getPersonAttributeTypeId();
                }
            } else if (request.getParameter("unretire") != null) {
                ps.unretirePersonAttributeType(attrType);
                httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                        "PersonAttributeType.unretiredSuccessfully");
                view = getSuccessView();
            }
        } catch (PersonAttributeTypeLockedException e) {
            log.error("PersonAttributeType.locked", e);
            errors.reject(e.getMessage());
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "PersonAttributeType.locked");
            return showForm(request, response, errors);
        } catch (APIException e) {
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                    "error.general: " + e.getLocalizedMessage());
            view = "personAttributeType.form?personAttributeTypeId=" + attrType.getPersonAttributeTypeId();
        }
    }

    return new ModelAndView(new RedirectView(view));
}

From source file:org.openmrs.web.controller.patient.PatientIdentifierTypeFormController.java

/**
 * The onSubmit function receives the form/command object that was modified by the input form
 * and saves it to the db//from w w w. ja v  a 2s.  c  om
 *
 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
 *      org.springframework.validation.BindException)
 */
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
        BindException errors) throws Exception {

    HttpSession httpSession = request.getSession();

    String view = getFormView();

    ModelAndView toReturn = new ModelAndView(new RedirectView(view));

    if (Context.isAuthenticated()) {

        PatientIdentifierType identifierType = (PatientIdentifierType) obj;
        PatientService ps = Context.getPatientService();

        //to save the patient identifier type
        try {
            if (request.getParameter("save") != null) {
                ps.savePatientIdentifierType(identifierType);
                httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "PatientIdentifierType.saved");
                toReturn = new ModelAndView(new RedirectView(getSuccessView()));
            }
            // if the user is retiring the identifierType
            else if (request.getParameter("retire") != null) {
                String retireReason = request.getParameter("retireReason");
                if (identifierType.getPatientIdentifierTypeId() != null
                        && !(StringUtils.hasText(retireReason))) {
                    errors.reject("retireReason", "general.retiredReason.empty");
                    return showForm(request, response, errors);
                }
                ps.retirePatientIdentifierType(identifierType, retireReason);
                httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                        "PatientIdentifierType.retiredSuccessfully");
                toReturn = new ModelAndView(new RedirectView(getSuccessView()));
            }
            // if the user is purging the identifierType
            else if (request.getParameter("purge") != null) {
                try {
                    ps.purgePatientIdentifierType(identifierType);
                    httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                            "PatientIdentifierType.purgedSuccessfully");
                    toReturn = new ModelAndView(new RedirectView(getSuccessView()));
                } catch (DataIntegrityViolationException e) {
                    httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                            "error.object.inuse.cannot.purge");
                    return showForm(request, response, errors);
                }
            }
            // if the user unretiring patient identifier type
            else if (request.getParameter("unretire") != null) {
                ps.unretirePatientIdentifierType(identifierType);
                httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                        "PatientIdentifierType.unretiredSuccessfully");
                toReturn = new ModelAndView(new RedirectView(getSuccessView()));
            }
        } catch (PatientIdentifierTypeLockedException e) {
            log.error("PatientIdentifierType.locked", e);
            errors.reject(e.getMessage());
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "PatientIdentifierType.locked");
            return showForm(request, response, errors);
        } catch (APIException e) {
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                    "error.general: " + e.getLocalizedMessage());
            return showForm(request, response, errors);
        }
    }

    return toReturn;
}