use of com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType in project midpoint by Evolveum.
the class Utils method resolveRef.
private static void resolveRef(PrismReferenceValue refVal, RepositoryService repository, boolean enforceReferentialIntegrity, boolean forceFilterReevaluation, EvaluationTimeType evaluationTimeType, PrismContext prismContext, String contextDesc, boolean throwExceptionOnFailure, OperationResult parentResult) {
String refName = refVal.getParent() != null ? refVal.getParent().getElementName().toString() : "(unnamed)";
if ((refVal.getResolutionTime() != null && refVal.getResolutionTime() != evaluationTimeType) || (refVal.getResolutionTime() == null && evaluationTimeType != EvaluationTimeType.IMPORT)) {
LOGGER.trace("Skipping resolution of reference {} in {} because the resolutionTime is set to {}", refName, contextDesc, refVal.getResolutionTime());
return;
}
OperationResult result = parentResult.createMinorSubresult(OPERATION_RESOLVE_REFERENCE);
result.addContext(OperationResult.CONTEXT_ITEM, refName);
QName typeQName = null;
if (refVal.getTargetType() != null) {
typeQName = refVal.getTargetType();
}
if (typeQName == null) {
PrismReferenceDefinition definition = (PrismReferenceDefinition) refVal.getParent().getDefinition();
if (definition != null) {
typeQName = definition.getTargetTypeName();
}
}
Class<? extends ObjectType> type = ObjectType.class;
if (typeQName != null) {
type = prismContext.getSchemaRegistry().determineCompileTimeClass(typeQName);
if (type == null) {
result.recordWarning("Unknown type specified in reference or definition of reference " + refName + ": " + typeQName);
type = ObjectType.class;
}
}
SearchFilterType filter = refVal.getFilter();
if (!StringUtils.isBlank(refVal.getOid()) && (!forceFilterReevaluation || filter == null)) {
// We have OID (and "force filter reevaluation" is not requested or not possible)
if (filter != null) {
// We have both filter and OID. We will choose OID, but let's at
// least log a warning
LOGGER.debug("Both OID and filter for property {} in {}, OID takes precedence", refName, contextDesc);
}
// Nothing to resolve, but let's check if the OID exists
PrismObject<? extends ObjectType> object = null;
try {
object = repository.getObject(type, refVal.getOid(), null, result);
} catch (ObjectNotFoundException e) {
String message = "Reference " + refName + " refers to a non-existing object " + refVal.getOid();
if (enforceReferentialIntegrity) {
LOGGER.error(message);
result.recordFatalError(message);
if (throwExceptionOnFailure) {
throw new SystemException(message, e);
}
} else {
LOGGER.warn(message);
result.recordWarning(message);
}
} catch (SchemaException e) {
String message = "Schema error while trying to retrieve object " + refVal.getOid() + " : " + e.getMessage();
result.recordPartialError(message, e);
LOGGER.error(message, e);
// But continue otherwise
}
if (object != null && refVal.getOriginType() != null) {
// Check if declared and actual type matches
if (!object.getClass().equals(type)) {
result.recordWarning("Type mismatch on property " + refName + ": declared:" + refVal.getOriginType() + ", actual: " + object.getClass());
}
}
result.recordSuccessIfUnknown();
parentResult.computeStatus();
return;
}
if (filter == null) {
if (refVal.getObject() != null) {
LOGGER.trace("Skipping resolution of reference {} in {} because the object is present and the filter is not", refName, contextDesc);
result.recordNotApplicableIfUnknown();
return;
}
// No OID and no filter. We are lost.
String message = "Neither OID nor filter for property " + refName + ": cannot resolve reference";
result.recordFatalError(message);
if (throwExceptionOnFailure) {
throw new SystemException(message);
}
return;
}
// No OID and we have filter. Let's check the filter a bit
ObjectFilter objFilter;
try {
PrismObjectDefinition objDef = prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(type);
objFilter = QueryConvertor.parseFilter(filter, objDef);
} catch (SchemaException ex) {
LOGGER.error("Failed to convert object filter from filter because of: " + ex.getMessage() + "; filter: " + filter.debugDump(), ex);
throw new SystemException("Failed to convert object filter from filter. Reason: " + ex.getMessage(), ex);
}
LOGGER.trace("Resolving using filter {}", objFilter.debugDump());
List<PrismObject<? extends ObjectType>> objects;
QName objectType = refVal.getTargetType();
if (objectType == null) {
String message = "Missing definition of type of reference " + refName;
result.recordFatalError(message);
if (throwExceptionOnFailure) {
throw new SystemException(message);
}
return;
}
if (containExpression(objFilter)) {
result.recordSuccessIfUnknown();
return;
}
try {
ObjectQuery query = ObjectQuery.createObjectQuery(objFilter);
objects = (List) repository.searchObjects(type, query, null, result);
} catch (SchemaException e) {
// This is unexpected, but may happen. Record fatal error
String message = "Repository schema error during resolution of reference " + refName;
result.recordFatalError(message, e);
if (throwExceptionOnFailure) {
throw new SystemException(message, e);
}
return;
} catch (SystemException e) {
// We don't want this to tear down entire import.
String message = "Repository system error during resolution of reference " + refName;
result.recordFatalError(message, e);
if (throwExceptionOnFailure) {
throw new SystemException(message, e);
}
return;
}
if (objects.isEmpty()) {
String message = "Repository reference " + refName + " cannot be resolved: filter matches no object";
result.recordFatalError(message);
if (throwExceptionOnFailure) {
throw new SystemException(message);
}
return;
}
if (objects.size() > 1) {
String message = "Repository reference " + refName + " cannot be resolved: filter matches " + objects.size() + " objects";
result.recordFatalError(message);
if (throwExceptionOnFailure) {
throw new SystemException(message);
}
return;
}
// Bingo. We have exactly one object.
String oid = objects.get(0).getOid();
refVal.setOid(oid);
result.recordSuccessIfUnknown();
}
use of com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType in project midpoint by Evolveum.
the class TestParseResource method assertResourcePrism.
private void assertResourcePrism(PrismObject<ResourceType> resource, boolean isSimple) throws SchemaException {
PrismContext prismContext = getPrismContext();
AssertJUnit.assertEquals("Wrong oid (prism)", TestConstants.RESOURCE_OID, resource.getOid());
// assertEquals("Wrong version", "42", resource.getVersion());
PrismObjectDefinition<ResourceType> resourceDefinition = resource.getDefinition();
assertNotNull("No resource definition", resourceDefinition);
PrismAsserts.assertObjectDefinition(resourceDefinition, new QName(SchemaConstantsGenerated.NS_COMMON, "resource"), ResourceType.COMPLEX_TYPE, ResourceType.class);
assertEquals("Wrong class in resource", ResourceType.class, resource.getCompileTimeClass());
ResourceType resourceType = resource.asObjectable();
assertNotNull("asObjectable resulted in null", resourceType);
assertPropertyValue(resource, "name", PrismTestUtil.createPolyString("Embedded Test OpenDJ"));
assertPropertyDefinition(resource, "name", PolyStringType.COMPLEX_TYPE, 0, 1);
if (!isSimple) {
assertPropertyValue(resource, "namespace", TestConstants.RESOURCE_NAMESPACE);
assertPropertyDefinition(resource, "namespace", DOMUtil.XSD_ANYURI, 0, 1);
}
PrismReference connectorRef = resource.findReference(ResourceType.F_CONNECTOR_REF);
assertNotNull("No connectorRef", connectorRef);
PrismReferenceValue connectorRefVal = connectorRef.getValue();
assertNotNull("No connectorRef value", connectorRefVal);
assertEquals("Wrong type in connectorRef value", ConnectorType.COMPLEX_TYPE, connectorRefVal.getTargetType());
SearchFilterType filter = connectorRefVal.getFilter();
assertNotNull("No filter in connectorRef value", filter);
if (!isSimple) {
ObjectFilter objectFilter = QueryConvertor.parseFilter(filter, ConnectorType.class, prismContext);
assertTrue("Wrong kind of filter: " + objectFilter, objectFilter instanceof EqualFilter);
EqualFilter equalFilter = (EqualFilter) objectFilter;
// should be extension/x:extConnType
ItemPath path = equalFilter.getPath();
PrismAsserts.assertPathEqualsExceptForPrefixes("Wrong filter path", namespaces ? new ItemPath(new QName("extension"), new QName("http://x/", "extConnType")) : new ItemPath(new QName("extension"), new QName("extConnType")), path);
PrismPropertyValue filterValue = (PrismPropertyValue) equalFilter.getValues().get(0);
assertEquals("Wrong filter value", "org.identityconnectors.ldap.LdapConnector", ((RawType) filterValue.getValue()).getParsedRealValue(String.class).trim());
//assertEquals("Wrong filter value", "org.identityconnectors.ldap.LdapConnector", ((String) filterValue.getValue()).trim());
}
EvaluationTimeType resolutionTime = connectorRefVal.getResolutionTime();
if (isSimple) {
assertEquals("Wrong resolution time in connectorRef value", EvaluationTimeType.RUN, resolutionTime);
} else {
assertEquals("Wrong resolution time in connectorRef value", EvaluationTimeType.IMPORT, resolutionTime);
}
PrismContainer<?> configurationContainer = resource.findContainer(ResourceType.F_CONNECTOR_CONFIGURATION);
assertContainerDefinition(configurationContainer, "configuration", ConnectorConfigurationType.COMPLEX_TYPE, 1, 1);
PrismContainerValue<?> configContainerValue = configurationContainer.getValue();
List<Item<?, ?>> configItems = configContainerValue.getItems();
assertEquals("Wrong number of config items", isSimple ? 1 : 4, configItems.size());
PrismContainer<?> ldapConfigPropertiesContainer = configurationContainer.findContainer(ICFC_CONFIGURATION_PROPERTIES);
assertNotNull("No icfcldap:configurationProperties container", ldapConfigPropertiesContainer);
PrismContainerDefinition<?> ldapConfigPropertiesContainerDef = ldapConfigPropertiesContainer.getDefinition();
assertNotNull("No icfcldap:configurationProperties container definition", ldapConfigPropertiesContainerDef);
assertEquals("icfcldap:configurationProperties container definition maxOccurs", 1, ldapConfigPropertiesContainerDef.getMaxOccurs());
List<Item<?, ?>> ldapConfigPropItems = ldapConfigPropertiesContainer.getValue().getItems();
assertEquals("Wrong number of ldapConfigPropItems items", 7, ldapConfigPropItems.size());
PrismContainer<Containerable> schemaContainer = resource.findContainer(ResourceType.F_SCHEMA);
if (isSimple) {
assertNull("Schema sneaked in", schemaContainer);
} else {
assertNotNull("No schema container", schemaContainer);
}
PrismContainer<?> schemaHandlingContainer = resource.findContainer(ResourceType.F_SCHEMA_HANDLING);
if (isSimple) {
assertNull("SchemaHandling sneaked in", schemaHandlingContainer);
} else {
assertNotNull("No schemaHandling container", schemaHandlingContainer);
}
if (!isSimple) {
PrismProperty<SynchronizationType> synchronizationProp = resource.findProperty(ResourceType.F_SYNCHRONIZATION);
SynchronizationType synchronizationType = synchronizationProp.getRealValue();
ObjectSynchronizationType objectSynchronizationType = synchronizationType.getObjectSynchronization().get(0);
List<ConditionalSearchFilterType> correlations = objectSynchronizationType.getCorrelation();
assertEquals("Wrong number of correlation expressions", 1, correlations.size());
ConditionalSearchFilterType correlationFilterType = correlations.get(0);
System.out.println("\nCorrelation filter");
System.out.println(correlationFilterType.debugDump());
ObjectFilter objectFilter = QueryConvertor.parseFilter(correlationFilterType.serializeToXNode(), prismContext);
PrismAsserts.assertAssignableFrom(EqualFilter.class, objectFilter);
EqualFilter equalsFilter = (EqualFilter) objectFilter;
equalsFilter.getFullPath();
assertNull("Unexpected values in correlation expression", equalsFilter.getValues());
ExpressionWrapper expression = equalsFilter.getExpression();
assertNotNull("No expressions in correlation expression", expression);
ExpressionType expressionType = (ExpressionType) expression.getExpression();
assertEquals("Wrong number of expression evaluators in correlation expression", 1, expressionType.getExpressionEvaluator().size());
ItemPathType itemPathType = (ItemPathType) expressionType.getExpressionEvaluator().get(0).getValue();
// $account/c:attributes/my:yyy
PrismAsserts.assertPathEqualsExceptForPrefixes("path in correlation expression", namespaces ? new ItemPath(new NameItemPathSegment(new QName("account"), true), new NameItemPathSegment(new QName(SchemaConstantsGenerated.NS_COMMON, "attributes")), new NameItemPathSegment(new QName("http://myself.me/schemas/whatever", "yyy"))) : new ItemPath(new NameItemPathSegment(new QName("account"), true), new NameItemPathSegment(new QName("attributes")), new NameItemPathSegment(new QName("yyy"))), itemPathType.getItemPath());
//PrismAsserts.assertAllParsedNodes(expression);
// TODO
}
}
Aggregations