Search in sources :

Example 21 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class TaskQuartzImpl method deleteExtensionPropertyAndPrepareDelta.

private ItemDelta<?, ?> deleteExtensionPropertyAndPrepareDelta(QName itemName, PrismPropertyDefinition definition, Collection<? extends PrismPropertyValue> values) throws SchemaException {
    ItemDelta delta = new PropertyDelta(new ItemPath(TaskType.F_EXTENSION, itemName), definition, getPrismContext());
    delta.addValuesToDelete(values);
    Collection<ItemDelta<?, ?>> modifications = new ArrayList<>(1);
    modifications.add(delta);
    // i.e. here we apply changes only locally (in memory)
    PropertyDelta.applyTo(modifications, taskPrism);
    return isPersistent() ? delta : null;
}
Also used : ArrayList(java.util.ArrayList) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 22 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class TaskQuartzImpl method setExtensionPropertyValueTransient.

@Override
public <T> void setExtensionPropertyValueTransient(QName propertyName, T value) throws SchemaException {
    PrismPropertyDefinition propertyDef = getPrismContext().getSchemaRegistry().findPropertyDefinitionByElementName(propertyName);
    if (propertyDef == null) {
        throw new SchemaException("Unknown property " + propertyName);
    }
    ArrayList<PrismPropertyValue<T>> values = new ArrayList(1);
    if (value != null) {
        values.add(new PrismPropertyValue<T>(value));
    }
    ItemDelta delta = new PropertyDelta(new ItemPath(TaskType.F_EXTENSION, propertyName), propertyDef, getPrismContext());
    delta.setValuesToReplace(values);
    Collection<ItemDelta<?, ?>> modifications = new ArrayList<>(1);
    modifications.add(delta);
    PropertyDelta.applyTo(modifications, taskPrism);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) F_WORKFLOW_CONTEXT(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType.F_WORKFLOW_CONTEXT) F_MODEL_OPERATION_CONTEXT(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType.F_MODEL_OPERATION_CONTEXT) ArrayList(java.util.ArrayList) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 23 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class TestQuartzTaskManagerContract method test004TaskProperties.

@Test(enabled = false)
public void test004TaskProperties() throws Exception {
    String test = "004TaskProperties";
    OperationResult result = createResult(test);
    addObjectFromFile(taskFilename(test));
    TaskQuartzImpl task = (TaskQuartzImpl) taskManager.getTask(taskOid(test), result);
    System.out.println("Task extension = " + task.getExtension());
    PrismPropertyDefinition delayDefinition = new PrismPropertyDefinitionImpl(SchemaConstants.NOOP_DELAY_QNAME, DOMUtil.XSD_INT, taskManager.getPrismContext());
    System.out.println("property definition = " + delayDefinition);
    PrismProperty<Integer> property = (PrismProperty<Integer>) delayDefinition.instantiate();
    property.setRealValue(100);
    PropertyDelta delta = new PropertyDelta(new ItemPath(TaskType.F_EXTENSION, property.getElementName()), property.getDefinition(), prismContext);
    //delta.addV(property.getValues());
    delta.setValuesToReplace(PrismValue.cloneCollection(property.getValues()));
    Collection<ItemDelta<?, ?>> modifications = new ArrayList<>(1);
    modifications.add(delta);
    // TODO fix this code
    //        Collection<ItemDeltaType> idts = DeltaConvertor.toPropertyModificationTypes(delta);
    //        for (ItemDeltaType idt : idts) {
    //            String idtxml = prismContext.getParserDom().marshalElementToString(idt, new QName("http://a/", "A"));
    //            System.out.println("item delta type = " + idtxml);
    //
    //            ItemDeltaType idt2 = prismContext.getPrismJaxbProcessor().unmarshalObject(idtxml, ItemDeltaType.class);
    //            ItemDelta id2 = DeltaConvertor.createItemDelta(idt2, TaskType.class, prismContext);
    //            System.out.println("unwrapped item delta = " + id2.debugDump());
    //
    //            task.modifyExtension(id2);
    //        }
    task.savePendingModifications(result);
    System.out.println("Task = " + task.debugDump());
    PrismObject<UserType> owner2 = repositoryService.getObject(UserType.class, TASK_OWNER2_OID, null, result);
    task.setBindingImmediate(TaskBinding.LOOSE, result);
    // other properties will be set in batched mode
    String newname = "Test task, name changed";
    task.setName(PrismTestUtil.createPolyStringType(newname));
    task.setProgress(10);
    long currentTime = System.currentTimeMillis();
    long currentTime1 = currentTime + 10000;
    long currentTime2 = currentTime + 25000;
    task.setLastRunStartTimestamp(currentTime);
    task.setLastRunFinishTimestamp(currentTime1);
    task.setExecutionStatus(TaskExecutionStatus.SUSPENDED);
    task.setHandlerUri("http://no-handler.org/");
    //task.setOwner(owner2);
    ScheduleType st0 = task.getSchedule();
    ScheduleType st1 = new ScheduleType();
    st1.setInterval(1);
    st1.setMisfireAction(MisfireActionType.RESCHEDULE);
    task.pushHandlerUri("http://no-handler.org/1", st1, TaskBinding.TIGHT, ((TaskQuartzImpl) task).createExtensionDelta(delayDefinition, 1));
    ScheduleType st2 = new ScheduleType();
    st2.setInterval(2);
    st2.setMisfireAction(MisfireActionType.EXECUTE_IMMEDIATELY);
    task.pushHandlerUri("http://no-handler.org/2", st2, TaskBinding.LOOSE, ((TaskQuartzImpl) task).createExtensionDelta(delayDefinition, 2));
    task.setRecurrenceStatus(TaskRecurrence.RECURRING);
    // to be compared with later
    OperationResultType ort = result.createOperationResultType();
    task.setResult(result);
    //PrismObject<UserType> requestee = task.getOwner();
    //task.setRequesteeRef(requestee);      does not work
    //task.setRequesteeOid(requestee.getOid());
    ObjectReferenceType objectReferenceType = new ObjectReferenceType();
    objectReferenceType.setType(UserType.COMPLEX_TYPE);
    String objectOid = "some-oid...";
    objectReferenceType.setOid(objectOid);
    task.setObjectRef(objectReferenceType);
    logger.trace("Saving modifications...");
    task.savePendingModifications(result);
    logger.trace("Retrieving the task (second time) and comparing its properties...");
    Task task001 = taskManager.getTask(taskOid(test), result);
    logger.trace("Task from repo: " + task001.debugDump());
    AssertJUnit.assertEquals(TaskBinding.LOOSE, task001.getBinding());
    PrismAsserts.assertEqualsPolyString("Name not", newname, task001.getName());
    //        AssertJUnit.assertEquals(newname, task001.getName());
    AssertJUnit.assertTrue(10 == task001.getProgress());
    AssertJUnit.assertNotNull(task001.getLastRunStartTimestamp());
    // e.g. MySQL cuts off millisecond information
    AssertJUnit.assertEquals("Start time is not correct", (Long) (currentTime / 1000L), (Long) (task001.getLastRunStartTimestamp() / 1000L));
    AssertJUnit.assertNotNull(task001.getLastRunFinishTimestamp());
    AssertJUnit.assertEquals("Finish time is not correct", (Long) (currentTime1 / 1000L), (Long) (task001.getLastRunFinishTimestamp() / 1000L));
    //        AssertJUnit.assertEquals(TaskExclusivityStatus.CLAIMED, task001.getExclusivityStatus());
    AssertJUnit.assertEquals(TaskExecutionStatus.SUSPENDED, task001.getExecutionStatus());
    AssertJUnit.assertEquals("Handler after 2xPUSH is not OK", "http://no-handler.org/2", task001.getHandlerUri());
    AssertJUnit.assertEquals("Schedule after 2xPUSH is not OK", st2, task001.getSchedule());
    AssertJUnit.assertEquals("Number of handlers is not OK", 3, task.getHandlersCount());
    UriStack us = task.getOtherHandlersUriStack();
    AssertJUnit.assertEquals("First handler from the handler stack does not match", "http://no-handler.org/", us.getUriStackEntry().get(0).getHandlerUri());
    AssertJUnit.assertEquals("First schedule from the handler stack does not match", st0, us.getUriStackEntry().get(0).getSchedule());
    AssertJUnit.assertEquals("Second handler from the handler stack does not match", "http://no-handler.org/1", us.getUriStackEntry().get(1).getHandlerUri());
    AssertJUnit.assertEquals("Second schedule from the handler stack does not match", st1, us.getUriStackEntry().get(1).getSchedule());
    AssertJUnit.assertTrue(task001.isCycle());
    OperationResult r001 = task001.getResult();
    AssertJUnit.assertNotNull(r001);
    //AssertJUnit.assertEquals("Owner OID is not correct", TASK_OWNER2_OID, task001.getOwner().getOid());
    PrismProperty<?> d = task001.getExtensionProperty(SchemaConstants.NOOP_DELAY_QNAME);
    AssertJUnit.assertNotNull("delay extension property was not found", d);
    AssertJUnit.assertEquals("delay extension property has wrong value", (Integer) 100, d.getRealValue(Integer.class));
    OperationResultType ort1 = r001.createOperationResultType();
    // handling of operation result in tasks is extremely fragile now...
    // in case of problems, just uncomment the following line ;)
    AssertJUnit.assertEquals(ort, ort1);
    //AssertJUnit.assertEquals("RequesteeRef was not stored/retrieved correctly", requestee.getOid(), task001.getRequesteeRef().getOid());
    //AssertJUnit.assertEquals("RequesteeOid was not stored/retrieved correctly", requestee.getOid(), task001.getRequesteeOid());
    AssertJUnit.assertEquals("ObjectRef OID was not stored/retrieved correctly", objectReferenceType.getOid(), task001.getObjectRef().getOid());
    AssertJUnit.assertEquals("ObjectRef ObjectType was not stored/retrieved correctly", objectReferenceType.getType(), task001.getObjectRef().getType());
    // now pop the handlers
    ((TaskQuartzImpl) task001).finishHandler(result);
    task001.refresh(result);
    AssertJUnit.assertEquals("Handler URI after first POP is not correct", "http://no-handler.org/1", task001.getHandlerUri());
    AssertJUnit.assertEquals("Schedule after first POP is not correct", st1, task001.getSchedule());
    AssertJUnit.assertEquals("Binding after first POP is not correct", TaskBinding.TIGHT, task001.getBinding());
    AssertJUnit.assertNotSame("Task state after first POP should not be CLOSED", TaskExecutionStatus.CLOSED, task001.getExecutionStatus());
    AssertJUnit.assertEquals("Extension element value is not correct after first POP", (Integer) 2, task001.getExtensionProperty(SchemaConstants.NOOP_DELAY_QNAME).getRealValue(Integer.class));
    ((TaskQuartzImpl) task001).finishHandler(result);
    task001.refresh(result);
    AssertJUnit.assertEquals("Handler URI after second POP is not correct", "http://no-handler.org/", task001.getHandlerUri());
    AssertJUnit.assertEquals("Schedule after second POP is not correct", st0, task001.getSchedule());
    AssertJUnit.assertEquals("Binding after second POP is not correct", TaskBinding.LOOSE, task001.getBinding());
    AssertJUnit.assertNotSame("Task state after second POP should not be CLOSED", TaskExecutionStatus.CLOSED, task001.getExecutionStatus());
    AssertJUnit.assertEquals("Extension element value is not correct after second POP", (Integer) 1, task001.getExtensionProperty(SchemaConstants.NOOP_DELAY_QNAME).getRealValue(Integer.class));
    ((TaskQuartzImpl) task001).finishHandler(result);
    task001.refresh(result);
    //AssertJUnit.assertNull("Handler URI after third POP is not null", task001.getHandlerUri());
    AssertJUnit.assertEquals("Handler URI after third POP is not correct", "http://no-handler.org/", task001.getHandlerUri());
    AssertJUnit.assertEquals("Task state after third POP is not CLOSED", TaskExecutionStatus.CLOSED, task001.getExecutionStatus());
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Test(org.testng.annotations.Test)

Example 24 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class ProjectionValuesProcessor method checkSchemaAndPolicies.

/**
 * Check that the primary deltas do not violate schema and policies
 * TODO: implement schema check
 */
private void checkSchemaAndPolicies(LensProjectionContext accountContext, String activityDescription) throws SchemaException, PolicyViolationException {
    ObjectDelta<ShadowType> primaryDelta = accountContext.getPrimaryDelta();
    if (primaryDelta == null || primaryDelta.isDelete()) {
        return;
    }
    ResourceObjectDefinition rAccountDef = accountContext.getCompositeObjectDefinition();
    if (rAccountDef == null) {
        throw new SchemaException("No definition for account type '" + accountContext.getResourceShadowDiscriminator() + "' in " + accountContext.getResource());
    }
    if (primaryDelta.isAdd()) {
        PrismObject<ShadowType> accountToAdd = primaryDelta.getObjectToAdd();
        ResourceAttributeContainer attributesContainer = ShadowUtil.getAttributesContainer(accountToAdd);
        if (attributesContainer != null) {
            for (ResourceAttribute<?> attribute : attributesContainer.getAttributes()) {
                ResourceAttributeDefinition<?> rAttrDef = requireNonNull(rAccountDef.findAttributeDefinition(attribute.getElementName()));
                if (!rAttrDef.isTolerant()) {
                    throw new PolicyViolationException("Attempt to add object with non-tolerant attribute " + attribute.getElementName() + " in " + "account " + accountContext.getResourceShadowDiscriminator() + " during " + activityDescription);
                }
            }
        }
    } else if (primaryDelta.isModify()) {
        for (ItemDelta<?, ?> modification : primaryDelta.getModifications()) {
            if (modification.getParentPath().equivalent(SchemaConstants.PATH_ATTRIBUTES)) {
                PropertyDelta<?> attrDelta = (PropertyDelta<?>) modification;
                ResourceAttributeDefinition<?> rAttrDef = requireNonNull(rAccountDef.findAttributeDefinition(attrDelta.getElementName()));
                if (!rAttrDef.isTolerant()) {
                    throw new PolicyViolationException("Attempt to modify non-tolerant attribute " + attrDelta.getElementName() + " in " + "account " + accountContext.getResourceShadowDiscriminator() + " during " + activityDescription);
                }
            }
        }
    } else {
        throw new IllegalStateException("Whoops!");
    }
}
Also used : ResourceAttributeContainer(com.evolveum.midpoint.schema.processor.ResourceAttributeContainer) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) ResourceObjectDefinition(com.evolveum.midpoint.schema.processor.ResourceObjectDefinition) ResourceAttributeDefinition(com.evolveum.midpoint.schema.processor.ResourceAttributeDefinition) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta)

Example 25 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class TestReconScript method test006TestReconDelete.

@Test
public void test006TestReconDelete() throws Exception {
    PrismObject<TaskType> task = getTask(TASK_RECON_DUMMY_OID);
    OperationResult parentResult = createOperationResult();
    PropertyDelta<Boolean> dryRunDelta = prismContext.deltaFactory().property().createModificationReplaceProperty(PATH_MODEL_EXTENSION_DRY_RUN, task.getDefinition(), false);
    Collection<PropertyDelta<?>> modifications = new ArrayList<>();
    modifications.add(dryRunDelta);
    repositoryService.modifyObject(TaskType.class, TASK_RECON_DUMMY_OID, modifications, parentResult);
    // WHEN
    when();
    waitForTaskStart(TASK_RECON_DUMMY_OID, false);
    waitForTaskNextRunAssertSuccess(TASK_RECON_DUMMY_OID, false);
    waitForTaskFinish(TASK_RECON_DUMMY_OID, false);
    // THEN
    then();
    assertTask(TASK_RECON_DUMMY_OID, "after").display();
    assertRepoShadow(ACCOUNT_BEFORE_SCRIPT_OID).display().assertDead().assertIsNotExists();
    PrismObject<FocusType> user = repositoryService.searchShadowOwner(ACCOUNT_BEFORE_SCRIPT_OID, null, parentResult);
    display("Account owner", user);
    AssertJUnit.assertNotNull("Owner for account " + ACCOUNT_BEFORE_SCRIPT_OID + " was not found", user);
}
Also used : ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) Test(org.testng.annotations.Test) AbstractInternalModelIntegrationTest(com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)

Aggregations

PropertyDelta (com.evolveum.midpoint.prism.delta.PropertyDelta)90 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)39 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)33 QName (javax.xml.namespace.QName)28 ArrayList (java.util.ArrayList)23 Test (org.testng.annotations.Test)20 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)19 Task (com.evolveum.midpoint.task.api.Task)19 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)18 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)15 Collection (java.util.Collection)15 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)14 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)11 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)9 SystemException (com.evolveum.midpoint.util.exception.SystemException)9 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)7 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)7 ProtectedStringType (com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType)7 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)7 PrismObject (com.evolveum.midpoint.prism.PrismObject)6