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

The following examples show how to use javax.jdo.Query#setResult() . 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: AlpineQueryManager.java    From Alpine with Apache License 2.0 7 votes vote down vote up
/**
 * Determines if the specified UserPrincipal has been assigned the specified permission.
 * @param user the UserPrincipal to query
 * @param permissionName the name of the permission
 * @param includeTeams if true, will query all Team membership assigned to the user for the specified permission
 * @return true if the user has the permission assigned, false if not
 * @since 1.0.0
 */
public boolean hasPermission(final UserPrincipal user, String permissionName, boolean includeTeams) {
    Query query;
    if (user instanceof ManagedUser) {
        query = pm.newQuery(Permission.class, "name == :permissionName && managedUsers.contains(:user)");
    } else if (user instanceof LdapUser) {
        query = pm.newQuery(Permission.class, "name == :permissionName && ldapUsers.contains(:user)");
    } else {
        query = pm.newQuery(Permission.class, "name == :permissionName && oidcUsers.contains(:user)");
    }
    query.setResult("count(id)");
    final long count = (Long) query.execute(permissionName, user);
    if (count > 0) {
        return true;
    }
    if (includeTeams) {
        for (final Team team: user.getTeams()) {
            if (hasPermission(team, permissionName)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 2
Source File: AbstractAlpineQueryManager.java    From Alpine with Apache License 2.0 6 votes vote down vote up
/**
 * Given a query, this method will decorate that query with pagination, ordering,
 * and sorting direction. Specific checks are performed to ensure the execution
 * of the query is capable of being paged and that ordering can be securely performed.
 * @param query the JDO Query object to execute
 * @return a Collection of objects
 * @since 1.0.0
 */
public Query decorate(final Query query) {
    // Clear the result to fetch if previously specified (i.e. by getting count)
    query.setResult(null);
    if (pagination != null && pagination.isPaginated()) {
        final long begin = pagination.getOffset();
        final long end = begin + pagination.getLimit();
        query.setRange(begin, end);
    }
    if (orderBy != null && RegexSequence.Pattern.STRING_IDENTIFIER.matcher(orderBy).matches() && orderDirection != OrderDirection.UNSPECIFIED) {
        // Check to see if the specified orderBy field is defined in the class being queried.
        boolean found = false;
        final org.datanucleus.store.query.Query iq = ((JDOQuery) query).getInternalQuery();
        final String candidateField = orderBy.contains(".") ? orderBy.substring(0, orderBy.indexOf('.')) : orderBy;
        for (final Field field: iq.getCandidateClass().getDeclaredFields()) {
            if (candidateField.equals(field.getName())) {
                found = true;
                break;
            }
        }
        if (found) {
            query.setOrdering(orderBy + " " + orderDirection.name().toLowerCase());
        }
    }
    return query;
}
 
Example 3
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
private <T> Long getCount(Class<T> tClass) {
  PersistenceManager pm = null;
  Long size = Long.valueOf(-1);
  try {
    pm = openTransaction();
    Query query = pm.newQuery();
    query.setClass(tClass);
    query.setResult("count(this)");
    size = (Long)query.execute();

  } finally {
    if (pm != null) {
      commitTransaction(pm);
    }
  }
  return size;
}
 
Example 4
Source File: AbstractAlpineQueryManager.java    From Alpine with Apache License 2.0 5 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
 * @return the number of items
 * @since 1.0.0
 */
public long getCount(final Query query) {
    //query.addExtension("datanucleus.query.resultSizeMethod", "count");
    final String ordering = ((JDOQuery) query).getInternalQuery().getOrdering();
    query.setResult("count(id)");
    query.setOrdering(null);
    final long count = (Long) query.execute();
    query.setOrdering(ordering);
    return count;
}
 
Example 5
Source File: IncomingInvoiceItemRepository.java    From estatio with Apache License 2.0 5 votes vote down vote up
@Programmatic
public List<LocalDate> findDistinctReportDates() {
    final PersistenceManager pm = isisJdoSupport.getJdoPersistenceManager();
    final Query query = pm.newQuery(IncomingInvoiceItem.class);
    query.setResultClass(LocalDate.class);
    query.setResult("distinct reportedDate");
    query.setOrdering("reportedDate descending");
    query.setFilter("reportedDate != null"); // EST-1838: Null value was set to LocalDate#now by DN
    return executeListAndClose(query);
}
 
Example 6
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private boolean hasAnyServerPrivileges(Set<String> roleNames, String serverName) {
  if (roleNames == null || roleNames.isEmpty()) {
    return false;
  }
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryPrivilege.class);
    query.declareVariables("org.apache.sentry.provider.db.service.model.MSentryRole role");
    List<String> rolesFiler = new LinkedList<String>();
    for (String rName : roleNames) {
      rolesFiler.add("role.roleName == \"" + rName.trim().toLowerCase() + "\"");
    }
    StringBuilder filters = new StringBuilder("roles.contains(role) "
        + "&& (" + Joiner.on(" || ").join(rolesFiler) + ") ");
    filters.append("&& serverName == \"" + serverName.trim().toLowerCase() + "\"");
    query.setFilter(filters.toString());
    query.setResult("count(this)");

    Long numPrivs = (Long) query.execute();
    rollbackTransaction = false;
    commitTransaction(pm);
    return numPrivs > 0;
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}
 
Example 7
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
private Set<MSentryPrivilege> getChildPrivileges(PersistenceManager pm, Set<String> roleNames,
    MSentryPrivilege parent) throws SentryInvalidInputException {
  // Column and URI do not have children
  if (!isNULL(parent.getColumnName()) || !isNULL(parent.getURI())) {
    return new HashSet<MSentryPrivilege>();
  }

  Query query = pm.newQuery(MSentryPrivilege.class);
  query.declareVariables("org.apache.sentry.provider.db.service.model.MSentryRole role");
  List<String> rolesFiler = new LinkedList<String>();
  for (String rName : roleNames) {
    rolesFiler.add("role.roleName == \"" + rName.trim().toLowerCase() + "\"");
  }
  StringBuilder filters = new StringBuilder("roles.contains(role) "
      + "&& (" + Joiner.on(" || ").join(rolesFiler) + ")");
  filters.append(" && serverName == \"" + parent.getServerName() + "\"");
  if (!isNULL(parent.getDbName())) {
    filters.append(" && dbName == \"" + parent.getDbName() + "\"");
    if (!isNULL(parent.getTableName())) {
      filters.append(" && tableName == \"" + parent.getTableName() + "\"");
      filters.append(" && columnName != \"__NULL__\"");
    } else {
      filters.append(" && tableName != \"__NULL__\"");
    }
  } else {
    filters.append(" && (dbName != \"__NULL__\" || URI != \"__NULL__\")");
  }

  query.setFilter(filters.toString());
  query.setResult("privilegeScope, serverName, dbName, tableName, columnName," +
      " URI, action, grantOption");
  Set<MSentryPrivilege> privileges = new HashSet<MSentryPrivilege>();
  for (Object[] privObj : (List<Object[]>) query.execute()) {
    MSentryPrivilege priv = new MSentryPrivilege();
    priv.setPrivilegeScope((String) privObj[0]);
    priv.setServerName((String) privObj[1]);
    priv.setDbName((String) privObj[2]);
    priv.setTableName((String) privObj[3]);
    priv.setColumnName((String) privObj[4]);
    priv.setURI((String) privObj[5]);
    priv.setAction((String) privObj[6]);
    priv.setGrantOption((Boolean) privObj[7]);
    privileges.add(priv);
  }
  return privileges;
}
 
Example 8
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;
}
 
Example 9
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.
 * @return the number of items
 * @since 1.0.0
 */
public long getCount(final Query query, final Object p1, final Object p2) {
    final String ordering = ((JDOQuery) query).getInternalQuery().getOrdering();
    query.setResult("count(id)");
    query.setOrdering(null);
    final long count = (Long) query.execute(p1, p2);
    query.setOrdering(ordering);
    return count;
}
 
Example 10
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 11
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 parameters the <code>Object</code> array with all of the parameters
 * @return the number of items
 * @since 1.0.0
 */
public long getCount(final Query query, final Object... parameters) {
    final String ordering = ((JDOQuery) query).getInternalQuery().getOrdering();
    query.setResult("count(id)");
    query.setOrdering(null);
    final long count = (Long) query.executeWithArray(parameters);
    query.setOrdering(ordering);
    return count;
}
 
Example 12
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 parameters the <code>Map</code> containing all of the parameters.
 * @return the number of items
 * @since 1.0.0
 */
public long getCount(final Query query, final Map parameters) {
    final String ordering = ((JDOQuery) query).getInternalQuery().getOrdering();
    query.setResult("count(id)");
    query.setOrdering(null);
    final long count = (Long) query.executeWithMap(parameters);
    query.setOrdering(ordering);
    return count;
}
 
Example 13
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 cls the persistence-capable class to query
 * @return the number of items
 * @param <T> candidate type for the query
 * @since 1.0.0
 */
public <T> long getCount(final Class<T> cls) {
    final Query query = pm.newQuery(cls);
    //query.addExtension("datanucleus.query.resultSizeMethod", "count");
    query.setResult("count(id)");
    return (Long) query.execute();
}
 
Example 14
Source File: AlpineQueryManager.java    From Alpine with Apache License 2.0 2 votes vote down vote up
/**
 * Determines if the specified Team has been assigned the specified permission.
 * @param team the Team to query
 * @param permissionName the name of the permission
 * @return true if the team has the permission assigned, false if not
 * @since 1.0.0
 */
public boolean hasPermission(final Team team, String permissionName) {
    final Query query = pm.newQuery(Permission.class, "name == :permissionName && teams.contains(:team)");
    query.setResult("count(id)");
    return (Long) query.execute(permissionName, team) > 0;
}