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