Subversion Repositories bacoAlunos

Rev

Rev 455 | 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();
72
        List<ISearchResults> results = new ArrayList<ISearchResults>();
73
 
156 jmachado 74
        for (ModuleEnum module : ModuleEnum.values())
75
        {
163 jmachado 76
            if (module.getModuleCommonServices() != null)
156 jmachado 77
            {
457 jmachado 78
                List<ISearchResults> iSearchResultsList = search(userSession, query, searchType, module,null, 0, from);
79
                if(iSearchResultsList != null && iSearchResultsList.size() > 0)
80
                    for(ISearchResults iSearchResults: iSearchResultsList)
81
                    {
82
                        if (iSearchResults != null && iSearchResults.getTotalResults() > 0)
83
                        {
84
                            results.add(iSearchResults);
85
                            commonSearchResults.addMoreResults(iSearchResults.getTotalResults());
86
                        }
87
                    }
90 jmachado 88
            }
89
        }
90
        commonSearchResults.setResults(results);
104 jmachado 91
        commonSearchResults.setQuery(query);
92
        commonSearchResults.setSearchType(searchType.getMessageKey());
90 jmachado 93
        return commonSearchResults;
94
    }
95
 
96
 
97
    /**
98
     * The service called inside implements method
99
     * <p/>
100
     * public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
101
     *
163 jmachado 102
     * @param userSession    asking
90 jmachado 103
     * @param query      to execute
104
     * @param searchType see SearchTypeEnum
105
     * @param moduleKey  to search in
106
     * @param page       of start result
165 jmachado 107
     * @param from interface from
90 jmachado 108
     * @return searchResults
109
     * @throws Throwable on not authorized and internal error
110
     */
457 jmachado 111
    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 112
    {
113
        try
114
        {
163 jmachado 115
//            sm = ServiceManager.getInstance();
116
//            String[] names = new String[]{};
117
//            Object[] args = new Object[]{query, searchType, page, SEARCH_MAX_RESULTS};
92 jmachado 118
 
457 jmachado 119
            List<ISearchResults> searchResultsList = moduleKey.getModuleCommonServices().searchList(moduleInternalKey, from, query, searchType, page, SEARCH_MAX_RESULTS, userSession); //(DefaultSearchResults) sm.execute(requester, moduleKey.getSearchService(), "search", args, names);
120
            if(searchResultsList == null || searchResultsList.size() == 0)
121
                return null;
122
            for(ISearchResults iSearchResults: searchResultsList)
92 jmachado 123
            {
457 jmachado 124
                DefaultSearchResults searchResults = (DefaultSearchResults) iSearchResults;
125
 
164 jmachado 126
                searchResults.setPage(page);
127
                searchResults.setMaxResultsPage(SEARCH_MAX_RESULTS);
128
                searchResults.setQuery(query.replace("'", " "));
129
                searchResults.setSearchType(searchType.getMessageKey());
130
                if (searchResults.getTotalResults() > 0)
92 jmachado 131
                {
164 jmachado 132
                    for (IResult result : searchResults.getResults())
92 jmachado 133
                    {
164 jmachado 134
                        DefaultResult defaultResult = (DefaultResult) result;
455 jmachado 135
                        if(defaultResult.getBestFragments() == null || defaultResult.getBestFragments().trim().length() == 0)
136
                            defaultResult.setBestFragments(LuceneUtils.doStandardHighlights(defaultResult.getText(), query, SEARCH_FRAGMENT_SIZE, SEARCH_MAX_FRAGMENTS));
164 jmachado 137
                        if (!defaultResult.isTitleKey())
138
                        {
139
                            1.5.0/docs/api/java/lang/String.html">String title = defaultResult.getTitle();
140
                            defaultResult.setTitle(LuceneUtils.highlight(defaultResult.getTitle(), query));
141
                            if (defaultResult.getTitle() == null || defaultResult.getTitle().length() == 0)
142
                                defaultResult.setTitle(title);
143
                        }
144
                        if (!defaultResult.isSubTitleKey())
145
                        {
146
                            1.5.0/docs/api/java/lang/String.html">String subTitle = defaultResult.getSubTitle();
147
                            defaultResult.setSubTitle(LuceneUtils.highlight(defaultResult.getSubTitle(), query));
148
                            if (defaultResult.getSubTitle() == null || defaultResult.getSubTitle().length() == 0)
149
                                defaultResult.setSubTitle(subTitle);
150
                        }
455 jmachado 151
                        if(defaultResult.getText() != null && defaultResult.getText().trim().length() > 0)
152
                        {
153
                            1.5.0/docs/api/java/lang/String.html">String text = defaultResult.getText();
154
 
155
                            defaultResult.setText(LuceneUtils.highlight(defaultResult.getText(), query));
156
                            if (defaultResult.getText() == null || defaultResult.getText().length() == 0)
157
                                defaultResult.setText(text);
158
                        }
92 jmachado 159
                    }
160
                }
165 jmachado 161
            }
457 jmachado 162
            return searchResultsList;
90 jmachado 163
        }
156 jmachado 164
        catch (FilterRetrieveException e)
165
        {
90 jmachado 166
            logger.error(e, e);
167
            throw new NotAuthorizedException(e.toString());
168
        }
156 jmachado 169
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
170
        {
373 jmachado 171
            logger.error(moduleKey.getMessageKey() + " - query:" + query + ": " + e, e);
90 jmachado 172
            throw e;
173
        }
174
    }
175
 
163 jmachado 176
    public List<IToDoCat> getToDosCats(UserSession userSession) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
156 jmachado 177
    {
90 jmachado 178
        List<IToDoCat> toDoCats = new ArrayList<IToDoCat>();
179
 
156 jmachado 180
        for (ModuleEnum module : ModuleEnum.values())
181
        {
163 jmachado 182
            if (module.getModuleCommonServices() != null)
156 jmachado 183
            {
163 jmachado 184
                IToDoCat toDoCat = getToDoCats(userSession, module);
156 jmachado 185
                if (toDoCat != null)
186
                {
90 jmachado 187
                    toDoCats.add(toDoCat);
188
                }
189
            }
190
        }
191
        return toDoCats;
192
    }
193
 
163 jmachado 194
    public List<IToDoCat> getAllToDosCats(UserSession userSession) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
156 jmachado 195
    {
196
        List<IToDoCat> toDoCats = new ArrayList<IToDoCat>();
197
        for (ModuleEnum module : ModuleEnum.values())
198
        {
163 jmachado 199
            if (module.getModuleCommonServices() != null)
156 jmachado 200
            {
163 jmachado 201
                IToDoCat toDoCat = getAllToDoCats(userSession, module);
156 jmachado 202
                if (toDoCat != null && toDoCat.getTotalToDo() > 0)
203
                {
204
                    toDoCats.add(toDoCat);
205
                }
206
            }
207
        }
208
        return toDoCats;
209
    }
210
 
163 jmachado 211
    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 212
    {
163 jmachado 213
        return getToDoCats(userSession, ModuleEnum.parse(moduleKey));
90 jmachado 214
    }
215
 
216
 
217
    /**
163 jmachado 218
     * @param userSession asking
219
     * @param module  to search
90 jmachado 220
     * @return Number of ToDos in given module
221
     * @throws Throwable on Error or NotAuthorized Exception
222
     */
163 jmachado 223
    public IToDoCat getToDoCats(UserSession userSession, ModuleEnum module) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
156 jmachado 224
    {
163 jmachado 225
//        IServiceManager sm;
156 jmachado 226
        try
227
        {
163 jmachado 228
            return module.getModuleCommonServices().getToDo(userSession);
229
//            sm = ServiceManager.getInstance();
230
//            String[] names = new String[]{};
231
//            Object[] args = new Object[]{};
232
//            return (IToDoCat) sm.execute(requester, module.getGetToDoService(), "getToDo", args, names);
90 jmachado 233
        }
156 jmachado 234
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
235
        {
90 jmachado 236
            logger.error(e, e);
237
            throw e;
238
        }
239
    }
240
 
156 jmachado 241
    /**
163 jmachado 242
     * @param userSession asking
156 jmachado 243
     * @param module    to search
244
     * @return Number of ToDos in given module
245
     * @throws Throwable on Error or NotAuthorized Exception
246
     */
163 jmachado 247
    public IToDoCat getAllToDoCats(UserSession userSession, ModuleEnum module) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
156 jmachado 248
    {
163 jmachado 249
//        IServiceManager sm;
156 jmachado 250
        try
251
        {
163 jmachado 252
//            sm = ServiceManager.getInstance();
253
//            String[] names = new String[]{};
254
//            Object[] args = new Object[]{};
255
//            List<IToDo> todos = (List<IToDo>) sm.execute(requester, module.getAllToDosService(), "getAllToDos", args, names);
256
            List<IToDo> todos = module.getModuleCommonServices().getAllToDos(userSession);
156 jmachado 257
            DefaultToDoCat toDoCat = new DefaultToDoCat();
258
            toDoCat.setAllToDos(todos);
259
            toDoCat.setDescription(module.getMessageKey());
260
            return toDoCat;
261
        }
262
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
263
        {
264
            logger.error(e, e);
265
            throw e;
266
        }
267
    }
90 jmachado 268
 
163 jmachado 269
    /**
270
     * Advise modules for new object creation
271
     * @param domainObject created
272
     */
164 jmachado 273
    public void adviseNew(DomainObject domainObject)
163 jmachado 274
    {
275
        for (ModuleEnum module : ModuleEnum.values())
276
        {
277
            module.getModuleCommonServices().adviseNew(domainObject);
278
        }
279
    }
156 jmachado 280
 
163 jmachado 281
    /**
164 jmachado 282
     * Advise modules for new object creation
283
     * @param domainObject created
284
     */
285
    public void adviseUpdate(DomainObject domainObject)
286
    {
287
        for (ModuleEnum module : ModuleEnum.values())
163 jmachado 288
        {
164 jmachado 289
            module.getModuleCommonServices().adviseUpdate(domainObject);
163 jmachado 290
        }
164 jmachado 291
    }
163 jmachado 292
 
293
 
294
 
90 jmachado 295
    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
296
    {
163 jmachado 297
        ICommonSearchResults commonSearchResults = new CommonServicesManager().search(UserSessionProxy.loadUserSession(new TestHttpServletRequest("2093FFF41F61E375D2CB4CBEDE4400E9"),null), "teste", SearchTypeEnum.AllWords);
156 jmachado 298
        for (ISearchResults searchResults : commonSearchResults.getSearchResults())
90 jmachado 299
        {
300
            1.5.0/docs/api/java/lang/System.html">System.out.println("-----------------------------------------------------");
301
            1.5.0/docs/api/java/lang/System.html">System.out.println("Module" + searchResults.getModule().getMessageKey());
156 jmachado 302
            for (IResult result : searchResults.getResults())
90 jmachado 303
            {
304
                1.5.0/docs/api/java/lang/System.html">System.out.println("");
156 jmachado 305
                1.5.0/docs/api/java/lang/System.html">System.out.println("\ttitle:" + result.getTitle());
306
                1.5.0/docs/api/java/lang/System.html">System.out.println("\tsubTitle:" + result.getSubTitle());
307
                1.5.0/docs/api/java/lang/System.html">System.out.println("\ttext:" + result.getText());
90 jmachado 308
                1.5.0/docs/api/java/lang/System.html">System.out.println("\turl:" + result.getUrl());
309
                1.5.0/docs/api/java/lang/System.html">System.out.println("\tbestFragments:" + result.getBestFragments());
92 jmachado 310
 
90 jmachado 311
            }
156 jmachado 312
//            System.out.println("NEXT PAGE TEST>>>>>>>>>>>>>>>>>>>");
313
//            ISearchResults searchResults2 = new CommonServicesManager().search("18D0D23A7C07FF478CE8DAFEBA58D37A","teste",SearchTypeEnum.AllWords,searchResults.getModule(),1);
314
//            for(IResult result2: searchResults2.getResults())
315
//            {
316
//                System.out.println("");
317
//                System.out.println("\ttitle:" +result2.getTitle());
318
//                System.out.println("\tsubTitle:" +result2.getSubTitle());
319
//                System.out.println("\ttext:" +result2.getText());
320
//                System.out.println("\turl:" + result2.getUrl());
321
//                System.out.println("\tbestFragments:" + result2.getBestFragments());
322
//            }
92 jmachado 323
 
90 jmachado 324
        }
163 jmachado 325
        CommonServicesManager.getInstance().getAllToDosCats(UserSessionProxy.loadUserSession(new TestHttpServletRequest("2093FFF41F61E375D2CB4CBEDE4400E9"),null));
326
        1.5.0/docs/api/java/lang/System.html">System.out.println(CommonServicesManager.getInstance().getAllToDosCats(UserSessionProxy.loadUserSession(new TestHttpServletRequest("2093FFF41F61E375D2CB4CBEDE4400E9"),null)));
90 jmachado 327
    }
328
 
329
}