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

The following examples show how to use javax.jdo.Query#closeAll() . 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: PaperclipsForInvoiceForLeaseRepository.java    From estatio with Apache License 2.0 7 votes vote down vote up
/**
 * It's necessary to use the low-level API because the candidate class ({@link PaperclipForInvoice}) and the
 * result class ({@link InvoiceForLease}) are different.
 */
@Programmatic
public List<InvoiceForLease> findInvoicesByYearWithSupportingDocuments(final int year) {

    final PersistenceManager jdoPersistenceManager = isisJdoSupport.getJdoPersistenceManager();
    final Query query = jdoPersistenceManager.newNamedQuery(
            PaperclipForInvoice.class,
            "findInvoicesByInvoiceDateBetweenWithSupportingDocuments");
    query.setResultClass(InvoiceForLease.class);

    try {
        final LocalDate invoiceDateFrom = new LocalDate(year,1,1);
        final LocalDate invoiceDateTo = invoiceDateFrom.plusYears(1);

        final List results = (List) query.executeWithMap(ImmutableMap.of(
                "invoiceDateFrom", invoiceDateFrom,
                "invoiceDateTo", invoiceDateTo
        ));
        return Lists.newArrayList(results);
    } finally {
        query.closeAll();
    }
}
 
Example 2
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public List<Long> getRecentItemIdsWithTags(int tagAttrId,Set<String> tags, int limit) {
	Query query;
	query = pm.newQuery("javax.jdo.query.SQL","select item_id,value from item_map_varchar where attr_id=? order by item_id desc limit "+limit);			

	Collection<Object[]> results = (Collection<Object[]>) query.execute(tagAttrId);
	List<Long> resf = new ArrayList<>();
	for(Object[] r : results)
	{
		Long itemId = (Long) r[0];
		String itemTags = (String) r[1];
		String[] parts = itemTags.split(",");
		for(int i=0;i<parts.length;i++)
			if (tags.contains(parts[i].toLowerCase().trim()))
			{
				resf.add(itemId);
				break;
			}
	}
	query.closeAll();
	return resf;
}
 
Example 3
Source File: JdoItemSimilarityPeer.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Long, Double> getRecommendations(long userId, int dimension, int max) {
	String sql = "select r.item_id,score from recommendations r join item_map_enum ime on (r.item_id=ime.item_id) join dimension d on (ime.attr_id=d.attr_id and ime.value_id=d.value_id) where dim_id=? and user_id=? order by score desc";
	if (max > 0)
		sql = sql + " limit "+max;
	Query query = getPM().newQuery( "javax.jdo.query.SQL", sql);
	Collection<Object[]> results = (Collection<Object[]>) query.execute(dimension,userId);
	Map<Long,Double> map = new HashMap<>();
	for(Object[] res : results)
	{
		Long item = (Long) res[0];
		Double score = (Double) res[1];
		map.put(item, score);
	}
	query.closeAll();
	return map;
}
 
Example 4
Source File: SqlBaselineRecommenderUtils.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Long, Double> getPopularItems(int dimension, int numRecommendations) {
	
	String sql = "select ip.item_id,opsum from items_popular ip join item_map_enum ime on (ip.item_id=ime.item_id) join dimension d on (ime.attr_id=d.attr_id and ime.value_id=d.value_id) where dim_id=? order by opsum desc limit "+numRecommendations;
	Query query = getPM().newQuery( "javax.jdo.query.SQL", sql);
	ArrayList<Object> args = new ArrayList<>();
	args.add(dimension);
	Collection<Object[]> results = (Collection<Object[]>) query.executeWithArray(args.toArray());
	Map<Long,Double> map = new HashMap<>();
	Double topScore = null;
	for(Object[] res : results)
	{
		if (topScore == null)
			topScore = (Double) res[1];
		Long item = (Long) res[0];
		Double score = (Double) res[1];
		map.put(item, score/topScore);
	}
	query.closeAll();
	return map;
}
 
Example 5
Source File: SqlBaselineRecommenderUtils.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Long, Double> reorderByMostPopular(Set<Long> itemIds) {
	String idStr = StringUtils.join(itemIds, ",");
	String sql = "select ip.item_id,opsum from items_popular where item_id in ("+idStr+") order by opsum desc";
	Query query = getPM().newQuery( "javax.jdo.query.SQL", sql);
	Collection<Object[]> results = (Collection<Object[]>) query.execute();
	Map<Long,Double> map = new HashMap<>();
	Double topScore = null;
	for(Object[] res : results)
	{
		if (topScore == null)
			topScore = (Double) res[1];
		Long item = (Long) res[0];
		Double score = (Double) res[1];
		map.put(item, score/topScore);
	}
	query.closeAll();
	return map;
}
 
Example 6
Source File: SqlBaselineRecommenderUtils.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Long, Double> getAllItemPopularity() {
	String sql = "select item_id,opsum from items_popular order by opsum desc";
	Query query = getPM().newQuery( "javax.jdo.query.SQL", sql);
	Collection<Object[]> results = (Collection<Object[]>) query.execute();
	Map<Long,Double> map = new HashMap<>();
	Double topScore = null;
	for(Object[] res : results)
	{
		if (topScore == null)
			topScore = (Double) res[1];
		Long item = (Long) res[0];
		Double score = (Double) res[1];
		map.put(item, score/topScore);
	}
	query.closeAll();
	return map;
}
 
Example 7
Source File: IncomingInvoiceRepository.java    From estatio with Apache License 2.0 6 votes vote down vote up
@Programmatic
public boolean findCreditNotesInStateOf(
        final BankAccount creditorBankAccount,
        final List<IncomingInvoiceApprovalState> upstreamStates) {
    final PersistenceManager jdoPersistenceManager = isisJdoSupport.getJdoPersistenceManager();
    final String sql = constructSqlForCreditNotesInStateOf(upstreamStates);
    final Query query = jdoPersistenceManager.newQuery("javax.jdo.query.SQL", sql);
    try {
        query.setResultClass(Integer.class);
        final DatastoreIdImpl datastoreId = (DatastoreIdImpl) JDOHelper.getObjectId(creditorBankAccount);
        final long idKeyAsObject = (long) datastoreId.getKeyAsObject();
        final ForwardQueryResult result = (ForwardQueryResult) query.executeWithMap(ImmutableMap.of("bankAccountId", idKeyAsObject));
        return !result.isEmpty();
    } finally {
        if (query!=null) query.closeAll();
    }
}
 
Example 8
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public List<Long> getRecentItemIds(Set<Integer> dimensions, int limit, ConsumerBean c) {
	Query query;
	if (dimensions.isEmpty() || (dimensions.size() == 1 && dimensions.iterator().next() == Constants.DEFAULT_DIMENSION))
		query = pm.newQuery("javax.jdo.query.SQL","select i.item_id from items i order by i.item_id desc limit "+limit);			
	else
		query = pm.newQuery("javax.jdo.query.SQL","select i.item_id from items i natural join item_map_enum e join dimension d on (d.dim_id in ("+StringUtils.join(dimensions, ",")+") and e.attr_id=d.attr_id and e.value_id=d.value_id and i.type=d.item_type) order by i.item_id desc limit "+limit);

	query.setResultClass(Long.class);
	Collection<Long> res = (Collection<Long>) query.execute();
	List<Long> resf = new ArrayList<>(res);
	query.closeAll();
	return resf;
}
 
Example 9
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public List<Long> getRecentItemIdsTwoDimensions(Set<Integer> dimensions, int dimension2,int limit, ConsumerBean c) {
	Query query;
	if (dimensions.isEmpty() || (dimensions.size() == 1 && dimensions.iterator().next() == Constants.DEFAULT_DIMENSION))
		query = pm.newQuery("javax.jdo.query.SQL","select i.item_id from items i natural join item_map_enum e join dimension d on (d.dim_id = ? and e.attr_id=d.attr_id and e.value_id=d.value_id and i.type=d.item_type) order by i.item_id desc limit "+limit);
	else
		query = pm.newQuery("javax.jdo.query.SQL","select i.item_id from items i natural join item_map_enum e join dimension d on (d.dim_id in ("+StringUtils.join(dimensions, ",")+") and e.attr_id=d.attr_id and e.value_id=d.value_id and i.type=d.item_type) join item_map_enum e2 on (i.item_id=e2.item_id) join dimension d2 on (d2.dim_id = ? and e2.attr_id=d2.attr_id and e2.value_id=d2.value_id and i.type=d2.item_type) order by i.item_id desc limit "+limit);
		
	query.setResultClass(Long.class);
	Collection<Long> res = (Collection<Long>) query.execute(dimension2);
	List<Long> resf = new ArrayList<>(res);
	query.closeAll();
	return resf;
}
 
Example 10
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Map<Long, List<String>> getRecentItemTags(Set<Long> ids, int attrId,String table) {
	Map<Long,List<String>> res = new HashMap<>();
	if (ids != null && ids.size() > 0)
	{
		String idStr = CollectionTools.join(ids, ",");
		Query query = pm.newQuery("javax.jdo.query.SQL","select item_id,value from item_map_"+table+" where attr_id="+attrId+" and item_id in ("+idStr+")");				
		Collection<Object[]> results = (Collection<Object[]>) query.execute();

		for(Object[] r : results)
		{
			Long itemId = (Long) r[0];
			String tags = (String) r[1];
			String[] parts = tags.split(",");
			List<String> tagList = new ArrayList<>();
			for(int i=0;i<parts.length;i++)
			{
				String tag = StringUtils.trimToEmpty(parts[i]);
				if (!StringUtils.isEmpty(tag))
					tagList.add(tag);
			}
			if (tagList.size() > 0)
				res.put(itemId, tagList);
		}
		query.closeAll();
	}
	return res;
}
 
Example 11
Source File: IncomingInvoiceItemRepository.java    From estatio with Apache License 2.0 4 votes vote down vote up
private static <T> List<T> executeListAndClose(final Query query) {
    final List<T> elements = (List<T>) query.execute();
    final List<T> list = Lists.newArrayList(elements);
    query.closeAll();
    return list;
}