@Override
 public StorageResults visit(Paging paging) {
   query.setFirstResult(paging.getStart());
   query.setFetchSize(AbstractQueryHandler.JDBC_FETCH_SIZE);
   query.setMaxResults(paging.getLimit());
   return null;
 }
Example #2
0
  public QProfileRuleResult search(ProfileRuleQuery query, Paging paging) {
    SearchHits ruleHits =
        searchRules(
            query,
            paging,
            ruleFilterForActiveRuleSearch(query)
                .must(hasChildFilter(ESActiveRule.TYPE_ACTIVE_RULE, activeRuleFilter(query))));
    List<Integer> ruleIds = Lists.newArrayList();
    for (SearchHit ruleHit : ruleHits) {
      ruleIds.add(Integer.valueOf(ruleHit.id()));
    }

    List<QProfileRule> result = Lists.newArrayList();
    if (!ruleIds.isEmpty()) {
      SearchHits activeRuleHits = searchActiveRules(query, ruleIds, FIELD_SOURCE, FIELD_PARENT);

      Map<String, SearchHit> activeRuleByParent = Maps.newHashMap();
      for (SearchHit activeRuleHit : activeRuleHits) {
        activeRuleByParent.put(
            (String) activeRuleHit.field(FIELD_PARENT).getValue(), activeRuleHit);
      }

      for (SearchHit ruleHit : ruleHits) {
        result.add(
            new QProfileRule(
                ruleHit.sourceAsMap(), activeRuleByParent.get(ruleHit.id()).sourceAsMap()));
      }
    }
    return new QProfileRuleResult(
        result,
        PagingResult.create(paging.pageSize(), paging.pageIndex(), ruleHits.getTotalHits()));
  }
Example #3
0
 public static long getMinValue(Paging<LongStepable> paging) {
   if (paging != null) {
     if (paging.getMin() != null) {
       return paging.getMin().getNumber();
     }
   }
   return getMinValue();
 }
Example #4
0
 public QProfileRuleResult searchInactives(ProfileRuleQuery query, Paging paging) {
   SearchHits hits =
       searchRules(
           query, paging, ruleFilterForInactiveRuleSearch(query), FIELD_SOURCE, FIELD_PARENT);
   List<QProfileRule> result = Lists.newArrayList();
   for (SearchHit hit : hits.getHits()) {
     result.add(new QProfileRule(hit.sourceAsMap()));
   }
   return new QProfileRuleResult(
       result, PagingResult.create(paging.pageSize(), paging.pageIndex(), hits.getTotalHits()));
 }
  private MetaList<Tweet> getSavedSearchFromTwitter(int searchId, boolean fromDbOnly) {
    MetaList<Tweet> messages;

    Paging paging = new Paging();
    paging.setCount(20);

    messages = th.getSavedSearchesTweets(searchId, fromDbOnly, paging);

    tweets = messages.getList();

    return messages;
  }
  private MetaList<DirectMessage> getDirectsFromTwitter(boolean fromDbOnly) {
    MetaList<DirectMessage> messages;

    long last = tdb.getLastRead(account.getId(), -2);
    Paging paging = new Paging();
    if (last > -1) paging.setSinceId(last);

    messages = th.getDirectMessages(fromDbOnly, paging);
    directs = messages.getList();

    return messages;
  }
Example #7
0
  @Test(enabled = true, timeOut = 60000)
  public void testNext() throws Exception {
    int size = 17;
    final Paging paging = new Paging(5);
    final List<Integer> input = Paging.generate(size);
    final List<Integer> expected = Paging.generate(size);

    Assert.assertEquals(paging.next(input), expected.subList(0, 5));
    Assert.assertEquals(paging.next(input), expected.subList(5, 10));
    Assert.assertEquals(paging.next(input), expected.subList(10, 15));
    Assert.assertEquals(paging.next(input), expected.subList(15, 17));
  }
Example #8
0
    private void setRSM() {
      RSMPacketExtension pagingInfo = new RSMPacketExtension();
      pagingInfo.setCount(pagingData.getCount());

      if (pagingData.getCount() > 0) {
        List<T> dataItems = pagingData.getItems();

        if (dataItems.size() > 0) {
          pagingInfo.setFirstValue(
              delegate.getPrimaryProperty(dataItems.get(0)), pagingData.getOffset());

          pagingInfo.setLastValue(delegate.getPrimaryProperty(dataItems.get(dataItems.size() - 1)));
        }
      }
      this.element.add(pagingInfo.getElement());
    }
Example #9
0
 private SearchHits searchRules(
     ProfileRuleQuery query, Paging paging, FilterBuilder filterBuilder, String... fields) {
   SearchRequestBuilder builder =
       index
           .client()
           .prepareSearch(INDEX_RULES)
           .setTypes(TYPE_RULE)
           .setPostFilter(filterBuilder)
           .setSize(paging.pageSize())
           .setFrom(paging.offset());
   if (fields.length > 0) {
     builder.addFields(fields);
   }
   addOrder(query, builder);
   return index.executeRequest(builder);
 }
Example #10
0
 private void assertCollection(final int x) {
   final List<Integer> nums = Paging.generate(x);
   Assert.assertEquals(x, nums.size());
   for (int i = 0; i < x; i++) {
     Assert.assertEquals(i, nums.get(i).intValue());
   }
 }
Example #11
0
    public PagingPacket(
        String namespace,
        Paging<T> pagingData,
        DataItemProcessDelegate<T> dataItemProcessDelegate) {

      super(namespace, pagingData.getItems(), dataItemProcessDelegate);
      this.pagingData = pagingData;
      this.setRSM();
    }
 @Override
 public StorageResults visit(Select select) {
   // TMDM-4654: Checks if entity has a composite PK.
   Set<ComplexTypeMetadata> compositeKeyTypes = new HashSet<ComplexTypeMetadata>();
   // TMDM-7496: Search should include references to reused types
   Collection<ComplexTypeMetadata> types =
       new HashSet<ComplexTypeMetadata>(select.accept(new SearchTransitiveClosure()));
   for (ComplexTypeMetadata type : types) {
     if (type.getKeyFields().size() > 1) {
       compositeKeyTypes.add(type);
     }
   }
   if (!compositeKeyTypes.isEmpty()) {
     StringBuilder message = new StringBuilder();
     Iterator it = compositeKeyTypes.iterator();
     while (it.hasNext()) {
       ComplexTypeMetadata compositeKeyType = (ComplexTypeMetadata) it.next();
       message.append(compositeKeyType.getName());
       if (it.hasNext()) {
         message.append(',');
       }
     }
     throw new FullTextQueryCompositeKeyException(message.toString());
   }
   // Removes Joins and joined fields.
   List<Join> joins = select.getJoins();
   if (!joins.isEmpty()) {
     Set<ComplexTypeMetadata> joinedTypes = new HashSet<ComplexTypeMetadata>();
     for (Join join : joins) {
       joinedTypes.add(join.getRightField().getFieldMetadata().getContainingType());
     }
     for (ComplexTypeMetadata joinedType : joinedTypes) {
       types.remove(joinedType);
     }
     List<TypedExpression> filteredFields = new LinkedList<TypedExpression>();
     for (TypedExpression expression : select.getSelectedFields()) {
       if (expression instanceof Field) {
         FieldMetadata fieldMetadata = ((Field) expression).getFieldMetadata();
         if (joinedTypes.contains(fieldMetadata.getContainingType())) {
           TypeMapping mapping =
               mappings.getMappingFromDatabase(fieldMetadata.getContainingType());
           filteredFields.add(
               new Alias(
                   new StringConstant(StringUtils.EMPTY),
                   mapping.getUser(fieldMetadata).getName()));
         } else {
           filteredFields.add(expression);
         }
       } else {
         filteredFields.add(expression);
       }
     }
     selectedFields.clear();
     selectedFields.addAll(filteredFields);
   }
   // Handle condition
   Condition condition = select.getCondition();
   if (condition == null) {
     throw new IllegalArgumentException("Expected a condition in select clause but got 0.");
   }
   // Create Lucene query (concatenates all sub queries together).
   FullTextSession fullTextSession = Search.getFullTextSession(session);
   Query parsedQuery = select.getCondition().accept(new LuceneQueryGenerator(types));
   // Create Hibernate Search query
   Set<Class> classes = new HashSet<Class>();
   for (ComplexTypeMetadata type : types) {
     String className = ClassCreator.getClassName(type.getName());
     try {
       ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
       classes.add(contextClassLoader.loadClass(className));
     } catch (ClassNotFoundException e) {
       throw new RuntimeException("Could not find class '" + className + "'.", e);
     }
   }
   FullTextQuery fullTextQuery =
       fullTextSession.createFullTextQuery(
           parsedQuery, classes.toArray(new Class<?>[classes.size()]));
   // Very important to leave this null (would disable ability to search across different types)
   fullTextQuery.setCriteriaQuery(null);
   fullTextQuery.setSort(Sort.RELEVANCE); // Default sort (if no order by specified).
   query =
       EntityFinder.wrap(
           fullTextQuery,
           (HibernateStorage) storage,
           session); // ensures only MDM entity objects are returned.
   // Order by
   for (OrderBy current : select.getOrderBy()) {
     current.accept(this);
   }
   // Paging
   Paging paging = select.getPaging();
   paging.accept(this);
   pageSize = paging.getLimit();
   boolean hasPaging = pageSize < Integer.MAX_VALUE;
   if (!hasPaging) {
     return createResults(query.scroll(ScrollMode.FORWARD_ONLY));
   } else {
     return createResults(query.list());
   }
 }
  /**
   * Retrieve a list of statuses. Depending on listId, this is taken from different sources:
   *
   * <ul>
   *   <li>0 : home timeline
   *   <li>-1 : mentions
   *   <li>>0 : User list
   * </ul>
   *
   * This method may trigger a network call if fromDbOnly is false. The filter if not null is a
   * regular expression, that if matches filters the tweet.
   *
   * @param fromDbOnly If true only statuses already in the DB are returned
   * @param listId Id of the list / timeline to fetch (see above)
   * @param updateStatusList Should the currently displayed list be updated?
   * @return List of status items along with some counts
   */
  private MetaList<Status> getTimlinesFromTwitter(
      boolean fromDbOnly, int listId, boolean updateStatusList) {
    Paging paging = new Paging();

    MetaList<Status> myStatuses;

    long last = tdb.getLastRead(account.getId(), listId);
    if (last > 0) // && !Debug.isDebuggerConnected())
    paging.sinceId(last).setCount(200);
    else paging.setCount(50); // 50 Tweets if we don't have the timeline yet

    switch (listId) {
      case 0:
        // Home time line
        myStatuses = th.getTimeline(paging, listId, fromDbOnly);

        break;
      case -1:
        myStatuses = th.getTimeline(paging, listId, fromDbOnly);
        break;
      case -2:
        // see below at getDirectsFromTwitter
        myStatuses = new MetaList<Status>();
        break;
      case -3:
        myStatuses = th.getTimeline(paging, listId, fromDbOnly);
        break;
      case -4:
        myStatuses = th.getTimeline(paging, listId, fromDbOnly);
        break;
      default:
        myStatuses = th.getUserList(paging, listId, fromDbOnly, unreadCount);
        if (unreadCount > -1) {
          List<Status> list = myStatuses.getList();
          if (list.size() <= unreadCount) unreadCount = list.size() - 1;
          if (unreadCount > -1) last = list.get(unreadCount).getId();
        }

        break;
    }

    long newLast = -1;
    // Update the 'since' id in the database
    if (myStatuses.getList().size() > 0) {
      newLast =
          myStatuses
              .getList()
              .get(0)
              .getId(); // assumption is that twitter sends the newest (=highest id) first
      tdb.updateOrInsertLastRead(account.getId(), listId, newLast);
    }

    // Sync with TweetMarker
    long newLast2 = -1;
    if (listId >= 0 && !account.isStatusNet() && !fromDbOnly) {
      if (listId == 0)
        newLast2 = TweetMarkerSync.syncFromTweetMarker("timeline", account.getName());
      else newLast2 = TweetMarkerSync.syncFromTweetMarker("lists." + listId, account.getName());

      if (newLast2 > newLast) {
        tdb.updateOrInsertLastRead(account.getId(), listId, newLast2);
      } else {
        if (listId == 0)
          TweetMarkerSync.syncToTweetMarker("timeline", newLast, account.getName(), th.getOAuth());
        else
          TweetMarkerSync.syncToTweetMarker(
              "lists." + listId, newLast, account.getName(), th.getOAuth());
      }
    }

    MetaList<Status> metaList;
    if (updateStatusList) {
      statuses = new ArrayList<Status>();
      List<Status> data = new ArrayList<Status>(myStatuses.getList().size());
      if (filterPattern == null) {
        setupFilter(); // TODO report errors?
      }
      for (Status status : myStatuses.getList()) {
        boolean shouldFilter = matchesFilter(status);
        if (shouldFilter) {
          Log.i(
              "TweetListActivity::filter, filtered ",
              status.getUser().getScreenName() + " - " + status.getText());
        } else {
          data.add(status);
          statuses.add(status);
        }
      }
      metaList = new MetaList<Status>(data, myStatuses.getNumOriginal(), myStatuses.getNumAdded());
    } else {
      metaList = new MetaList<Status>(new ArrayList<Status>(), 0, 0);
    }

    if (newLast2 > last) {
      metaList.oldLast = newLast2;
      // the read status from remote is newer than the last read locally, so lets mark those in
      // between as read
      for (Status s : statuses) {
        long id = s.getId();
        if (id > last) {
          th.markStatusAsOld(id);
        }
      }
    } else {
      metaList.oldLast = last;
    }

    for (Status status : metaList.getList()) {
      AccountHolder accountHolder = AccountHolder.getInstance();
      accountHolder.addUserName(status.getUser().getScreenName());
      if (status.getHashtagEntities() != null) {
        for (HashtagEntity hte : status.getHashtagEntities()) {
          accountHolder.addHashTag(hte.getText());
        }
      }
    }

    return metaList;
  }