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

edu.internet2.middleware.grouper.attr.AttributeDefNameSaveBatch Maven / Gradle / Ivy

There is a newer version: 5.13.5
Show newest version
package edu.internet2.middleware.grouper.attr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import edu.internet2.middleware.grouper.attr.finder.AttributeDefNameFinder;
import edu.internet2.middleware.grouper.util.GrouperUtil;

public class AttributeDefNameSaveBatch {

  
  private boolean makeChangesIfExist = true;

  public AttributeDefNameSaveBatch assignMakeChangesIfExist(boolean theMakeChangesIfExist) {
    this.makeChangesIfExist = theMakeChangesIfExist;
    return this;
  }
  
  private List attributeDefNameSaves = new ArrayList();
  
  public AttributeDefNameSaveBatch addAttributeDefNameSaves(Collection theAttributeDefNameSaves) {
    if (theAttributeDefNameSaves != null) {
      this.attributeDefNameSaves.addAll(theAttributeDefNameSaves);
    }
    return this;
  }

  public Map save() {
    
    Set nameOfAttributeDefNames = new HashSet();

    for (AttributeDefNameSave attributeDefNameSave : attributeDefNameSaves) {
      if (!StringUtils.isBlank(attributeDefNameSave.getName())) {
        nameOfAttributeDefNames.add(attributeDefNameSave.getName());
      }
      if (!StringUtils.isBlank(attributeDefNameSave.getAttributeDefNameNameToEdit())) {
        nameOfAttributeDefNames.add(attributeDefNameSave.getAttributeDefNameNameToEdit());
      }
    }
    
    Set attributeDefNames = new AttributeDefNameFinder().assignNamesOfAttributeDefNames(nameOfAttributeDefNames).findAttributeNames();
    
    Map nameOfAttributeDefNameToAttributeDefName = new HashMap();
    
    for (AttributeDefName attributeDefName : GrouperUtil.nonNull(attributeDefNames)) {
      nameOfAttributeDefNameToAttributeDefName.put(attributeDefName.getName(), attributeDefName);
    }
    
    for (AttributeDefNameSave attributeDefNameSave : attributeDefNameSaves) {
      if (!StringUtils.isBlank(attributeDefNameSave.getName())) {
        AttributeDefName attributeDefName = nameOfAttributeDefNameToAttributeDefName.get(attributeDefNameSave.getName());
        if (attributeDefName != null) {
          if (this.makeChangesIfExist) {
            attributeDefName = attributeDefNameSave.save();
          }
        } else {
          attributeDefName = attributeDefNameSave.save();
        }
        if (attributeDefName != null) {
          nameOfAttributeDefNameToAttributeDefName.put(attributeDefName.getName(), attributeDefName);
        }
      }
    }
    return nameOfAttributeDefNameToAttributeDefName;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy