Skip to content

Commit

Permalink
Support for name in object references
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Sep 7, 2015
1 parent c7af524 commit 37ddab0
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 54 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,6 +52,8 @@
public class PrismObject<O extends Objectable> extends PrismContainer<O> {

private static final long serialVersionUID = 7321429132391159949L;

private static final String PROPERTY_NAME_LOCALPART = "name";

protected String oid;
protected String version;
Expand Down Expand Up @@ -118,7 +120,19 @@ public O asObjectable() {
throw new SystemException("Couldn't create jaxb object instance of '" + clazz + "': "+ex.getMessage(), ex);
}
}

public PolyString getName() {
PrismProperty<PolyString> nameProperty = getValue().findProperty(getNamePropertyElementName());
if (nameProperty == null) {
return null;
}
return nameProperty.getRealValue();
}

private QName getNamePropertyElementName() {
return new QName(getElementName().getNamespaceURI(), PrismConstants.NAME_LOCAL_NAME);
}

public PrismContainer<?> getExtension() {
return (PrismContainer<?>) getValue().findItem(getExtensionContainerElementName(), PrismContainer.class);
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.ItemPathSegment;
import com.evolveum.midpoint.prism.path.NameItemPathSegment;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.DebugDumpable;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class PrismReferenceValue extends PrismValue implements DebugDumpable, Se
private QName relation = null;
private String description = null;
private SearchFilterType filter = null;
private PolyString targetName = null;


private Referencable referencable;
Expand Down Expand Up @@ -119,15 +121,6 @@ public QName getTargetType() {
return null;
}

public Class<Objectable> getTargetTypeCompileTimeClass() {
QName type = getTargetType();
if (type == null) {
return null;
} else {
return getPrismContext().getSchemaRegistry().findObjectDefinitionByType(type).getCompileTimeClass();
}
}

public void setTargetType(QName targetType) {
setTargetType(targetType, false);
}
Expand All @@ -146,6 +139,38 @@ public void setTargetType(QName targetType, boolean allowEmptyNamespace) {
this.targetType = targetType;
}

/**
* Returns cached name of the target object.
* This is a ephemeral value. It is usually not stored.
* It may be computed at object retrieval time or it may not be present at all.
* This is NOT an authoritative information. Setting it or changing it will
* not influence the reference meaning. OID is the only authoritative linking
* mechanism.
* @return cached name of the target object.
*/
public PolyString getTargetName() {
if (targetName != null) {
return targetName;
}
if (object != null) {
return object.getName();
}
return null;
}

public void setTargetName(PolyString name) {
this.targetName = name;
}

public Class<Objectable> getTargetTypeCompileTimeClass() {
QName type = getTargetType();
if (type == null) {
return null;
} else {
return getPrismContext().getSchemaRegistry().findObjectDefinitionByType(type).getCompileTimeClass();
}
}

public QName getRelation() {
return relation;
}
Expand Down Expand Up @@ -404,6 +429,9 @@ public String toString() {
if (object == null) {
sb.append("oid=").append(oid);
sb.append(", targetType=").append(PrettyPrinter.prettyPrint(targetType));
if (targetName != null) {
sb.append(", targetName=").append(PrettyPrinter.prettyPrint(targetName.getOrig()));
}
} else {
sb.append("object=").append(object);
}
Expand All @@ -423,7 +451,7 @@ public String toString() {
return sb.toString();
}

public Referencable asReferencable(){
public Referencable asReferencable() {
if (referencable == null){
Itemable parent = getParent();
QName xsdType = parent.getDefinition().getTypeName();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;
import com.evolveum.prism.xml.ns._public.types_3.ObjectType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

import org.apache.commons.lang.Validate;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -416,21 +417,6 @@ public static PrismReference getReference(PrismContainerValue parent, QName fiel
throw new IllegalStateException("Internal schema error: "+e.getMessage(),e);
}
}

// public static void setReferenceFilterElement(PrismReferenceValue rval, Element filterElement) {
// DomParser parser = getDomParser(rval);
// try {
// if (filterElement == null || DOMUtil.isEmpty(filterElement)){
// return;
// }
// MapXNode filterXNode = parser.parseElementAsMap(filterElement);
// SearchFilterType filter = new SearchFilterType();
// filter.parseFromXNode(filterXNode);
// rval.setFilter(filter);
// } catch (SchemaException e) {
// throw new SystemException("Error parsing filter: "+e.getMessage(),e);
// }
// }

public static void setReferenceFilterClauseXNode(PrismReferenceValue rval, SearchFilterType filterType) {
if (filterType != null) {
Expand All @@ -440,37 +426,29 @@ public static void setReferenceFilterClauseXNode(PrismReferenceValue rval, Searc
}
}

// public static Element getReferenceFilterElement(PrismReferenceValue rval) {
// SearchFilterType filter = rval.getFilter();
// if (filter == null) {
// return null;
// }
// Element filterElement;
// PrismContext prismContext = rval.getPrismContext();
// // We have to work even if prismContext is null. This is needed for
// // equals and hashcode and similar methods.
// DomParser parser = getDomParser(rval);
// try {
// MapXNode filterXmap = filter.serializeToXNode();
// if (filterXmap.size() != 1) {
// // This is supposed to be a map with just a single entry. This is an internal error
// throw new IllegalArgumentException("Unexpected map in filter processing, it has "+filterXmap.size()+" entries");
// }
// Entry<QName, XNode> entry = filterXmap.entrySet().iterator().next();
// filterElement = parser.serializeXMapToElement((MapXNode) entry.getValue(), entry.getKey());
// } catch (SchemaException e) {
// throw new SystemException("Error serializing filter: "+e.getMessage(),e);
// }
// return filterElement;
// }

public static MapXNode getReferenceFilterClauseXNode(PrismReferenceValue rval) {
SearchFilterType filter = rval.getFilter();
if (filter == null || !filter.containsFilterClause()) {
return null;
}
return filter.getFilterClauseXNode();
}

public static PolyStringType getReferenceTargetName(PrismReferenceValue rval) {
PolyString targetName = rval.getTargetName();
if (targetName == null) {
return null;
}
return new PolyStringType(targetName);
}

public static void setReferenceTargetName(PrismReferenceValue rval, PolyStringType name) {
if (name == null) {
rval.setTargetName(null);
} else {
rval.setTargetName(name.toPolyString());
}
}

private static DomParser getDomParser(PrismValue pval) {
PrismContext prismContext = pval.getPrismContext();
Expand Down
18 changes: 16 additions & 2 deletions infra/prism/src/main/resources/xml/ns/public/types-3.xsd
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Copyright (c) 2010-2014 Evolveum
~ Copyright (c) 2010-2015 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,7 @@
<xsd:documentation>
Basic Prism types.

Version: 3.1
Version: 3.3-SNAPSHOT
Recommended namespace prefix: t
</xsd:documentation>
</xsd:annotation>
Expand Down Expand Up @@ -308,6 +308,20 @@
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="targetName" type="tns:PolyStringType" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<p>
Cached name of the target object.
This is a ephemeral value. It is usually not stored.
It may be computed at object retrieval time or it may not be present at all.
This is NOT an authoritative information. Setting it or changing it will
not influence the reference meaning. OID is the only authoritative linking
mechanism.
</p>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="oid" type="xsd:string" use="optional">
<xsd:annotation>
Expand Down
Expand Up @@ -46,7 +46,7 @@
<a href="https://wiki.evolveum.com/display/midPoint/Data+Model">https://wiki.evolveum.com/display/midPoint/Data+Model</a>
for more details.

<p>Version: 3.2</p>
<p>Version: 3.3-SNAPSHOT</p>

<p>Recommended namespace prefix: (default), c</p>
</xsd:documentation>
Expand Down Expand Up @@ -480,6 +480,20 @@
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="targetName" type="t:PolyStringType" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<p>
Cached name of the target object.
This is a ephemeral value. It is not stored in the repository.
It may be computed at object retrieval time or it may not be present at all.
This is NOT an authoritative information. Setting it or changing it will
not influence the reference meaning. OID is the only authoritative linking
mechanism.
</p>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="oid" type="xsd:string" use="optional">
<xsd:annotation>
Expand Down
Expand Up @@ -168,6 +168,8 @@ public class SchemaProcessor implements Processor {
private static final String METHOD_PRISM_UTIL_SET_REFERENCE_VALUE_AS_OBJECT = "setReferenceValueAsObject";
private static final String METHOD_PRISM_UTIL_GET_REFERENCE_FILTER_CLAUSE_XNODE = "getReferenceFilterClauseXNode";
private static final String METHOD_PRISM_UTIL_SET_REFERENCE_FILTER_CLAUSE_XNODE = "setReferenceFilterClauseXNode";
private static final String METHOD_PRISM_UTIL_GET_REFERENCE_TARGET_NAME = "getReferenceTargetName";
private static final String METHOD_PRISM_UTIL_SET_REFERENCE_TARGET_NAME = "setReferenceTargetName";
private static final String METHOD_PRISM_UTIL_OBJECTABLE_AS_REFERENCE_VALUE = "objectableAsReferenceValue";
private static final String METHOD_PRISM_UTIL_SETUP_CONTAINER_VALUE = "setupContainerValue";
private static final String METHOD_PRISM_UTIL_CREATE_TARGET_INSTANCE = "createTargetInstance";
Expand Down Expand Up @@ -297,11 +299,28 @@ private void updateObjectReferenceType(JDefinedClass definedClass, JMethod getRe
body._return(JExpr.invoke(JExpr.invoke(getReference), "getTargetType"));

definedClass.removeField(typeField);

JMethod setType = recreateMethod(findMethod(definedClass, "setType"), definedClass);
body = setType.body();
JInvocation invocation = body.invoke(JExpr.invoke(getReference), "setTargetType");
invocation.arg(setType.listParams()[0]);
invocation.arg(JExpr.lit(true));

JFieldVar targetNameField = definedClass.fields().get("targetName");
JMethod getTargetName = recreateMethod(findMethod(definedClass, "getTargetName"), definedClass);
copyAnnotations(getTargetName, targetNameField);
JBlock getTargetNamebody = getTargetName.body();
JInvocation getTargetNameInvocation = CLASS_MAP.get(PrismForJAXBUtil.class).staticInvoke(METHOD_PRISM_UTIL_GET_REFERENCE_TARGET_NAME);
getTargetNameInvocation.arg(JExpr.invoke(getReference));
getTargetNamebody._return(getTargetNameInvocation);

definedClass.removeField(targetNameField);

JMethod setTargetName = recreateMethod(findMethod(definedClass, "setTargetName"), definedClass);
JBlock setTargetNamebody = setTargetName.body();
JInvocation setTagetNameInvocation = setTargetNamebody.staticInvoke(CLASS_MAP.get(PrismForJAXBUtil.class), METHOD_PRISM_UTIL_SET_REFERENCE_TARGET_NAME);
setTagetNameInvocation.arg(JExpr.invoke(getReference));
setTagetNameInvocation.arg(setTargetName.listParams()[0]);
}

private void updateObjectReferenceRelation(JDefinedClass definedClass, JMethod getReference) {
Expand Down

0 comments on commit 37ddab0

Please sign in to comment.