package info.bukova.isspst.ui.search;

import info.bukova.isspst.UrlResolverHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Pepa Rokos
 * 
 * Transformuje list různých entit na list objektů třídy {@link SearchResult}
 *
 */
public class SearchListTransformer {
	
	private UrlResolverHolder urlResolverHolder;
	
	public SearchListTransformer() {
		urlResolverHolder = null;
	}
	
	
	
	/**
	 * @param urlResolverHolder kontajner vyhodnocovačů URL (objekt se konfiguruje přes Spring context)
	 */
	public SearchListTransformer(UrlResolverHolder urlResolverHolder) {
		super();
		this.urlResolverHolder = urlResolverHolder;
	}



	public List<SearchResult> transform(List<?> source) {
		List<SearchResult> ret = new ArrayList<SearchResult>();		
		
		for (int i = 0; i < source.size(); i++) {
			ResultTransformer transformer = TransformerFactory.transformerFor(source.get(i));
			if (urlResolverHolder != null) {
				transformer.setUrlResolver(urlResolverHolder.resolverFor(source.get(i).getClass()));
			}
			ret.add(transformer.transformObject(source.get(i)));
		}
		
		return ret;
	}
	
	

}
