View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * DocumentSearchService
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      // Constants corresponding to the variables defined in the lutece.properties file
66      private static CalendarSearchService _singleton;
67  
68      /**
69       * Get the HelpdeskSearchService instance
70       * 
71       * @return The {@link CalendarSearchService}
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       * Return search results
85       * @param arrayAgendaIds The calendar ids
86       * @param arrayCategory the category ids
87       * @param strQuery The search query
88       * @param dateBegin The date begin
89       * @param dateEnd The date end
90       * @param plugin The plugin
91       * @return Results as a collection of SearchResult
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                     //Retrieve all occurences of an event
110                     OccurrenceEvent occurence = CalendarHome.findOccurrence(
111                             Integer.parseInt( searchResult.getId( ).substring( 0,
112                                     searchResult.getId( ).indexOf( UNDERSCORE ) ) ), plugin );
113 
114                     //Retrieve the event related to the occurence
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                     //if it is not already present stroring the event to the temp list
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         //Adding the event to final list
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 }