Subversion Repositories bacoAlunos

Rev

Rev 1814 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.services.common;

import jomm.ir.lucene.LuceneUtils;
import org.apache.log4j.Logger;
import pt.estgp.estgweb.domain.DomainObject;
import pt.estgp.estgweb.domain.UserSession;
import pt.estgp.estgweb.services.common.impl.CommonSearchResults;
import pt.estgp.estgweb.services.common.impl.DefaultResult;
import pt.estgp.estgweb.services.common.impl.DefaultSearchResults;
import pt.estgp.estgweb.services.common.impl.DefaultToDoCat;
import pt.estgp.estgweb.utils.Globals;
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;
import pt.utl.ist.berserk.logic.filterManager.exceptions.FilterRetrieveException;

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

//import pt.estgp.estgweb.web.utils.TestHttpServletRequest;

/**
 * @author Jorge Machado
 * @date 24/Abr/2008
 * @time 14:55:22
 * @see pt.estgp.estgweb.services.common
 */

public class CommonServicesManager
{

    private static final int SEARCH_FRAGMENT_SIZE = Globals.SEARCH_BEST_FRAGMENTS_MAX_FRAGMENT_SIZE;
    private static final int SEARCH_MAX_FRAGMENTS = Globals.SEARCH_BEST_FRAGMENTS_MAX_FRAGMENTS;
    private static final int SEARCH_MAX_RESULTS = Globals.SEARCH_MAX_RESULTS;

    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(CommonServicesManager.class);

    private static CommonServicesManager ourInstance = new CommonServicesManager();

    public static CommonServicesManager getInstance()
    {
        return ourInstance;
    }

    private CommonServicesManager()
    {
    }

    /**
     * @param userSession    asking
     * @param query      to execute
     * @param searchType see SearchTypeEnum
     * @return Cluster of Results for each Module with total module results, only return modules with results
     * @throws Throwable on notAuthorized or internal error
     */

    public ICommonSearchResults search(UserSession userSession, 1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
        return search(userSession,query,searchType,null);
    }

    /**
     * @param userSession    asking
     * @param query      to execute
     * @param searchType see SearchTypeEnum
     * @param from interface from
     * @return Cluster of Results for each Module with total module results, only return modules with results
     * @throws Throwable on notAuthorized or internal error
     */

    public ICommonSearchResults search(UserSession userSession, 1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType, 1.5.0/docs/api/java/lang/String.html">String from) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {

        if (searchType == null)
            searchType = SearchTypeEnum.AllWords;
        CommonSearchResults commonSearchResults = new CommonSearchResults();
        if(query != null && query.trim().length() > 0)
        {


            List<ISearchResults> results = new ArrayList<ISearchResults>();

            for (ModuleEnum module : ModuleEnum.values())
            {
                if (module.getModuleCommonServices() != null)
                {
                    try{
                        List<ISearchResults> iSearchResultsList = search(userSession, query, searchType, module,null, 0, from);
                        if(iSearchResultsList != null && iSearchResultsList.size() > 0)
                            for(ISearchResults iSearchResults: iSearchResultsList)
                            {
                                if (iSearchResults != null && iSearchResults.getTotalResults() > 0)
                                {
                                    results.add(iSearchResults);
                                    commonSearchResults.addMoreResults(iSearchResults.getTotalResults());
                                }
                            }
                    }catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
                    {
                        logger.error(e);
                    }
                }
            }
            commonSearchResults.setResults(results);
        }
        else
        {
            commonSearchResults.setResults(new ArrayList<ISearchResults>());
        }
        commonSearchResults.setQuery(query);
        commonSearchResults.setSearchType(searchType.getMessageKey());
        return commonSearchResults;
    }


    /**
     * The service called inside implements method
     * <p/>
     * public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
     *
     * @param userSession    asking
     * @param query      to execute
     * @param searchType see SearchTypeEnum
     * @param moduleKey  to search in
     * @param page       of start result
     * @param from interface from
     * @return searchResults
     * @throws Throwable on not authorized and internal error
     */

    public List<ISearchResults> search(UserSession userSession, 1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType, ModuleEnum moduleKey,1.5.0/docs/api/java/lang/String.html">String moduleInternalKey, int page, 1.5.0/docs/api/java/lang/String.html">String from) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
        try
        {
//            sm = ServiceManager.getInstance();
//            String[] names = new String[]{};
//            Object[] args = new Object[]{query, searchType, page, SEARCH_MAX_RESULTS};

            List<ISearchResults> searchResultsList = moduleKey.getModuleCommonServices().searchList(moduleInternalKey, from, query, searchType, page, SEARCH_MAX_RESULTS, userSession); //(DefaultSearchResults) sm.execute(requester, moduleKey.getSearchService(), "search", args, names);
            if(searchResultsList == null || searchResultsList.size() == 0)
                return null;
            for(ISearchResults iSearchResults: searchResultsList)
            {
                DefaultSearchResults searchResults = (DefaultSearchResults) iSearchResults;

                searchResults.setPage(page);
                searchResults.setMaxResultsPage(SEARCH_MAX_RESULTS);
                searchResults.setQuery(query.replace("'", " "));
                searchResults.setSearchType(searchType.getMessageKey());
                if (searchResults.getTotalResults() > 0)
                {
                    for (IResult result : searchResults.getResults())
                    {
                        DefaultResult defaultResult = (DefaultResult) result;
                        if(defaultResult.getBestFragments() == null || defaultResult.getBestFragments().trim().length() == 0)
                        {
                            defaultResult.setBestFragments(LuceneUtils.doStandardHighlights(defaultResult.getText(), query, SEARCH_FRAGMENT_SIZE, SEARCH_MAX_FRAGMENTS));
                        }
                        if (!defaultResult.isTitleKey())
                        {
                            1.5.0/docs/api/java/lang/String.html">String title = defaultResult.getTitle();
                            defaultResult.setTitle(LuceneUtils.highlight(defaultResult.getTitle(), query));
                            if (defaultResult.getTitle() == null || defaultResult.getTitle().length() == 0)
                                defaultResult.setTitle(title);
                        }
                        if (!defaultResult.isSubTitleKey())
                        {
                            1.5.0/docs/api/java/lang/String.html">String subTitle = defaultResult.getSubTitle();
                            defaultResult.setSubTitle(LuceneUtils.highlight(defaultResult.getSubTitle(), query));
                            if (defaultResult.getSubTitle() == null || defaultResult.getSubTitle().length() == 0)
                                defaultResult.setSubTitle(subTitle);
                        }
                        if(defaultResult.getText() != null && defaultResult.getText().trim().length() > 0)
                        {
                            1.5.0/docs/api/java/lang/String.html">String text = defaultResult.getText();

                            defaultResult.setText(LuceneUtils.highlight(defaultResult.getText(), query));
                            if (defaultResult.getText() == null || defaultResult.getText().length() == 0)
                                defaultResult.setText(text);
                        }

                        if(defaultResult.getCreator() != null && defaultResult.getCreator().trim().length() > 0)
                        {
                            1.5.0/docs/api/java/lang/String.html">String creator = defaultResult.getCreator();

                            defaultResult.setCreator(LuceneUtils.highlight(defaultResult.getCreator(), query));
                            if (defaultResult.getCreator() == null || defaultResult.getCreator().length() == 0)
                                defaultResult.setCreator(creator);
                        }
                    }
                }
            }
            return searchResultsList;
        }
        catch (FilterRetrieveException e)
        {
            logger.error(e, e);
            throw new NotAuthorizedException(e.toString());
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(moduleKey.getMessageKey() + " - query:" + query + ": " + e, e);
            throw e;
        }
    }

    public List<IToDoCat> getToDosCats(UserSession userSession) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
        List<IToDoCat> toDoCats = new ArrayList<IToDoCat>();

        for (ModuleEnum module : ModuleEnum.values())
        {
            if (module.getModuleCommonServices() != null)
            {
                IToDoCat toDoCat = getToDoCats(userSession, module);
                if (toDoCat != null)
                {
                    toDoCats.add(toDoCat);
                }
            }
        }
        return toDoCats;
    }

    public List<IToDoCat> getAllToDosCats(UserSession userSession) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
        List<IToDoCat> toDoCats = new ArrayList<IToDoCat>();
        for (ModuleEnum module : ModuleEnum.values())
        {
            if (module.getModuleCommonServices() != null)
            {
                IToDoCat toDoCat = getAllToDoCats(userSession, module);
                if (toDoCat != null && toDoCat.getTotalToDo() > 0)
                {
                    toDoCats.add(toDoCat);
                }
            }
        }
        return toDoCats;
    }

    public IToDoCat getToDoCats(UserSession userSession, 1.5.0/docs/api/java/lang/String.html">String moduleKey) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
        return getToDoCats(userSession, ModuleEnum.parse(moduleKey));
    }


    /**
     * @param userSession asking
     * @param module  to search
     * @return Number of ToDos in given module
     * @throws Throwable on Error or NotAuthorized Exception
     */

    public IToDoCat getToDoCats(UserSession userSession, ModuleEnum module) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
//        IServiceManager sm;
        try
        {
            return module.getModuleCommonServices().getToDo(userSession);
//            sm = ServiceManager.getInstance();
//            String[] names = new String[]{};
//            Object[] args = new Object[]{};
//            return (IToDoCat) sm.execute(requester, module.getGetToDoService(), "getToDo", args, names);
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    /**
     * @param userSession asking
     * @param module    to search
     * @return Number of ToDos in given module
     * @throws Throwable on Error or NotAuthorized Exception
     */

    public IToDoCat getAllToDoCats(UserSession userSession, ModuleEnum module) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
//        IServiceManager sm;
        try
        {
//            sm = ServiceManager.getInstance();
//            String[] names = new String[]{};
//            Object[] args = new Object[]{};
//            List<IToDo> todos = (List<IToDo>) sm.execute(requester, module.getAllToDosService(), "getAllToDos", args, names);
            List<IToDo> todos = module.getModuleCommonServices().getAllToDos(userSession);
            DefaultToDoCat toDoCat = new DefaultToDoCat();
            toDoCat.setAllToDos(todos);
            toDoCat.setDescription(module.getMessageKey());
            return toDoCat;
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    /**
     * Advise modules for new object creation
     * @param domainObject created
     */

    public void adviseNew(DomainObject domainObject)
    {
        for (ModuleEnum module : ModuleEnum.values())
        {
            module.getModuleCommonServices().adviseNew(domainObject);
        }
    }

    /**
     * Advise modules for new object creation
     * @param domainObject created
     */

    public void adviseUpdate(DomainObject domainObject)
    {
        for (ModuleEnum module : ModuleEnum.values())
        {
            module.getModuleCommonServices().adviseUpdate(domainObject);
        }
    }



    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
        /*ICommonSearchResults commonSearchResults = new CommonServicesManager().search(UserSessionProxy.loadUserSession(new TestHttpServletRequest("2093FFF41F61E375D2CB4CBEDE4400E9"),null), "teste", SearchTypeEnum.AllWords);
        for (ISearchResults searchResults : commonSearchResults.getSearchResults())
        {
            System.out.println("-----------------------------------------------------");
            System.out.println("Module" + searchResults.getModule().getMessageKey());
            for (IResult result : searchResults.getResults())
            {
                System.out.println("");
                System.out.println("\ttitle:" + result.getTitle());
                System.out.println("\tsubTitle:" + result.getSubTitle());
                System.out.println("\ttext:" + result.getText());
                System.out.println("\turl:" + result.getUrl());
                System.out.println("\tbestFragments:" + result.getBestFragments());

            }
//            System.out.println("NEXT PAGE TEST>>>>>>>>>>>>>>>>>>>");
//            ISearchResults searchResults2 = new CommonServicesManager().search("18D0D23A7C07FF478CE8DAFEBA58D37A","teste",SearchTypeEnum.AllWords,searchResults.getModule(),1);
//            for(IResult result2: searchResults2.getResults())
//            {
//                System.out.println("");
//                System.out.println("\ttitle:" +result2.getTitle());
//                System.out.println("\tsubTitle:" +result2.getSubTitle());
//                System.out.println("\ttext:" +result2.getText());
//                System.out.println("\turl:" + result2.getUrl());
//                System.out.println("\tbestFragments:" + result2.getBestFragments());
//            }

        }
        //CommonServicesManager.getInstance().getAllToDosCats(UserSessionProxy.loadUserSession(new TestHttpServletRequest("2093FFF41F61E375D2CB4CBEDE4400E9"),null));
        //System.out.println(CommonServicesManager.getInstance().getAllToDosCats(UserSessionProxy.loadUserSession(new TestHttpServletRequest("2093FFF41F61E375D2CB4CBEDE4400E9"),null)));
        */

    }

}