package pt.estgp.estgweb.services.common;
import pt.estgp.estgweb.web.utils.RequestUtils;
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.services.common.impl.CommonSearchResults;
import pt.estgp.estgweb.services.common.impl.DefaultSearchResults;
import pt.estgp.estgweb.services.common.impl.DefaultResult;
import pt.utl.ist.berserk.logic.serviceManager.IServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.ServiceManager;
import pt.utl.ist.berserk.logic.filterManager.exceptions.*;
import java.util.List;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jomm.ir.lucene.LuceneUtils;
/**
* @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 request 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
(HttpServletRequest request,HttpServletResponse response,
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
(RequestUtils.
getRequester(request, response
), query, searchType
);
}
/**
* @param requester 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
(1.5.0/docs/api/java/lang/String.html">String requester,
1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType
) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
if(searchType ==
null)
searchType = SearchTypeEnum.
AllWords;
CommonSearchResults commonSearchResults =
new CommonSearchResults
();
List
<ISearchResults
> results =
new ArrayList
<ISearchResults
>();
for (ModuleEnum module : ModuleEnum.
values()) {
if (module.
getServiceName() !=
null) {
ISearchResults iSearchResults = search
(requester, query, searchType, module, 0
);
if (iSearchResults.
getTotalResults() > 0
) {
results.
add(iSearchResults
);
commonSearchResults.
addMoreResults(iSearchResults.
getTotalResults());
}
}
}
commonSearchResults.
setResults(results
);
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 request asking
* @param query to execute
* @param searchType see SearchTypeEnum
* @param moduleKey to search in
* @param page of start result
* @return searchResults
* @throws Throwable on not authorized and internal error
*/
public ISearchResults search
(HttpServletRequest request,HttpServletResponse response,
1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType,
1.5.0/docs/api/java/lang/String.html">String moduleKey,
int page
) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
return search
(RequestUtils.
getRequester(request, response
), query, searchType, ModuleEnum.
parse(moduleKey
), page
);
}
/**
* The service called inside implements method
* <p/>
* public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
*
* @param requester asking
* @param query to execute
* @param searchType see SearchTypeEnum
* @param moduleKey to search in
* @param page of start result
* @return searchResults
* @throws Throwable on not authorized and internal error
*/
public ISearchResults search
(1.5.0/docs/api/java/lang/String.html">String requester,
1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType,
1.5.0/docs/api/java/lang/String.html">String moduleKey,
int page
) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
return search
(requester, query, searchType, ModuleEnum.
parse(moduleKey
), page
);
}
/**
* The service called inside implements method
* <p/>
* public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
*
* @param requester asking
* @param query to execute
* @param searchType see SearchTypeEnum
* @param moduleKey to search in
* @param page of start result
* @return searchResults
* @throws Throwable on not authorized and internal error
*/
public ISearchResults search
(1.5.0/docs/api/java/lang/String.html">String requester,
1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType, ModuleEnum moduleKey,
int page
) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
IServiceManager sm
;
try {
sm = ServiceManager.
getInstance();
1.5.0/docs/api/java/lang/String.html">String[] names =
new 1.5.0/docs/api/java/lang/String.html">String[]{};
5+0%2Fdocs%2Fapi+Object">Object[] args =
new 5+0%2Fdocs%2Fapi+Object">Object[]{query, searchType, page, SEARCH_MAX_RESULTS
};
DefaultSearchResults searchResults =
(DefaultSearchResults
) sm.
execute(requester, moduleKey.
getServiceName(),
"search", args, names
);
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
;
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
);
}
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
);
}
}
return searchResults
;
}
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(e, e
);
throw e
;
}
}
public List
<IToDoCat
> getToDoCats
(1.5.0/docs/api/java/lang/String.html">String requester
) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
List
<IToDoCat
> toDoCats =
new ArrayList
<IToDoCat
>();
for (ModuleEnum module : ModuleEnum.
values()) {
if (module.
getServiceName() !=
null) {
IToDoCat toDoCat = getToDoCats
(requester, module
);
if (toDoCat
!=
null) {
toDoCats.
add(toDoCat
);
}
}
}
return toDoCats
;
}
public IToDoCat getToDoCats
(HttpServletRequest request, HttpServletResponse response,
1.5.0/docs/api/java/lang/String.html">String moduleKey
) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
return getToDoCats
(RequestUtils.
getRequester(request, response
), ModuleEnum.
parse(moduleKey
));
}
public IToDoCat getToDoCats
(HttpServletRequest request, HttpServletResponse response, ModuleEnum module
) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
return getToDoCats
(RequestUtils.
getRequester(request, response
), module
);
}
/**
*
* @param requester asking
* @param module to search
* @return Number of ToDos in given module
* @throws Throwable on Error or NotAuthorized Exception
*/
public IToDoCat getToDoCats
(1.5.0/docs/api/java/lang/String.html">String requester, ModuleEnum module
) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
IServiceManager sm
;
try {
sm = ServiceManager.
getInstance();
1.5.0/docs/api/java/lang/String.html">String[] names =
new 1.5.0/docs/api/java/lang/String.html">String[]{};
5+0%2Fdocs%2Fapi+Object">Object[] args =
new 5+0%2Fdocs%2Fapi+Object">Object[]{};
return (IToDoCat
) sm.
execute(requester, module.
getServiceName(),
"getToDo", args, names
);
}
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(e, e
);
throw e
;
}
}
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("18D0D23A7C07FF478CE8DAFEBA58D37A",
"teste",SearchTypeEnum.
AllWords);
for(ISearchResults searchResults: commonSearchResults.
getSearchResults())
{
1.5.0/docs/api/java/lang/System.html">System.
out.
println("-----------------------------------------------------");
1.5.0/docs/api/java/lang/System.html">System.
out.
println("Module" + searchResults.
getModule().
getMessageKey());
for(IResult result: searchResults.
getResults())
{
1.5.0/docs/api/java/lang/System.html">System.
out.
println("");
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\ttitle:" +result.
getTitle());
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\tsubTitle:" +result.
getSubTitle());
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\ttext:" +result.
getText());
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\turl:" + result.
getUrl());
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\tbestFragments:" + result.
getBestFragments());
}
1.5.0/docs/api/java/lang/System.html">System.
out.
println("NEXT PAGE TEST>>>>>>>>>>>>>>>>>>>");
ISearchResults searchResults2 =
new CommonServicesManager
().
search("18D0D23A7C07FF478CE8DAFEBA58D37A",
"teste",SearchTypeEnum.
AllWords,searchResults.
getModule(),1
);
for(IResult result2: searchResults2.
getResults())
{
1.5.0/docs/api/java/lang/System.html">System.
out.
println("");
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\ttitle:" +result2.
getTitle());
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\tsubTitle:" +result2.
getSubTitle());
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\ttext:" +result2.
getText());
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\turl:" + result2.
getUrl());
1.5.0/docs/api/java/lang/System.html">System.
out.
println("\tbestFragments:" + result2.
getBestFragments());
}
}
}
}