dpillot     2005/09/29 18:29:02 CEST

  Modified files:
    core/src/java/org/jahia/engines/importexport 
                                                 ManageContentPicker.java 
  Log:
  added comparator and new method to make sorting on metadata and other info 
available
  
  Revision  Changes    Path
  1.22      +284 -202  
jahia/core/src/java/org/jahia/engines/importexport/ManageContentPicker.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/engines/importexport/ManageContentPicker.java.diff?r1=1.21&r2=1.22&f=h
  
  
  
  Index: ManageContentPicker.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/engines/importexport/ManageContentPicker.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- ManageContentPicker.java  28 Sep 2005 15:27:28 -0000      1.21
  +++ ManageContentPicker.java  29 Sep 2005 16:29:01 -0000      1.22
  @@ -23,6 +23,7 @@
   import org.jahia.services.importexport.ImportHandler;
   import org.jahia.services.search.*;
   import org.jahia.services.sites.SiteLanguageSettings;
  +import org.jahia.services.sites.JahiaSite;
   import org.jahia.services.version.EntryLoadRequest;
   import org.jahia.services.fields.ContentField;
   import org.jahia.services.fields.ContentSmallTextField;
  @@ -34,323 +35,404 @@
   import java.util.*;
   
   /**
  - *
  - *
    * Created by IntelliJ IDEA.
    * Date: ${Date} ${Time}
    * Copyright Codeva 2003
  - * @author Thomas Draier, joe
  + *
  + * @author Thomas Draier, joe Pillot
    * @author ${User}
  - * @version $Id: ManageContentPicker.java,v 1.21 2005/09/28 15:27:28 tdraier 
Exp $
  + * @version $Id: ManageContentPicker.java,v 1.22 2005/09/29 16:29:01 dpillot 
Exp $
    */
   public class ManageContentPicker {
       private static org.apache.log4j.Logger logger =
               org.apache.log4j.Logger.getLogger(ManageContentPicker.class);
   
       private static ManageContentPicker instance = null;
  -    private static final String JSP = 
"/jsp/jahia/engines/importexport/contentpick.jsp";
  +    private static final String JSP = 
"/jsp/jahia/engines/importexport/contentpick_joe.jsp";
       private static ImportExportService ie;
       //private static JahiaUser user;
       private static JahiaSearchService searchService;
       //private static MetadataService metaservice;
   
  +    private ManageContentPicker() {
  +    }
  +
       /**
        * @return a single instance of the object
        */
  -    public static synchronized ManageContentPicker getInstance()
  -    {
  +    public static synchronized ManageContentPicker getInstance() {
           if (instance == null) {
               instance = new ManageContentPicker();
  -            // ref the services
  -            ie=ServicesRegistry.getInstance().getImportExportService();
  -            searchService=ServicesRegistry .getInstance 
().getJahiaSearchService ();
  +
  +            // reference the useful services
  +            ie = ServicesRegistry.getInstance().getImportExportService();
  +            searchService = ServicesRegistry 
.getInstance().getJahiaSearchService();
               logger.debug("creating instance");
           }
           return instance;
       }
   
  -    public boolean handleActions (ProcessingContext jParams, int mode, 
HashMap engineMap, ContentObject object)
  -            throws  JahiaException, JahiaSessionExpirationException {
  +    public boolean handleActions(ProcessingContext jParams, int mode, 
HashMap engineMap, ContentObject object)
  +            throws JahiaException, JahiaSessionExpirationException {
           switch (mode) {
               case (JahiaEngine.LOAD_MODE) :
  -                logger.debug("loading mode");
  -                return load(jParams, engineMap,  object);
  +
  +                return load(jParams, engineMap, object);
               case (JahiaEngine.SAVE_MODE) :
  -                logger.debug("saving mode");
  -                return save( jParams, engineMap, object );
  +
  +                return save(jParams, engineMap, object);
           }
           return false;
       }
   
  -    public boolean load (ProcessingContext processingContext, HashMap 
engineMap, ContentObject object)
  -            throws  JahiaException, JahiaSessionExpirationException {
  +    /**
  +     * @param processingContext
  +     * @param engineMap
  +     * @param object
  +     * @return
  +     * @throws JahiaException
  +     * @throws JahiaSessionExpirationException
  +     *
  +     */
  +    public boolean load(ProcessingContext processingContext, HashMap 
engineMap, ContentObject object)
  +            throws JahiaException, JahiaSessionExpirationException {
           //JahiaUser user = processingContext.getUser ();
  +        logger.debug("loading mode");
  +        engineMap.put("fieldsEditCallingEngineName", "contentpick_engine");
   
  -        engineMap.put("fieldsEditCallingEngineName","contentpick_engine");
  +        //check if we just need a sort on previous results
  +        String o = processingContext.getParameter("orderby");
  +        //o not null nor empty,lasto exist, o!=lasto, results not null
  +        logger.debug("o="+o+" lasto="+engineMap.get("orderby"));
  +        if (o != null
  +                && !o.equalsIgnoreCase("")
  +                && engineMap.containsKey("orderby")
  +                && !o.equalsIgnoreCase((String)engineMap.get("orderby"))
  +                && engineMap.get("searchResults") != null
  +                ) {
  +            orderBy(o, engineMap);
  +            engineMap.remove("orderby");
  +            return true;
  +        }
   
  +        //go to search
           try {
               doSearch(object, processingContext, engineMap);
           } catch (Exception e) {
  -            logger.error("error:",e);
  -            throw new JahiaException("","",0,0,e);
  +            logger.error("error:", e);
  +            throw new JahiaException("", "", 0, 0, e);
           }
   
  -        engineMap.put("contentpick_engine.fieldForm", 
ServicesRegistry.getInstance ().
  -                
getJahiaFetcherService().fetchServlet((ParamBean)processingContext, JSP));
  +        engineMap.put("contentpick_engine.fieldForm", 
ServicesRegistry.getInstance().
  +                getJahiaFetcherService().fetchServlet((ParamBean) 
processingContext, JSP));
   
           return true;
       }
   
  -    public boolean update (ProcessingContext jParams, HashMap engineMap, 
ContentObject object)
  -            throws  JahiaException, JahiaSessionExpirationException {
  +
  +
  +    public boolean update(ProcessingContext jParams, HashMap engineMap, 
ContentObject object)
  +            throws JahiaException, JahiaSessionExpirationException {
  +        logger.debug("update mode");
           return true;
       }
   
   
  -    private boolean save( ProcessingContext jParams, HashMap engineMap, 
ContentObject object)
  -    throws JahiaException {
  +    private boolean save(ProcessingContext jParams, HashMap engineMap, 
ContentObject object)
  +            throws JahiaException {
  +        logger.debug("saving mode");
           try {
               String op = jParams.getParameter("contentPickOp");
               if (op != null) {
                   String copytype = op.substring(0, op.indexOf('_'));
  -                String sourceId = op.substring(op.indexOf('_') +1);
  -
  -                //ImportExportService ie = 
ServicesRegistry.getInstance().getImportExportService();
  +                String sourceId = op.substring(op.indexOf('_') + 1);
                   ContentObject source = 
ContentContainer.getContainer(Integer.parseInt(sourceId));
                   ie.copy(source, object, jParams, EntryLoadRequest.STAGED, 
"link".equals(copytype));
   
                   return true;
               }
           } catch (Exception e) {
  -            throw new JahiaException("","",0,0,e);
  +            throw new JahiaException("", "", 0, 0, e);
           }
           return false;
       }
   
  -    private void doSearch(ContentObject object, ProcessingContext 
processingContext, HashMap engineMap) throws Exception {
  +    private void doSearch(ContentObject object, ProcessingContext 
processingContext, HashMap engineMap)
  +            throws Exception {
           //ImportExportService ie = 
ServicesRegistry.getInstance().getImportExportService();
   
  -        engineMap.put ("sites", 
ServicesRegistry.getInstance().getJahiaSitesService().getSites());
   
  +        Enumeration sites = 
ServicesRegistry.getInstance().getJahiaSitesService().getSites();
  +        engineMap.put("sites", sites);
  +
  +        //get all parameters
  +        // main query
           String searchString = (String) 
processingContext.getParameter("search");
           if (searchString == null) {
               searchString = "";
  -            logger.warn("searching string:"+searchString);
  +            logger.warn("searching string is empty");
           }
  -        engineMap.put("searchString",searchString);
  +
  +        //orderby parameter
  +        String orderBy = (String) processingContext.getParameter("orderby");
  +        if (orderBy == null) orderBy = "score";
  +
  +        //domain scope
  +        String scope = (String) processingContext.getParameter("searchSite");
  +        if (scope == null) scope = "all";
  +
  +        logger.debug("orderby:" + orderBy);
  +        logger.debug("searchstring:" + searchString);
  +        logger.debug("searchSite:" + scope);
  +
  +        //store all the params for subsequent requests
  +        engineMap.put("searchString", searchString);
  +        engineMap.put("orderby", orderBy);
  +        engineMap.put("searchSite", scope);
  +
  +        //previous results (maybe we can store the history of results?)
  +        JahiaSearchResult searchResults = (JahiaSearchResult) 
engineMap.get("searchResults");
  +
  +        // we search only if we have smtg to search
           if (!"".equals(searchString)) {
  -            ArrayList languageCodes = new ArrayList ();
  -            String[] languageCodesVal = processingContext.getParameterValues 
("searchlang");
  +
  +
  +            ArrayList languageCodes = new ArrayList();
  +            String[] languageCodesVal = 
processingContext.getParameterValues("searchlang");
               if (languageCodesVal != null) {
  -                if (!JahiaTools.inValues (Search_Engine.SEARCH_ALL_LANG, 
languageCodesVal)) {
  +                if (!JahiaTools.inValues(Search_Engine.SEARCH_ALL_LANG, 
languageCodesVal)) {
                       for (int i = 0; i < languageCodesVal.length; i++) {
  -                        languageCodes.add (languageCodesVal[i]);
  +                        languageCodes.add(languageCodesVal[i]);
                       }
                   }
               } else {
  -                languageCodes.add (processingContext.getLocale ().toString 
());
  +                languageCodes.add(processingContext.getLocale().toString());
               }
  -            logger.debug("searching in what languages:"+languageCodes);
  -            engineMap.put ("searchLanguageCodes", languageCodes);
  +            logger.debug("searching in what languages:" + languageCodes);
  +            engineMap.put("searchLanguageCodes", languageCodes);
   
  +            //String theScreen = (String)engineMap.get("screen");
   
  -            String theScreen = (String)engineMap.get("screen");
  -            JahiaSearchResult searchResults = 
(JahiaSearchResult)engineMap.get("searchResults");
   
  -            JahiaSearchResultBuilder resultBuilder =
  -                    new ContainerSearchResultBuilderImpl();
  -
  -            // we run the search only if there is someting to query
  -            if (!searchString.equals("") ){
  -                searchResults = ServicesRegistry
  -                        .getInstance ().getJahiaSearchService ()
  -                        .search 
(Integer.parseInt(processingContext.getParameter("searchSite")),
  -                                searchString, processingContext, 
languageCodes, resultBuilder);
  -
  -                logger.debug("# of hits in raw 
searchresults:"+searchResults.getHitCount()+" "+searchResults);
  -
  -                // subset of results
  -                JahiaSearchResult myResults = new 
JahiaSearchResult(resultBuilder);
  -                Map results = new HashMap();
  -
  -                // destination object cdk & cdfn
  -                ContainerDefinitionKey cdk = (ContainerDefinitionKey) 
((ContentContainerList)object).getDefinitionKey(null);
  -                JahiaContainerDefinition thisJcd = 
(JahiaContainerDefinition) ContentObject.getInstance(cdk);
  -                logger.debug("destination cdk:"+cdk+" cdfn:"+thisJcd);
  -
  -                EntryLoadRequest lr = new 
EntryLoadRequest(EntryLoadRequest.ACTIVE_WORKFLOW_STATE,0,processingContext.getLocaleList());
  -
  -                // big loop to test the compatibility of container type
  -                for (Iterator iterator = searchResults.results().iterator(); 
iterator.hasNext();) {
  -                    JahiaSearchHit jahiaSearchHit = (JahiaSearchHit) 
iterator.next();
  -                    ContentObject current = 
ContentContainer.getContainer(Integer.parseInt(jahiaSearchHit.getId()));
  -                    int score = jahiaSearchHit.getScore();
  -                    while (current != null && current.hasActiveEntries()) {
  -                        if (current.getPickedObject() != null) {
  -                            // this object is already a copy, we ignore it
  -                            break;
  -                        }
  +            JahiaSearchResultBuilder resultBuilder = new 
ContainerSearchResultBuilderImpl();
   
  -                        //check if container
  -                        if (current instanceof ContentContainer) {
  -                            ContainerDefinitionKey currentCdk = 
(ContainerDefinitionKey) ((ContentContainer)current).getDefinitionKey(null);
  -                            JahiaContainerDefinition currentJcd = 
(JahiaContainerDefinition) ContentObject.getInstance(currentCdk);
  -                            logger.debug("Hit-id:"+jahiaSearchHit.getId()+" 
[score:"+score+"] cdk:"+currentCdk+" cdfn:"+currentJcd);
  -
  -                            //is compatible
  -                            if (ie.isCompatible(thisJcd, currentJcd)) {
  -                                String key = 
Integer.toString(current.getID());
  -                                logger.debug("found compatible?"+key);
  -                                String t = key;
  -                                List l = 
((ContentContainer)current).getChilds(null, lr, null);
  -                                if (!l.isEmpty()) {
  -                                    t = 
((ContentField)l.iterator().next()).getValue(processingContext, lr);
  -                                    logger.debug("tkey="+t);
  -                                }
  -                                for (Iterator iterator1 = l.iterator(); 
iterator1.hasNext();) {
  -                                    ContentField contentField = 
(ContentField) iterator1.next();
  -                                    if (contentField instanceof 
ContentSmallTextField) {
  -                                        t = 
((ContentField)l.iterator().next()).getValue(processingContext, lr);
  -                                        logger.debug("smalltext:"+t);
  -                                        break;
  -                                    }
  -                                }
   
  -                                //looping list of childs to check page type?
  -                                for (Iterator iterator1 = l.iterator(); 
iterator1.hasNext();) {
  -                                    ContentField contentField = 
(ContentField) iterator1.next();
  -                                    if (contentField instanceof 
ContentPageField) {
  -                                        t = 
((ContentPageField)l.iterator().next()).getContentPage(lr).getTitle(lr);
  -                                        logger.debug("page:"+t);
  -                                        break;
  -                                    }
  -                                }
  -                                logger.debug("fields of 
searchhit"+jahiaSearchHit.getParsedObject().getFields());
  +            if (scope.equalsIgnoreCase("all")) {
  +                //multidomain search
  +                List handlers = new ArrayList(50);
  +
  +                while (sites.hasMoreElements()) {
  +                    SearchHandler h = 
searchService.getSearchHandler(((JahiaSite) sites.nextElement()).getID());
  +                    handlers.add(h.getName());
  +                }
  +                String[] strings = (String[]) handlers.toArray(new 
String[handlers.size()]);
  +                logger.debug(strings);
  +                searchResults = searchService.search(strings, searchString, 
processingContext, languageCodes, resultBuilder);
  +            } else {
  +                //just one domain search
  +                searchResults = 
searchService.search(Integer.parseInt(processingContext.getParameter("searchSite")),
  +                        searchString, processingContext, languageCodes, 
resultBuilder);
  +            }
  +            logger.debug("# of hits in raw searchresults:" + 
searchResults.getHitCount() + " " + searchResults);
   
   
  +            // subset of results
  +            JahiaSearchResult myResults = new 
JahiaSearchResult(resultBuilder);
  +            Map results = new HashMap();
  +
  +            // destination object cdk & cdfn
  +            ContainerDefinitionKey cdk = (ContainerDefinitionKey) 
((ContentContainerList) object).getDefinitionKey(null);
  +            JahiaContainerDefinition thisJcd = (JahiaContainerDefinition) 
ContentObject.getInstance(cdk);
  +            logger.debug("destination cdk:" + cdk + " cdfn:" + thisJcd);
  +
  +            EntryLoadRequest lr = new 
EntryLoadRequest(EntryLoadRequest.ACTIVE_WORKFLOW_STATE, 0, 
processingContext.getLocaleList());
  +
  +            // big loop to test the compatibility of container type
  +            for (Iterator iterator = searchResults.results().iterator(); 
iterator.hasNext();) {
  +                JahiaSearchHit jahiaSearchHit = (JahiaSearchHit) 
iterator.next();
  +                ContentObject current = 
ContentContainer.getContainer(Integer.parseInt(jahiaSearchHit.getId()));
  +                int score = jahiaSearchHit.getScore();
  +                while (current != null && current.hasActiveEntries()) {
  +                    if (current.getPickedObject() != null) {
  +                        // this object is already a copy, we ignore it
  +                        break;
  +                    }
   
  -                                if (results.containsKey(key)) {
  -                                    JahiaContainerSearchHit hit = 
(JahiaContainerSearchHit) results.get(key);
  -                                    hit.setScore( score + hit.getScore() );
  -                                    logger.debug("improving score :"+key+" 
new score:"+hit.getScore());
  +                    //check if container
  +                    if (current instanceof ContentContainer) {
  +                        ContainerDefinitionKey currentCdk = 
(ContainerDefinitionKey) ((ContentContainer) current).getDefinitionKey(null);
  +                        JahiaContainerDefinition currentJcd = 
(JahiaContainerDefinition) ContentObject.getInstance(currentCdk);
  +                        logger.debug("Hit-id:" + jahiaSearchHit.getId() + " 
[score:" + score + "] cdk:" + currentCdk + " cdfn:" + currentJcd);
  +
  +                        //is compatible
  +                        if (ie.isCompatible(thisJcd, currentJcd)) {
  +                            String key = Integer.toString(current.getID());
  +                            logger.debug("found compatible?" + key);
  +                            String t = key;
  +                            List l = ((ContentContainer) 
current).getChilds(null, lr, null);
  +                            if (!l.isEmpty()) {
  +                                t = ((ContentField) 
l.iterator().next()).getValue(processingContext, lr);
  +                                logger.debug("tkey=" + t);
  +                            }
  +                            for (Iterator iterator1 = l.iterator(); 
iterator1.hasNext();) {
  +                                ContentField contentField = (ContentField) 
iterator1.next();
  +                                if (contentField instanceof 
ContentSmallTextField) {
  +                                    t = ((ContentField) 
l.iterator().next()).getValue(processingContext, lr);
  +                                    logger.debug("smalltext:" + t);
                                       break;
  -                                } else {
  -                                    JahiaContainerSearchHit hit = new 
JahiaContainerSearchHit(jahiaSearchHit.getParsedObject());
  -                                    hit.setId(key);
  -                                    
hit.setPage(((ContentContainer)current).getPage().getPage(processingContext));
  -                                    
hit.setPageId(((ContentContainer)current).getPageID());
  -                                    hit.setTeaser(t);
  -                                    hit.setScore(score);
  -
  -                                    while (current != null) {
  -                                        current = current.getParent(null);
  -                                        if (current != null && 
current.getPickedObject() != null) {
  -                                            break;
  -                                        }
  -                                    }
  +                                }
  +                            }
   
  -                                    results.put(key, hit);
  +                            //looping list of childs to check page type?
  +                            for (Iterator iterator1 = l.iterator(); 
iterator1.hasNext();) {
  +                                ContentField contentField = (ContentField) 
iterator1.next();
  +                                if (contentField instanceof 
ContentPageField) {
  +                                    t = ((ContentPageField) 
l.iterator().next()).getContentPage(lr).getTitle(lr);
  +                                    logger.debug("page:" + t);
                                       break;
                                   }
  +                            }
  +                            logger.debug("fields of searchhit" + 
jahiaSearchHit.getParsedObject().getFields());
  +
  +
  +                            if (results.containsKey(key)) {
  +                                JahiaContainerSearchHit hit = 
(JahiaContainerSearchHit) results.get(key);
  +                                hit.setScore(score + hit.getScore());
  +                                logger.debug("improving score :" + key + " 
new score:" + hit.getScore());
  +                                break;
                               } else {
  -                                logger.debug("no-compatible");
  +                                JahiaContainerSearchHit hit = new 
JahiaContainerSearchHit(jahiaSearchHit.getParsedObject());
  +                                hit.setId(key);
  +                                hit.setPage(((ContentContainer) 
current).getPage().getPage(processingContext));
  +                                hit.setPageId(((ContentContainer) 
current).getPageID());
  +                                
hit.setURL(processingContext.composePageUrl(((ContentContainer) 
current).getPageID()));
  +                                hit.setTeaser(t);
  +                                hit.setScore(score);
  +
  +                                while (current != null) {
  +                                    current = current.getParent(null);
  +                                    if (current != null && 
current.getPickedObject() != null) {
  +                                        break;
  +                                    }
  +                                }
  +
  +                                results.put(key, hit);
  +                                break;
                               }
  -                            // end of compatible
  +                        } else {
  +                            logger.debug("no-compatible");
                           }
  -                        current = current.getParent(null);
  -                        score /= 1.5;
  +                        // end of compatible
                       }
  +                    current = current.getParent(null);
  +                    score /= 1.5;
                   }
  +            }
   
  -                //test for metadatas values - joe
  -                for (Iterator iterator = results.values().iterator(); 
iterator.hasNext();) {
  -                    JahiaSearchHit hit = (JahiaSearchHit) iterator.next();
  -                    ParsedObject po=hit.getParsedObject();
  -                    Hashtable t=po.getFields();
  -                    //name of metadatas
  -                    for(Iterator it=t.keySet().iterator();it.hasNext();){
  -                        String name=(String) it.next();
  -                        String[] val=po.getValues(name);
  -                        String values="";
  -                        logger.debug("size:"+val.length);
  -                        for(int v=0;v <val.length;v++){
  -                            values +=val[v]+" ";
  -                        }
  -                        logger.debug(name+":"+values);
  +            //test for metadatas values - joe
  +            for (Iterator iterator = results.values().iterator(); 
iterator.hasNext();) {
  +                JahiaSearchHit hit = (JahiaSearchHit) iterator.next();
  +                ParsedObject po = hit.getParsedObject();
  +                Hashtable t = po.getFields();
  +                //name of metadatas
  +                for (Iterator it = t.keySet().iterator(); it.hasNext();) {
  +                    String name = (String) it.next();
  +                    String[] val = po.getValues(name);
  +                    String values = "";
  +                    logger.debug("size:" + val.length);
  +                    for (int v = 0; v < val.length; v++) {
  +                        values += val[v] + " ";
                       }
  +                    logger.debug(name + ":" + values);
  +                }
   
  -                    //other loops
  -
  -                    ContentObject o = 
ContentContainer.getContainer(Integer.parseInt(hit.getId()));
  -                    ArrayList a=o.getMetadatas();
  -                    for(Iterator ia=a.iterator();ia.hasNext();){
  -                        JahiaObject jo=(JahiaObject) ia.next();
  -                        ContentField cf=(ContentField) jo;
  +                //other loops
   
  -                        logger.debug("LOOPING 
"+cf.getDisplayName(processingContext)+":"+cf.getValue(processingContext));
  -                    }
  +                ContentObject o = 
ContentContainer.getContainer(Integer.parseInt(hit.getId()));
  +                ArrayList a = o.getMetadatas();
  +                for (Iterator ia = a.iterator(); ia.hasNext();) {
  +                    JahiaObject jo = (JahiaObject) ia.next();
  +                    ContentField cf = (ContentField) jo;
   
  +                    logger.debug("LOOPING " + 
cf.getDisplayName(processingContext) + ":" + cf.getValue(processingContext));
                   }
   
  +            }//end big loop.
   
  -                for (Iterator iterator = new 
TreeSet(results.values()).iterator(); iterator.hasNext();) {
  -                    JahiaSearchHit jahiaSearchHit = (JahiaSearchHit) 
iterator.next();
  -                    myResults.addHit(jahiaSearchHit);
  -                }
  -
  -                engineMap.put ("searchResults", myResults);
  +            //sorting by score
  +            orderBy("score", engineMap, myResults,results);
  +            /*
  +            for (Iterator iterator = new 
TreeSet(results.values()).iterator(); iterator.hasNext();) {
  +                JahiaSearchHit jahiaSearchHit = (JahiaSearchHit) 
iterator.next();
  +                myResults.addHit(jahiaSearchHit);
               }
  +
  +            engineMap.put("searchResults", myResults);
  +            */
           }
       }
   
  -
  -    //SOME TESTING method designed to be ajaxed
       /**
  -     *  to get a subset of results from ajax
  -     * @param offset from where the display start
  -     * @param rcount how much results are displayed
  -     * @return vector
  +     *
  +     * @param key string key to order by
  +     * @param engineMap The engine map
  +     * @param myResults output results
  +     * @param results input results
        */
  -    public Vector getResults(int offset,int rcount){
  -        logger.debug("call to getResults");
  -        Vector v = new Vector();
  -        v.add("funny thing");
  -        return v;
  +    private void orderBy(String key, HashMap engineMap,JahiaSearchResult 
myResults,Map results) {
  +
  +        logger.debug("sort searching by" + key);
  +
  +        if (key.equalsIgnoreCase("score")) {
  +            // by score (default)
  +            for (Iterator iterator = new 
TreeSet(results.values()).iterator(); iterator.hasNext();) {
  +                JahiaSearchHit jahiaSearchHit = (JahiaSearchHit) 
iterator.next();
  +                myResults.addHit(jahiaSearchHit);
  +            }
  +
  +        } else if (key.equalsIgnoreCase("id")) {
  +            //by id
  +        }
  +
  +        engineMap.put("searchResults", myResults);
       }
   
       /**
  -     * to reorder the results displayed
  -     * @param offset from where the display start
  -     * @param rcount how much results are displayed
  -     * @param param  string key used to reorder
  -     * @return vector
  +     *
  +     * @param key
  +     * @param engineMap
        */
  -    public Vector reorderResults(int offset, int rcount, String param){
  -        logger.debug("call to reorderResults");
  -        Vector v = new Vector();
  -        v.add("funny thing");
  -        return v;
  -    }
  +    private void orderBy(final String key, HashMap engineMap) {
   
  -    private HashMap orderBy(String key,HashMap r){
  -        HashMap m=new HashMap();
  -        if(key.equalsIgnoreCase("score")){
  -          // by score
  -        } else if(key.equalsIgnoreCase("id")){
  -          //by id
  -        } else if(key.equalsIgnoreCase("")) {
  -            // by
  -        }
  -        /*
  -        JahiaSearchHit h;
  -        if()
  -        h.getId()
  -        */
  -        return m;
  +        logger.debug("sort by" + key);
  +        JahiaSearchResult myResults =(JahiaSearchResult) 
engineMap.get("searchResults");
  +        // create a comparator to compare from key and score
  +        Comparator comp=new Comparator() {
  +                       public int compare(Object o1, Object o2) {
  +                           String 
metadata1=((JahiaSearchHit)o1).getParsedObject().getValue(key);
  +                           int sc1=((JahiaSearchHit)o1).getScore();
  +                           String 
metadata2=((JahiaSearchHit)o2).getParsedObject().getValue(key);
  +                           int sc2=((JahiaSearchHit)o2).getScore();
  +                           if(metadata1.equalsIgnoreCase(metadata2)){
  +                               if(sc1<=sc2) return 1;
  +                               else return -1;
  +                           }
  +                           if(metadata1.compareTo(metadata2)<0) return 1;
  +                            else return -1;
  +
  +
  +                       }
  +                   };
  +
  +       Collections.sort(myResults.results(),comp);
  +       engineMap.put("searchResults", myResults);
       }
   }
   /**
    * $Log: ManageContentPicker.java,v $
  + * Revision 1.22  2005/09/29 16:29:01  dpillot
  + * added comparator and new method to make sorting on metadata and other 
info available
  + *
    * Revision 1.21  2005/09/28 15:27:28  tdraier
    * add update menu on content picked root objects
    *
  @@ -363,4 +445,4 @@
    * Revision 1.17  2005/09/16 10:43:07  dpillot
    * my 1st commit: just added trace on this
    *
  -*/
  \ No newline at end of file
  + */
  \ No newline at end of file
  

Reply via email to