View Javadoc

1   /*** ObservableFactory.java - part of the MirkE (say murky) application for colormetric analysis emphesizing 
2   kinetics.
3   
4   Created by: Scott Menor on 21 July, 2004.
5   Last modified by: Scott Menor on 12 December, 2004
6   
7   Copyright (c) 2004 Arizona State University - Cancer Research Institute. All rights reserved.
8   
9   MirkE is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13  
14  MirkE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18  
19  You should have received a copy of the GNU General Public License
20  along with MirkE; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
22  */
23  
24  package edu.asu.cri.MirkE.util;
25  
26  import edu.asu.cri.MirkE.dataStructures.*;
27  import edu.asu.cri.MirkE.exceptions.MirkEApplicationException;
28  
29  import java.util.List;
30  
31  /*** Simple utility class to prevent duplication of Observable objects 
32  
33  TODO - depreciate / remove this class by using features of Hibernate with a non-unique key (like Observable.name) 
34  */
35  public class ObservableFactory {
36  	private DataSet dataSet;
37  	
38  	/*** default constructor
39  	
40  	*/
41  	public ObservableFactory() {}
42  	
43  	/*** constructor
44  	
45  	 @param dataSet
46  	*/
47  	public ObservableFactory(DataSet dataSet) {
48  		setDataSet(dataSet);
49  	}
50  	
51  	/***
52  	@param dataSet
53  		*/
54  	public void setDataSet(DataSet dataSet) {
55  		this.dataSet = dataSet;
56  	}
57  	
58  	/***
59  		@return dataSet
60  	 */
61  	public DataSet getDataSet() {
62  		return this.dataSet;
63  	}
64  	
65  	/***
66  	 * @param name
67  	 * @return <code>Observable</code>
68  	 * @throws MirkEApplicationException
69  	 */
70  	public edu.asu.cri.MirkE.dataStructures.Observable getObservable(String name) throws MirkEApplicationException{
71  		if (dataSet != null) {
72  			try {
73  				
74  				List list = dataSet.find("from Observable as o where o.name='" + name + "'");
75  				
76  				if (list.size() > 0) {
77  					return (Observable)list.get(0);
78  					
79  					// TODO - should we do something special when more than one Observable matches the criteria? (ie - list.size()>1)
80  				}
81  				
82  				// list.size() == 0 - create and return a new Observable
83  				
84  			} catch (Exception exception) {
85  				exception.printStackTrace();
86  				
87  			}
88  		}
89  		
90  		edu.asu.cri.MirkE.dataStructures.Observable observable = new edu.asu.cri.MirkE.dataStructures.Observable(name);
91  		
92  		dataSet.save(observable);
93  		
94  		return observable;
95  	}
96  }