Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
Issue #737
  • Loading branch information
rsoika committed Apr 13, 2021
1 parent 0869c90 commit 7ca1563
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.Set;
import java.util.Vector;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.imixs.workflow.exceptions.InvalidAccessException;
Expand Down Expand Up @@ -150,20 +151,55 @@ public Object clone() {
* This method clones the current ItemCollection with a subset of items. The
* method makes a deep copy of the current instance and removes items not
* defined by the list of itemNames.
* <p>
* The list of itemNames can contain exact names or a regular expression.
* <p>
* A itemName can also be mapped into a new itemName by separating the target
* name with a | (e.g. name|parentName)
*
* @param itemNames - list of properties to be copied into the clone
* @param itemNames - list of items to be copied into the clone
* @return new ItemCollection
*/
@SuppressWarnings("unchecked")
public ItemCollection clone(final List<String> itemNames) {
ItemCollection clone = (ItemCollection) this.clone();
// remove all undefined items
// remove all undefined items if a list of itemNames is defined.
if (itemNames != null && itemNames.size() > 0) {
Iterator<?> it = hash.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, List<Object>> entry = (Map.Entry<String, List<Object>>) it.next();
if (!itemNames.contains(entry.getKey())) {
clone.removeItem(entry.getKey());
// we build a list with all items to be cloned...
List<String> cloneItemList = new ArrayList<String>();
Set<String> originItemNameList = hash.keySet();
for (String itemPattern : itemNames) {
// first test an exact match....
if (originItemNameList.contains(itemPattern.toLowerCase())) {
cloneItemList.add(itemPattern.toLowerCase());
} else {

// if we have a | char than copy the item into a new itemname....
// default behavior without reg ex
if (itemPattern.indexOf('|') > -1) {
String targetItemName = itemPattern.substring(itemPattern.indexOf('|') + 1).trim();
String sourceItemName = itemPattern.substring(0, itemPattern.indexOf('|')).trim();
// dose the sourceItemName exist?
if (clone.hasItem(sourceItemName)) {
clone.replaceItemValue(targetItemName, clone.getItemValue(sourceItemName));
cloneItemList.add(targetItemName);
continue;
}
}
// finally we test if field is a reg ex
Pattern pattern = Pattern.compile(itemPattern);
for (String originItemName : originItemNameList) {
if (pattern.matcher(originItemName).find()) {
cloneItemList.add(originItemName);
}
}

}
}
// now we have list with all items to be cloned
for (String itemName : originItemNameList) {
if (!cloneItemList.contains(itemName)) {
// remove not matching items....
clone.removeItem(itemName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,17 @@ public void testCloning() {

ItemCollection itemCol1 = new ItemCollection();
itemCol1.replaceItemValue("a", 1);
itemCol1.replaceItemValue("$a", true);
itemCol1.replaceItemValue("b", "hello");
itemCol1.replaceItemValue("c", "world");
itemCol1.replaceItemValue("d", "cats & dogs");


// clone only some attributes
List<String> attributes = new ArrayList<String>();
attributes.add("a");
attributes.add("b");
attributes.add("d | animals");
ItemCollection itemCol2 = itemCol1.clone(attributes);

Assert.assertNotNull(itemCol2);
Expand All @@ -552,6 +556,8 @@ public void testCloning() {
Assert.assertEquals(1, itemCol2.getItemValueInteger("a"));
Assert.assertEquals("hello", itemCol2.getItemValueString("b"));
Assert.assertEquals("", itemCol2.getItemValueString("c"));
Assert.assertEquals("cats & dogs", itemCol2.getItemValueString("animals"));
Assert.assertFalse(itemCol2.hasItem("d"));

// test full clone
ItemCollection itemCol3 = null;
Expand Down

0 comments on commit 7ca1563

Please sign in to comment.