package no.dusken.aranea.service; import no.dusken.aranea.model.AraneaObject; import no.dusken.common.service.GenericService; import org.compass.core.Compass; import org.compass.core.CompassHit; import org.compass.core.CompassTemplate; import org.compass.core.support.search.CompassSearchCommand; import org.compass.core.support.search.CompassSearchHelper; import org.compass.core.support.search.CompassSearchResults; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Required; import java.util.LinkedList; import java.util.List; /* * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class SearchServiceImpl<T extends AraneaObject> implements SearchService, InitializingBean { private CompassSearchHelper searchHelper; private Integer pageSize; private Compass compass; private GenericService<T> genericService; /** * The type to search for. Should match the SearchableConstant named type. * Example: <p/> if the following is in the model for article, this should * be "article": * * @SearchableConstant(name = "type", values = {"article"}) */ private String type; private CompassTemplate compassTemplate; public List<T> search(String query, Integer page) { if (query == null || query.equalsIgnoreCase("")) { query = "\" \""; } CompassSearchCommand searchCommand = new CompassSearchCommand("(" + query + ") AND type:(" + type + ")", page); CompassSearchResults searchResults = searchHelper.search(searchCommand); List<T> list = new LinkedList<T>(); for (CompassHit ch : searchResults.getHits()) { // get the full object, not only the compass shadow object list.add(genericService.getEntity(((T) ch.data()).getID())); } return list; } public void afterPropertiesSet() { this.compassTemplate = new CompassTemplate(compass); if (searchHelper == null) { searchHelper = new CompassSearchHelper(compass, pageSize); } } @Required public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } @Required @Autowired public void setCompass(Compass compass) { this.compass = compass; } /** * Returns a <code>CompassTemplate</code> that wraps the injected * <code>Compass</code> instance. * * @return Compass template that wraps the injected Compass instance. */ protected CompassTemplate getCompassTemplate() { return this.compassTemplate; } public void setType(String type) { this.type = type; } @Required public void setGenericService(GenericService<T> genericService) { this.genericService = genericService; } }