Java Code Examples for javax.jdo.Query#setOrdering()

The following examples show how to use javax.jdo.Query#setOrdering() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryManager.java    From dependency-track with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a list of all Components defined in the datastore.
 * @return a List of Components
 */
@SuppressWarnings("unchecked")
public PaginatedResult getComponents(final boolean includeMetrics) {
    final PaginatedResult result;
    final Query query = pm.newQuery(Component.class);
    if (orderBy == null) {
        query.setOrdering("name asc, version desc");
    }
    if (filter != null) {
        query.setFilter("name.toLowerCase().matches(:name)");
        final String filterString = ".*" + filter.toLowerCase() + ".*";
        result = execute(query, filterString);
    } else {
        result = execute(query);
    }
    if (includeMetrics) {
        // Populate each Component object in the paginated result with transitive related
        // data to minimize the number of round trips a client needs to make, process, and render.
        for (Component component : result.getList(Component.class)) {
            component.setMetrics(getMostRecentComponentMetrics(component));
            component.setUsedBy(Math.toIntExact(getDependencyCount(component)));
        }
    }
    return result;
}
 
Example 2
Source File: GuestServiceImpl.java    From appengine-gwtguestbook-namespaces-java with Apache License 2.0 6 votes vote down vote up
@Override
public List<GuestbookEntryTransferObject> getTenLatestEntries() {
  PersistenceManager pm = PersistenceManagerHelper.getPersistenceManager();
  try {
    // Set the query to get the ten latest guest entries
    Query query = pm.newQuery(GuestbookEntry.class);
    query.setOrdering("timestamp DESC");
    query.setRange("0, 10");
    List<GuestbookEntry> entries = (List<GuestbookEntry>) query.execute();

    // Create a new guestbook entry transfer object for each entry and add
    // them to the list
    List<GuestbookEntryTransferObject> entryTransferObjects =
        new ArrayList<GuestbookEntryTransferObject>(entries.size());
    for (GuestbookEntry entry : entries) {
      entryTransferObjects.add(new GuestbookEntryTransferObject(entry
          .getName(), entry.getMessage()));
    }
    return entryTransferObjects;
  } finally {
    if (pm.currentTransaction().isActive()) {
      pm.currentTransaction().rollback();
    }
  }
}
 
Example 3
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a paginated list of all notification rules.
 * @return a paginated list of NotificationRules
 */
@SuppressWarnings("unchecked")
public PaginatedResult getNotificationRules() {
    final Query query = pm.newQuery(NotificationRule.class);
    if (orderBy == null) {
        query.setOrdering("name asc");
    }
    if (filter != null) {
        query.setFilter("name.toLowerCase().matches(:name) || publisher.name.toLowerCase().matches(:name)");
        final String filterString = ".*" + filter.toLowerCase() + ".*";
        return execute(query, filterString);
    }
    return execute(query);
}
 
Example 4
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves PortfolioMetrics in descending order starting with the most recent.
 * @return a PaginatedResult object
 */
@SuppressWarnings("unchecked")
public PaginatedResult getPortfolioMetrics() {
    final Query query = pm.newQuery(PortfolioMetrics.class);
    query.setOrdering("lastOccurrence desc");
    return execute(query);
}
 
Example 5
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves ComponentMetrics in descending order starting with the most recent.
 * @param component the Component to retrieve metrics for
 * @return a PaginatedResult object
 */
@SuppressWarnings("unchecked")
public PaginatedResult getComponentMetrics(Component component) {
    final Query query = pm.newQuery(ComponentMetrics.class, "component == :component");
    query.setOrdering("lastOccurrence desc");
    return execute(query, component);
}
 
Example 6
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of all components.
 * This method if designed NOT to provide paginated results.
 * @return a List of Components
 */
@SuppressWarnings("unchecked")
public List<Component> getAllComponents() {
    final Query query = pm.newQuery(Component.class);
    query.setOrdering("id asc");
    return query.executeResultList(Component.class);
}
 
Example 7
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of projects by it's name.
 * @param name the name of the Projects (required)
 * @return a List of Project objects
 */
@SuppressWarnings("unchecked")
public PaginatedResult getProjects(final String name, final boolean excludeInactive) {
    final Query query = pm.newQuery(Project.class);
    if (orderBy == null) {
        query.setOrdering("version desc");
    }
    if (excludeInactive) {
        query.setFilter("name == :name && active == true");
    } else {
        query.setFilter("name == :name");
    }
    return execute(query, name);
}
 
Example 8
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of all repositories.
 * @return a List of Repositories
 */
@SuppressWarnings("unchecked")
public PaginatedResult getRepositories() {
    final Query query = pm.newQuery(Repository.class);
    if (orderBy == null) {
        query.setOrdering("type asc, identifier asc");
    }
    if (filter != null) {
        query.setFilter("identifier.toLowerCase().matches(:identifier)");
        final String filterString = ".*" + filter.toLowerCase() + ".*";
        return execute(query, filterString);
    }
    return execute(query);
}
 
Example 9
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a List of Dependency for the specified Component.
 * @param component the Component to retrieve dependencies of
 * @return a List of Dependency objects
 */
@SuppressWarnings("unchecked")
public PaginatedResult getDependencies(Component component) {
    final Query query = pm.newQuery(Dependency.class, "component == :component");
    query.setOrdering("id asc");
    query.getFetchPlan().addGroup(Dependency.FetchGroup.PROJECT_ONLY.name());
    return execute(query, component);
}
 
Example 10
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Action> getUserItemActions(long itemId,long userId,int limit) {
	Query query = pm.newQuery( Action.class, "itemId == i &&  userId == u" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.Long i,java.lang.Long u");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(itemId,userId);
	return c;
}
 
Example 11
Source File: AlpineQueryManager.java    From Alpine with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a complete list of all Team objects, in ascending order by name.
 * @return a List of Teams
 * @since 1.0.0
 */
@SuppressWarnings("unchecked")
public List<Team> getTeams() {
    pm.getFetchPlan().addGroup(Team.FetchGroup.ALL.name());
    final Query query = pm.newQuery(Team.class);
    query.setOrdering("name asc");
    return (List<Team>) query.execute();
}
 
Example 12
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Action> getItemActions(long itemId, int limit) {
	Query query = pm.newQuery( Action.class, "itemId == i" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.Long i");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(itemId);
	return c;
}
 
Example 13
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves DependencyMetrics in ascending order starting with the oldest since the date specified.
 * @return a List of metrics
 */
@SuppressWarnings("unchecked")
public List<DependencyMetrics> getDependencyMetricsSince(Dependency dependency, Date since) {
    final Query query = pm.newQuery(DependencyMetrics.class, "project == :project && component == :component && lastOccurrence >= :since");
    query.setOrdering("lastOccurrence asc");
    return (List<DependencyMetrics>)query.execute(dependency.getProject(), dependency.getComponent(), since);
}
 
Example 14
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a List of all License objects.
 * @return a List of all License objects
 */
@SuppressWarnings("unchecked")
public PaginatedResult getLicenses() {
    final Query query = pm.newQuery(License.class);
    query.getFetchPlan().addGroup(License.FetchGroup.ALL.name());
    if (orderBy == null) {
        query.setOrdering("name asc");
    }
    if (filter != null) {
        query.setFilter("name.toLowerCase().matches(:filter) || licenseId.toLowerCase().matches(:filter)");
        final String filterString = ".*" + filter.toLowerCase() + ".*";
        return execute(query, filterString);
    }
    return execute(query);
}
 
Example 15
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a List of all CPE objects.
 * @return a List of all CPE objects
 */
@SuppressWarnings("unchecked")
public PaginatedResult getCpes() {
    final Query query = pm.newQuery(Cpe.class);
    if (orderBy == null) {
        query.setOrdering("id asc");
    }
    if (filter != null) {
        query.setFilter("vendor.toLowerCase().matches(:filter) || product.toLowerCase().matches(:filter)");
        final String filterString = ".*" + filter.toLowerCase() + ".*";
        return execute(query, filterString);
    }
    return execute(query);
}
 
Example 16
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves ProjectMetrics in ascending order starting with the oldest since the date specified.
 * @return a List of metrics
 */
@SuppressWarnings("unchecked")
public List<ProjectMetrics> getProjectMetricsSince(Project project, Date since) {
    final Query query = pm.newQuery(ProjectMetrics.class, "project == :project && lastOccurrence >= :since");
    query.setOrdering("lastOccurrence asc");
    return (List<ProjectMetrics>)query.execute(project, since);
}
 
Example 17
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of all projects.
 * This method if designed NOT to provide paginated results.
 * @return a List of Projects
 */
@SuppressWarnings("unchecked")
public List<Project> getAllProjects() {
    final Query query = pm.newQuery(Project.class);
    query.setOrdering("name asc");
    return query.executeResultList(Project.class);
}
 
Example 18
Source File: QueryManager.java    From dependency-track with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieves the most recent PortfolioMetrics.
 * @return a PortfolioMetrics object
 */
public PortfolioMetrics getMostRecentPortfolioMetrics() {
    final Query query = pm.newQuery(PortfolioMetrics.class);
    query.setOrdering("lastOccurrence desc");
    return singleResult(query.execute());
}
 
Example 19
Source File: AbstractAlpineQueryManager.java    From Alpine with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the number of items that would have resulted from returning all object.
 * This method is performant in that the objects are not actually retrieved, only
 * the count.
 * @param query the query to return a count from
 * @param p1 the value of the first parameter declared.
 * @param p2 the value of the second parameter declared.
 * @param p3 the value of the third parameter declared.
 * @return the number of items
 * @since 1.0.0
 */
public long getCount(final Query query, final Object p1, final Object p2, final Object p3) {
    final String ordering = ((JDOQuery) query).getInternalQuery().getOrdering();
    query.setResult("count(id)");
    query.setOrdering(null);
    final long count = (Long) query.execute(p1, p2, p3);
    query.setOrdering(ordering);
    return count;
}
 
Example 20
Source File: AbstractAlpineQueryManager.java    From Alpine with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the number of items that would have resulted from returning all object.
 * This method is performant in that the objects are not actually retrieved, only
 * the count.
 * @param query the query to return a count from
 * @param p1 the value of the first parameter declared.
 * @return the number of items
 * @since 1.0.0
 */
public long getCount(final Query query, final Object p1) {
    final String ordering = ((JDOQuery) query).getInternalQuery().getOrdering();
    query.setResult("count(id)");
    query.setOrdering(null);
    final long count = (Long) query.execute(p1);
    query.setOrdering(ordering);
    return count;
}