1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.calendar.service.search;
35
36 import fr.paris.lutece.plugins.calendar.business.CalendarHome;
37 import fr.paris.lutece.plugins.calendar.business.Event;
38 import fr.paris.lutece.plugins.calendar.business.OccurrenceEvent;
39 import fr.paris.lutece.plugins.calendar.business.SimpleEvent;
40 import fr.paris.lutece.plugins.calendar.service.EventComparator;
41 import fr.paris.lutece.plugins.calendar.service.EventImageResourceService;
42 import fr.paris.lutece.plugins.calendar.web.Constants;
43 import fr.paris.lutece.portal.service.plugin.Plugin;
44 import fr.paris.lutece.portal.service.spring.SpringContextService;
45 import fr.paris.lutece.portal.service.util.AppLogService;
46
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.Date;
51 import java.util.HashMap;
52 import java.util.List;
53
54
55
56
57
58 public class CalendarSearchService
59 {
60 private static final String BEAN_SEARCH_ENGINE = "calendar.calendarLuceneSearchEngine";
61 private static final String REGEX_ID_EVENT = "^[\\d]+_" + Constants.CALENDAR_SHORT_NAME + "$";
62 private static final String REGEX_ID_DOCUMENT = "^[\\d]+_" + Constants.DOCUMENT_SHORT_NAME + "$";
63 private static final String UNDERSCORE = "_";
64
65
66 private static CalendarSearchService _singleton;
67
68
69
70
71
72
73 public static CalendarSearchService getInstance( )
74 {
75 if ( _singleton == null )
76 {
77 _singleton = new CalendarSearchService( );
78 }
79
80 return _singleton;
81 }
82
83
84
85
86
87
88
89
90
91
92
93 public List<Event> getSearchResults( String[] arrayAgendaIds, String[] arrayCategory, String strQuery,
94 Date dateBegin, Date dateEnd, Plugin plugin )
95 {
96 List<Event> listEvent = new ArrayList<Event>( );
97 HashMap<String, Event> hmListEvent = new HashMap<String, Event>( );
98 CalendarSearchEngine engine = SpringContextService.getBean( BEAN_SEARCH_ENGINE );
99 List<CalendarSearchResult> listResults = engine.getSearchResults( arrayAgendaIds, arrayCategory, strQuery,
100 dateBegin, dateEnd );
101
102 for ( CalendarSearchResult searchResult : listResults )
103 {
104 if ( ( ( searchResult.getId( ) != null ) && searchResult.getId( ).matches( REGEX_ID_EVENT ) )
105 || ( ( searchResult.getId( ) != null ) && searchResult.getId( ).matches( REGEX_ID_DOCUMENT ) ) )
106 {
107 try
108 {
109
110 OccurrenceEvent occurence = CalendarHome.findOccurrence(
111 Integer.parseInt( searchResult.getId( ).substring( 0,
112 searchResult.getId( ).indexOf( UNDERSCORE ) ) ), plugin );
113
114
115 SimpleEvent event = CalendarHome.findEvent( occurence.getEventId( ), plugin );
116
117 event.setUrl( searchResult.getUrl( ) );
118 event.setType( searchResult.getType( ) );
119 event.setImageUrl( EventImageResourceService.getInstance( ).getResourceImageEvent( event.getId( ) ) );
120
121 event.setDescription( searchResult.getHtmlSummary( ) );
122
123
124 if ( !hmListEvent.containsKey( Integer.toString( event.getId( ) ) ) )
125 {
126 hmListEvent.put( Integer.toString( event.getId( ) ), event );
127 }
128 }
129 catch ( NullPointerException e )
130 {
131 AppLogService.error( e );
132 }
133 }
134 }
135
136
137 if ( !hmListEvent.isEmpty( ) )
138 {
139 Collection<Event> collection = hmListEvent.values( );
140 listEvent = new ArrayList<Event>( collection );
141 Collections.sort( listEvent, new EventComparator( ) );
142 }
143
144 return listEvent;
145 }
146 }