Example usage for org.hibernate.exception ConstraintViolationException getConstraintName

List of usage examples for org.hibernate.exception ConstraintViolationException getConstraintName

Introduction

In this page you can find the example usage for org.hibernate.exception ConstraintViolationException getConstraintName.

Prototype

public String getConstraintName() 

Source Link

Document

Returns the name of the violated constraint, if known.

Usage

From source file:com.ah.bo.mgmt.impl.MapMgmtImpl.java

public HiveAp createMapLeafNode(HiveAp hiveAp, MapLeafNode node, MapContainerNode parent) throws Exception {
    if (parent == null || hiveAp == null) {
        return hiveAp;
    }//  w  w w  .  ja v a 2  s .c o m
    if (parent.getMapType() == MapContainerNode.MAP_TYPE_BUILDING) {
        throw new HmException(
                "create map leaf node for device: " + hiveAp.getId()
                        + " failed, the assigned map node cannot be a building.",
                "error.map.building.assign.device");
    }

    EntityManager em = null;
    EntityTransaction tx = null;

    try {
        em = QueryUtil.getEntityManager();
        tx = em.getTransaction();
        tx.begin();

        synchronized (BoMgmt.getHiveApMgmt()) {
            // Merge HiveAp.
            hiveAp.setMapContainer(parent);
            hiveAp = em.merge(hiveAp);

            // Persist MapLeafNode.
            node.setParentMap(parent);
            node.setOwner(parent.getOwner());
            node.setHiveAp(hiveAp);
            node.setApId(hiveAp.getMacAddress());
            node.setApName(hiveAp.getHostName());
            node.setSeverity(hiveAp.getSeverity());

            em.persist(node);
        }
        tx.commit();
        BoObserver.notifyListeners(new BoEvent<MapLeafNode>(node, BoEventType.CREATED));
        return hiveAp;
    } catch (RuntimeException e) {
        QueryUtil.rollback(tx);
        log.error("createMapLeafNode", "Create MapLeafNode object failed.", e);
        if (e instanceof OptimisticLockException && e.getCause() instanceof StaleObjectStateException) {
            throw new HmException("Update object " + hiveAp.getId() + " failed, stale object state.", e,
                    HmMessageCodes.STALE_OBJECT);
        } else if (e.getCause() instanceof ConstraintViolationException) {
            ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
            log.info("createMapLeafNode", "Constraint: " + cve.getConstraintName());
            throw new HmException("Update BO '" + hiveAp.getLabel() + "' failed.", e,
                    HmMessageCodes.CONSTRAINT_VIOLATION,
                    new String[] { hiveAp.getLabel(), cve.getConstraintName() });
        } else {
            throw e;
        }
    } finally {
        QueryUtil.closeEntityManager(em);
    }
}

From source file:com.ah.bo.mgmt.impl.MapMgmtImpl.java

public HiveAp removeMapLeafNode(HiveAp hiveAp) throws Exception {
    if (null == hiveAp) {
        return null;
    }//w  w  w . j  av  a  2s. c  o  m

    MapLeafNode oldMapLeafNode = hiveAp.findMapLeafNode();
    EntityManager em = null;
    EntityTransaction tx = null;

    try {
        em = QueryUtil.getEntityManager();
        tx = em.getTransaction();
        tx.begin();

        synchronized (this) {
            if (null != oldMapLeafNode) {
                oldMapLeafNode = em.find(MapLeafNode.class, oldMapLeafNode.getId());
            }
            removeMapLeafNode(em, oldMapLeafNode);

            synchronized (BoMgmt.getHiveApMgmt()) {
                hiveAp.setMapContainer(null);
                hiveAp = em.merge(hiveAp);
                tx.commit();
            }
        }

        if (oldMapLeafNode != null) {
            // We can not use the oldMapLeafNode object itself, because the
            // class name was enhanced by Hibernate. Just create a new node
            // and fill in the parent map, which is all that the
            // MapAlarmsCache uses.
            MapLeafNode mapLeafNode = new MapLeafNode();
            mapLeafNode.setParentMap(oldMapLeafNode.getParentMap());
            BoObserver.notifyListeners(new BoEvent<MapLeafNode>(mapLeafNode, BoEventType.REMOVED));
        }

        return hiveAp;
    } catch (RuntimeException e) {
        QueryUtil.rollback(tx);
        log.error("removeMapLeafNode", "Remove MapLeafNode object failed.", e);
        if (e instanceof OptimisticLockException && e.getCause() instanceof StaleObjectStateException) {
            throw new HmException("Update object " + hiveAp.getId() + " failed, stale object state.", e,
                    HmMessageCodes.STALE_OBJECT);
        } else if (e.getCause() instanceof ConstraintViolationException) {
            ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
            log.info("removeMapLeafNode", "Constraint: " + cve.getConstraintName());
            throw new HmException("Create BO '" + hiveAp.getLabel() + "' failed.", e,
                    HmMessageCodes.CONSTRAINT_VIOLATION,
                    new String[] { hiveAp.getLabel(), cve.getConstraintName() });
        } else {
            throw e;
        }
    } finally {
        QueryUtil.closeEntityManager(em);
    }
}

From source file:com.ah.bo.mgmt.impl.MapMgmtImpl.java

public HiveAp replaceMapLeafNode(HiveAp hiveAp, MapLeafNode newNode, MapContainerNode newParent)
        throws Exception {
    if (null == hiveAp || null == newNode || null == newParent) {
        return hiveAp;
    }/*from   ww w .  j ava 2 s . c om*/
    if (newParent.getMapType() == MapContainerNode.MAP_TYPE_BUILDING) {
        throw new HmException(
                "replace map leaf node for device: " + hiveAp.getId()
                        + " failed, the assigned map node cannot be a building.",
                "error.map.building.assign.device");
    }

    MapLeafNode oldMapLeafNode = hiveAp.findMapLeafNode();
    EntityManager em = null;
    EntityTransaction tx = null;

    try {
        em = QueryUtil.getEntityManager();
        tx = em.getTransaction();
        tx.begin();

        synchronized (this) {
            if (null != oldMapLeafNode) {
                oldMapLeafNode = em.find(MapLeafNode.class, oldMapLeafNode.getId());
            }
            // Remove the old MapLeafNode.
            removeMapLeafNode(em, oldMapLeafNode);

            synchronized (BoMgmt.getHiveApMgmt()) {
                // Merge the MapContainerNode object into HiveAp.
                hiveAp.setMapContainer(newParent);
                hiveAp = em.merge(hiveAp);

                // Persist the new MapLeafNode object into database.
                newNode.setParentMap(newParent);
                newNode.setOwner(newParent.getOwner());
                newNode.setHiveAp(hiveAp);
                newNode.setApId(hiveAp.getMacAddress());
                newNode.setApName(hiveAp.getHostName());
                newNode.setSeverity(hiveAp.getSeverity());

                em.persist(newNode);
            }
            tx.commit();
        }

        // Notify MapAlarmsCache objects
        if (oldMapLeafNode != null) {
            // We can not use the oldMapLeafNode object itself, because
            // the
            // class name was enhanced by Hibernate. Just create a new
            // node
            // and fill in the parent map, which is all that the
            // MapAlarmsCache uses.
            MapLeafNode mapLeafNode = new MapLeafNode();
            mapLeafNode.setParentMap(oldMapLeafNode.getParentMap());
            BoObserver.notifyListeners(new BoEvent<MapLeafNode>(mapLeafNode, BoEventType.REMOVED));
        }
        BoObserver.notifyListeners(new BoEvent<MapLeafNode>(newNode, BoEventType.CREATED));
        return hiveAp;
    } catch (RuntimeException e) {
        QueryUtil.rollback(tx);
        log.error("replaceMapLeafNode", "Replace MapLeafNode object failed.", e);
        if (e instanceof OptimisticLockException && e.getCause() instanceof StaleObjectStateException) {
            throw new HmException("Update object " + hiveAp.getId() + " failed, stale object state.", e,
                    HmMessageCodes.STALE_OBJECT);
        } else if (e.getCause() instanceof ConstraintViolationException) {
            ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
            log.info("replaceMapLeafNode", "Constraint: " + cve.getConstraintName());
            throw new HmException("Create BO '" + hiveAp.getLabel() + "' failed.", e,
                    HmMessageCodes.CONSTRAINT_VIOLATION,
                    new String[] { hiveAp.getLabel(), cve.getConstraintName() });
        } else {
            throw e;
        }
    } finally {
        QueryUtil.closeEntityManager(em);
    }
}

From source file:com.ah.bo.mgmt.impl.MapMgmtImpl.java

public HiveAp updateHiveAp(HiveAp hiveAp) throws Exception {
    if (null == hiveAp) {
        return null;
    }/* w w w.ja va  2  s.co m*/

    EntityManager em = null;
    EntityTransaction tx = null;
    MapLeafNode mapLeafNode = hiveAp.findMapLeafNode();

    try {
        em = QueryUtil.getEntityManager();
        tx = em.getTransaction();
        tx.begin();

        synchronized (this) {
            // Merge MapLeafNode object.
            if (null != mapLeafNode) {
                mapLeafNode = em.find(MapLeafNode.class, mapLeafNode.getId());
            }

            if (null != mapLeafNode && !hiveAp.getHostName().equals(mapLeafNode.getApName())) {
                mapLeafNode.setApName(hiveAp.getHostName());
                em.merge(mapLeafNode);
            }

            synchronized (BoMgmt.getHiveApMgmt()) {
                // Merge HiveAp object.
                HiveAp mergedHiveAp = em.merge(hiveAp);
                tx.commit();

                return mergedHiveAp;
            }
        }
    } catch (RuntimeException e) {
        QueryUtil.rollback(tx);
        log.error("updateHiveAp", "Update HiveAp object failed.", e);
        if (e instanceof OptimisticLockException && e.getCause() instanceof StaleObjectStateException) {
            throw new HmException("Update object " + hiveAp.getId() + " failed, stale object state.", e,
                    HmMessageCodes.STALE_OBJECT);
        } else if (e.getCause() instanceof ConstraintViolationException) {
            ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
            log.info("updateHiveAp", "Constraint: " + cve.getConstraintName());
            throw new HmException("Create BO '" + hiveAp.getLabel() + "' failed.", e,
                    HmMessageCodes.CONSTRAINT_VIOLATION,
                    new String[] { hiveAp.getLabel(), cve.getConstraintName() });
        } else {
            throw e;
        }
    } finally {
        QueryUtil.closeEntityManager(em);
    }
}

From source file:com.cgi.poc.dw.exception.mapper.CustomSQLConstraintViolationExceptionMapper.java

@Override
public Response toResponse(ConstraintViolationException exception) {
    Response response;//w w  w . java 2  s.  c o m
    ErrorInfo errRet = new ErrorInfo();
    String errorString = GeneralErrors.CONSTRAINT_VIOLATION.getMessage().replace("REPLACE",
            exception.getConstraintName());
    errRet.addError(GeneralErrors.CONSTRAINT_VIOLATION.getCode(),
            errorString + ": " + exception.getSQLException().getMessage());

    response = Response.noContent().status(Response.Status.BAD_REQUEST).entity(errRet).build();

    return response;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceConfigureDAO.java

License:Open Source License

/**
 * Create DeviceConfigure./*from   ww w  .j  av a  2  s.com*/
 * 
 * @param deviceConfigure
 * @return the DeviceConfigureEntity
 */
public DeviceConfigureEntity createDeviceConfigure(DeviceConfigureEntity deviceConfigure)
        throws AsmManagerCheckedException {

    Session session = null;
    Transaction tx = null;

    // Save the device in the db.
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        session.save(deviceConfigure);
        GregorianCalendar now = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
        deviceConfigure.setCreatedDate(now);
        deviceConfigure.setCreatedBy(_dao.extractUserFromRequest());
        // Commit transaction.
        tx.commit();
    } catch (ConstraintViolationException cve) {
        logger.warn("Caught exception during deviceConfigure entity creation: " + cve);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during deviceConfigure entity creation: " + ex);
        }
        if (cve.getConstraintName().contains("id")) {
            // throw new
            // AsmManagerDAOException(AsmManagerDAOException.REASON_CODE.DUPLICATE_JOBID,
            // cve);
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_JOBID,
                    AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage()));
        }
    } catch (Exception e) {
        logger.warn("Caught exception during deviceConfigure creation: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create deviceConfigure: " + ex);
        }

        throw new AsmManagerInternalErrorException("Error Creating deviceConfigure entity",
                "DeviceConfigureDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during create device configure: " + ex);
        }
    }

    return deviceConfigure;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceDiscoverDAO.java

License:Open Source License

/**
 * Create DeviceDiscover.//w  w w  . j a v a  2  s  .c  o  m
 * 
 * @param deviceDiscover
 * @return the DeviceDiscoverEntity
 */
public DeviceDiscoverEntity createDeviceDiscover(DeviceDiscoverEntity deviceDiscover)
        throws AsmManagerCheckedException {

    Session session = null;
    Transaction tx = null;

    // Save the device in the db.
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        GregorianCalendar now = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
        deviceDiscover.setCreatedDate(now);
        deviceDiscover.setCreatedBy(_dao.extractUserFromRequest());
        session.save(deviceDiscover);
        // Commit transaction.
        tx.commit();
    } catch (ConstraintViolationException cve) {
        logger.warn("Caught exception during DeviceDiscover entity creation: " + cve);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during device discover entity creation: " + ex);
        }
        if (cve.getConstraintName().contains("id")) {
            // throw new
            // AsmManagerDAOException(AsmManagerDAOException.REASON_CODE.DUPLICATE_JOBID,
            // cve);
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_JOBID,
                    AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage()));
        }
    } catch (Exception e) {
        logger.warn("Caught exception during deviceDiscover creation: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create deviceDiscover: " + ex);
        }

        throw new AsmManagerInternalErrorException("Error Creating deviceDiscover entity", "DeviceDiscoverDAO",
                e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during create device discover: " + ex);
        }
    }

    return deviceDiscover;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceGroupDAO.java

License:Open Source License

/**
 * Create Device Group /* www.  j  av a 2 s . c  om*/
 * 
 * @param entity - device group entity
 * 
 * @return entity - device group entity
 *
 * @throws AsmManagerCheckedException
 */
public DeviceGroupEntity createGroupDevice(DeviceGroupEntity entity) throws AsmManagerCheckedException {

    Session session = null;
    Transaction tx = null;
    DeviceGroupEntity duplicateEntity = null;

    try {
        duplicateEntity = getDeviceGroupByName(entity.getName());
    } catch (AsmManagerCheckedException ex) {
        logger.warn("Device Group with same name doesnt exists: " + ex);
    }

    if (null != duplicateEntity) {
        throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_RECORD,
                AsmManagerMessages.duplicateDeviceGroupName(entity.getName()));
    }

    // Save the device in the db.
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        logger.info("Creating device group in inventory: ");
        entity.setCreatedDate(new GregorianCalendar());
        entity.setUpdatedDate(new GregorianCalendar());
        entity.setCreatedBy(_dao.extractUserFromRequest());
        entity.setUpdatedBy(_dao.extractUserFromRequest());

        if (null == entity.getDeviceInventories())
            entity.setDeviceInventories(new ArrayList<DeviceInventoryEntity>());

        if (null == entity.getGroupsUsers())
            entity.setGroupsUsers(new HashSet<Long>());

        session.save(entity);

        // Commit transaction.
        tx.commit();
    } catch (ConstraintViolationException cve) {
        logger.warn("Caught exception during device group creation: " + cve);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create device group: " + ex);
        }
        if (cve.getConstraintName().contains("seqId")) {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_REFID,
                    AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage()));
        } else {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_RECORD,
                    AsmManagerMessages.duplicateRecord(cve.getSQLException().getMessage()));
        }
    } catch (Exception e) {
        logger.warn("Caught exception during device group inventory creation: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create device group: " + ex);
        }
        throw new AsmManagerInternalErrorException("Create device group", "DeviceGroupDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during create device group: " + ex);
        }
    }

    return entity;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryDAO.java

License:Open Source License

public DeviceInventoryEntity createDeviceInventoryForDiscoveryFailed(DeviceInventoryEntity device)
        throws AsmManagerCheckedException {

    Session session = null;//  ww  w . ja v a  2s.  co m
    Transaction tx = null;

    // Save the device in the db.
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        logger.info("Creating device in inventory: " + device.getRefId());

        device.setCreatedDate(new GregorianCalendar());
        device.setDiscoveredDate(new GregorianCalendar());
        device.setCreatedBy(_dao.extractUserFromRequest());
        device.setState(DeviceState.DISCOVERY_FAILED);
        session.save(device);

        // Commit transaction.
        tx.commit();
    } catch (ConstraintViolationException cve) {
        logger.warn("Caught exception during device inventory creation: " + cve);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create device: " + ex);
        }
        if (cve.getConstraintName().contains("refid")) {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_REFID,
                    AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage()));
        } else if (cve.getConstraintName().contains("service_tag")) {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_SERVICETAG,
                    AsmManagerMessages.duplicateServiceTag(cve.getSQLException().getMessage()));
        } else {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_RECORD,
                    AsmManagerMessages.duplicateRecord(cve.getSQLException().getMessage()));
        }
    } catch (Exception e) {
        logger.warn("Caught exception during device inventory creation: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create device: " + ex);
        }
        throw new AsmManagerInternalErrorException("Create devices", "DeviceInventoryDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during create device: " + ex);
        }
    }

    return device;

}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryDAO.java

License:Open Source License

/**
 * Create Device Inventory.//from  ww w .  j a  va  2s.c  om
 *
 * @param device
 * @return the entity
 */
public DeviceInventoryEntity createDeviceInventory(DeviceInventoryEntity device)
        throws AsmManagerCheckedException {

    Session session = null;
    Transaction tx = null;

    // Save the device in the db.
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        logger.info("Creating device in inventory: " + device.getRefId());

        device.setCreatedDate(new GregorianCalendar());
        device.setDiscoveredDate(new GregorianCalendar());
        device.setCreatedBy(_dao.extractUserFromRequest());
        device.setState(DeviceState.READY);
        session.save(device);

        // Commit transaction.
        tx.commit();
    } catch (ConstraintViolationException cve) {
        logger.warn("Caught exception during device inventory creation: " + cve);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create device: " + ex);
        }
        if (cve.getConstraintName().contains("refid")) {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_REFID,
                    AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage()));
        } else if (cve.getConstraintName().contains("service_tag")) {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_SERVICETAG,
                    AsmManagerMessages.duplicateServiceTag(cve.getSQLException().getMessage()));
        } else {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_RECORD,
                    AsmManagerMessages.duplicateRecord(cve.getSQLException().getMessage()));
        }
    } catch (Exception e) {
        logger.warn("Caught exception during device inventory creation: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create device: " + ex);
        }
        throw new AsmManagerInternalErrorException("Create devices", "DeviceInventoryDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during create device: " + ex);
        }
    }

    return device;
}