Subversion Repositories bacoAlunos

Rev

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