Subversion Repositories bacoAlunos

Rev

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