com.memority.citadel.shared.api.context.OperationContext Maven / Gradle / Ivy
Show all versions of citadel-api Show documentation
/*
* Copyright (c) 2016-2023 Memority. All Rights Reserved.
*
* This file is part of Memority Citadel API , a Memority project.
*
* This file is released under the Memority Public Artifacts End-User License Agreement,
* see
* Unauthorized copying of this file, via any medium is strictly prohibited.
*/
package com.memority.citadel.shared.api.context;
import com.memority.citadel.shared.api.im.ApiObject;
import com.memority.citadel.shared.api.im.AttributeValue;
import com.memority.citadel.shared.api.im.HasAttributeValues;
import com.memority.citadel.shared.api.im.ObjectKind;
import com.memority.citadel.shared.api.im.operation.ObjectOperation;
import com.memority.citadel.shared.api.im.operation.ObjectPatch;
import com.memority.toolkit.rule.api.context.ContextConfigurer;
import org.apache.commons.lang3.Validate;
import java.util.Arrays;
import java.util.List;
/**
* This context provides information about an operation performed on a Managed Object. It holds
* all of the current object apiObject, potentially being worked on, as well as the original
* apiObject, as existing in the IDM repository (if available).
*
* All identifier values are exposed as strings in those attribute values (this includes
* the object id, the object type id, the entity id and all apiObject that reference
* other objects.
*
Note: this context is immutable.
*/
public class OperationContext {
private ObjectKind kind;
private String type;
private String id;
private ObjectOperation operation;
private ApiObject apiObject;
private boolean objectIsMutable = false;
private ApiObject originalApiObject;
private ObjectPatch objectPatch;
public OperationContext(ObjectKind objectKind) {
Validate.notNull(objectKind);
this.kind = objectKind;
}
/**
* @deprecated use {@link #getObject()} instead
* @return a deprecated attribute container
*/
@Deprecated
public HasAttributeValues getSnapshot() {
return getObject() == null ? null : new SimpleAttributeValues(getObject().attributeValues());
}
/**
* Same as {@link #getObject()}, for compatibility and semantically nicer method calls.
*
* @return the current object apiObject
*/
public ApiObject getAttributes() {
return getObject();
}
/**
* @return the current object apiObject, maybe null
*/
public ApiObject getObject() {
if (apiObject == null) {
return null;
}
if (objectIsMutable) {
return apiObject;
} else {
return apiObject.immutable();
}
}
/**
* Same as {@link #getOriginalObject()}, for compatibility and semantically nicer method calls.
*
* @return the original object apiObject, maybe null
*/
public ApiObject getOriginalAttributes() {
return getOriginalObject();
}
/**
* Note: this is only available in the context of a PATCH operation.
*
* @return the original object apiObject, maybe null
*/
@SuppressWarnings("unused")
public ApiObject getOriginalObject() {
if (originalApiObject == null) {
// The original object is not available in the current context
return null;
}
return originalApiObject.immutable();
}
/**
* The current object patch if any
*
* @return the converted object patch
*/
public ObjectPatch getObjectPatch() {
return objectPatch;
}
/**
* @return the kind of object being worked on, never null
*/
public ObjectKind getKind() {
return kind;
}
/**
* Note: may be null if it has not been generated already.
*
* @return the object id, as a String, may be null
*/
public String getId() {
return id;
}
/**
* @return the type of the object being worked on (never null
)
*/
public String getType() {
return type;
}
/**
* @return the object operation being performed (never null
)
*/
public ObjectOperation getOperation() {
return operation;
}
/**
* @return a standalone configurer of this context.
*/
public OperationContextConfigurer configure() {
return new OperationContextConfigurerImpl<>(this);
}
/**
* @param returningContext the parent, returning context
* @param the type of returning context
* @return a configurer of this context with the given parent
*/
public OperationContextConfigurer configureFrom(C returningContext) {
return new OperationContextConfigurerImpl<>(returningContext);
}
public interface OperationContextConfigurer extends ContextConfigurer {
OperationContextConfigurer doing(ObjectOperation op);
OperationContextConfigurer withObject(ApiObject object);
OperationContextConfigurer withMutableObject(ApiObject object);
OperationContextConfigurer withOriginalObject(ApiObject originalObject);
OperationContextConfigurer withObjectPatch(ObjectPatch objectPatch);
default OperationContextConfigurer withObject(AttributeValue>... values) {
return withObject(Arrays.asList(values));
}
default OperationContextConfigurer withObject(List> values) {
return withObject(new ApiObject(values));
}
@SuppressWarnings("unused")
default OperationContextConfigurer withOriginalObject(AttributeValue>... values) {
return withOriginalObject(new ApiObject(Arrays.asList(values)));
}
default C end() {
return and();
}
}
class OperationContextConfigurerImpl implements OperationContextConfigurer {
private C returningContext;
public OperationContextConfigurerImpl(C returningContext) {
this.returningContext = returningContext;
}
@Override
public OperationContextConfigurer doing(ObjectOperation op) {
OperationContext.this.operation = op;
return this;
}
private OperationContextConfigurer identifiedAs(ApiObject object) {
if (object == null) {
return this;
}
OperationContext.this.type = object.getType();
OperationContext.this.id = object.getId();
return this;
}
@Override
public OperationContextConfigurer withObject(ApiObject object) {
if (object == null) {
return this;
}
OperationContext.this.apiObject = object;
identifiedAs(object);
return this;
}
@Override
public OperationContextConfigurer withMutableObject(ApiObject object) {
this.withObject(object);
OperationContext.this.objectIsMutable = true;
return this;
}
@Override
public OperationContextConfigurer withOriginalObject(ApiObject originalObject) {
if (originalObject == null) {
return this;
}
OperationContext.this.originalApiObject = originalObject;
identifiedAs(originalObject);
return this;
}
@Override
public OperationContextConfigurer withObjectPatch(ObjectPatch objectPatch) {
if (objectPatch == null) {
return this;
}
OperationContext.this.objectPatch = objectPatch;
return this;
}
@Override
public C and() {
return this.returningContext;
}
}
}