if (oldIdValue != null && oldIdValue instanceof Long)
mapOldIdToImportedLabel.put((Long) oldIdValue, importedLabel);
}
for (ILabel importedLabel : labels) {
ILabel existingLabel = mapExistingLabelToName.get(importedLabel.getName());
/* Update Existing */
if (existingLabel != null) {
existingLabel.setColor(importedLabel.getColor());
if (existingLabel.getOrder() != importedLabel.getOrder())
fixLabelOrder = true;
existingLabel.setOrder(importedLabel.getOrder());
DynamicDAO.save(existingLabel);
}
/* Save as New */
else {
importedLabel.removeProperty(ITypeImporter.ID_KEY);
DynamicDAO.save(importedLabel);
fixLabelOrder = true;
}
}
/* Fix Order to be a sequence again */
if (fixLabelOrder && !existingLabels.isEmpty()) {
Set<ILabel> sortedLabels = CoreUtils.loadSortedLabels();
int index = 0;
for (Iterator<?> iterator = sortedLabels.iterator(); iterator.hasNext();) {
ILabel label = (ILabel) iterator.next();
label.setOrder(index);
index++;
}
DynamicDAO.saveAll(sortedLabels);
}
}
/* Import Filters */
if (filters != null && !filters.isEmpty()) {
int existingFiltersCount = DynamicDAO.loadAll(ISearchFilter.class).size();
/* Fix locations in Searches if required */
List<ISearch> locationConditionSearches = getLocationConditionSearchesFromFilters(filters);
if (!locationConditionSearches.isEmpty())
updateLocationConditions(mapOldIdToFolderChild, locationConditionSearches);
/* Fix locations in Actions if required */
for (ISearchFilter filter : filters) {
List<IFilterAction> actions = filter.getActions();
for (IFilterAction action : actions) {
if (MoveNewsAction.ID.equals(action.getActionId()) || CopyNewsAction.ID.equals(action.getActionId())) {
Object data = action.getData();
if (data != null && data instanceof Long[]) {
Long[] oldBinLocations = (Long[]) data;
List<Long> newBinLocations = new ArrayList<Long>(oldBinLocations.length);
for (int i = 0; i < oldBinLocations.length; i++) {
Long oldLocation = oldBinLocations[i];
if (mapOldIdToFolderChild.containsKey(oldLocation)) {
IFolderChild location = mapOldIdToFolderChild.get(oldLocation);
newBinLocations.add(location.getId());
}
}
action.setData(newBinLocations.toArray(new Long[newBinLocations.size()]));
}
}
}
}
/* Fix labels in Actions if required */
for (ISearchFilter filter : filters) {
List<IFilterAction> actions = filter.getActions();
for (IFilterAction action : actions) {
if (LabelNewsAction.ID.equals(action.getActionId())) {
Object data = action.getData();
if (data != null && data instanceof Long) {
ILabel label = mapOldIdToImportedLabel.get(data);
if (label != null) {
String name = label.getName();
ILabel existingLabel = mapExistingLabelToName.get(name);
if (existingLabel != null)
action.setData(existingLabel.getId());
else
action.setData(label.getId());
}
}
}