@Override public List<NodePropertyEntity> selectNodePropertiesByTypes(Set<QName> qnames) { final List<NodePropertyEntity> properties = new ArrayList<NodePropertyEntity>(); // qnames of properties that are encrypted Set<Long> qnameIds = qnameDAO.convertQNamesToIds(qnames, false); if(qnameIds.size() > 0) { IdsEntity param = new IdsEntity(); param.setIds(new ArrayList<Long>(qnameIds)); // TODO - use a callback approach template.select(SELECT_PROPERTIES_BY_TYPES, param, new ResultHandler() { @Override public void handleResult(ResultContext context) { properties.add((NodePropertyEntity)context.getResultObject()); } }); } return properties; }
@Override public List<NodePropertyEntity> selectNodePropertiesByDataType(QName dataType, long minNodeId, long maxNodeId) { int typeOrdinal = NodePropertyValue.convertToTypeOrdinal(dataType); IdsEntity ids = new IdsEntity(); ids.setIdOne((long)typeOrdinal); ids.setIdTwo(minNodeId); ids.setIdThree(maxNodeId); final List<NodePropertyEntity> properties = new ArrayList<NodePropertyEntity>(); template.select(SELECT_PROPERTIES_BY_ACTUAL_TYPE, ids, new ResultHandler() { @Override public void handleResult(ResultContext context) { properties.add((NodePropertyEntity)context.getResultObject()); } }); return properties; }
@Override protected void selectNodesWithAspects( List<Long> qnameIds, Long minNodeId, Long maxNodeId, final NodeRefQueryCallback resultsCallback) { ResultHandler resultHandler = new ResultHandler() { public void handleResult(ResultContext context) { NodeEntity entity = (NodeEntity) context.getResultObject(); Pair<Long, NodeRef> nodePair = new Pair<Long, NodeRef>(entity.getId(), entity.getNodeRef()); resultsCallback.handle(nodePair); } }; IdsEntity parameters = new IdsEntity(); parameters.setIdOne(minNodeId); parameters.setIdTwo(maxNodeId); parameters.setIds(qnameIds); template.select(SELECT_NODES_WITH_ASPECT_IDS, parameters, resultHandler); }
@Test public void testDeferLoadDuringResultHandler() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { class MyResultHandler implements ResultHandler { @Override public void handleResult(ResultContext context) { Child child = (Child)context.getResultObject(); assertNotNull(child.getFather()); } }; sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler()); } finally { sqlSession.close(); } }
@Test public void testDeferLoadDuringResultHandlerWithLazyLoad() { SqlSession sqlSession = lazyLoadSqlSessionFactory.openSession(); try { class MyResultHandler implements ResultHandler { @Override public void handleResult(ResultContext context) { Child child = (Child)context.getResultObject(); assertNotNull(child.getFather()); } }; sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler()); } finally { sqlSession.close(); } }
@Test // issue #542 public void testGetPersonWithHandler() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { sqlSession.select("getPersons", new ResultHandler() { public void handleResult(ResultContext context) { Person person = (Person) context.getResultObject(); if ("grandma".equals(person.getName())) { Assert.assertEquals(2, person.getItems().size()); } } }); } finally { sqlSession.close(); } }
@Test(expected=PersistenceException.class) public void testUnorderedGetPersonWithHandler() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { sqlSession.select("getPersonsWithItemsOrdered", new ResultHandler() { public void handleResult(ResultContext context) { Person person = (Person) context.getResultObject(); if ("grandma".equals(person.getName())) { Assert.assertEquals(2, person.getItems().size()); } } }); } finally { sqlSession.close(); } }
@Test public void shouldHandleRowBounds() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date targetMonth = fmt.parse("2014-01-01"); final List<Account> accounts = new ArrayList<Account>(); try { sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), new ResultHandler() { @Override public void handleResult(ResultContext context) { Account account = (Account) context.getResultObject(); accounts.add(account); } }); } finally { sqlSession.close(); } assertEquals(2, accounts.size()); assertEquals("Bob2", accounts.get(0).getAccountName()); assertEquals("Bob3", accounts.get(1).getAccountName()); }
@Test public void shouldHandleStop() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); final List<Account> accounts = new ArrayList<Account>(); try { Date targetMonth = fmt.parse("2014-01-01"); sqlSession.select("collectPageByBirthMonth", targetMonth, new ResultHandler() { @Override public void handleResult(ResultContext context) { Account account = (Account) context.getResultObject(); accounts.add(account); if (accounts.size() > 1) context.stop(); } }); } finally { sqlSession.close(); } assertEquals(2, accounts.size()); assertEquals("Bob1", accounts.get(0).getAccountName()); assertEquals("Bob2", accounts.get(1).getAccountName()); }
/** * handle the result * * @param context the result context */ @Override public void handleResult(ResultContext context) { super.handleResult(context); Map<K, V> mappedResults = getMappedResults(); K key = null; for (Map.Entry<K, V> entry : mappedResults.entrySet()) { V value = entry.getValue(); key = entry.getKey(); /** * 默认的Map实例为HashMap * {@link DefaultObjectFactory#resolveInterface} */ if (value.getClass().isAssignableFrom(HashMap.class)) { key = entry.getKey(); break; } } if (mappedResults.get(key) == null) { getMappedResults().put(key, null); } else { Map subMap = (Map) mappedResults.get(key); getMappedResults().put(key, subMap.get(this.mapValue)); } }
@Override protected void findPropertiesByIds(List<Long> ids, final PropertyFinderCallback callback) { ResultHandler valueResultHandler = new ResultHandler() { public void handleResult(ResultContext context) { PropertyIdQueryResult result = (PropertyIdQueryResult) context.getResultObject(); Long id = result.getPropId(); // Make the serializable value List<PropertyIdSearchRow> rows = result.getPropValues(); Serializable value = convertPropertyIdSearchRows(rows); callback.handleProperty(id, value); } }; // A row handler to roll up individual rows Configuration configuration = template.getConfiguration(); RollupResultHandler rollupResultHandler = new RollupResultHandler( configuration, KEY_COLUMNS_FINDBYIDS, "propValues", valueResultHandler); // Query using the IDs PropertyIdQueryParameter params = new PropertyIdQueryParameter(); params.setRootPropIds(ids); template.select(SELECT_PROPERTIES_BY_IDS, params, rollupResultHandler); // Process any remaining results rollupResultHandler.processLastResults(); // Done }
@SuppressWarnings("unchecked") @Override public void handleResult(ResultContext ctx) { if (stopped || ctx.isStopped()) { return; // Fly through results without further callbacks } boolean more = this.target.handleResult((R)ctx.getResultObject()); if (!more) { ctx.stop(); stopped = true; } }
@SuppressWarnings("unchecked") public void handleResult(ResultContext context) { handler.handle((Map<String, Object>)context.getResultObject()); total++; if (logger.isDebugEnabled() && (total == 0 || (total % 1000 == 0) )) { logger.debug(" Listed " + total + " map entries"); } }
@Override public Pair<Long, Long> getNodeIdsIntervalForType(QName type, Long startTxnTime, Long endTxnTime) { final Pair<Long, Long> intervalPair = new Pair<Long, Long>(LONG_ZERO, LONG_ZERO); Pair<Long, QName> typePair = qnameDAO.getQName(type); if (typePair == null) { // Return default return intervalPair; } TransactionQueryEntity txnQuery = new TransactionQueryEntity(); txnQuery.setTypeQNameId(typePair.getFirst()); txnQuery.setMinCommitTime(startTxnTime); txnQuery.setMaxCommitTime(endTxnTime); ResultHandler resultHandler = new ResultHandler() { @SuppressWarnings("unchecked") public void handleResult(ResultContext context) { Map<Long, Long> result = (Map<Long, Long>) context.getResultObject(); if (result != null) { intervalPair.setFirst(result.get("minId")); intervalPair.setSecond(result.get("maxId")); } } }; template.select(SELECT_NODE_INTERVAL_BY_TYPE, txnQuery, resultHandler); return intervalPair; }
public void handleResult(ResultContext context) { // Shortcut if we have processed enough results if (maxResults > 0 && resultCount >= maxResults) { return; } Object valueObject = context.getResultObject(); MetaObject probe = configuration.newMetaObject(valueObject); // Check if the key has changed if (lastKeyValues == null) { lastKeyValues = getKeyValues(probe); resultCount = 0; } // Check if it has changed Object[] currentKeyValues = getKeyValues(probe); if (!Arrays.deepEquals(lastKeyValues, currentKeyValues)) { // Key has changed, so handle the results Object resultObject = coalesceResults(configuration, rawResults, collectionProperty); if (resultObject != null) { DefaultResultContext resultContext = new DefaultResultContext(); resultContext.nextResultObject(resultObject); resultHandler.handleResult(resultContext); resultCount++; } rawResults.clear(); lastKeyValues = currentKeyValues; } // Add the new value to the results for next time rawResults.add(valueObject); // Done }
@Override public void handleResult(ResultContext<? extends V> context) { final V value = context.getResultObject(); final MetaObject mo = MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory); // TODO is that assignment always true? final K key = (K) mo.getValue(mapKey); mappedResults.put(key, value); }
@Override public void handleResult(ResultContext context) { @SuppressWarnings("unchecked") E obj = (E)context.getResultObject(); log.debug(obj); resultList.add(obj); }