예제 #1
0
  /**
   * @param searchMarks The Set of <code>ISearchMark</code> to update the results in.
   * @param fromUserEvent Indicates whether to update the saved searches due to a user initiated
   *     event or an automatic one.
   */
  public void updateSavedSearches(Collection<ISearchMark> searchMarks, boolean fromUserEvent) {
    boolean firstUpdate = !fUpdatedOnce.get();

    fUpdatedOnce.set(true);
    IModelSearch modelSearch = Owl.getPersistenceService().getModelSearch();
    Set<SearchMarkEvent> events = new HashSet<SearchMarkEvent>(searchMarks.size());

    /* For each Search Mark */
    for (ISearchMark searchMark : searchMarks) {

      /* Return early if shutting down */
      if (Controller.getDefault().isShuttingDown()) return;

      /* Execute the search */
      List<SearchHit<NewsReference>> results =
          modelSearch.searchNews(searchMark.getSearchConditions(), searchMark.matchAllConditions());

      /* Fill Result into Map Buckets */
      Map<INews.State, List<NewsReference>> resultsMap =
          new EnumMap<INews.State, List<NewsReference>>(INews.State.class);

      Set<State> visibleStates = INews.State.getVisible();
      for (SearchHit<NewsReference> searchHit : results) {

        /* Return early if shutting down */
        if (Controller.getDefault().isShuttingDown()) return;

        INews.State state = (State) searchHit.getData(INews.STATE);
        if (visibleStates.contains(state)) {
          List<NewsReference> newsRefs = resultsMap.get(state);
          if (newsRefs == null) {
            newsRefs = new ArrayList<NewsReference>(results.size() / 3);
            resultsMap.put(state, newsRefs);
          }
          newsRefs.add(searchHit.getResult());
        }
      }

      /* Set Result */
      Pair<Boolean, Boolean> result = searchMark.setNewsRefs(resultsMap);
      boolean changed = result.getFirst();
      boolean newNewsAdded = result.getSecond();

      /* Create Event to indicate changed results if any */
      if (changed)
        events.add(
            new SearchMarkEvent(
                searchMark, null, true, !firstUpdate && !fromUserEvent && newNewsAdded));
    }

    /* Notify Listeners */
    if (!events.isEmpty() && !Controller.getDefault().isShuttingDown())
      DynamicDAO.getDAO(ISearchMarkDAO.class).fireNewsChanged(events);
  }
예제 #2
0
 private void unregisterListeners() {
   Owl.getPersistenceService().getModelSearch().removeIndexListener(fIndexListener);
   DynamicDAO.removeEntityListener(IBookMark.class, fBookmarkListener);
   DynamicDAO.removeEntityListener(INewsBin.class, fNewsBinListener);
   DynamicDAO.removeEntityListener(IFolder.class, fFolderListener);
 }
예제 #3
0
  private void registerListeners() {

    /* Index Listener */
    fIndexListener =
        new IndexListener() {
          public void indexUpdated(int entitiesCount) {
            updateSavedSearchesFromEvent(entitiesCount);
          }
        };

    Owl.getPersistenceService().getModelSearch().addIndexListener(fIndexListener);

    /* Bookmark Listener: Update on Reparent */
    fBookmarkListener =
        new BookMarkAdapter() {
          @Override
          public void entitiesUpdated(Set<BookMarkEvent> events) {
            for (BookMarkEvent event : events) {
              if (event.isRoot()) {
                IFolder oldParent = event.getOldParent();
                IFolder parent = event.getEntity().getParent();

                if (oldParent != null && !oldParent.equals(parent)) {
                  updateSavedSearchesFromEvent(1);
                  break;
                }
              }
            }
          }
        };

    DynamicDAO.addEntityListener(IBookMark.class, fBookmarkListener);

    /* News Bin Listener: Update on Reparent */
    fNewsBinListener =
        new NewsBinAdapter() {
          @Override
          public void entitiesUpdated(Set<NewsBinEvent> events) {
            for (NewsBinEvent event : events) {
              if (event.isRoot()) {
                IFolder oldParent = event.getOldParent();
                IFolder parent = event.getEntity().getParent();

                if (oldParent != null && !oldParent.equals(parent)) {
                  updateSavedSearchesFromEvent(1);
                  break;
                }
              }
            }
          }
        };

    DynamicDAO.addEntityListener(INewsBin.class, fNewsBinListener);

    /* Folder Listener: Update on Reparent */
    fFolderListener =
        new FolderAdapter() {
          @Override
          public void entitiesUpdated(Set<FolderEvent> events) {
            for (FolderEvent event : events) {
              if (event.isRoot()) {
                IFolder oldParent = event.getOldParent();
                IFolder parent = event.getEntity().getParent();

                if (oldParent != null && !oldParent.equals(parent)) {
                  updateSavedSearchesFromEvent(1);
                  break;
                }
              }
            }
          }
        };

    DynamicDAO.addEntityListener(IFolder.class, fFolderListener);
  }