Subversion Repositories bacoAlunos

Rev

Rev 92 | Rev 142 | 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);
104 jmachado 80
        commonSearchResults.setQuery(query);
81
        commonSearchResults.setSearchType(searchType.getMessageKey());
90 jmachado 82
        return commonSearchResults;
83
    }
84
 
85
    /**
86
     * The service called inside implements method
87
     * <p/>
88
     * public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
89
     *
90
     * @param request    asking
91
     * @param query      to execute
92
     * @param searchType see SearchTypeEnum
93
     * @param moduleKey  to search in
94
     * @param page       of start result
95
     * @return searchResults
96
     * @throws Throwable on not authorized and internal error
97
     */
98
 
99
    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 {
100
        return search(RequestUtils.getRequester(request), query, searchType, ModuleEnum.parse(moduleKey), page);
101
    }
102
 
103
    /**
104
     * The service called inside implements method
105
     * <p/>
106
     * public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
107
     *
108
     * @param requester  asking
109
     * @param query      to execute
110
     * @param searchType see SearchTypeEnum
111
     * @param moduleKey  to search in
112
     * @param page       of start result
113
     * @return searchResults
114
     * @throws Throwable on not authorized and internal error
115
     */
116
 
117
    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 {
118
        return search(requester, query, searchType, ModuleEnum.parse(moduleKey), page);
119
    }
120
 
121
    /**
122
     * The service called inside implements method
123
     * <p/>
124
     * public ISearchResults search(String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession);
125
     *
126
     * @param requester  asking
127
     * @param query      to execute
128
     * @param searchType see SearchTypeEnum
129
     * @param moduleKey  to search in
130
     * @param page       of start result
131
     * @return searchResults
132
     * @throws Throwable on not authorized and internal error
133
     */
134
    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 {
135
        IServiceManager sm;
136
        try {
137
            sm = ServiceManager.getInstance();
138
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{};
92 jmachado 139
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{query, searchType, page, SEARCH_MAX_RESULTS};
140
 
141
            DefaultSearchResults searchResults = (DefaultSearchResults) sm.execute(requester, moduleKey.getServiceName(), "search", args, names);
142
            searchResults.setPage(page);
143
            searchResults.setMaxResultsPage(SEARCH_MAX_RESULTS);
144
            searchResults.setQuery(query.replace("'"," "));
145
            searchResults.setSearchType(searchType.getMessageKey());
146
            if(searchResults.getTotalResults() > 0)
147
            {
148
                for(IResult result: searchResults.getResults())
149
                {
150
                    DefaultResult defaultResult = (DefaultResult) result;
151
                    defaultResult.setBestFragments(LuceneUtils.doStandardHighlights(defaultResult.getText(),query,SEARCH_FRAGMENT_SIZE,SEARCH_MAX_FRAGMENTS));
152
                    if(!defaultResult.isTitleKey())
153
                    {
154
                        1.5.0/docs/api/java/lang/String.html">String title = defaultResult.getTitle();
155
                        defaultResult.setTitle(LuceneUtils.highlight(defaultResult.getTitle(),query));
156
                        if(defaultResult.getTitle() == null || defaultResult.getTitle().length() == 0)
157
                            defaultResult.setTitle(title);
158
                    }
159
                    if(!defaultResult.isSubTitleKey())
160
                    {
161
                        1.5.0/docs/api/java/lang/String.html">String subTitle = defaultResult.getSubTitle();
162
                        defaultResult.setSubTitle(LuceneUtils.highlight(defaultResult.getSubTitle(),query));
163
                        if(defaultResult.getSubTitle() == null || defaultResult.getSubTitle().length() == 0)
164
                            defaultResult.setSubTitle(subTitle);
165
                    }
166
                    1.5.0/docs/api/java/lang/String.html">String text = defaultResult.getText();
167
                    defaultResult.setText(LuceneUtils.highlight(defaultResult.getText(),query));
168
                    if(defaultResult.getText() == null || defaultResult.getText().length() == 0)
169
                        defaultResult.setText(text);
170
                }
171
            }
172
            return searchResults;
90 jmachado 173
        }
174
        catch (FilterRetrieveException e) {
175
            logger.error(e, e);
176
            throw new NotAuthorizedException(e.toString());
177
        }
178
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) {
179
            logger.error(e, e);
180
            throw e;
181
        }
182
    }
183
 
184
    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 {
185
        List<IToDoCat> toDoCats = new ArrayList<IToDoCat>();
186
 
187
        for (ModuleEnum module : ModuleEnum.values()) {
188
            if (module.getServiceName() != null) {
189
                IToDoCat toDoCat = getToDoCats(requester, module);
190
                if (toDoCat != null) {
191
                    toDoCats.add(toDoCat);
192
 
193
                }
194
            }
195
        }
196
        return toDoCats;
197
    }
198
 
199
    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 {
200
        return getToDoCats(RequestUtils.getRequester(request), ModuleEnum.parse(moduleKey));
201
    }
202
 
203
    public IToDoCat getToDoCats(HttpServletRequest request, ModuleEnum module) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
204
        return getToDoCats(RequestUtils.getRequester(request), module);
205
    }
206
 
207
    /**
208
     *
209
     * @param requester asking
210
     * @param module to search
211
     * @return Number of ToDos in given module
212
     * @throws Throwable on Error or NotAuthorized Exception
213
     */
214
    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 {
215
        IServiceManager sm;
216
        try {
217
            sm = ServiceManager.getInstance();
218
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{};
219
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{};
220
            return (IToDoCat) sm.execute(requester, module.getServiceName(), "getToDo", args, names);
221
        }
222
        catch (FilterRetrieveException e) {
223
            logger.error(e, e);
224
            throw new NotAuthorizedException(e.toString());
225
        }
226
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) {
227
            logger.error(e, e);
228
            throw e;
229
        }
230
    }
231
 
232
 
233
    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
234
    {
235
        ICommonSearchResults commonSearchResults = new CommonServicesManager().search("18D0D23A7C07FF478CE8DAFEBA58D37A","teste",SearchTypeEnum.AllWords);
236
        for(ISearchResults searchResults: commonSearchResults.getSearchResults())
237
        {
238
            1.5.0/docs/api/java/lang/System.html">System.out.println("-----------------------------------------------------");
239
            1.5.0/docs/api/java/lang/System.html">System.out.println("Module" + searchResults.getModule().getMessageKey());
240
            for(IResult result: searchResults.getResults())
241
            {
242
                1.5.0/docs/api/java/lang/System.html">System.out.println("");
243
                1.5.0/docs/api/java/lang/System.html">System.out.println("\ttitle:" +result.getTitle());
244
                1.5.0/docs/api/java/lang/System.html">System.out.println("\tsubTitle:" +result.getSubTitle());
245
                1.5.0/docs/api/java/lang/System.html">System.out.println("\ttext:" +result.getText());
246
                1.5.0/docs/api/java/lang/System.html">System.out.println("\turl:" + result.getUrl());
247
                1.5.0/docs/api/java/lang/System.html">System.out.println("\tbestFragments:" + result.getBestFragments());
92 jmachado 248
 
90 jmachado 249
            }
92 jmachado 250
            1.5.0/docs/api/java/lang/System.html">System.out.println("NEXT PAGE TEST>>>>>>>>>>>>>>>>>>>");
251
            ISearchResults searchResults2 = new CommonServicesManager().search("18D0D23A7C07FF478CE8DAFEBA58D37A","teste",SearchTypeEnum.AllWords,searchResults.getModule(),1);
252
            for(IResult result2: searchResults2.getResults())
253
            {
254
                1.5.0/docs/api/java/lang/System.html">System.out.println("");
255
                1.5.0/docs/api/java/lang/System.html">System.out.println("\ttitle:" +result2.getTitle());
256
                1.5.0/docs/api/java/lang/System.html">System.out.println("\tsubTitle:" +result2.getSubTitle());
257
                1.5.0/docs/api/java/lang/System.html">System.out.println("\ttext:" +result2.getText());
258
                1.5.0/docs/api/java/lang/System.html">System.out.println("\turl:" + result2.getUrl());
259
                1.5.0/docs/api/java/lang/System.html">System.out.println("\tbestFragments:" + result2.getBestFragments());
260
            }
261
 
90 jmachado 262
        }
263
    }
264
 
265
}