Skip to content

Commit

Permalink
Interim commit. Some schema tests are now passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Oct 19, 2016
1 parent 1a04fe3 commit fdc202b
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 103 deletions.
10 changes: 4 additions & 6 deletions infra/prism/src/main/java/com/evolveum/midpoint/prism/Item.java
Expand Up @@ -607,12 +607,10 @@ public void revive(PrismContext prismContext) throws SchemaException {
definition.revive(prismContext);
}
}
if (values != null) {
for (V value: values) {
value.revive(prismContext);
}
}
}
for (V value: values) {
value.revive(prismContext);
}
}

public abstract Item clone();

Expand Down
Expand Up @@ -79,6 +79,13 @@ public PrismContainer(QName name, Class<C> compileTimeClass) {
public PrismContainer(QName name, Class<C> compileTimeClass, PrismContext prismContext) {
this(name, compileTimeClass);
this.prismContext = prismContext;
if (prismContext != null) {
try {
prismContext.adopt(this);
} catch (SchemaException e) {
throw new SystemException("Schema exception when adopting freshly created PrismContainer: " + this);
}
}
}


Expand Down
Expand Up @@ -781,25 +781,17 @@ public <X extends Containerable> PrismContainer<X> createContainer(QName contain

public <X> PrismProperty<X> createProperty(QName propertyName) throws SchemaException {
checkMutability();
PrismPropertyDefinition propertyDefinition = null;
ComplexTypeDefinition complexTypeDefinition = getComplexTypeDefinition();
if (complexTypeDefinition != null) {
propertyDefinition = complexTypeDefinition.findPropertyDefinition(propertyName);
if (propertyDefinition == null) {
// container has definition, but there is no property definition. This is either runtime schema
// or an error
if (getParent().getDefinition().isRuntimeSchema()) {
// TODO: create opportunistic runtime definition
//propertyDefinition = new PrismPropertyDefinitionImpl(propertyName, propertyName, typeName, container.prismContext);
} else {
throw new IllegalArgumentException("No definition for property "+propertyName+" in "+complexTypeDefinition);
}
}
}
PrismProperty<X> property = null;
PrismPropertyDefinition propertyDefinition = determineItemDefinition(propertyName, getComplexTypeDefinition());
if (propertyDefinition == null) {
// container has definition, but there is no property definition. This is either runtime schema
// or an error
if (getParent() != null && getDefinition() != null && !getDefinition().isRuntimeSchema()) { // TODO clean this up
throw new IllegalArgumentException("No definition for property "+propertyName+" in "+complexTypeDefinition);
}
}
PrismProperty<X> property;
if (propertyDefinition == null) {
// Definitionless
property = new PrismProperty<X>(propertyName, prismContext);
property = new PrismProperty<X>(propertyName, prismContext); // Definitionless
} else {
property = propertyDefinition.instantiate();
}
Expand Down
Expand Up @@ -21,16 +21,12 @@
import com.evolveum.midpoint.prism.marshaller.JaxbDomHack;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyStringNormalizer;
import com.evolveum.midpoint.prism.schema.SchemaDefinitionFactory;
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.prism.util.PrismMonitor;
import com.evolveum.midpoint.prism.xnode.RootXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.prism.xml.ns._public.types_3.RawType;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Element;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;

import javax.xml.namespace.QName;
Expand Down Expand Up @@ -121,9 +117,9 @@ public interface PrismContext {
//endregion

//region Adopt methods
<T extends Objectable> void adopt(PrismObject<T> object, Class<T> declaredType) throws SchemaException;
<C extends Containerable> void adopt(PrismContainer<C> object, Class<C> declaredType) throws SchemaException;

<T extends Objectable> void adopt(PrismObject<T> object) throws SchemaException;
<T extends Containerable> void adopt(PrismContainer<T> object) throws SchemaException;

void adopt(Objectable objectable) throws SchemaException;

Expand Down
Expand Up @@ -247,14 +247,14 @@ public <T extends Objectable> PrismObject<T> parseObject(String dataString) thro
* Set up the specified object with prism context instance and schema definition.
*/
@Override
public <T extends Objectable> void adopt(PrismObject<T> object, Class<T> declaredType) throws SchemaException {
object.revive(this);
getSchemaRegistry().applyDefinition(object, declaredType, false);
public <C extends Containerable> void adopt(PrismContainer<C> container, Class<C> declaredType) throws SchemaException {
container.revive(this);
getSchemaRegistry().applyDefinition(container, declaredType, false);
}

@Override
public <T extends Objectable> void adopt(PrismObject<T> object) throws SchemaException {
adopt(object, object.getCompileTimeClass());
public <C extends Containerable> void adopt(PrismContainer<C> container) throws SchemaException {
adopt(container, container.getCompileTimeClass());
}

@Override
Expand Down
Expand Up @@ -37,10 +37,7 @@ public JsonParser getParser() {
public T parse(QName typeName, XNodeProcessorEvaluationMode mode) throws SchemaException {
ObjectMapper mapper = (ObjectMapper) parser.getCodec();
Class clazz = XsdTypeMapper.toJavaType(typeName);
if (clazz == null) {
throw new SchemaException("Unsupported type " + typeName);
}


ObjectReader r = mapper.readerFor(clazz);
try {
return r.readValue(node);
Expand Down
Expand Up @@ -110,6 +110,9 @@ private static <ID extends ItemDefinition> ID augmentWithType(ID definition, Cla
}
@SuppressWarnings("unchecked")
ID defFromType = (ID) rawDefFromType;
if (definition instanceof PrismReferenceDefinition && defFromType instanceof PrismObjectDefinition) {
return definition; // nothing to do; this is a reference holding a composite object
}
return schemaRegistry.selectMoreSpecific(definition, defFromType);
}

Expand Down
Expand Up @@ -103,7 +103,7 @@ public boolean canProcess(QName typeName) {
return getSchemaRegistry().determineCompileTimeClass(typeName) != null;
}

public boolean canProcess(Class<?> clazz) {
public boolean canProcess(@NotNull Class<?> clazz) {
return RawType.class.equals(clazz) || clazz.getAnnotation(XmlType.class) != null;
}

Expand Down
Expand Up @@ -52,6 +52,7 @@
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedDataType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @author semancik
Expand Down Expand Up @@ -87,11 +88,11 @@ RootXNode marshalItem(@NotNull Item<?, ?> item, QName itemName,
if (item instanceof PrismObject) {
content = marshalObjectContent((PrismObject) item, (PrismObjectDefinition) realDefinition, context);
} else if (item.size() == 1) {
content = marshalItemValue(item.getValue(0), realDefinition, context);
content = marshalItemValue(item.getValue(0), realDefinition, null, context);
} else {
ListXNode xlist = new ListXNode();
for (PrismValue val : item.getValues()) {
xlist.add(marshalItemValue(val, realDefinition, context));
xlist.add(marshalItemValue(val, realDefinition, null, context));
}
content = xlist;
}
Expand All @@ -101,7 +102,7 @@ RootXNode marshalItem(@NotNull Item<?, ?> item, QName itemName,
RootXNode marshalItemValueAsRoot(@NotNull PrismValue value, @NotNull QName itemName, ItemDefinition itemDefinition,
SerializationContext context) throws SchemaException {
ItemInfo itemInfo = ItemInfo.determineFromValue(value, itemName, itemDefinition, beanConverter.getPrismContext().getSchemaRegistry());
XNode valueNode = marshalItemValue(value, itemInfo.getItemDefinition(), context);
XNode valueNode = marshalItemValue(value, itemInfo.getItemDefinition(), itemInfo.getTypeName(), context);
return new RootXNode(itemName, valueNode);
}

Expand Down Expand Up @@ -148,14 +149,15 @@ private <O extends Objectable> MapXNode marshalObjectContent(@NotNull PrismObjec
}

@NotNull
private <V extends PrismValue> XNode marshalItemValue(@NotNull PrismValue itemValue, ItemDefinition definition, SerializationContext ctx) throws SchemaException {
private <V extends PrismValue> XNode marshalItemValue(@NotNull PrismValue itemValue, @Nullable ItemDefinition definition,
@Nullable QName typeName, SerializationContext ctx) throws SchemaException {
XNode xnode;
if (definition == null && itemValue instanceof PrismPropertyValue) {
if (definition == null && typeName == null && itemValue instanceof PrismPropertyValue) {
return serializePropertyRawValue((PrismPropertyValue<?>) itemValue);
} else if (itemValue instanceof PrismReferenceValue) {
xnode = serializeReferenceValue((PrismReferenceValue)itemValue, (PrismReferenceDefinition) definition, ctx);
} else if (itemValue instanceof PrismPropertyValue<?>) {
xnode = serializePropertyValue((PrismPropertyValue<?>)itemValue, (PrismPropertyDefinition)definition);
xnode = serializePropertyValue((PrismPropertyValue<?>)itemValue, (PrismPropertyDefinition)definition, typeName);
} else if (itemValue instanceof PrismContainerValue<?>) {
xnode = marshalContainerValue((PrismContainerValue<?>)itemValue, (PrismContainerDefinition)definition, ctx);
} else {
Expand Down Expand Up @@ -276,21 +278,22 @@ private QName createReferenceQName(QName qname, String namespace) {
//endregion

//region Serializing properties - specific functionality
private <T> XNode serializePropertyValue(PrismPropertyValue<T> value, PrismPropertyDefinition<T> definition) throws SchemaException {
QName typeQName = definition.getTypeName();
private <T> XNode serializePropertyValue(@NotNull PrismPropertyValue<T> value, PrismPropertyDefinition<T> definition, QName typeNameIfNoDefinition) throws SchemaException {
@Nullable QName typeName = definition != null ? definition.getTypeName() : typeNameIfNoDefinition;
T realValue = value.getValue();
if (realValue instanceof PolyString) {
return serializePolyString((PolyString) realValue);
} else if (beanConverter.canProcess(typeQName)) {
} else if (beanConverter.canProcess(typeName)) {
XNode xnode = beanConverter.marshall(realValue);
if (realValue instanceof ProtectedDataType<?> && (definition == null || definition.isDynamic())) { // why is this?
// why is this?
if (realValue instanceof ProtectedDataType<?> && (definition == null || definition.isDynamic())) {
xnode.setExplicitTypeDeclaration(true);
xnode.setTypeQName(definition.getTypeName());
xnode.setTypeQName(typeName);
}
return xnode;
} else {
// primitive value
return createPrimitiveXNode(realValue, typeQName);
return createPrimitiveXNode(realValue, typeName);
}
}

Expand Down
Expand Up @@ -188,12 +188,15 @@ private <T> T doParseRealValue(Class<T> clazz, RootXNode root) throws IOExceptio
if (clazz == null && info.getTypeName() != null) {
clazz = (Class) prismContext.getSchemaRegistry().determineClassForType(info.getTypeName());
}
if (clazz == null) {
throw new IllegalArgumentException("Couldn't determine type for " + root);
}
// if (clazz == null) {
// throw new IllegalArgumentException("Couldn't determine type for " + root);
// }
}

if (Containerable.class.isAssignableFrom(clazz)) {
PrismBeanConverter beanConverter = prismContext.getBeanConverter();
if (clazz != null && beanConverter.canProcess(clazz)) {
return beanConverter.unmarshall(root, clazz, context);
} else {
PrismValue prismValue = doParseItemValue(root);
if (prismValue == null) {
return null;
Expand All @@ -206,8 +209,6 @@ private <T> T doParseRealValue(Class<T> clazz, RootXNode root) throws IOExceptio
} else {
throw new IllegalStateException("Unsupported value: " + prismValue.getClass());
}
} else {
return prismContext.getBeanConverter().unmarshall(root, clazz, context);
}
}

Expand All @@ -219,7 +220,7 @@ <T> T doParseRealValue() throws IOException, SchemaException {
@SuppressWarnings("unchecked")
<T> JAXBElement<T> doParseAnyValueAsJAXBElement() throws IOException, SchemaException {
RootXNode root = getLexicalProcessor().read(source, context);
T real = (T) doParseRealValue(Object.class, root);
T real = (T) doParseRealValue(null, root);
return real != null ?
new JAXBElement<>(root.getRootElementName(), (Class<T>) real.getClass(), real) :
null;
Expand Down
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2010-2016 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.evolveum.midpoint.prism.schema;

import com.evolveum.midpoint.prism.ItemDefinition;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author mederly
*/
public class DefinitionStoreUtils {
public static <ID extends ItemDefinition> ID getOne(List<ID> list) {
if (list.isEmpty()) {
return null;
} else if (list.size() == 1) {
return list.get(0);
} else {
// remove all deprecated ones
List<ID> notDeprecated = list.stream()
.filter(def -> !def.isDeprecated())
.collect(Collectors.toList());
if (notDeprecated.size() == 1) {
return notDeprecated.get(0);
} else {
throw new IllegalStateException("More than one definition found: " + list);
}
}
}
}
Expand Up @@ -74,7 +74,7 @@ <ID extends ItemDefinition> List<ID> findItemDefinitionsByCompileTimeClass(

default <ID extends ItemDefinition> ID findItemDefinitionByCompileTimeClass(
@NotNull Class<?> compileTimeClass, @NotNull Class<ID> definitionClass) {
return getOne(findItemDefinitionsByCompileTimeClass(compileTimeClass, definitionClass));
return DefinitionStoreUtils.getOne(findItemDefinitionsByCompileTimeClass(compileTimeClass, definitionClass));
}

@SuppressWarnings("unchecked")
Expand All @@ -99,16 +99,6 @@ default <C extends Containerable> PrismContainerDefinition<C> findContainerDefin
return findItemDefinitionByCompileTimeClass(compileTimeClass, PrismContainerDefinition.class);
}

static <ID extends ItemDefinition> ID getOne(List<ID> list) {
if (list.isEmpty()) {
return null;
} else if (list.size() == 1) {
return list.get(0);
} else {
throw new IllegalStateException("More than one definition found: " + list);
}
}

@SuppressWarnings("unchecked")
default <C extends Containerable> PrismContainerDefinition<C> findContainerDefinitionByType(@NotNull QName typeName) {
return findItemDefinitionByType(typeName, PrismContainerDefinition.class);
Expand Down
Expand Up @@ -92,10 +92,12 @@ <T extends Containerable> ItemDefinition locateItemDefinition(@NotNull QName ite

/**
* This method will try to locate the appropriate object definition and apply it.
* @param container
* @param type
*/
<O extends Objectable> void applyDefinition(PrismObject<O> prismObject, Class<O> type) throws SchemaException;
<C extends Containerable> void applyDefinition(PrismContainer<C> container, Class<C> type) throws SchemaException;

<O extends Objectable> void applyDefinition(PrismObject<O> prismObject, Class<O> type, boolean force) throws SchemaException;
<C extends Containerable> void applyDefinition(PrismContainer<C> prismObject, Class<C> type, boolean force) throws SchemaException;

<T extends Objectable> void applyDefinition(ObjectDelta<T> objectDelta, Class<T> type, boolean force) throws SchemaException;

Expand Down

0 comments on commit fdc202b

Please sign in to comment.