Skip to content

Commit

Permalink
Many expressions/report/archetypes improvements:
Browse files Browse the repository at this point in the history
* ArchetypeManager
* Adapting ModelInteractionService and model utils code
* Fixing report-impl for proper use of expressions
* improving report-impl tests
* some work on introducing profiles to report-impl
* cleaning up expression code (but still quite a mess)
  • Loading branch information
semancik committed Mar 25, 2019
1 parent 77c9b4f commit 0e730a8
Show file tree
Hide file tree
Showing 50 changed files with 1,078 additions and 1,664 deletions.
Expand Up @@ -17,10 +17,6 @@

import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.AssignmentCandidatesSpecification;
import com.evolveum.midpoint.model.api.util.ModelContextUtil;
import com.evolveum.midpoint.model.api.util.ModelUtils;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.logging.LoggingUtils;
Expand Down
Expand Up @@ -56,14 +56,6 @@ public void add(ExpressionEvaluatorProfile evaluatorProfile) {
evaluatorProfiles.add(evaluatorProfile);
}

// public AccessDecision decideEvaluator(QName type) {
// ExpressionEvaluatorProfile evaluatorProfile = getEvaluatorProfile(type);
// if (evaluatorProfile == null) {
// return decision;
// }
// return evaluatorProfile.getDecision();
// }

public ExpressionEvaluatorProfile getEvaluatorProfile(QName type) {
for (ExpressionEvaluatorProfile evaluatorProfile : evaluatorProfiles) {
if (QNameUtil.match(evaluatorProfile.getType(), type)) {
Expand All @@ -78,99 +70,6 @@ public String toString() {
return "ExpressionProfile(" + identifier + ")";
}












// private final List<Rule> classAccessRules = new ArrayList<>();
// private AccessDecision defaultClassAccessDecision = AccessDecision.DEFAULT;
// private boolean groovyTypeChecking;
//
//
//
// public AccessDecision getDefaultClassAccessDecision() {
// return defaultClassAccessDecision;
// }
//
// public void setDefaultClassAccessDecision(AccessDecision defaultClassAccessDecision) {
// this.defaultClassAccessDecision = defaultClassAccessDecision;
// }
//
// public boolean isGroovyTypeChecking() {
// return groovyTypeChecking;
// }
//
// public void setGroovyTypeChecking(boolean groovyTypeChecking) {
// this.groovyTypeChecking = groovyTypeChecking;
// }
//
// public AccessDecision decideClassAccess(String className, String methodName) {
// for (Rule rule : classAccessRules) {
// if (rule.match(className, methodName)) {
// return rule.getDecision();
// }
// }
// return defaultClassAccessDecision;
// }
//
// /**
// * Used to easily set up access for built-in class access rules.
// */
// public void addClassAccessRule(String className, String methodName, AccessDecision decision) {
// classAccessRules.add(new Rule(className, methodName, decision));
// }
//
// /**
// * Used to easily set up access for built-in class access rules (convenience).
// */
// public void addClassAccessRule(Class<?> clazz, String methodName, AccessDecision decision) {
// addClassAccessRule(clazz.getName(), methodName, decision);
// }
//
//
//
// class Rule {
// private final String className;
// private final String methodName;
// private final AccessDecision decision;
//
// public Rule(String className, String methodName, AccessDecision decision) {
// super();
// this.className = className;
// this.methodName = methodName;
// this.decision = decision;
// }
//
// public String getClassName() {
// return className;
// }
//
// public String getMethodName() {
// return methodName;
// }
//
// public AccessDecision getDecision() {
// return decision;
// }
//
// public boolean match(String aClassName, String aMethodName) {
// if (!aClassName.equals(className)) {
// return false;
// }
// if (methodName == null) {
// return true;
// }
// return methodName.equals(aMethodName);
// }
//
// }


}
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2019 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.schema.expression;

import java.util.HashMap;
import java.util.Map;

/**
* @author semancik
*
*/
public class ExpressionProfiles {

private Map<String,ExpressionProfile> profiles = new HashMap<>();

public ExpressionProfile getProfile(String identifier) {
return profiles.get(identifier);
}

public void add(ExpressionProfile profile) {
profiles.put(profile.getIdentifier(), profile);
}
}
Expand Up @@ -75,18 +75,21 @@ public TypedValue(Item<?, ?> prismItem) {

public TypedValue(Object value, ItemDefinition<?> definition) {
super();
validateValue(value);
this.value = value;
this.definition = definition;
}

public TypedValue(Object value, Class<T> typeClass) {
super();
validateValue(value);
this.value = value;
this.typeClass = typeClass;
}

public TypedValue(Object value, ItemDefinition<?> definition, Class<T> typeClass) {
super();
validateValue(value);
this.value = value;
this.definition = definition;
this.typeClass = typeClass;
Expand All @@ -97,9 +100,19 @@ public Object getValue() {
}

public void setValue(Object value) {
validateValue(value);
this.value = value;
}

private void validateValue(Object value) {
if (value == null) {
return;
}
if (value instanceof TypedValue) {
throw new IllegalArgumentException("TypedValue in TypedValue in "+this);
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public <D extends ItemDefinition> D getDefinition() {
return (D) definition;
Expand Down
Expand Up @@ -89,6 +89,9 @@ public TypedValue put(String key, TypedValue typedValue) {

@SuppressWarnings("rawtypes")
public <D extends ItemDefinition> TypedValue put(String key, Object value, D definition) {
if (definition == null) {
throw new IllegalArgumentException("Attempt to set variable '"+key+"' without definition");
}
return variables.put(key, new TypedValue<>(value, definition));
}

Expand All @@ -98,6 +101,9 @@ public <D extends ItemDefinition> TypedValue put(String key, Object value, D def
* of the value precisely.
*/
public <T> TypedValue put(String key, Object value, Class<T> typeClass) {
if (typeClass == null) {
throw new IllegalArgumentException("Attempt to set variable '"+key+"' without class specification");
}
return variables.put(key, new TypedValue<>(value, typeClass));
}

Expand Down
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2010-2018 Evolveum
* Copyright (c) 2019 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,17 +13,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.model.api.util;
package com.evolveum.midpoint.model.common;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.FocusTypeUtil;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ArchetypePolicyType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ArchetypeType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LifecycleStateModelType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectPolicyConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
Expand All @@ -32,11 +42,69 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;

/**
* @author semancik
*
* Component that can efficiently determine archetypes for objects.
* It is backed by caches, therefore this is supposed to be a low-overhead service that can be
* used in many places.
*
* @author Radovan Semancik
*/
public class ModelUtils {
@Component
public class ArchetypeManager {

private static final Trace LOGGER = TraceManager.getTrace(ArchetypeManager.class);

@Autowired private SystemObjectCache systemObjectCache;

public <O extends AssignmentHolderType> PrismObject<ArchetypeType> determineArchetype(PrismObject<O> assignmentHolder, OperationResult result) throws SchemaException, ConfigurationException {
if (assignmentHolder == null) {
return null;
}
if (!assignmentHolder.canRepresent(AssignmentHolderType.class)) {
return null;
}
List<ObjectReferenceType> archetypeRefs = ((AssignmentHolderType)assignmentHolder.asObjectable()).getArchetypeRef();
if (archetypeRefs == null || archetypeRefs.isEmpty()) {
return null;
}
if (archetypeRefs.size() > 1) {
throw new SchemaException("Only a single archetype for an object is supported: "+assignmentHolder);
}
ObjectReferenceType archetypeRef = archetypeRefs.get(0);

PrismObject<ArchetypeType> archetype;
try {
archetype = systemObjectCache.getArchetype(archetypeRef.getOid(), result);
} catch (ObjectNotFoundException e) {
LOGGER.warn("Archetype {} for object {} cannot be found", archetypeRef.getOid(), assignmentHolder);
return null;
}
return archetype;
}

public <O extends ObjectType> ArchetypePolicyType determineArchetypePolicy(PrismObject<O> object, OperationResult result) throws SchemaException, ConfigurationException {
if (object == null) {
return null;
}
if (object.canRepresent(AssignmentHolderType.class)) {
PrismObject<ArchetypeType> archetype = determineArchetype((PrismObject<? extends AssignmentHolderType>) object, result);
if (archetype != null) {
return archetype.asObjectable().getArchetypePolicy();
}
}
// No archetype for this object. Try to find appropriate system configuration section for this object.
return determineObjectPolicyConfiguration(object, result);
}

public <O extends ObjectType> ObjectPolicyConfigurationType determineObjectPolicyConfiguration(PrismObject<O> object, OperationResult result) throws SchemaException, ConfigurationException {
if (object == null) {
return null;
}
return determineObjectPolicyConfiguration(object, systemObjectCache.getSystemConfiguration(result).asObjectable());
}

/**
* This has to remain static due to use from LensContext. Hopefully it will get refactored later.
*/
public static <O extends ObjectType> ObjectPolicyConfigurationType determineObjectPolicyConfiguration(PrismObject<O> object, SystemConfigurationType systemConfigurationType) throws ConfigurationException {
List<String> subTypes = FocusTypeUtil.determineSubTypes(object);
return determineObjectPolicyConfiguration(object.getCompileTimeClass(), subTypes, systemConfigurationType);
Expand Down Expand Up @@ -111,5 +179,4 @@ public static <O extends ObjectType> LifecycleStateModelType determineLifecycleM
}
return objectPolicyConfiguration.getLifecycleStateModel();
}

}

0 comments on commit 0e730a8

Please sign in to comment.