View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 p3j.database.hibernate;
17  
18  import java.util.Iterator;
19  
20  import org.hibernate.FlushMode;
21  import org.hibernate.ScrollableResults;
22  import org.hibernate.SessionFactory;
23  import org.hibernate.classic.Session;
24  import org.hibernate.criterion.Order;
25  import org.hibernate.criterion.Restrictions;
26  
27  import p3j.database.IProjectionResultsIterator;
28  import p3j.experiment.results.ResultsOfTrial;
29  import p3j.pppm.ProjectionModel;
30  
31  /**
32   * HIbernate implementation of a results iterator.
33   * 
34   * @author Christina Bohk
35   * @author Roland Ewald
36   * 
37   */
38  public class ProjectionResultsIterator implements IProjectionResultsIterator {
39  
40  	/** The hibernate session. */
41  	private final Session session;
42  
43  	/** The scrollable result set. */
44  	private final ScrollableResults results;
45  
46  	/** The previous results. */
47  	private ResultsOfTrial previousResult;
48  
49  	/** The next results. */
50  	private ResultsOfTrial nextResult;
51  
52  	/**
53  	 * Instantiates a new projection results iterator.
54  	 * 
55  	 * @param sessionFactory
56  	 *          the session factory
57  	 * @param projectionID
58  	 *          the projection ID
59  	 */
60  	public ProjectionResultsIterator(SessionFactory sessionFactory,
61  	    int projectionID) {
62  
63  		session = sessionFactory.openSession();
64  		session.setFlushMode(FlushMode.AUTO);
65  		ProjectionModel currentProjection = (ProjectionModel) session
66  		    .createCriteria(ProjectionModel.class)
67  		    .add(Restrictions.eq("ID", projectionID)).list().get(0);
68  
69  		results = session.createCriteria(ResultsOfTrial.class)
70  		    .add(Restrictions.eq("projection", currentProjection))
71  		    .addOrder(Order.asc("ID")).scroll();
72  		nextResult = getNextElement();
73  	}
74  
75  	private ResultsOfTrial getNextElement() {
76  		return !results.next() ? null : (ResultsOfTrial) results.get(0);
77  	}
78  
79  	@Override
80  	public ResultsOfTrial getNextResult() {
81  		if (previousResult != null) {
82  			clearFromCache(previousResult);
83  		}
84  
85  		ResultsOfTrial result = null;
86  		if (nextResult != null) {
87  			result = nextResult;
88  			nextResult = getNextElement();
89  			previousResult = result;
90  		}
91  		return result;
92  	}
93  
94  	/**
95  	 * Clears result from cache.
96  	 * 
97  	 * @param resultsOfTrial
98  	 *          the trial results to be cleared from the cache
99  	 */
100 	public void clearFromCache(ResultsOfTrial resultsOfTrial) {
101 		session.evict(resultsOfTrial);
102 		session.flush();
103 	}
104 
105 	@Override
106 	public Iterator<ResultsOfTrial> iterator() {
107 		return this;
108 	}
109 
110 	@Override
111 	public boolean hasNext() {
112 		return nextResult != null;
113 	}
114 
115 	@Override
116 	public ResultsOfTrial next() {
117 		return getNextResult();
118 	}
119 
120 	@Override
121 	public void remove() {
122 		throw new UnsupportedOperationException("Removal is not supported.");
123 	}
124 
125 }