EN · DE · RU · FR · ES

#358: CollectionPropertyDelta.java

projectforge-business/src/main/java/de/micromata/hibernate/history/delta/CollectionPropertyDelta.java Type: Java · Role: Component · Source: projectforge-business/src/main/java/de/micromata/hibernate/history/delta/CollectionPropertyDelta.java 112 lines · 65 code · 33 comments · 14 blank
Source code file at projectforge-business/src/main/java/de/micromata/hibernate/history/delta/CollectionPropertyDelta.java containing Java code for the Component layer.

Code Structure

Package: de.micromata.hibernate.history.delta

Classes: CollectionPropertyDelta

Extends: PropertyDelta

Methods (5): calculateAdditionsAndRemovals, toString, getNewObjectValue, splitElements, getOldObjectValue

Imports: 4 packages

Source Code (abridged)

package de.micromata.hibernate.history.delta;

import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;

import java.util.*;

/**
 * Legacy used for XML persistence of DB.
 * 
 * @author Wolfgang Jung (w.jung@micromata.de)
 * 
 */
public class CollectionPropertyDelta extends PropertyDelta
{

  private transient Set<Object> additions = new HashSet<>();

  private transient Set<Object> removals = new HashSet<>();

  protected CollectionPropertyDelta()
  {
    // do nothing
  }

  public CollectionPropertyDelta(String propertyName, Class<?> propertyType, Collection<Object> oldValue,
      Collection<Object> newValue)
  {
    this.propertyName = propertyName;
    this.propertyType = ClassUtils.getShortClassName(propertyType);
    calculateAdditionsAndRemovals(oldValue, newValue);
    this.oldValue = StringUtils.join(removals.iterator(), ",");
    this.newValue = StringUtils.join(additions.iterator(), ",");
  }

  private void calculateAdditionsAndRemovals(Collection<Object> oldValue, Collection<Object> newValue)
  {
    // //////////////////////////////////////////////////////////////////////
    // First, determine additions
    if (newValue != null) {
      additions.addAll(newValue);
    }
    if (oldValue != null) {
      additions.removeAll(oldValue);
    }

    // //////////////////////////////////////////////////////////////////////
    // Then, determine removals
    if (oldValue != null) {
      removals.addAll(oldValue);
    }
    if (newValue != null) {
      removals.removeAll(newValue);
    }
  }

  @Override
  public String toString()
  {
    return "changes of " + propertyName + " new=" + newValue + " old=" + oldValue;
  }

  @Override
  public Object getNewObjectValue(final Session session)
  {
    return splitElements(getNewValue(), session);
  }

  private List<Object> splitElements(final String keyList, final Session session)
  {
    List<Object> entityList = new ArrayList<>();
    if (!StringUtils.isEmpty(keyList)) {
      for (String key : keyList.split(",")) {
        if (StringUtils.isEmpty(key)) {
          continue;
        }
        entityList.add(loadItem(propertyType, key, session));
      }
    }
    return entityList;
  }

  @Override
  public Object getOldObjectValue(final Session session)
  {
    return splitElements(getOldValue(), session);
  }
}

Git History

868d6abb7 2025 -> 2026
63081666f Source file headers: 2024-> 2025.
b6092df09 Copyright 2023 -> 2024
ab45d51fa Copyright 2001-2022 -> 2001-2023.
5f7ef41b8 Copyright 2021 -> 2022