All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ebeaninternal.server.persist.MergeNodeAssocManyToMany Maven / Gradle / Ivy

There is a newer version: 15.8.0
Show newest version
package io.ebeaninternal.server.persist;

import io.ebean.SqlUpdate;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.IntersectionTable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Node for processing merge on ManyToMany properties.
 */
final class MergeNodeAssocManyToMany extends MergeNode {

  private final BeanPropertyAssocMany many;

  MergeNodeAssocManyToMany(String fullPath, BeanPropertyAssocMany property) {
    super(fullPath, property);
    this.many = property;
  }

  @Override
  public void merge(MergeRequest request) {
    EntityBean parentBean = request.getBean();
    Collection beans = many.rawCollection(parentBean);
    Collection outlines = many.rawCollection(request.getOutline());

    Map outlineIds = toMap(outlines);
    List additions = new ArrayList<>();
    if (beans != null) {
      for (Object bean : beans) {
        EntityBean entityBean = (EntityBean) bean;
        Object beanId = targetDescriptor.getId(entityBean);
        if (beanId != null) {
          if (outlineIds.remove(beanId) == null) {
            additions.add(entityBean);
          }
        }
      }
    }

    // any remaining are considered deletes
    List deletions = new ArrayList<>(outlineIds.values());

    SpiEbeanServer server = request.getServer();
    SpiTransaction transaction = request.getTransaction();

    IntersectionTable intersectionTable = many.intersectionTable();

    if (!deletions.isEmpty()) {
      transaction.flush();
      SqlUpdate delete = intersectionTable.delete(server);
      for (EntityBean deletion : deletions) {
        many.intersectionBind(delete, parentBean, deletion);
        delete.addBatch();
      }
      delete.execute();
    }

    if (!additions.isEmpty()) {
      transaction.flush();
      SqlUpdate insert = intersectionTable.insert(server);
      for (EntityBean addition : additions) {
        many.intersectionBind(insert, parentBean, addition);
        insert.addBatch();
      }
      insert.execute();
    }
    many.resetMany(parentBean);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy