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

org.loom.addons.autocompleter.MultipleAutocompletedConverter Maven / Gradle / Ivy

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() : new ArrayList();
	}

	@SuppressWarnings("unchecked")
	public String getAsText(Object value, MessagesRepository repository) {
		if (value == null) {
			return null;
		}
		StringBuilder buffer = new StringBuilder();
		for (Iteratori = ((Iterable)value).iterator(); i.hasNext(); ) {
			buffer.append(getPropertyWrapper().get(i.next(), false));
			if (i.hasNext()) {
				buffer.append(separatorTokens.charAt(0));
			}
		}
		return buffer.toString();
	}

	public void setSeparatorTokens(String tokenSeparators) {
		this.separatorTokens = tokenSeparators;
	}

	public String getSeparatorTokens() {
		return separatorTokens;
	}

	

}