View Javadoc
1   package org.exoplatform.commons.search.indexing.listeners;
2   
3   import org.exoplatform.commons.api.indexing.IndexingService;
4   import org.exoplatform.commons.api.indexing.data.SearchEntry;
5   import org.exoplatform.commons.api.indexing.data.SearchEntryId;
6   import org.exoplatform.services.cms.CmsService;
7   import org.exoplatform.services.listener.Event;
8   import org.exoplatform.services.listener.Listener;
9   import org.exoplatform.services.log.ExoLogger;
10  import org.exoplatform.services.log.Log;
11  
12  import javax.jcr.Node;
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  /**
17   * Indexing with :
18   * - collection : "content"
19   * - type : (file|document)
20   * - name : question id
21   *
22   * TODO No event for content deletion
23   */
24  public class UnifiedSearchContentListener extends Listener {
25  
26    private static Log log = ExoLogger.getLogger(UnifiedSearchContentListener.class);
27  
28    private final IndexingService indexingService;
29  
30    public UnifiedSearchContentListener(IndexingService indexingService) {
31      this.indexingService = indexingService;
32    }
33  
34    @Override
35    public void onEvent(Event event) throws Exception {
36      if(indexingService != null) {
37        if(CmsService.POST_CREATE_CONTENT_EVENT.equals(event.getEventName())) {
38          Map<String, Object> content = new HashMap<String, Object>();
39          Node contentNode = (Node) event.getData();
40          content.put("content", contentNode);
41          SearchEntry searchEntry = new SearchEntry("content", contentNode.getPrimaryNodeType().getName().equals("nt:file") ? "file" : "document", contentNode.getUUID(), content);
42          indexingService.add(searchEntry);
43        } else if(CmsService.POST_EDIT_CONTENT_EVENT.equals(event.getEventName())) {
44          Map<String, Object> content = new HashMap<String, Object>();
45          Node contentNode = (Node) event.getData();
46          content.put("content", contentNode);
47          SearchEntryId searchEntryId = new SearchEntryId("content", contentNode.getPrimaryNodeType().getName().equals("nt:file") ? "file" : "document", contentNode.getUUID());
48          indexingService.update(searchEntryId, content);
49        }
50      }
51    }
52  
53  }