Subversion Repositories bacoAlunos

Rev

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