Search in sources :

Example 66 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project opennms by OpenNMS.

the class BusinessServiceDaoIT method verifyUniqueNameConstraint.

@Test()
@Transactional
public void verifyUniqueNameConstraint() {
    BusinessServiceEntity entity1 = new BusinessServiceEntityBuilder().name("Some Custom Name").reduceFunction(m_highestSeverity).toEntity();
    m_businessServiceDao.save(entity1);
    m_businessServiceDao.flush();
    BusinessServiceEntity entity2 = new BusinessServiceEntityBuilder().name("Some Custom Name").reduceFunction(m_highestSeverity).toEntity();
    m_businessServiceDao.save(entity2);
    // Should throw a ConstraintViolationException (name not unique)
    try {
        m_businessServiceDao.flush();
        fail("ConstraintViolationException must be thrown");
    } catch (final DataIntegrityViolationException e) {
    }
}
Also used : BusinessServiceEntity(org.opennms.netmgt.bsm.persistence.api.BusinessServiceEntity) BusinessServiceEntityBuilder(org.opennms.netmgt.bsm.test.BusinessServiceEntityBuilder) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 67 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-data-mongodb by spring-projects.

the class MongoChangeSetPersister method getPersistentState.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentState(java.lang.Class, java.lang.Object, org.springframework.data.crossstore.ChangeSet)
	 */
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, final ChangeSet changeSet) throws DataAccessException, NotFoundException {
    if (id == null) {
        log.debug("Unable to load MongoDB data for null id");
        return;
    }
    String collName = getCollectionNameForEntity(entityClass);
    final Document dbk = new Document();
    dbk.put(ENTITY_ID, id);
    dbk.put(ENTITY_CLASS, entityClass.getName());
    if (log.isDebugEnabled()) {
        log.debug("Loading MongoDB data for {}", dbk);
    }
    mongoTemplate.execute(collName, new CollectionCallback<Object>() {

        public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
            for (Document dbo : collection.find(dbk)) {
                String key = (String) dbo.get(ENTITY_FIELD_NAME);
                if (log.isDebugEnabled()) {
                    log.debug("Processing key: {}", key);
                }
                if (!changeSet.getValues().containsKey(key)) {
                    String className = (String) dbo.get(ENTITY_FIELD_CLASS);
                    if (className == null) {
                        throw new DataIntegrityViolationException("Unble to convert property " + key + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available");
                    }
                    Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
                    Object value = mongoTemplate.getConverter().read(clazz, dbo);
                    if (log.isDebugEnabled()) {
                        log.debug("Adding to ChangeSet: {}", key);
                    }
                    changeSet.set(key, value);
                }
            }
            return null;
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) Document(org.bson.Document) DataAccessException(org.springframework.dao.DataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 68 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project webofneeds by researchstudio-sat.

the class RestUserController method registerUser.

/**
 * Registers the specified user with password and an opional role.
 * Assumes values have already been checked for syntactic validity.
 * @param email
 * @param password
 * @param role
 * @throws UserAlreadyExistsException
 */
private void registerUser(String email, String password, String role) throws UserAlreadyExistsException {
    User user = userRepository.findByUsername(email);
    if (user != null) {
        throw new UserAlreadyExistsException();
    }
    try {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        user = new User(email, passwordEncoder.encode(password), role);
        user.setEmail(email);
        KeystorePasswordHolder keystorePassword = new KeystorePasswordHolder();
        // generate a password for the keystore and save it in the database, encrypted with a symmetric key
        // derived from the user's password
        keystorePassword.setPassword(KeystorePasswordUtils.generatePassword(KeystorePasswordUtils.KEYSTORE_PASSWORD_BYTES), password);
        // keystorePassword = keystorePasswordRepository.save(keystorePassword);
        // generate the keystore for the user
        KeystoreHolder keystoreHolder = new KeystoreHolder();
        try {
            // create the keystore if it doesnt exist yet
            keystoreHolder.getKeystore(keystorePassword.getPassword(password));
        } catch (Exception e) {
            throw new IllegalStateException("could not create keystore for user " + email);
        }
        // keystoreHolder = keystoreHolderRepository.save(keystoreHolder);
        user.setKeystorePasswordHolder(keystorePassword);
        user.setKeystoreHolder(keystoreHolder);
        userRepository.save(user);
    } catch (DataIntegrityViolationException e) {
        // username is already in database
        throw new UserAlreadyExistsException();
    }
}
Also used : User(won.owner.model.User) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) KeystoreHolder(won.owner.model.KeystoreHolder) UserAlreadyExistsException(won.owner.service.impl.UserAlreadyExistsException) KeystorePasswordHolder(won.owner.model.KeystorePasswordHolder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) URISyntaxException(java.net.URISyntaxException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserAlreadyExistsException(won.owner.service.impl.UserAlreadyExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 69 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project elastest-torm by elastest.

the class TestLinkService method syncTestCaseExec.

public void syncTestCaseExec(Execution exec, ExternalTestCase externalTestCase) {
    ExternalTestExecution externalTestExec = new ExternalTestExecution(new Long(0));
    externalTestExec.setExTestCase(externalTestCase);
    externalTestExec.setFields(this.getTestExecFields(exec));
    externalTestExec.setResult(exec.getStatus().name());
    externalTestExec.setExternalId(exec.getId().toString());
    externalTestExec.setExternalSystemId(this.getSystemId());
    try {
        externalTestExec = externalTestExecutionRepository.save(externalTestExec);
    } catch (DataIntegrityViolationException existException) {
        ExternalTestExecution savedTestExec = externalTestExecutionRepository.findByExternalIdAndExternalSystemId(externalTestExec.getExternalId(), externalTestExec.getExternalSystemId());
        externalTestExec.setId(savedTestExec.getId());
        externalTestExec.setExTJobExec(savedTestExec.getExTJobExec());
        externalTestExec.setMonitoringIndex(savedTestExec.getMonitoringIndex());
        externalTestExec = externalTestExecutionRepository.save(externalTestExec);
    }
}
Also used : ExternalTestExecution(io.elastest.etm.model.external.ExternalTestExecution) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 70 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project sw360portal by sw360.

the class Sw360ComponentService method createComponent.

public Component createComponent(Component component, User sw360User) throws TException {
    ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
    AddDocumentRequestSummary documentRequestSummary = sw360ComponentClient.addComponent(component, sw360User);
    if (documentRequestSummary.getRequestStatus() == AddDocumentRequestStatus.SUCCESS) {
        component.setId(documentRequestSummary.getId());
        return component;
    } else if (documentRequestSummary.getRequestStatus() == AddDocumentRequestStatus.DUPLICATE) {
        throw new DataIntegrityViolationException("sw360 component with name '" + component.getName() + "' already exists.");
    }
    return null;
}
Also used : AddDocumentRequestSummary(org.eclipse.sw360.datahandler.thrift.AddDocumentRequestSummary) ComponentService(org.eclipse.sw360.datahandler.thrift.components.ComponentService) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)117 Test (org.junit.Test)26 HashMap (java.util.HashMap)12 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Test (org.junit.jupiter.api.Test)10 Transactional (javax.transaction.Transactional)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 User (ca.corefacility.bioinformatics.irida.model.user.User)8 SQLException (java.sql.SQLException)8 ConstraintViolation (javax.validation.ConstraintViolation)8 HashSet (java.util.HashSet)7 Locale (java.util.Locale)6 ConstraintViolationException (javax.validation.ConstraintViolationException)6 Date (java.util.Date)5 List (java.util.List)5 Set (java.util.Set)5 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)4 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)4 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)4