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.log.ExoLogger;
7   import org.exoplatform.services.log.Log;
8   import org.exoplatform.services.organization.UserProfile;
9   import org.exoplatform.services.organization.UserProfileEventListener;
10  
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  /**
15   * Indexing with :
16   * - collection : "social"
17   * - type : "profile"
18   * - name : username
19   */
20  public class UnifiedSearchOrganizationProfileListener extends UserProfileEventListener {
21  
22    private static Log log = ExoLogger.getLogger(UnifiedSearchOrganizationProfileListener.class);
23  
24    private final IndexingService indexingService;
25  
26    public UnifiedSearchOrganizationProfileListener(IndexingService indexingService) {
27      this.indexingService = indexingService;
28    }
29  
30    @Override
31    public void preSave(UserProfile user, boolean isNew) throws Exception {
32    }
33  
34    @Override
35    public void postSave(UserProfile user, boolean isNew) throws Exception {
36      if(indexingService != null) {
37        Map<String, Object> content = new HashMap<String, Object>();
38        content.put("profile", user);
39        if(isNew) {
40          SearchEntry searchEntry = new SearchEntry("social", "profile", user.getUserName(), content);
41          indexingService.add(searchEntry);
42        } else {
43          SearchEntryId searchEntryId = new SearchEntryId("social", "profile", user.getUserName());
44          indexingService.update(searchEntryId, content);
45        }
46      }
47    }
48  
49    @Override
50    public void preDelete(UserProfile user) throws Exception {
51    }
52  
53    @Override
54    public void postDelete(UserProfile user) throws Exception {
55      // TODO this method is never called as org.exoplatform.services.organization.idm.UserDAOImpl.removeUser() calls 
56      // orgService.getUserProfileHandler().removeUserProfile() with an hardcoded broadcast to false
57  
58      if(indexingService != null) {
59        SearchEntryId searchEntryId = new SearchEntryId("social", "profile", user.getUserName());
60        indexingService.delete(searchEntryId);
61      }
62    }
63  }