Search in sources :

Example 91 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project ma-core-public by infiniteautomation.

the class UserDao method updateUser.

void updateUser(User user) {
    // Potential fix for "An attempt was made to get a data value of type 'VARCHAR' from a data value of type 'null'"
    if (user.getPhone() == null)
        user.setPhone("");
    if (user.getHomeUrl() == null)
        user.setHomeUrl("");
    if (user.getTimezone() == null)
        user.setTimezone("");
    if (user.getName() == null)
        user.setName("");
    if (user.getLocale() == null)
        user.setLocale("");
    int originalPwVersion = user.getPasswordVersion();
    try {
        User old = getTransactionTemplate().execute(new TransactionCallback<User>() {

            @Override
            public User doInTransaction(TransactionStatus status) {
                User old = getUser(user.getId());
                if (old == null) {
                    return null;
                }
                boolean passwordChanged = !old.getPassword().equals(user.getPassword());
                if (passwordChanged) {
                    user.setPasswordVersion(old.getPasswordVersion() + 1);
                } else {
                    user.setPasswordVersion(old.getPasswordVersion());
                }
                ejt.update(USER_UPDATE, new Object[] { user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(), boolToChar(user.isDisabled()), user.getHomeUrl(), user.getReceiveAlarmEmails(), boolToChar(user.isReceiveOwnAuditEvents()), user.getTimezone(), boolToChar(user.isMuted()), user.getPermissions(), user.getName(), user.getLocale(), user.getPasswordVersion(), user.getId() }, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.INTEGER });
                return old;
            }
        });
        if (old == null) {
            throw new NotFoundException();
        }
        AuditEventType.raiseChangedEvent(AuditEventType.TYPE_USER, old, user);
        boolean permissionsChanged = !old.getPermissions().equals(user.getPermissions());
        if (user.getPasswordVersion() > originalPwVersion || permissionsChanged || user.isDisabled()) {
            MangoSecurityConfiguration.sessionRegistry.exireSessionsForUser(old);
        }
        userCache.remove(old.getUsername());
        if (handler != null)
            handler.notify("update", user);
    } catch (DataIntegrityViolationException e) {
        // Log some information about the user object.
        LOG.error("Error updating user: " + user, e);
        throw e;
    }
}
Also used : User(com.serotonin.m2m2.vo.User) TransactionStatus(org.springframework.transaction.TransactionStatus) NotFoundException(com.serotonin.m2m2.vo.exception.NotFoundException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 92 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project cloudbreak by hortonworks.

the class NetworkService method create.

@Transactional(TxType.NEVER)
public Network create(IdentityUser user, Network network) {
    LOGGER.info("Creating network: [User: '{}', Account: '{}']", user.getUsername(), user.getAccount());
    network.setOwner(user.getUserId());
    network.setAccount(user.getAccount());
    try {
        return networkRepository.save(network);
    } catch (DataIntegrityViolationException ex) {
        String msg = String.format("Error with resource [%s], error: [%s]", APIResourceType.NETWORK, getProperSqlErrorMessage(ex));
        throw new BadRequestException(msg);
    }
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Transactional(javax.transaction.Transactional)

Example 93 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project cloudbreak by hortonworks.

the class BlueprintService method create.

@Transactional(TxType.NEVER)
public Blueprint create(IdentityUser user, Blueprint blueprint, Collection<Map<String, Map<String, String>>> properties) {
    LOGGER.debug("Creating blueprint: [User: '{}', Account: '{}']", user.getUsername(), user.getAccount());
    Blueprint savedBlueprint;
    blueprint.setOwner(user.getUserId());
    blueprint.setAccount(user.getAccount());
    if (properties != null && !properties.isEmpty()) {
        LOGGER.info("Extend validation with the following properties: {}", properties);
        Map<String, Map<String, String>> configs = new HashMap<>(properties.size());
        for (Map<String, Map<String, String>> property : properties) {
            for (Entry<String, Map<String, String>> entry : property.entrySet()) {
                Map<String, String> configValues = configs.get(entry.getKey());
                if (configValues != null) {
                    configValues.putAll(entry.getValue());
                } else {
                    configs.put(entry.getKey(), entry.getValue());
                }
            }
        }
        String extendedBlueprint = blueprintProcessorFactory.get(blueprint.getBlueprintText()).extendBlueprintGlobalConfiguration(SiteConfigurations.fromMap(configs), false).asText();
        LOGGER.info("Extended validation result: {}", extendedBlueprint);
        blueprint.setBlueprintText(extendedBlueprint);
    }
    try {
        savedBlueprint = blueprintRepository.save(blueprint);
    } catch (DataIntegrityViolationException ex) {
        String msg = String.format("Error with resource [%s], error: [%s]", APIResourceType.BLUEPRINT, getProperSqlErrorMessage(ex));
        throw new BadRequestException(msg);
    }
    return savedBlueprint;
}
Also used : HashMap(java.util.HashMap) Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) HashMap(java.util.HashMap) Map(java.util.Map) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Transactional(javax.transaction.Transactional)

Example 94 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project cloudbreak by hortonworks.

the class AmbariClusterService method create.

@Override
@Transactional(TxType.NEVER)
public Cluster create(IdentityUser user, Stack stack, Cluster cluster, List<ClusterComponent> components) {
    LOGGER.info("Cluster requested [BlueprintId: {}]", cluster.getBlueprint().getId());
    String stackName = stack.getName();
    if (stack.getCluster() != null) {
        throw new BadRequestException(String.format("A cluster is already created on this stack! [cluster: '%s']", stack.getCluster().getName()));
    }
    long start = System.currentTimeMillis();
    if (clusterRepository.findByNameInAccount(cluster.getName(), user.getAccount()) != null) {
        throw new DuplicateKeyValueException(APIResourceType.CLUSTER, cluster.getName());
    }
    LOGGER.info("Cluster name collision check took {} ms for stack {}", System.currentTimeMillis() - start, stackName);
    if (Status.CREATE_FAILED.equals(stack.getStatus())) {
        throw new BadRequestException("Stack creation failed, cannot create cluster.");
    }
    start = System.currentTimeMillis();
    for (HostGroup hostGroup : cluster.getHostGroups()) {
        constraintRepository.save(hostGroup.getConstraint());
    }
    LOGGER.info("Host group constrainst saved in {} ms for stack {}", System.currentTimeMillis() - start, stackName);
    start = System.currentTimeMillis();
    if (cluster.getFileSystem() != null) {
        fileSystemRepository.save(cluster.getFileSystem());
    }
    LOGGER.info("Filesystem config saved in {} ms for stack {}", System.currentTimeMillis() - start, stackName);
    if (cluster.getKerberosConfig() != null) {
        kerberosConfigRepository.save(cluster.getKerberosConfig());
    }
    cluster.setStack(stack);
    cluster.setOwner(user.getUserId());
    cluster.setAccount(user.getAccount());
    stack.setCluster(cluster);
    start = System.currentTimeMillis();
    generateSignKeys(cluster.getGateway());
    LOGGER.info("Sign key generated in {} ms for stack {}", System.currentTimeMillis() - start, stackName);
    try {
        start = System.currentTimeMillis();
        cluster = clusterRepository.save(cluster);
        LOGGER.info("Cluster object saved in {} ms for stack {}", System.currentTimeMillis() - start, stackName);
        clusterComponentConfigProvider.store(components, cluster);
    } catch (DataIntegrityViolationException ex) {
        String msg = String.format("Error with resource [%s], error: [%s]", APIResourceType.CLUSTER, getProperSqlErrorMessage(ex));
        throw new BadRequestException(msg);
    }
    if (stack.isAvailable()) {
        flowManager.triggerClusterInstall(stack.getId());
        InMemoryStateStore.putCluster(cluster.getId(), statusToPollGroupConverter.convert(cluster.getStatus()));
        if (InMemoryStateStore.getStack(stack.getId()) == null) {
            InMemoryStateStore.putStack(stack.getId(), statusToPollGroupConverter.convert(stack.getStatus()));
        }
    }
    return cluster;
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) HostGroup(com.sequenceiq.cloudbreak.domain.HostGroup) DuplicateKeyValueException(com.sequenceiq.cloudbreak.service.DuplicateKeyValueException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Transactional(javax.transaction.Transactional)

Example 95 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project cloudbreak by hortonworks.

the class ClusterTemplateService method create.

@Transactional(TxType.NEVER)
public ClusterTemplate create(IdentityUser user, ClusterTemplate clusterTemplate) {
    LOGGER.debug("Creating clusterTemplate: [User: '{}', Account: '{}']", user.getUsername(), user.getAccount());
    ClusterTemplate savedClusterTemplate;
    clusterTemplate.setOwner(user.getUserId());
    clusterTemplate.setAccount(user.getAccount());
    try {
        savedClusterTemplate = clusterTemplateRepository.save(clusterTemplate);
    } catch (DataIntegrityViolationException ex) {
        String msg = String.format("Error with resource [%s], error: [%s]", APIResourceType.CLUSTER_TEMPLATE, getProperSqlErrorMessage(ex));
        throw new BadRequestException(msg);
    }
    return savedClusterTemplate;
}
Also used : ClusterTemplate(com.sequenceiq.cloudbreak.domain.ClusterTemplate) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Transactional(javax.transaction.Transactional)

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