Subversion Repositories bacoAlunos

Rev

Rev 142 | Rev 163 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
90 jmachado 1
package pt.estgp.estgweb.services.common;
2
 
3
import pt.estgp.estgweb.web.utils.RequestUtils;
4
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;
5
import pt.estgp.estgweb.Globals;
6
import pt.estgp.estgweb.services.common.impl.CommonSearchResults;
92 jmachado 7
import pt.estgp.estgweb.services.common.impl.DefaultSearchResults;
8
import pt.estgp.estgweb.services.common.impl.DefaultResult;
156 jmachado 9
import pt.estgp.estgweb.services.common.impl.DefaultToDoCat;
90 jmachado 10
import pt.utl.ist.berserk.logic.serviceManager.IServiceManager;
11
import pt.utl.ist.berserk.logic.serviceManager.ServiceManager;
12
import pt.utl.ist.berserk.logic.filterManager.exceptions.*;
13
 
14
import java.util.List;
15
import java.util.ArrayList;
16
 
17
import org.apache.log4j.Logger;
18
 
19
import javax.servlet.http.HttpServletRequest;
142 jmachado 20
import javax.servlet.http.HttpServletResponse;
90 jmachado 21
 
92 jmachado 22
import jomm.ir.lucene.LuceneUtils;
23
 
90 jmachado 24
/**
25
 * @author Jorge Machado
26
 * @date 24/Abr/2008
27
 * @time 14:55:22
28
 * @see pt.estgp.estgweb.services.common
29
 */
156 jmachado 30
public class CommonServicesManager
31
{
90 jmachado 32
 
92 jmachado 33
    private static final int SEARCH_FRAGMENT_SIZE = Globals.SEARCH_BEST_FRAGMENTS_MAX_FRAGMENT_SIZE;
34
    private static final int SEARCH_MAX_FRAGMENTS = Globals.SEARCH_BEST_FRAGMENTS_MAX_FRAGMENTS;
35
    private static final int SEARCH_MAX_RESULTS = Globals.SEARCH_MAX_RESULTS;
90 jmachado 36
 
37
    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);
38
 
39
    private static CommonServicesManager ourInstance = new CommonServicesManager();
40
 
156 jmachado 41
    public static CommonServicesManager getInstance()
42
    {
90 jmachado 43
        return ourInstance;
44
    }
45
 
156 jmachado 46
    private CommonServicesManager()
47
    {
90 jmachado 48
    }
49
 
50
    /**
51
     * @param request    asking
52
     * @param query      to execute
53
     * @param searchType see SearchTypeEnum
54
     * @return Cluster of Results for each Module with total module results, only return modules with results
55
     * @throws Throwable on notAuthorized or internal error
56
     */
156 jmachado 57
    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
58
    {
142 jmachado 59
        return search(RequestUtils.getRequester(request, response), query, searchType);
90 jmachado 60
    }
61
 
62
    /**
63
     * @param requester  asking
64
     * @param query      to execute
65
     * @param searchType see SearchTypeEnum
66
     * @return Cluster of Results for each Module with total module results, only return modules with results
67
     * @throws Throwable on notAuthorized or internal error
68
     */
156 jmachado 69
    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
70
    {
90 jmachado 71
 
156 jmachado 72
        if (searchType == null)
92 jmachado 73
            searchType = SearchTypeEnum.AllWords;
90 jmachado 74
        CommonSearchResults commonSearchResults = new CommonSearchResults();
75
        List<ISearchResults> results = new ArrayList<ISearchResults>();
76
 
156 jmachado 77
        for (ModuleEnum module : ModuleEnum.values())
78
        {
79
            if (module.getServiceName() != null)
80
            {
90 jmachado 81
                ISearchResults iSearchResults = search(requester, query, searchType, module, 0);
156 jmachado 82
                if (iSearchResults.getTotalResults() > 0)
83
                {
90 jmachado 84
                    results.add(iSearchResults);
85
                    commonSearchResults.addMoreResults(iSearchResults.getTotalResults());
86
                }
87
            }
88
        }
89
        commonSearchResults.setResults(results);
104 jmachado 90
        commonSearchResults.setQuery(query);
91
        commonSearchResults.setSearchType(searchType.getMessageKey());
90 jmachado 92
        return commonSearchResults;
93
    }
94
 
95
    /**
96
     * The service called inside implements method
97
     * <p/>
98
     * public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
99
     *
100
     * @param request    asking
101
     * @param query      to execute
102
     * @param searchType see SearchTypeEnum
103
     * @param moduleKey  to search in
104
     * @param page       of start result
105
     * @return searchResults
106
     * @throws Throwable on not authorized and internal error
107
     */
108
 
156 jmachado 109
    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
110
    {
142 jmachado 111
        return search(RequestUtils.getRequester(request, response), query, searchType, ModuleEnum.parse(moduleKey), page);
90 jmachado 112
    }
113
 
114
    /**
115
     * The service called inside implements method
116
     * <p/>
117
     * public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
118
     *
119
     * @param requester  asking
120
     * @param query      to execute
121
     * @param searchType see SearchTypeEnum
122
     * @param moduleKey  to search in
123
     * @param page       of start result
124
     * @return searchResults
125
     * @throws Throwable on not authorized and internal error
126
     */
127
 
156 jmachado 128
    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
129
    {
90 jmachado 130
        return search(requester, query, searchType, ModuleEnum.parse(moduleKey), page);
131
    }
132
 
133
    /**
134
     * The service called inside implements method
135
     * <p/>
136
     * public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
137
     *
138
     * @param requester  asking
139
     * @param query      to execute
140
     * @param searchType see SearchTypeEnum
141
     * @param moduleKey  to search in
142
     * @param page       of start result
143
     * @return searchResults
144
     * @throws Throwable on not authorized and internal error
145
     */
156 jmachado 146
    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
147
    {
90 jmachado 148
        IServiceManager sm;
156 jmachado 149
        try
150
        {
90 jmachado 151
            sm = ServiceManager.getInstance();
152
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{};
92 jmachado 153
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{query, searchType, page, SEARCH_MAX_RESULTS};
154
 
156 jmachado 155
            DefaultSearchResults searchResults = (DefaultSearchResults) sm.execute(requester, moduleKey.getSearchService(), "search", args, names);
92 jmachado 156
            searchResults.setPage(page);
157
            searchResults.setMaxResultsPage(SEARCH_MAX_RESULTS);
156 jmachado 158
            searchResults.setQuery(query.replace("'", " "));
92 jmachado 159
            searchResults.setSearchType(searchType.getMessageKey());
156 jmachado 160
            if (searchResults.getTotalResults() > 0)
92 jmachado 161
            {
156 jmachado 162
                for (IResult result : searchResults.getResults())
92 jmachado 163
                {
164
                    DefaultResult defaultResult = (DefaultResult) result;
156 jmachado 165
                    defaultResult.setBestFragments(LuceneUtils.doStandardHighlights(defaultResult.getText(), query, SEARCH_FRAGMENT_SIZE, SEARCH_MAX_FRAGMENTS));
166
                    if (!defaultResult.isTitleKey())
92 jmachado 167
                    {
168
                        1.5.0/docs/api/java/lang/String.html">String title = defaultResult.getTitle();
156 jmachado 169
                        defaultResult.setTitle(LuceneUtils.highlight(defaultResult.getTitle(), query));
170
                        if (defaultResult.getTitle() == null || defaultResult.getTitle().length() == 0)
92 jmachado 171
                            defaultResult.setTitle(title);
172
                    }
156 jmachado 173
                    if (!defaultResult.isSubTitleKey())
92 jmachado 174
                    {
175
                        1.5.0/docs/api/java/lang/String.html">String subTitle = defaultResult.getSubTitle();
156 jmachado 176
                        defaultResult.setSubTitle(LuceneUtils.highlight(defaultResult.getSubTitle(), query));
177
                        if (defaultResult.getSubTitle() == null || defaultResult.getSubTitle().length() == 0)
92 jmachado 178
                            defaultResult.setSubTitle(subTitle);
179
                    }
180
                    1.5.0/docs/api/java/lang/String.html">String text = defaultResult.getText();
156 jmachado 181
                    defaultResult.setText(LuceneUtils.highlight(defaultResult.getText(), query));
182
                    if (defaultResult.getText() == null || defaultResult.getText().length() == 0)
92 jmachado 183
                        defaultResult.setText(text);
184
                }
185
            }
186
            return searchResults;
90 jmachado 187
        }
156 jmachado 188
        catch (FilterRetrieveException e)
189
        {
90 jmachado 190
            logger.error(e, e);
191
            throw new NotAuthorizedException(e.toString());
192
        }
156 jmachado 193
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
194
        {
90 jmachado 195
            logger.error(e, e);
196
            throw e;
197
        }
198
    }
199
 
156 jmachado 200
    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
201
    {
90 jmachado 202
        List<IToDoCat> toDoCats = new ArrayList<IToDoCat>();
203
 
156 jmachado 204
        for (ModuleEnum module : ModuleEnum.values())
205
        {
206
            if (module.getServiceName() != null)
207
            {
90 jmachado 208
                IToDoCat toDoCat = getToDoCats(requester, module);
156 jmachado 209
                if (toDoCat != null)
210
                {
90 jmachado 211
                    toDoCats.add(toDoCat);
212
 
213
                }
214
            }
215
        }
216
        return toDoCats;
217
    }
218
 
156 jmachado 219
    public List<IToDoCat> getAllToDosCats(1.5.0/docs/api/java/lang/String.html">String requester) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
220
    {
221
        List<IToDoCat> toDoCats = new ArrayList<IToDoCat>();
222
        for (ModuleEnum module : ModuleEnum.values())
223
        {
224
            if (module.getServiceName() != null)
225
            {
226
                IToDoCat toDoCat = getAllToDoCats(requester, module);
227
                if (toDoCat != null && toDoCat.getTotalToDo() > 0)
228
                {
229
                    toDoCats.add(toDoCat);
230
                }
231
            }
232
        }
233
        return toDoCats;
234
    }
235
 
236
    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
237
    {
142 jmachado 238
        return getToDoCats(RequestUtils.getRequester(request, response), ModuleEnum.parse(moduleKey));
90 jmachado 239
    }
240
 
156 jmachado 241
    public IToDoCat getToDoCats(HttpServletRequest request, HttpServletResponse response, ModuleEnum module) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
242
    {
142 jmachado 243
        return getToDoCats(RequestUtils.getRequester(request, response), module);
90 jmachado 244
    }
245
 
246
    /**
247
     * @param requester asking
156 jmachado 248
     * @param module    to search
90 jmachado 249
     * @return Number of ToDos in given module
250
     * @throws Throwable on Error or NotAuthorized Exception
251
     */
156 jmachado 252
    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
253
    {
90 jmachado 254
        IServiceManager sm;
156 jmachado 255
        try
256
        {
90 jmachado 257
            sm = ServiceManager.getInstance();
258
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{};
259
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{};
156 jmachado 260
            return (IToDoCat) sm.execute(requester, module.getGetToDoService(), "getToDo", args, names);
90 jmachado 261
        }
156 jmachado 262
        catch (FilterRetrieveException e)
263
        {
90 jmachado 264
            logger.error(e, e);
265
            throw new NotAuthorizedException(e.toString());
266
        }
156 jmachado 267
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
268
        {
90 jmachado 269
            logger.error(e, e);
270
            throw e;
271
        }
272
    }
273
 
156 jmachado 274
    /**
275
     * @param requester asking
276
     * @param module    to search
277
     * @return Number of ToDos in given module
278
     * @throws Throwable on Error or NotAuthorized Exception
279
     */
280
    public IToDoCat getAllToDoCats(1.5.0/docs/api/java/lang/String.html">String requester, ModuleEnum module) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
281
    {
282
        IServiceManager sm;
283
        try
284
        {
285
            sm = ServiceManager.getInstance();
286
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{};
287
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{};
288
            List<IToDo> todos = (List<IToDo>) sm.execute(requester, module.getAllToDosService(), "getAllToDos", args, names);
289
            DefaultToDoCat toDoCat = new DefaultToDoCat();
290
            toDoCat.setAllToDos(todos);
291
            toDoCat.setDescription(module.getMessageKey());
292
            return toDoCat;
293
        }
294
        catch (FilterRetrieveException e)
295
        {
296
            logger.error(e, e);
297
            throw new NotAuthorizedException(e.toString());
298
        }
299
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
300
        {
301
            logger.error(e, e);
302
            throw e;
303
        }
304
    }
90 jmachado 305
 
156 jmachado 306
 
90 jmachado 307
    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
308
    {
156 jmachado 309
        ICommonSearchResults commonSearchResults = new CommonServicesManager().search("2093FFF41F61E375D2CB4CBEDE4400E9", "teste", SearchTypeEnum.AllWords);
310
        for (ISearchResults searchResults : commonSearchResults.getSearchResults())
90 jmachado 311
        {
312
            1.5.0/docs/api/java/lang/System.html">System.out.println("-----------------------------------------------------");
313
            1.5.0/docs/api/java/lang/System.html">System.out.println("Module" + searchResults.getModule().getMessageKey());
156 jmachado 314
            for (IResult result : searchResults.getResults())
90 jmachado 315
            {
316
                1.5.0/docs/api/java/lang/System.html">System.out.println("");
156 jmachado 317
                1.5.0/docs/api/java/lang/System.html">System.out.println("\ttitle:" + result.getTitle());
318
                1.5.0/docs/api/java/lang/System.html">System.out.println("\tsubTitle:" + result.getSubTitle());
319
                1.5.0/docs/api/java/lang/System.html">System.out.println("\ttext:" + result.getText());
90 jmachado 320
                1.5.0/docs/api/java/lang/System.html">System.out.println("\turl:" + result.getUrl());
321
                1.5.0/docs/api/java/lang/System.html">System.out.println("\tbestFragments:" + result.getBestFragments());
92 jmachado 322
 
90 jmachado 323
            }
156 jmachado 324
//            System.out.println("NEXT PAGE TEST>>>>>>>>>>>>>>>>>>>");
325
//            ISearchResults searchResults2 = new CommonServicesManager().search("18D0D23A7C07FF478CE8DAFEBA58D37A","teste",SearchTypeEnum.AllWords,searchResults.getModule(),1);
326
//            for(IResult result2: searchResults2.getResults())
327
//            {
328
//                System.out.println("");
329
//                System.out.println("\ttitle:" +result2.getTitle());
330
//                System.out.println("\tsubTitle:" +result2.getSubTitle());
331
//                System.out.println("\ttext:" +result2.getText());
332
//                System.out.println("\turl:" + result2.getUrl());
333
//                System.out.println("\tbestFragments:" + result2.getBestFragments());
334
//            }
92 jmachado 335
 
90 jmachado 336
        }
156 jmachado 337
        List<IToDoCat> l = CommonServicesManager.getInstance().getAllToDosCats("2093FFF41F61E375D2CB4CBEDE4400E9");
338
        1.5.0/docs/api/java/lang/System.html">System.out.println(CommonServicesManager.getInstance().getAllToDosCats("2093FFF41F61E375D2CB4CBEDE4400E9"));
90 jmachado 339
    }
340
 
341
}