org.loom.addons.autocompleter.MultipleAutocompletedConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of loom-addons Show documentation
Show all versions of loom-addons Show documentation
Uploads all artifacts belonging to configuration ':archives'.
The newest version!
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache 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.apache.org/licenses/LICENSE-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.loom.addons.autocompleter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.NoResultException;
import org.apache.commons.lang.StringUtils;
import org.loom.i18n.Messages;
import org.loom.i18n.MessagesRepository;
/**
* Convert multiple autocompleted values ("customer1, customer2, customer3")
* @author icoloma
*/
public class MultipleAutocompletedConverter extends AbstractAutocompletedConverter {
/** the list of tokens to use tokenized autocompletion */
private String separatorTokens;
@SuppressWarnings("unchecked")
public Object getAsObject(String paramName, String paramValue, Messages messages, MessagesRepository repository) {
int i = 0;
Collection result = createCollection();
List wrongValues = new ArrayList();
if (paramValue != null && paramValue.length() > 0) {
for (String fragment : StringUtils.split(paramValue, separatorTokens)) {
try {
result.add(retrieveEntity(fragment));
} catch (NoResultException e) {
wrongValues.add(fragment);
}
i++;
}
}
if (wrongValues.isEmpty()) {
return result;
}
messages.error(paramName, "persistence.multipleAutocompletedEntityNotFound").addArg("wrongValues", wrongValues);
return null;
}
/**
* Create a collection holder. Right now, only Set and List are supported
*/
private Collection> createCollection() {
return Set.class.isAssignableFrom(getConvertedClass())? new LinkedHashSet
© 2015 - 2025 Weber Informatics LLC | Privacy Policy