edu.internet2.middleware.grouper.stem.StemSaveBatch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grouper Show documentation
Show all versions of grouper Show documentation
Internet2 Groups Management Toolkit
package edu.internet2.middleware.grouper.stem;
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.Stem;
import edu.internet2.middleware.grouper.StemFinder;
import edu.internet2.middleware.grouper.StemSave;
import edu.internet2.middleware.grouper.util.GrouperUtil;
public class StemSaveBatch {
private boolean makeChangesIfExist = true;
public StemSaveBatch assignMakeChangesIfExist(boolean theMakeChangesIfExist) {
this.makeChangesIfExist = theMakeChangesIfExist;
return this;
}
private List stemSaves = new ArrayList();
public StemSaveBatch addStemSaves(Collection theStemSaves) {
if (theStemSaves != null) {
this.stemSaves.addAll(theStemSaves);
}
return this;
}
public Map save() {
Set stemNames = new HashSet();
for (StemSave stemSave : stemSaves) {
if (!StringUtils.isBlank(stemSave.getName())) {
stemNames.add(stemSave.getName());
}
if (!StringUtils.isBlank(stemSave.getStemNameToEdit())) {
stemNames.add(stemSave.getStemNameToEdit());
}
}
Set stems = StemFinder.findByNames(stemNames, false);
Map stemNameToStem = new HashMap();
for (Stem stem : GrouperUtil.nonNull(stems)) {
stemNameToStem.put(stem.getName(), stem);
}
for (StemSave stemSave : stemSaves) {
if (!StringUtils.isBlank(stemSave.getName())) {
Stem stem = stemNameToStem.get(stemSave.getName());
if (stem != null) {
if (this.makeChangesIfExist) {
stem = stemSave.save();
}
} else {
stem = stemSave.save();
}
if (stem != null) {
stemNameToStem.put(stem.getName(), stem);
}
}
}
return stemNameToStem;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy