Search in sources :

Example 46 with ObjectAlreadyExistsException

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);
    }
}
Also used : File(java.io.File) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) Test(org.testng.annotations.Test)

Example 47 with ObjectAlreadyExistsException

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);
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) PrismObject(com.evolveum.midpoint.prism.PrismObject) SystemException(com.evolveum.midpoint.util.exception.SystemException) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) Test(org.testng.annotations.Test)

Example 48 with ObjectAlreadyExistsException

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;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismIdentifierGenerator(com.evolveum.midpoint.repo.sql.util.PrismIdentifierGenerator) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) Session(org.hibernate.Session)

Example 49 with ObjectAlreadyExistsException

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;
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TaskManagerInitializationException(com.evolveum.midpoint.task.api.TaskManagerInitializationException) NodeType(com.evolveum.midpoint.xml.ns._public.common.common_3.NodeType) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Example 50 with ObjectAlreadyExistsException

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);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Task(com.evolveum.midpoint.task.api.Task) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) PrismObject(com.evolveum.midpoint.prism.PrismObject) SystemException(com.evolveum.midpoint.util.exception.SystemException) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) PolicyViolationException(com.evolveum.midpoint.util.exception.PolicyViolationException) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)

Aggregations

ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)93 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)57 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)50 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)45 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)35 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)33 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)32 SystemException (com.evolveum.midpoint.util.exception.SystemException)31 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)25 PrismObject (com.evolveum.midpoint.prism.PrismObject)21 PolicyViolationException (com.evolveum.midpoint.util.exception.PolicyViolationException)18 Task (com.evolveum.midpoint.task.api.Task)17 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)14 GenericFrameworkException (com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException)14 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)13 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)12 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)12 ArrayList (java.util.ArrayList)11 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)10 PropertyDelta (com.evolveum.midpoint.prism.delta.PropertyDelta)9