org.kuali.common.impex.model.util.ModelUtils Maven / Gradle / Ivy
/**
* Copyright 2011 The Kuali Foundation Licensed under the
* Educational Community License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.osedu.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.kuali.common.impex.model.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.kuali.common.impex.model.Column;
import org.kuali.common.impex.model.NamedElement;
import org.kuali.common.impex.model.Table;
import org.kuali.common.util.CollectionUtils;
import org.kuali.common.util.StringFilter;
import org.springframework.util.Assert;
public class ModelUtils {
public static List getPrimaryKeyColumnNames(Table t) {
List names = new ArrayList();
for (Column col : CollectionUtils.toEmptyList(t.getColumns())) {
if (col.isPrimaryKey()) {
names.add(col.getName());
}
}
return names;
}
public static String getCsvPrimaryKeyColumnNames(Table t) {
List names = getPrimaryKeyColumnNames(t);
if (names.isEmpty()) {
return "";
} else {
return CollectionUtils.getCSV(names);
}
}
public static Map getColumnNameMap(Table t) {
Map result = new HashMap();
for (Column c : t.getColumns()) {
result.put(c.getName(), c);
}
return result;
}
public static String getCsvColumnNames(List columns) {
List names = new ArrayList(columns.size());
for (Column col : CollectionUtils.toEmptyList(columns)) {
names.add(col.getName());
}
return CollectionUtils.getCSV(names);
}
public static Map buildNameMap(List namedElements) {
Map results = new HashMap(namedElements.size());
for (T n : namedElements) {
results.put(n.getName(), n);
}
return results;
}
/**
* Alter the List
passed in by removing any elements that don't belong and then sorting the ones that remain by name
*/
public static void filterAndSortElements(List elements, StringFilter filter) {
// Remove elements that don't belong
filterElements(elements, filter);
// Sort the elements by name
Collections.sort(elements, NamedElementComparator.getInstance());
}
/**
* Remove elements from the list that should not be there
*/
public static void filterElements(List elements, StringFilter filter) {
// Make sure we are configured correctly
Assert.notNull(elements, "elements is null");
// Iterate over the elements, removing elements that shouldn't be there
Iterator itr = elements.iterator();
while (itr.hasNext()) {
NamedElement element = itr.next();
String name = element.getName();
if (isExcluded(name, filter)) {
itr.remove();
}
}
}
/**
* Return true only if we've been provided a filter
and s
is excluded by that filter
*/
public static boolean isExcluded(String s, StringFilter filter) {
return filter != null && filter.exclude(s);
}
}