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;
}
}
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);
}
}
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();
}
}
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();
}
}
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;
}
Aggregations