Subversion Repositories bacoAlunos

Rev

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