Search in sources :

Example 1 with CompositeRefinedObjectClassDefinition

use of com.evolveum.midpoint.common.refinery.CompositeRefinedObjectClassDefinition in project midpoint by Evolveum.

the class ContainerWrapperFactory method createProperties.

private List<ItemWrapper> createProperties(ContainerWrapper cWrapper, OperationResult result) {
    ObjectWrapper objectWrapper = cWrapper.getObject();
    PrismContainer container = cWrapper.getItem();
    PrismContainerDefinition containerDefinition = cWrapper.getItemDefinition();
    List<ItemWrapper> properties = new ArrayList<>();
    PrismContainerDefinition definition;
    if (objectWrapper == null) {
        definition = containerDefinition;
    } else {
        PrismObject parent = objectWrapper.getObject();
        Class clazz = parent.getCompileTimeClass();
        if (ShadowType.class.isAssignableFrom(clazz)) {
            QName name = containerDefinition.getName();
            if (ShadowType.F_ATTRIBUTES.equals(name)) {
                try {
                    definition = objectWrapper.getRefinedAttributeDefinition();
                    if (definition == null) {
                        PrismReference resourceRef = parent.findReference(ShadowType.F_RESOURCE_REF);
                        PrismObject<ResourceType> resource = resourceRef.getValue().getObject();
                        definition = modelServiceLocator.getModelInteractionService().getEditObjectClassDefinition((PrismObject<ShadowType>) objectWrapper.getObject(), resource, AuthorizationPhaseType.REQUEST).toResourceAttributeContainerDefinition();
                        if (LOGGER.isTraceEnabled()) {
                            LOGGER.trace("Refined account def:\n{}", definition.debugDump());
                        }
                    }
                } catch (Exception ex) {
                    LoggingUtils.logUnexpectedException(LOGGER, "Couldn't load definitions from refined schema for shadow", ex);
                    result.recordFatalError("Couldn't load definitions from refined schema for shadow, reason: " + ex.getMessage(), ex);
                    return properties;
                }
            } else {
                definition = containerDefinition;
            }
        } else if (ResourceType.class.isAssignableFrom(clazz)) {
            if (containerDefinition != null) {
                definition = containerDefinition;
            } else {
                definition = container.getDefinition();
            }
        } else {
            definition = containerDefinition;
        }
    }
    if (definition == null) {
        LOGGER.error("Couldn't get property list from null definition {}", new Object[] { container.getElementName() });
        return properties;
    }
    // they are filtered out by ObjectWrapper)
    if (container.getCompileTimeClass() != null && AssignmentType.class.isAssignableFrom(container.getCompileTimeClass())) {
        for (Object o : container.getValues()) {
            PrismContainerValue<AssignmentType> pcv = (PrismContainerValue<AssignmentType>) o;
            AssignmentType assignmentType = pcv.asContainerable();
            if (assignmentType.getTargetRef() == null) {
                continue;
            }
            // hack... we want to create a definition for Name
            // PrismPropertyDefinition def = ((PrismContainerValue)
            // pcv.getContainer().getParent()).getContainer().findProperty(ObjectType.F_NAME).getDefinition();
            PrismPropertyDefinitionImpl def = new PrismPropertyDefinitionImpl(ObjectType.F_NAME, DOMUtil.XSD_STRING, pcv.getPrismContext());
            if (OrgType.COMPLEX_TYPE.equals(assignmentType.getTargetRef().getType())) {
                def.setDisplayName("Org.Unit");
                def.setDisplayOrder(100);
            } else if (RoleType.COMPLEX_TYPE.equals(assignmentType.getTargetRef().getType())) {
                def.setDisplayName("Role");
                def.setDisplayOrder(200);
            } else {
                continue;
            }
            PrismProperty<Object> temp = def.instantiate();
            String value = formatAssignmentBrief(assignmentType);
            temp.setValue(new PrismPropertyValue<Object>(value));
            // TODO: do this.isReadOnly() - is that OK? (originally it was the default behavior for all cases)
            properties.add(new PropertyWrapper(cWrapper, temp, cWrapper.isReadonly(), ValueStatus.NOT_CHANGED));
        }
    } else if (isShadowAssociation(cWrapper)) {
        // HACK: this should not be here. Find a better place.
        cWrapper.setDisplayName("prismContainer.shadow.associations");
        PrismContext prismContext = objectWrapper.getObject().getPrismContext();
        Map<QName, PrismContainer<ShadowAssociationType>> assocMap = new HashMap<>();
        PrismContainer<ShadowAssociationType> associationContainer = cWrapper.getItem();
        if (associationContainer != null && associationContainer.getValues() != null) {
            // Do NOT load shadows here. This will be huge overhead if there are many associations.
            // Load them on-demand (if necessary at all).
            List<PrismContainerValue<ShadowAssociationType>> associations = associationContainer.getValues();
            if (associations != null) {
                for (PrismContainerValue<ShadowAssociationType> cval : associations) {
                    ShadowAssociationType associationType = cval.asContainerable();
                    QName assocName = associationType.getName();
                    PrismContainer<ShadowAssociationType> fractionalContainer = assocMap.get(assocName);
                    if (fractionalContainer == null) {
                        fractionalContainer = new PrismContainer<>(ShadowType.F_ASSOCIATION, ShadowAssociationType.class, cval.getPrismContext());
                        fractionalContainer.setDefinition(cval.getParent().getDefinition());
                        // HACK: set the name of the association as the element name so wrapper.getName() will return correct data.
                        fractionalContainer.setElementName(assocName);
                        assocMap.put(assocName, fractionalContainer);
                    }
                    try {
                        fractionalContainer.add(cval.clone());
                    } catch (SchemaException e) {
                        // Should not happen
                        throw new SystemException("Unexpected error: " + e.getMessage(), e);
                    }
                }
            }
        }
        PrismReference resourceRef = objectWrapper.getObject().findReference(ShadowType.F_RESOURCE_REF);
        PrismObject<ResourceType> resource = resourceRef.getValue().getObject();
        // of resources.
        try {
            resource.revive(prismContext);
        } catch (SchemaException e) {
            throw new SystemException(e.getMessage(), e);
        }
        RefinedResourceSchema refinedSchema;
        CompositeRefinedObjectClassDefinition rOcDef;
        try {
            refinedSchema = RefinedResourceSchemaImpl.getRefinedSchema(resource);
            rOcDef = refinedSchema.determineCompositeObjectClassDefinition(objectWrapper.getObject());
        } catch (SchemaException e) {
            throw new SystemException(e.getMessage(), e);
        }
        // Make sure even empty associations have their wrappers so they can be displayed and edited
        for (RefinedAssociationDefinition assocDef : rOcDef.getAssociationDefinitions()) {
            QName name = assocDef.getName();
            if (!assocMap.containsKey(name)) {
                PrismContainer<ShadowAssociationType> fractionalContainer = new PrismContainer<>(ShadowType.F_ASSOCIATION, ShadowAssociationType.class, prismContext);
                fractionalContainer.setDefinition(cWrapper.getItemDefinition());
                // HACK: set the name of the association as the element name so wrapper.getName() will return correct data.
                fractionalContainer.setElementName(name);
                assocMap.put(name, fractionalContainer);
            }
        }
        for (Map.Entry<QName, PrismContainer<ShadowAssociationType>> assocEntry : assocMap.entrySet()) {
            RefinedAssociationDefinition assocRDef = rOcDef.findAssociationDefinition(assocEntry.getKey());
            AssociationWrapper assocWrapper = new AssociationWrapper(cWrapper, assocEntry.getValue(), cWrapper.isReadonly(), ValueStatus.NOT_CHANGED, assocRDef);
            properties.add(assocWrapper);
        }
    } else {
        if ((container.getValues().size() == 1 || container.getValues().isEmpty()) && (containerDefinition == null || containerDefinition.isSingleValue())) {
            // there's no point in showing properties for non-single-valued
            // parent containers,
            // so we continue only if the parent is single-valued
            Collection<ItemDefinition> propertyDefinitions = definition.getDefinitions();
            for (ItemDefinition itemDef : propertyDefinitions) {
                //TODO temporary decision to hide adminGuiConfiguration attribute (MID-3305)
                if (itemDef != null && itemDef.getName() != null && itemDef.getName().getLocalPart() != null && itemDef.getName().getLocalPart().equals("adminGuiConfiguration")) {
                    continue;
                }
                if (itemDef instanceof PrismPropertyDefinition) {
                    PrismPropertyDefinition def = (PrismPropertyDefinition) itemDef;
                    if (def.isIgnored() || skipProperty(def)) {
                        continue;
                    }
                    if (!cWrapper.isShowInheritedObjectAttributes() && INHERITED_OBJECT_ATTRIBUTES.contains(def.getName())) {
                        continue;
                    }
                    // capability handling for activation properties
                    if (isShadowActivation(cWrapper) && !hasActivationCapability(cWrapper, def)) {
                        continue;
                    }
                    if (isShadowAssociation(cWrapper)) {
                        continue;
                    }
                    PrismProperty property = container.findProperty(def.getName());
                    boolean propertyIsReadOnly;
                    // to an existing object)
                    if (objectWrapper == null || objectWrapper.getStatus() == ContainerStatus.MODIFYING) {
                        propertyIsReadOnly = cWrapper.isReadonly() || !def.canModify();
                    } else {
                        propertyIsReadOnly = cWrapper.isReadonly() || !def.canAdd();
                    }
                    if (property == null) {
                        properties.add(new PropertyWrapper(cWrapper, def.instantiate(), propertyIsReadOnly, ValueStatus.ADDED));
                    } else {
                        properties.add(new PropertyWrapper(cWrapper, property, propertyIsReadOnly, ValueStatus.NOT_CHANGED));
                    }
                } else if (itemDef instanceof PrismReferenceDefinition) {
                    PrismReferenceDefinition def = (PrismReferenceDefinition) itemDef;
                    if (INHERITED_OBJECT_ATTRIBUTES.contains(def.getName())) {
                        continue;
                    }
                    PrismReference reference = container.findReference(def.getName());
                    boolean propertyIsReadOnly;
                    // to an existing object)
                    if (objectWrapper == null || objectWrapper.getStatus() == ContainerStatus.MODIFYING) {
                        propertyIsReadOnly = !def.canModify();
                    } else {
                        propertyIsReadOnly = !def.canAdd();
                    }
                    if (reference == null) {
                        properties.add(new ReferenceWrapper(cWrapper, def.instantiate(), propertyIsReadOnly, ValueStatus.ADDED));
                    } else {
                        properties.add(new ReferenceWrapper(cWrapper, reference, propertyIsReadOnly, ValueStatus.NOT_CHANGED));
                    }
                }
            }
        }
    }
    Collections.sort(properties, new ItemWrapperComparator());
    result.recomputeStatus();
    return properties;
}
Also used : CompositeRefinedObjectClassDefinition(com.evolveum.midpoint.common.refinery.CompositeRefinedObjectClassDefinition) RefinedAssociationDefinition(com.evolveum.midpoint.common.refinery.RefinedAssociationDefinition) SystemException(com.evolveum.midpoint.util.exception.SystemException) RefinedResourceSchema(com.evolveum.midpoint.common.refinery.RefinedResourceSchema) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QName(javax.xml.namespace.QName) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Aggregations

CompositeRefinedObjectClassDefinition (com.evolveum.midpoint.common.refinery.CompositeRefinedObjectClassDefinition)1 RefinedAssociationDefinition (com.evolveum.midpoint.common.refinery.RefinedAssociationDefinition)1 RefinedResourceSchema (com.evolveum.midpoint.common.refinery.RefinedResourceSchema)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 QName (javax.xml.namespace.QName)1