use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class AddGetObjectTest method addGetDSEESyncDoubleTest.
@Test
public void addGetDSEESyncDoubleTest() throws Exception {
final File OBJECTS_FILE = new File("./../../samples/dsee/odsee-localhost-advanced-sync.xml");
if (!OBJECTS_FILE.exists()) {
LOGGER.warn("skipping addGetDSEESyncDoubleTest, file {} not found.", new Object[] { OBJECTS_FILE.getPath() });
return;
}
addGetCompare(OBJECTS_FILE);
try {
// WHEN
addGetCompare(OBJECTS_FILE);
assert false : "Unexpected success";
} catch (ObjectAlreadyExistsException e) {
TestUtil.assertExceptionSanity(e);
}
}
use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class SearchIterativeTest method test130AddOneForOne.
@Test
public void test130AddOneForOne() throws Exception {
OperationResult result = new OperationResult("test130AddOneForOne");
final List<PrismObject<UserType>> objects = new ArrayList<>();
ResultHandler handler = new ResultHandler() {
@Override
public boolean handle(PrismObject object, OperationResult parentResult) {
objects.add(object);
System.out.print("Got object " + object.getOid());
try {
int number = Integer.parseInt(((UserType) object.asObjectable()).getCostCenter());
if (number >= 0) {
UserType user = prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(UserType.class).instantiate().asObjectable();
user.setOid("user-" + (2 * BASE + COUNT - number) + "-FF");
user.setName(new PolyStringType(new PolyString("user-new-" + number)));
user.setCostCenter(String.valueOf(-number));
repositoryService.addObject(user.asPrismObject(), null, parentResult);
System.out.print(" ... creating object " + user.getOid());
}
} catch (ObjectAlreadyExistsException | SchemaException e) {
throw new SystemException(e);
}
System.out.println();
return true;
}
};
repositoryService.searchObjectsIterative(UserType.class, null, handler, null, true, result);
result.recomputeStatus();
assertTrue(result.isSuccess());
boolean[] map = assertObjects(objects, null);
for (int i = 0; i < COUNT; i++) {
if (i % 2 == 0) {
assertFalse("Object " + (BASE + i) + " does exist but it should not", map[i]);
} else {
assertTrue("Object " + (BASE + i) + " does not exist but it should", map[i]);
}
}
System.out.println("Object processed: " + objects.size());
int count = repositoryService.countObjects(UserType.class, null, result);
assertEquals("Wrong # of objects after operation", COUNT, count);
}
use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class ObjectUpdater method addObjectAttempt.
public <T extends ObjectType> String addObjectAttempt(PrismObject<T> object, RepoAddOptions options, OperationResult result) throws ObjectAlreadyExistsException, SchemaException {
LOGGER_PERFORMANCE.debug("> add object {}, oid={}, overwrite={}", object.getCompileTimeClass().getSimpleName(), object.getOid(), options.isOverwrite());
String oid = null;
Session session = null;
OrgClosureManager.Context closureContext = null;
// it is needed to keep the original oid for example for import options. if we do not keep it
// and it was null it can bring some error because the oid is set when the object contains orgRef
// or it is org. and by the import we do not know it so it will be trying to delete non-existing object
String originalOid = object.getOid();
try {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Object\n{}", object.debugDump());
}
ObjectTypeUtil.normalizeAllRelations(object);
LOGGER.trace("Translating JAXB to data type.");
PrismIdentifierGenerator.Operation operation = options.isOverwrite() ? PrismIdentifierGenerator.Operation.ADD_WITH_OVERWRITE : PrismIdentifierGenerator.Operation.ADD;
RObject rObject = createDataObjectFromJAXB(object, operation);
session = baseHelper.beginTransaction();
closureContext = closureManager.onBeginTransactionAdd(session, object, options.isOverwrite());
if (options.isOverwrite()) {
oid = overwriteAddObjectAttempt(object, rObject, originalOid, session, closureContext, result);
} else {
oid = nonOverwriteAddObjectAttempt(object, rObject, originalOid, session, closureContext);
}
session.getTransaction().commit();
LOGGER.trace("Saved object '{}' with oid '{}'", object.getCompileTimeClass().getSimpleName(), oid);
object.setOid(oid);
} catch (ConstraintViolationException ex) {
handleConstraintViolationException(session, ex, result);
baseHelper.rollbackTransaction(session, ex, result, true);
LOGGER.debug("Constraint violation occurred (will be rethrown as ObjectAlreadyExistsException).", ex);
// to the original oid and prevent of unexpected behaviour (e.g. by import with overwrite option)
if (StringUtils.isEmpty(originalOid)) {
object.setOid(null);
}
String constraintName = ex.getConstraintName();
// Breaker to avoid long unreadable messages
if (constraintName != null && constraintName.length() > SqlRepositoryServiceImpl.MAX_CONSTRAINT_NAME_LENGTH) {
constraintName = null;
}
throw new ObjectAlreadyExistsException("Conflicting object already exists" + (constraintName == null ? "" : " (violated constraint '" + constraintName + "')"), ex);
} catch (ObjectAlreadyExistsException | SchemaException ex) {
baseHelper.rollbackTransaction(session, ex, result, true);
throw ex;
} catch (DtoTranslationException | RuntimeException ex) {
baseHelper.handleGeneralException(ex, session, result);
} finally {
cleanupClosureAndSessionAndResult(closureContext, session, result);
}
return oid;
}
use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class NodeRegistrar method createNodeObject.
/**
* Executes node startup registration: if Node object with a give name (node ID) exists, deletes it.
* Then creates a new Node with the information relevant to this node.
*
* @param result Node prism to be used for periodic re-registrations.
*/
PrismObject<NodeType> createNodeObject(OperationResult result) throws TaskManagerInitializationException {
nodePrism = createNodePrism(taskManager.getConfiguration());
NodeType node = nodePrism.asObjectable();
LOGGER.info("Registering this node in the repository as " + node.getNodeIdentifier() + " at " + node.getHostname() + ":" + node.getJmxPort());
List<PrismObject<NodeType>> nodes;
try {
nodes = findNodesWithGivenName(result, node.getName());
} catch (SchemaException e) {
throw new TaskManagerInitializationException("Node registration failed because of schema exception", e);
}
for (PrismObject<NodeType> n : nodes) {
LOGGER.trace("Removing existing NodeType with oid = {}, name = {}", n.getOid(), n.getElementName());
try {
getRepositoryService().deleteObject(NodeType.class, n.getOid(), result);
} catch (ObjectNotFoundException e) {
LoggingUtils.logException(LOGGER, "Cannot remove NodeType with oid = {}, name = {}, because it does not exist.", e, n.getOid(), n.getElementName());
// continue, because the error is not that severe (we hope so)
}
}
try {
String oid = getRepositoryService().addObject(nodePrism, null, result);
nodePrism.setOid(oid);
} catch (ObjectAlreadyExistsException e) {
taskManager.setNodeErrorStatus(NodeErrorStatusType.NODE_REGISTRATION_FAILED);
throw new TaskManagerInitializationException("Cannot register this node, because it already exists (this should not happen, as nodes with such a name were just removed)", e);
} catch (SchemaException e) {
taskManager.setNodeErrorStatus(NodeErrorStatusType.NODE_REGISTRATION_FAILED);
throw new TaskManagerInitializationException("Cannot register this node because of schema exception", e);
}
LOGGER.trace("Node was successfully registered in the repository.");
return nodePrism;
}
use of com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException in project midpoint by Evolveum.
the class TestRetirement method reconcileAllUsers.
private void reconcileAllUsers() throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
final Task task = createTask("reconcileAllUsers");
OperationResult result = task.getResult();
ResultHandler<UserType> handler = new ResultHandler<UserType>() {
@Override
public boolean handle(PrismObject<UserType> object, OperationResult parentResult) {
try {
display("reconciling " + object);
reconcileUser(object.getOid(), task, parentResult);
} catch (SchemaException | PolicyViolationException | ExpressionEvaluationException | ObjectNotFoundException | ObjectAlreadyExistsException | CommunicationException | ConfigurationException | SecurityViolationException e) {
throw new SystemException(e.getMessage(), e);
}
return true;
}
};
display("Reconciling all users");
modelService.searchObjectsIterative(UserType.class, null, handler, null, task, result);
}
Aggregations