View Javadoc

1   /**
2    * Copyright 2005-2014 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.krad.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 final class TransactionalServiceUtils {
26  	
27  	private TransactionalServiceUtils() {
28  		throw new UnsupportedOperationException("do not call");
29  	}
30      /**
31       * Copys iterators so that they may be used outside of this class.  Often, the DAO may
32       * return iterators that may not be used outside of this class because the transaction/
33       * connection may be automatically closed by Spring.
34       * 
35       * This method copies all of the elements in the OJB backed iterators into list-based iterators
36       * by placing the returned BOs into a list
37       * 
38       * @param iter an OJB backed iterator to copy
39       * @return an Iterator that may be used outside of this class
40       */
41      public static <E> Iterator<E> copyToExternallyUsuableIterator(Iterator<E> iter) {
42          return IteratorUtils.toList(iter).iterator();
43      }
44      
45      /**
46       * Returns the first element and exhausts an iterator
47       * 
48       * @param <E> the type of elements in the iterator
49       * @param iterator the iterator to exhaust
50       * @return the first element of the iterator; null if the iterator's empty
51       */
52      public static <E> E retrieveFirstAndExhaustIterator(Iterator<E> iterator) {
53          E returnVal = null;
54          if (iterator.hasNext()) {
55              returnVal = iterator.next();
56          }
57          exhaustIterator(iterator);
58          return returnVal;
59      }
60      
61      /**
62       * Exhausts (i.e. complete iterates through) an iterator
63       * 
64       * @param iterator
65       */
66      public static void exhaustIterator(Iterator<?> iterator) {
67          while (iterator.hasNext()) {
68              iterator.next();
69          }
70      }
71  }