use of com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType in project midpoint by Evolveum.
the class PrismMarshaller method serializeReferenceValue.
private XNode serializeReferenceValue(PrismReferenceValue value, PrismReferenceDefinition definition, SerializationContext ctx) throws SchemaException {
MapXNode xmap = new MapXNode();
boolean containsOid = false;
// namespace for filter and description
String namespace = definition != null ? definition.getNamespace() : null;
if (StringUtils.isNotBlank(value.getOid())) {
containsOid = true;
xmap.put(XNode.KEY_REFERENCE_OID, createPrimitiveXNodeStringAttr(value.getOid()));
}
QName relation = value.getRelation();
if (relation != null) {
xmap.put(XNode.KEY_REFERENCE_RELATION, createPrimitiveXNodeAttr(relation, DOMUtil.XSD_QNAME));
}
QName targetType = value.getTargetType();
if (targetType != null) {
xmap.put(XNode.KEY_REFERENCE_TYPE, createPrimitiveXNodeAttr(targetType, DOMUtil.XSD_QNAME));
}
String description = value.getDescription();
if (description != null) {
xmap.put(createReferenceQName(XNode.KEY_REFERENCE_DESCRIPTION, namespace), createPrimitiveXNode(description, DOMUtil.XSD_STRING));
}
SearchFilterType filter = value.getFilter();
if (filter != null) {
XNode xsubnode = filter.serializeToXNode();
if (xsubnode != null) {
xmap.put(createReferenceQName(XNode.KEY_REFERENCE_FILTER, namespace), xsubnode);
}
}
EvaluationTimeType resolutionTime = value.getResolutionTime();
if (resolutionTime != null) {
xmap.put(createReferenceQName(XNode.KEY_REFERENCE_RESOLUTION_TIME, namespace), createPrimitiveXNode(resolutionTime.value(), DOMUtil.XSD_STRING));
}
if (value.getTargetName() != null) {
if (SerializationContext.isSerializeReferenceNames(ctx)) {
XNode xsubnode = createPrimitiveXNode(value.getTargetName(), PolyStringType.COMPLEX_TYPE);
xmap.put(createReferenceQName(XNode.KEY_REFERENCE_TARGET_NAME, namespace), xsubnode);
} else {
String commentValue = " " + value.getTargetName().getOrig() + " ";
xmap.setComment(commentValue);
}
}
boolean isComposite = false;
if (definition != null) {
isComposite = definition.isComposite();
}
if ((SerializationContext.isSerializeCompositeObjects(ctx) || isComposite || !containsOid) && value.getObject() != null) {
XNode xobjnode = marshalObjectContent(value.getObject(), value.getObject().getDefinition(), ctx);
xmap.put(createReferenceQName(XNode.KEY_REFERENCE_OBJECT, namespace), xobjnode);
}
return xmap;
}
use of com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType in project midpoint by Evolveum.
the class PrismUnmarshaller method parseReferenceValueAsReference.
private PrismReferenceValue parseReferenceValueAsReference(@NotNull XNode xnode, @NotNull PrismReferenceDefinition definition, @NotNull ParsingContext pc, boolean allowMissingRefTypesOverride) throws SchemaException {
if (!(xnode instanceof MapXNode)) {
throw new IllegalArgumentException("Cannot parse reference from " + xnode);
}
MapXNode map = (MapXNode) xnode;
String oid = map.getParsedPrimitiveValue(XNode.KEY_REFERENCE_OID, DOMUtil.XSD_STRING);
PrismReferenceValue refVal = new PrismReferenceValue(oid);
QName type = map.getParsedPrimitiveValue(XNode.KEY_REFERENCE_TYPE, DOMUtil.XSD_QNAME);
if (type == null) {
if (!pc.isAllowMissingRefTypes() && !allowMissingRefTypesOverride) {
type = definition.getTargetTypeName();
if (type == null) {
throw new SchemaException("Target type in reference " + definition.getName() + " not specified in reference nor in the schema");
}
}
} else {
if (QNameUtil.noNamespace(type)) {
type = getSchemaRegistry().resolveUnqualifiedTypeName(type);
}
QName defTargetType = definition.getTargetTypeName();
if (defTargetType != null) {
if (!(prismContext.getSchemaRegistry().isAssignableFrom(defTargetType, type))) {
throw new SchemaException("Target type specified in reference " + definition.getName() + " (" + type + ") does not match target type in schema (" + defTargetType + ")");
}
}
}
PrismObjectDefinition<Objectable> objectDefinition = null;
if (type != null) {
objectDefinition = getSchemaRegistry().findObjectDefinitionByType(type);
if (objectDefinition == null) {
throw new SchemaException("No definition for type " + type + " in reference");
}
refVal.setTargetType(type);
}
QName relationAttribute = map.getParsedPrimitiveValue(XNode.KEY_REFERENCE_RELATION, DOMUtil.XSD_QNAME);
refVal.setRelation(relationAttribute);
refVal.setDescription(map.getParsedPrimitiveValue(XNode.KEY_REFERENCE_DESCRIPTION, DOMUtil.XSD_STRING));
refVal.setFilter(parseFilter(map.get(XNode.KEY_REFERENCE_FILTER)));
String resolutionTimeString = map.getParsedPrimitiveValue(XNode.KEY_REFERENCE_RESOLUTION_TIME, DOMUtil.XSD_STRING);
if (resolutionTimeString != null) {
EvaluationTimeType resolutionTime = EvaluationTimeType.fromValue(resolutionTimeString);
refVal.setResolutionTime(resolutionTime);
}
XNode xnodeForTargetName = map.get(XNode.KEY_REFERENCE_TARGET_NAME);
if (xnodeForTargetName != null) {
PolyStringType targetName = getBeanUnmarshaller().unmarshal(xnodeForTargetName, PolyStringType.class, pc);
refVal.setTargetName(targetName);
}
XNode xrefObject = map.get(XNode.KEY_REFERENCE_OBJECT);
if (xrefObject != null) {
if (!(xrefObject instanceof MapXNode)) {
throw new SchemaException("Cannot parse object from " + xrefObject);
}
if (type == null) {
throw new SchemaException("Cannot parse object from " + xrefObject + " without knowing its type");
}
PrismObject<Objectable> object = parseObject((MapXNode) xrefObject, objectDefinition, pc);
setReferenceObject(refVal, object);
}
return refVal;
}
use of com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType in project midpoint by Evolveum.
the class PrismReferenceValue method asReferencable.
public Referencable asReferencable() {
if (referencable != null) {
return referencable;
}
Itemable parent = getParent();
if (parent != null) {
QName xsdType = parent.getDefinition().getTypeName();
Class clazz = getPrismContext().getSchemaRegistry().getCompileTimeClass(xsdType);
if (clazz != null) {
try {
referencable = (Referencable) clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new SystemException("Couldn't create jaxb object instance of '" + clazz + "': " + e.getMessage(), e);
}
}
referencable.setupReferenceValue(this);
}
// A hack, just to avoid crashes. TODO think about this!
return new Referencable() {
PrismReferenceValue referenceValue = PrismReferenceValue.this;
@Override
public PrismReferenceValue asReferenceValue() {
return referenceValue;
}
@Override
public void setupReferenceValue(PrismReferenceValue value) {
referenceValue = value;
}
@Override
public String getOid() {
return referenceValue.getOid();
}
@Override
public QName getType() {
return referenceValue.getTargetType();
}
@Override
public PolyStringType getTargetName() {
return PrismForJAXBUtil.getReferenceTargetName(referenceValue);
}
@Override
public QName getRelation() {
return referenceValue.getRelation();
}
@Override
public String getDescription() {
return referenceValue.getDescription();
}
@Override
public EvaluationTimeType getResolutionTime() {
return referenceValue.getResolutionTime();
}
@Override
public SearchFilterType getFilter() {
SearchFilterType filter = new SearchFilterType();
filter.setFilterClauseXNode(PrismForJAXBUtil.getReferenceFilterClauseXNode(referenceValue));
return filter;
}
};
}
use of com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType in project midpoint by Evolveum.
the class ModelImplUtils 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 {
// Maybe too courageous (we are not sure what clients do with the resolved objects).
object = repository.getObject(type, refVal.getOid(), createReadOnlyCollection(), 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) {
// noinspection EqualsBetweenInconvertibleTypes - both are Class, false alarm
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 = prismContext.getQueryConverter().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 = prismContext.queryFactory().createQuery(objFilter);
objects = (List) repository.searchObjects(type, query, createReadOnlyCollection(), 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 assertResourceJaxb.
private void assertResourceJaxb(ResourceType resourceType, boolean isSimple) throws SchemaException {
assertEquals("Wrong oid (JAXB)", TestConstants.RESOURCE_OID, resourceType.getOid());
assertEquals("Wrong name (JAXB)", PrismTestUtil.createPolyStringType("Embedded Test OpenDJ"), resourceType.getName());
assertEquals("Wrong namespace (JAXB)", MidPointConstants.NS_RI, MidPointConstants.NS_RI);
ObjectReferenceType connectorRef = resourceType.getConnectorRef();
assertNotNull("No connectorRef (JAXB)", connectorRef);
assertEquals("Wrong type in connectorRef (JAXB)", ConnectorType.COMPLEX_TYPE, connectorRef.getType());
SearchFilterType filter = connectorRef.getFilter();
assertNotNull("No filter in connectorRef (JAXB)", filter);
MapXNode filterElement = filter.getFilterClauseXNode();
assertNotNull("No filter element in connectorRef (JAXB)", filterElement);
EvaluationTimeType resolutionTime = connectorRef.getResolutionTime();
if (isSimple) {
assertEquals("Wrong resolution time in connectorRef (JAXB)", EvaluationTimeType.RUN, resolutionTime);
} else {
assertEquals("Wrong resolution time in connectorRef (JAXB)", EvaluationTimeType.IMPORT, resolutionTime);
}
XmlSchemaType xmlSchemaType = resourceType.getSchema();
SchemaHandlingType schemaHandling = resourceType.getSchemaHandling();
if (isSimple) {
assertNull("Schema sneaked in", xmlSchemaType);
assertNull("SchemaHandling sneaked in", schemaHandling);
} else {
assertNotNull("No schema element (JAXB)", xmlSchemaType);
SchemaDefinitionType definition = xmlSchemaType.getDefinition();
assertNotNull("No definition element in schema (JAXB)", definition);
List<Element> anyElements = definition.getAny();
assertNotNull("Null element list in definition element in schema (JAXB)", anyElements);
assertFalse("Empty element list in definition element in schema (JAXB)", anyElements.isEmpty());
assertNotNull("No schema handling (JAXB)", schemaHandling);
for (ResourceObjectTypeDefinitionType accountType : schemaHandling.getObjectType()) {
String name = accountType.getIntent();
assertNotNull("Account type without a name", name);
assertNotNull("Account type " + name + " does not have an objectClass", accountType.getObjectClass());
boolean foundDescription = false;
boolean foundDepartmentNumber = false;
for (ResourceAttributeDefinitionType attributeDefinitionType : accountType.getAttribute()) {
if ("description".equals(ItemPathTypeUtil.asSingleNameOrFail(attributeDefinitionType.getRef()).getLocalPart())) {
foundDescription = true;
MappingType outbound = attributeDefinitionType.getOutbound();
JAXBElement<?> valueEvaluator = outbound.getExpression().getExpressionEvaluator().get(0);
System.out.println("value evaluator for description = " + valueEvaluator);
assertNotNull("no expression evaluator for description", valueEvaluator);
assertEquals("wrong expression evaluator element name for description", SchemaConstantsGenerated.C_VALUE, valueEvaluator.getName());
assertEquals("wrong expression evaluator actual type for description", RawType.class, valueEvaluator.getValue().getClass());
} else if ("departmentNumber".equals(ItemPathTypeUtil.asSingleNameOrFail(attributeDefinitionType.getRef()).getLocalPart())) {
foundDepartmentNumber = true;
MappingType outbound = attributeDefinitionType.getOutbound();
VariableBindingDefinitionType source = outbound.getSource().get(0);
System.out.println("source for departmentNumber = " + source);
assertNotNull("no source for outbound mapping for departmentNumber", source);
// <path xmlns:z="http://z/">$user/extension/z:dept</path>
ItemPath expected = ItemPath.create(new VariableItemPathSegment(new QName("user")), new QName("extension"), namespaces ? new QName("http://z/", "dept") : new QName("dept"));
PrismAsserts.assertPathEqualsExceptForPrefixes("source for departmentNubmer", expected, source.getPath().getItemPath());
}
}
assertTrue("ri:description attribute was not found", foundDescription);
assertTrue("ri:departmentNumber attribute was not found", foundDepartmentNumber);
}
// checking <class> element in fetch result
OperationResultType fetchResult = resourceType.getFetchResult();
assertNotNull("No fetchResult (JAXB)", fetchResult);
JAXBElement<?> value = fetchResult.getParams().getEntry().get(0).getEntryValue();
assertNotNull("No fetchResult param value (JAXB)", value);
assertEquals("Wrong value class", UnknownJavaObjectType.class, value.getValue().getClass());
UnknownJavaObjectType unknownJavaObjectType = (UnknownJavaObjectType) value.getValue();
assertEquals("Wrong value class", "my.class", unknownJavaObjectType.getClazz());
assertEquals("Wrong value toString value", "my.value", unknownJavaObjectType.getToString());
}
}
Aggregations