/**
 * Created on Jul 4, 2005
 * $id:  $
 */
package org.jvnet.lookout.ui.core.tree.adapter;

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

import org.jvnet.lookout.core.model.SearchResult;
import org.jvnet.lookout.core.model.SearchResultProperty;
import org.jvnet.lookout.ui.core.tree.SearchResultItem;
import org.jvnet.lookout.ui.core.tree.TreeObject;

abstract public class AbstractModelToUIAdapter implements ModelToUIAdapter {
    
    //CXB_EXTERNALIZE
    protected final static String NO_SNIPPET_MESSAGE = "< no snippet available >";
    
    public List<TreeObject> adapt(List<SearchResult> modelList) {
        
        List<TreeObject> uiList = new ArrayList<TreeObject>(modelList.size());
        
        for (SearchResult searchResult : modelList) {
            
            /////////////////////
            //ITEM
            /////////////////////
            //Create result item
            SearchResultItem item = new SearchResultItem();
            
            /////////////////////
            //DETAILS
            /////////////////////

            //Set the name
            item.setPropertyValue(SearchResultProperty.NAME, searchResult.getProperty(SearchResultProperty.NAME), true);
            
            //Set the type
            item.setPropertyValue(SearchResultProperty.TYPE, searchResult.getType(), true);
            
            //Set the source
            item.setPropertyValue(SearchResultProperty.SOURCE, searchResult.getSource(), true);
                      
            //Set the URL
            item.setPropertyValue(SearchResultProperty.URL, searchResult.getProperty(SearchResultProperty.URL), true);

            //Set the location
            item.setPropertyValue(SearchResultProperty.LOCATION, searchResult.getProperty(SearchResultProperty.LOCATION), true);

            //Snippet (optional)
            Object snippet = searchResult.getProperty(SearchResultProperty.SNIPPET);
            
            if(null == snippet) {                
                snippet = NO_SNIPPET_MESSAGE;
            }
            
            item.setPropertyValue(SearchResultProperty.SNIPPET, snippet, true);
                                                
            //read additional details
            readAdditionalDetails(item, searchResult);
            
            //save it...
            uiList.add(item);
        }
        
        return uiList;
    }
    
    /**
     * Should be used by subclasses to read additional details from the 
     * SearchResult into the SearchResultItem.
     * 
     * @param item
     * @param searchResult
     */
    abstract protected void readAdditionalDetails(SearchResultItem item, SearchResult searchResult);
    
}
