1 /*
2 * Copyright 2007 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.kns.util;
17
18 import java.util.Iterator;
19
20 import org.apache.commons.collections.IteratorUtils;
21
22 /**
23 * This class provides utility methods to support the operation of transactional services
24 */
25 public class TransactionalServiceUtils {
26 /**
27 * Copys iterators so that they may be used outside of this class. Often, the DAO may
28 * return iterators that may not be used outside of this class because the transaction/
29 * connection may be automatically closed by Spring.
30 *
31 * This method copies all of the elements in the OJB backed iterators into list-based iterators
32 * by placing the returned BOs into a list
33 *
34 * @param iter an OJB backed iterator to copy
35 * @return an Iterator that may be used outside of this class
36 */
37 public static <E> Iterator<E> copyToExternallyUsuableIterator(Iterator<E> iter) {
38 return IteratorUtils.toList(iter).iterator();
39 }
40
41 /**
42 * Returns the first element and exhausts an iterator
43 *
44 * @param <E> the type of elements in the iterator
45 * @param iterator the iterator to exhaust
46 * @return the first element of the iterator; null if the iterator's empty
47 */
48 public static <E> E retrieveFirstAndExhaustIterator(Iterator<E> iterator) {
49 E returnVal = null;
50 if (iterator.hasNext()) {
51 returnVal = iterator.next();
52 }
53 exhaustIterator(iterator);
54 return returnVal;
55 }
56
57 /**
58 * Exhausts (i.e. complete iterates through) an iterator
59 *
60 * @param iterator
61 */
62 public static void exhaustIterator(Iterator<?> iterator) {
63 while (iterator.hasNext()) {
64 iterator.next();
65 }
66 }
67 }