use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class BeanUnmarshaller method unmarshalEnumFromPrimitive.
private <T> T unmarshalEnumFromPrimitive(PrimitiveXNode prim, Class<T> beanClass, ParsingContext pc) throws SchemaException {
String primValue = (String) prim.getParsedValue(DOMUtil.XSD_STRING, String.class);
primValue = StringUtils.trim(primValue);
if (StringUtils.isEmpty(primValue)) {
return null;
}
String javaEnumString = inspector.findEnumFieldName(beanClass, primValue);
if (javaEnumString == null) {
for (Field field : beanClass.getDeclaredFields()) {
if (field.getName().equals(primValue)) {
javaEnumString = field.getName();
break;
}
}
}
if (javaEnumString == null) {
throw new SchemaException("Cannot find enum value for string '" + primValue + "' in " + beanClass);
}
@SuppressWarnings("unchecked") T bean = (T) Enum.valueOf((Class<Enum>) beanClass, javaEnumString);
return bean;
}
use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class ItemInfo method augmentWithClass.
private static <ID extends ItemDefinition> ID augmentWithClass(ID definition, Class<ID> definitionClass, SchemaRegistry schemaRegistry, Class<?> clazz) throws SchemaException {
if (clazz == null) {
return definition;
}
if (definition instanceof PrismContainerDefinition && clazz.equals(((PrismContainerDefinition) definition).getCompileTimeClass())) {
return definition;
}
QName typeNameFromClass = schemaRegistry.determineTypeForClass(clazz);
if (typeNameFromClass == null) {
throw new SchemaException("Unknown class " + clazz.getName());
}
if (definition != null && definition.getTypeName().equals(typeNameFromClass)) {
return definition;
}
@SuppressWarnings("unchecked") List<ID> defFromClass = schemaRegistry.findItemDefinitionsByCompileTimeClass((Class) clazz, (Class) definitionClass);
if (defFromClass.size() != 1) {
return definition;
} else {
return schemaRegistry.selectMoreSpecific(definition, defFromClass.get(0));
}
}
use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class JaxbDomHack method parseRawElement.
/**
* This is used in a form of "fromAny" to parse elements from a JAXB getAny method to prism.
*/
public <IV extends PrismValue, ID extends ItemDefinition, C extends Containerable> Item<IV, ID> parseRawElement(Object element, PrismContainerDefinition<C> definition) throws SchemaException {
Validate.notNull(definition, "Attempt to parse raw element in a container without definition");
QName elementName = JAXBUtil.getElementQName(element);
ItemDefinition itemDefinition = definition.findItemDefinition(elementName);
if (itemDefinition == null) {
itemDefinition = locateItemDefinition(definition, elementName, element);
if (itemDefinition == null) {
throw new SchemaException("No definition for item " + elementName);
}
}
PrismContext prismContext = definition.getPrismContext();
Item<IV, ID> subItem;
if (element instanceof Element) {
// DOM Element
subItem = prismContext.parserFor((Element) element).name(elementName).definition(itemDefinition).parseItem();
} else if (element instanceof JAXBElement<?>) {
// JAXB Element
JAXBElement<?> jaxbElement = (JAXBElement<?>) element;
Object jaxbBean = jaxbElement.getValue();
if (itemDefinition == null) {
throw new SchemaException("No definition for item " + elementName + " in container " + definition + " (parsed from raw element)", elementName);
}
if (itemDefinition instanceof PrismPropertyDefinition<?>) {
// property
PrismProperty<?> property = ((PrismPropertyDefinition<?>) itemDefinition).instantiate();
property.setRealValue(jaxbBean);
subItem = (Item<IV, ID>) property;
} else if (itemDefinition instanceof PrismContainerDefinition<?>) {
if (jaxbBean instanceof Containerable) {
PrismContainer<?> container = ((PrismContainerDefinition<?>) itemDefinition).instantiate();
PrismContainerValue subValue = ((Containerable) jaxbBean).asPrismContainerValue();
container.add(subValue);
subItem = (Item<IV, ID>) container;
} else {
throw new IllegalArgumentException("Unsupported JAXB bean " + jaxbBean.getClass());
}
} else if (itemDefinition instanceof PrismReferenceDefinition) {
// TODO
if (jaxbBean instanceof Referencable) {
PrismReference reference = ((PrismReferenceDefinition) itemDefinition).instantiate();
PrismReferenceValue refValue = ((Referencable) jaxbBean).asReferenceValue();
reference.merge(refValue);
subItem = (Item<IV, ID>) reference;
} else {
throw new IllegalArgumentException("Unsupported JAXB bean" + jaxbBean);
}
} else {
throw new IllegalArgumentException("Unsupported definition type " + itemDefinition.getClass());
}
} else {
throw new IllegalArgumentException("Unsupported element type " + element.getClass());
}
return subItem;
}
use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class PrismUnmarshaller method setReferenceObject.
private void setReferenceObject(PrismReferenceValue refVal, PrismObject<Objectable> object) throws SchemaException {
refVal.setObject(object);
if (object.getOid() != null) {
if (refVal.getOid() == null) {
refVal.setOid(object.getOid());
} else {
if (!refVal.getOid().equals(object.getOid())) {
throw new SchemaException("OID in reference (" + refVal.getOid() + ") does not match OID in composite object (" + object.getOid() + ")");
}
}
}
QName objectTypeName = object.getDefinition().getTypeName();
if (refVal.getTargetType() == null) {
refVal.setTargetType(objectTypeName);
} else {
if (!refVal.getTargetType().equals(objectTypeName)) {
throw new SchemaException("Target type in reference (" + refVal.getTargetType() + ") does not match type in composite object (" + objectTypeName + ")");
}
}
}
use of com.evolveum.midpoint.util.exception.SchemaException 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;
}
Aggregations