Search in sources :

Example 76 with DataIntegrityViolationException

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

the class MongoPersistentEntityIndexCreator method createIndex.

void createIndex(IndexDefinitionHolder indexDefinition) {
    try {
        IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection());
        indexOperations.ensureIndex(indexDefinition);
    } catch (UncategorizedMongoDbException ex) {
        if (ex.getCause() instanceof MongoException && MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) {
            IndexInfo existingIndex = fetchIndexInformation(indexDefinition);
            String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.";
            if (existingIndex != null) {
                message += " Index already defined as '%s'.";
            }
            throw new DataIntegrityViolationException(String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex.getCause());
        }
        throw ex;
    }
}
Also used : MongoException(com.mongodb.MongoException) UncategorizedMongoDbException(org.springframework.data.mongodb.UncategorizedMongoDbException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 77 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project metacat by Netflix.

the class PolarisConnectorTableService method update.

/**
 * {@inheritDoc}.
 */
@Override
public void update(final ConnectorRequestContext requestContext, final TableInfo tableInfo) {
    final QualifiedName name = tableInfo.getName();
    final Config conf = connectorContext.getConfig();
    try {
        final Map<String, String> newTableMetadata = tableInfo.getMetadata();
        if (MapUtils.isEmpty(newTableMetadata)) {
            final String message = String.format("No parameters defined for iceberg table %s", name);
            log.warn(message);
            throw new InvalidMetaException(name, message, null);
        }
        final String prevLoc = newTableMetadata.get(DirectSqlTable.PARAM_PREVIOUS_METADATA_LOCATION);
        final String newLoc = newTableMetadata.get(DirectSqlTable.PARAM_METADATA_LOCATION);
        if (StringUtils.isBlank(prevLoc) || StringUtils.isBlank(newLoc)) {
            final String message = String.format("Invalid metadata for %s. Provided previous %s or new %s location is empty.", name, prevLoc, newLoc);
            log.error(message);
            throw new InvalidMetaException(name, message, null);
        }
        if (conf.isIcebergPreviousMetadataLocationCheckEnabled() && !icebergTableHandler.doesMetadataLocationExist(name, prevLoc)) {
            final String message = String.format("Provided previous metadata location: %s for table: %s does not exist.", name, prevLoc);
            log.error(message);
            throw new InvalidMetaException(name, message, null);
        }
        // optimistically attempt to update metadata location
        final boolean updated = polarisStoreService.updateTableMetadataLocation(name.getDatabaseName(), name.getTableName(), prevLoc, newLoc);
        // if succeeded then done, else try to figure out why and throw corresponding exception
        if (updated) {
            requestContext.setIgnoreErrorsAfterUpdate(true);
            return;
        }
        final PolarisTableEntity table = polarisStoreService.getTable(name.getDatabaseName(), name.getTableName()).orElseThrow(() -> new TableNotFoundException(name));
        final String existingLoc = table.getMetadataLocation();
        if (StringUtils.isBlank(existingLoc)) {
            final String message = String.format("Invalid metadata location for %s existing location is empty.", name);
            log.error(message);
            throw new TablePreconditionFailedException(name, message, existingLoc, prevLoc);
        }
        if (StringUtils.equalsIgnoreCase(existingLoc, newLoc)) {
            log.warn("Existing metadata location is the same as new. Existing: {}, New: {}", existingLoc, newLoc);
            return;
        }
        if (!Objects.equals(existingLoc, prevLoc)) {
            final String message = String.format("Invalid metadata location for %s expected: %s, provided: %s", name, existingLoc, prevLoc);
            log.error(message);
            throw new TablePreconditionFailedException(name, message, existingLoc, prevLoc);
        }
    } catch (TableNotFoundException | InvalidMetaException | TablePreconditionFailedException exception) {
        throw exception;
    } catch (DataIntegrityViolationException exception) {
        throw new InvalidMetaException(name, exception);
    } catch (Exception exception) {
        final String msg = String.format("Failed updating polaris table %s", tableInfo.getName());
        log.error(msg, exception);
        throw new ConnectorException(msg, exception);
    }
}
Also used : TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) Config(com.netflix.metacat.common.server.properties.Config) QualifiedName(com.netflix.metacat.common.QualifiedName) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException)

Example 78 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project apollo by ctripcorp.

the class InstanceConfigAuditUtil method prepareInstanceId.

private long prepareInstanceId(InstanceConfigAuditModel auditModel) {
    Instance instance = instanceService.findInstance(auditModel.getAppId(), auditModel.getClusterName(), auditModel.getDataCenter(), auditModel.getIp());
    if (instance != null) {
        return instance.getId();
    }
    instance = new Instance();
    instance.setAppId(auditModel.getAppId());
    instance.setClusterName(auditModel.getClusterName());
    instance.setDataCenter(auditModel.getDataCenter());
    instance.setIp(auditModel.getIp());
    try {
        return instanceService.createInstance(instance).getId();
    } catch (DataIntegrityViolationException ex) {
        // return the one exists
        return instanceService.findInstance(instance.getAppId(), instance.getClusterName(), instance.getDataCenter(), instance.getIp()).getId();
    }
}
Also used : Instance(com.ctrip.framework.apollo.biz.entity.Instance) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 79 with DataIntegrityViolationException

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

the class UserService method transferUser.

/**
 * Transfers the specific user to a non existant new user with password and an
 * optional role.
 *
 * @param newEmail
 * @param newPassword
 * @param privateUsername
 * @param privatePassword
 * @param role
 * @throws UserAlreadyExistsException when the new User already exists
 * @throws won.owner.service.impl.UserNotFoundException when the private User is
 * not found
 */
public User transferUser(String newEmail, String newPassword, String privateUsername, String privatePassword, String role) throws UserAlreadyExistsException, UserNotFoundException {
    User user = getByUsername(newEmail);
    if (user != null) {
        throw new UserAlreadyExistsException();
    }
    try {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        User privateUser = getByUsernameWithKeystorePassword(privateUsername);
        if (privateUser == null) {
            throw new UserNotFoundException();
        }
        // change the username/email and keystorpw holder
        privateUser.setUsername(newEmail);
        privateUser.setPassword(passwordEncoder.encode(newPassword));
        privateUser.setEmail(newEmail);
        privateUser.setEmailVerified(false);
        privateUser.setPrivateId(null);
        // transfer only available when flag is set therefore we can
        privateUser.setAcceptedTermsOfService(true);
        // this to true (i think)
        if (role != null) {
            privateUser.setRole(role);
        }
        KeystorePasswordHolder privateKeystorePassword = privateUser.getKeystorePasswordHolder();
        String keystorePassword = privateKeystorePassword.getPassword(privatePassword);
        // ************************************************
        KeystorePasswordHolder newKeystorePassword = new KeystorePasswordHolder();
        // generate a newPassword for the keystore and save it in the database,
        // encrypted with a symmetric key
        // derived from the user's new password
        newKeystorePassword.setPassword(keystorePassword, newPassword);
        privateUser.setKeystorePasswordHolder(newKeystorePassword);
        // we delete the recoverable keystore key as it will no longer work
        privateUser.setRecoverableKeystorePasswordHolder(null);
        save(privateUser);
        return privateUser;
    } catch (DataIntegrityViolationException e) {
        throw new UserAlreadyExistsException();
    }
}
Also used : User(won.owner.model.User) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) ExpensiveSecureRandomString(won.protocol.util.ExpensiveSecureRandomString) KeystorePasswordHolder(won.owner.model.KeystorePasswordHolder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 80 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project Workload by amoxu.

the class RemoteTeachServiceImpl method insertLoad.

@Override
public boolean insertLoad(RemoteTeachWorkload workload) throws Exception {
    try {
        int people = workload.getPeople();
        // 46个人为一个班,每增加一个班增加0.1系数
        workload.setClassCoefficient((float) (Math.round(people / 46F) * 0.1 + 0.9));
        // 计算工作量 课时
        workload.setWorkload(workload.getPeriod() * workload.getClassCoefficient());
        // 计算酬金
        workload.setMoney(PropertyUtil.getAllowance() * workload.getWorkload());
    } catch (NullPointerException | DataIntegrityViolationException nop) {
        throw new CustomException("请输入班级人数,学时,教学班");
    }
    if (workloadMapper.insertSelective(workload) == 0) {
        return false;
    }
    return true;
}
Also used : CustomException(com.hfut.exception.CustomException) 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