Skip to content

Commit

Permalink
Prism cloneComplex, fixing object template handling in model-intest
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Feb 21, 2018
1 parent 601b1df commit c6b50aa
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 60 deletions.
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2018 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;

/**
* @author semancik
*
*/
public enum CloneStrategy {

/**
* Literal clone. All properties of the clone are the same as those of the original.
*/
LITERAL,

/**
* Clone for reuse.
* Create clone of the object that is suitable to be reused
* in a different object or delta. The cloned object will
* have the same values, but it will not be presented as the
* same object as was the source of cloning.
*
* E.g. in case of containers it will create a container
* with the same values but with not identifiers.
* References will not have full object inside them.
*/
REUSE;

}
11 changes: 10 additions & 1 deletion infra/prism/src/main/java/com/evolveum/midpoint/prism/Item.java
Expand Up @@ -678,9 +678,18 @@ public void revive(PrismContext prismContext) throws SchemaException {
}
}

/**
* Literal clone.
*/
public abstract Item clone();

protected void copyValues(Item clone) {
/**
* Complex clone with different cloning strategies.
* @see CloneStrategy
*/
public abstract Item cloneComplex(CloneStrategy strategy);

protected void copyValues(CloneStrategy strategy, Item clone) {
clone.elementName = this.elementName;
clone.definition = this.definition;
clone.prismContext = this.prismContext;
Expand Down
Expand Up @@ -753,17 +753,22 @@ public List<? extends ItemDelta> diffModifications(PrismContainer<C> other, bool

@Override
public PrismContainer<C> clone() {
PrismContainer<C> clone = new PrismContainer<C>(getElementName(), getDefinition(), prismContext);
copyValues(clone);
return cloneComplex(CloneStrategy.LITERAL);
}

@Override
public PrismContainer<C> cloneComplex(CloneStrategy strategy) {
PrismContainer<C> clone = new PrismContainer<>(getElementName(), getDefinition(), prismContext);
copyValues(strategy, clone);
return clone;
}

protected void copyValues(PrismContainer<C> clone) {
super.copyValues(clone);
protected void copyValues(CloneStrategy strategy, PrismContainer<C> clone) {
super.copyValues(strategy, clone);
clone.compileTimeClass = this.compileTimeClass;
for (PrismContainerValue<C> pval : getValues()) {
try {
clone.add(pval.clone());
clone.add(pval.cloneComplex(strategy));
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2017 Evolveum
* Copyright (c) 2010-2018 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1335,19 +1335,27 @@ public void assertDefinitions(boolean tolerateRaw, String sourceDescription) thr
}
}

public PrismContainerValue<C> clone() { // TODO resolve also the definition?
PrismContainerValue<C> clone = new PrismContainerValue<>(getOriginType(), getOriginObject(), getParent(), getId(),
@Override
public PrismContainerValue<C> clone() {
return cloneComplex(CloneStrategy.LITERAL);
}

@Override
public PrismContainerValue<C> cloneComplex(CloneStrategy strategy) { // TODO resolve also the definition?
PrismContainerValue<C> clone = new PrismContainerValue<>(getOriginType(), getOriginObject(), getParent(), null,
this.complexTypeDefinition, this.prismContext);
copyValues(clone);
copyValues(strategy, clone);
return clone;
}

protected void copyValues(PrismContainerValue<C> clone) {
super.copyValues(clone);
clone.id = this.id;
protected void copyValues(CloneStrategy strategy, PrismContainerValue<C> clone) {
super.copyValues(strategy, clone);
if (strategy == CloneStrategy.LITERAL) {
clone.id = this.id;
}
if (this.items != null) {
for (Item<?,?> item : this.items) {
Item<?,?> clonedItem = item.clone();
Item<?,?> clonedItem = item.cloneComplex(strategy);
clonedItem.setParent(clone);
if (clone.items == null) {
clone.items = new ArrayList<>(this.items.size());
Expand All @@ -1356,7 +1364,7 @@ protected void copyValues(PrismContainerValue<C> clone) {
}
}
}

protected void deepCloneDefinition(boolean ultraDeep, PrismContainerDefinition<C> clonedContainerDef, Consumer<ItemDefinition> postCloneAction) {
// special treatment of CTD (we must not simply overwrite it with clonedPCD.CTD!)
PrismContainerable parent = getParent();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2017 Evolveum
* Copyright (c) 2010-2018 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -229,12 +229,17 @@ public void addReplaceExisting(Item<?,?> item) throws SchemaException {

@Override
public PrismObject<O> clone() {
return cloneComplex(CloneStrategy.LITERAL);
}

@Override
public PrismObject<O> cloneComplex(CloneStrategy strategy) {
if (prismContext != null && prismContext.getMonitor() != null) {
prismContext.getMonitor().beforeObjectClone(this);
}

PrismObject<O> clone = new PrismObject<>(getElementName(), getDefinition(), prismContext);
copyValues(clone);
copyValues(strategy, clone);

if (prismContext != null && prismContext.getMonitor() != null) {
prismContext.getMonitor().afterObjectClone(this, clone);
Expand All @@ -243,8 +248,8 @@ public PrismObject<O> clone() {
return clone;
}

protected void copyValues(PrismObject<O> clone) {
super.copyValues(clone);
protected void copyValues(CloneStrategy strategy, PrismObject<O> clone) {
super.copyValues(strategy, clone);
}

public PrismObjectDefinition<O> deepCloneDefinition(boolean ultraDeep, Consumer<ItemDefinition> postCloneAction) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2017 Evolveum
* Copyright (c) 2010-2018 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -97,9 +97,14 @@ public PrismContainer<?> getExtension() {

@Override
public PrismObjectValue<O> clone() {
return cloneComplex(CloneStrategy.LITERAL);
}

@Override
public PrismObjectValue<O> cloneComplex(CloneStrategy strategy) {
PrismObjectValue<O> clone = new PrismObjectValue<>(
getOriginType(), getOriginObject(), getParent(), getId(), complexTypeDefinition, this.prismContext, oid, version);
copyValues(clone);
copyValues(strategy, clone);
return clone;
}

Expand Down
Expand Up @@ -436,15 +436,20 @@ protected void checkDefinition(PrismPropertyDefinition<T> def) {

@Override
public PrismProperty<T> clone() {
return cloneComplex(CloneStrategy.LITERAL);
}

@Override
public PrismProperty<T> cloneComplex(CloneStrategy strategy) {
PrismProperty<T> clone = new PrismProperty<T>(getElementName(), getDefinition(), prismContext);
copyValues(clone);
copyValues(strategy, clone);
return clone;
}

protected void copyValues(PrismProperty<T> clone) {
super.copyValues(clone);
protected void copyValues(CloneStrategy strategy, PrismProperty<T> clone) {
super.copyValues(strategy, clone);
for (PrismPropertyValue<T> value : getValues()) {
clone.addValue(value.clone());
clone.addValue(value.cloneComplex(strategy));
}
}

Expand Down
Expand Up @@ -338,13 +338,18 @@ public boolean isEmpty() {

@Override
public PrismPropertyValue<T> clone() {
PrismPropertyValue clone = new PrismPropertyValue(getOriginType(), getOriginObject());
copyValues(clone);
return cloneComplex(CloneStrategy.LITERAL);
}

@Override
public PrismPropertyValue<T> cloneComplex(CloneStrategy strategy) {
PrismPropertyValue<T> clone = new PrismPropertyValue<>(getOriginType(), getOriginObject());
copyValues(strategy, clone);
return clone;
}

protected void copyValues(PrismPropertyValue clone) {
super.copyValues(clone);
protected void copyValues(CloneStrategy strategy, PrismPropertyValue<T> clone) {
super.copyValues(strategy, clone);
clone.value = CloneUtil.clone(this.value);
if (this.expression != null) {
clone.expression = this.expression.clone();
Expand Down
Expand Up @@ -220,15 +220,20 @@ protected void checkDefinition(PrismReferenceDefinition def) {

@Override
public PrismReference clone() {
return cloneComplex(CloneStrategy.LITERAL);
}

@Override
public PrismReference cloneComplex(CloneStrategy strategy) {
PrismReference clone = new PrismReference(getElementName(), getDefinition(), prismContext);
copyValues(clone);
copyValues(strategy, clone);
return clone;
}

protected void copyValues(PrismReference clone) {
super.copyValues(clone);
protected void copyValues(CloneStrategy strategy, PrismReference clone) {
super.copyValues(strategy, clone);
for (PrismReferenceValue value : getValues()) {
clone.add(value.clone());
clone.add(value.cloneComplex(strategy));
}
}

Expand Down
Expand Up @@ -613,19 +613,20 @@ public String debugDump(int indent, boolean expandObject) {

@Override
public PrismReferenceValue clone() {
return clone(true);
return cloneComplex(CloneStrategy.LITERAL);
}

public PrismReferenceValue clone(boolean copyFullObject) {

@Override
public PrismReferenceValue cloneComplex(CloneStrategy strategy) {
PrismReferenceValue clone = new PrismReferenceValue(getOid(), getOriginType(), getOriginObject());
copyValues(clone, copyFullObject);
copyValues(strategy, clone);
return clone;
}

protected void copyValues(PrismReferenceValue clone, boolean copyFullObject) {
super.copyValues(clone);
protected void copyValues(CloneStrategy strategy, PrismReferenceValue clone) {
super.copyValues(strategy, clone);
clone.targetType = this.targetType;
if (this.object != null && copyFullObject) {
if (this.object != null && strategy == CloneStrategy.LITERAL) {
clone.object = this.object.clone();
}
clone.description = this.description;
Expand Down
Expand Up @@ -295,10 +295,19 @@ public static <X extends PrismValue> Collection<X> cloneValues(Collection<X> val
}
return clonedCollection;
}

/**
* Literal clone.
*/
public abstract PrismValue clone();

public abstract PrismValue clone();
/**
* Complex clone with different cloning strategies.
* @see CloneStrategy
*/
public abstract PrismValue cloneComplex(CloneStrategy strategy);

protected void copyValues(PrismValue clone) {
protected void copyValues(CloneStrategy strategy, PrismValue clone) {
clone.originType = this.originType;
clone.originObject = this.originObject;
// Do not clone parent. The clone will most likely go to a different prism
Expand All @@ -312,10 +321,15 @@ protected void copyValues(PrismValue clone) {

@NotNull
public static <T extends PrismValue> Collection<T> cloneCollection(Collection<T> values) {
return cloneCollectionComplex(CloneStrategy.LITERAL, values);
}

@NotNull
public static <T extends PrismValue> Collection<T> cloneCollectionComplex(CloneStrategy strategy, Collection<T> values) {
Collection<T> clones = new ArrayList<T>();
if (values != null) {
for (T value : values) {
clones.add((T) value.clone());
clones.add((T) value.cloneComplex(strategy));
}
}
return clones;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2017 Evolveum
* Copyright (c) 2010-2018 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 @@ -18,6 +18,7 @@

import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.CloneStrategy;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismProperty;

Expand All @@ -36,7 +37,6 @@
* @author Radovan Semancik
*/
public class ResourceAttribute<T> extends PrismProperty<T> {

private static final long serialVersionUID = -6149194956029296486L;

public ResourceAttribute(QName name, ResourceAttributeDefinition<T> definition, PrismContext prismContext) {
Expand Down Expand Up @@ -83,13 +83,18 @@ public String getNativeAttributeName() {

@Override
public ResourceAttribute<T> clone() {
return cloneComplex(CloneStrategy.LITERAL);
}

@Override
public ResourceAttribute<T> cloneComplex(CloneStrategy strategy) {
ResourceAttribute<T> clone = new ResourceAttribute<T>(getElementName(), getDefinition(), getPrismContext());
copyValues(clone);
copyValues(strategy, clone);
return clone;
}

protected void copyValues(ResourceAttribute<T> clone) {
super.copyValues(clone);
protected void copyValues(CloneStrategy strategy, ResourceAttribute<T> clone) {
super.copyValues(strategy, clone);
// Nothing to copy
}

Expand Down

0 comments on commit c6b50aa

Please sign in to comment.