View Javadoc

1   /*
2    Wotonomy: OpenStep design patterns for pure Java applications.
3    Copyright (C) 2001 Michael Powers
4   
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14  
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, see http://www.gnu.org
17   */
18  package net.wotonomy.access;
19  
20  import java.lang.reflect.Constructor;
21  
22  import net.wotonomy.control.EOFetchSpecification;
23  import net.wotonomy.control.EOQualifier;
24  import net.wotonomy.foundation.NSArray;
25  import net.wotonomy.foundation.NSDictionary;
26  
27  /***
28  *
29  * @author ezamudio@nasoft.com
30  * @author $Author: cgruber $
31  * @version $Revision: 894 $
32  */
33  public class EOSQLExpressionFactory {
34  
35  	protected EOAdaptor _adaptor;
36  	protected Class _expressionClass;
37  	private Constructor _instantiator;
38  
39  	public EOSQLExpressionFactory(EOAdaptor adaptor) {
40  		super();
41  		_adaptor = adaptor;
42  		_expressionClass = _adaptor.expressionClass();
43  		if (_expressionClass == null)
44  			throw new IllegalStateException("EOAdaptor " + _adaptor.name() + " returned null for expressionClass()");
45  	}
46  
47  	public EOAdaptor adaptor() {
48  		return _adaptor;
49  	}
50  
51  	/***
52  	 * Creates an instance of the adaptor's expression class,
53  	 * with entity assigned to it.
54  	 * @param entity The entity with which to initialize the expression.
55  	 * @return An EOSQLExpression.
56  	 */
57  	public EOSQLExpression createExpression(EOEntity entity) {
58  		EOSQLExpression expr = null;
59  		if (_instantiator == null) {
60  			try {
61  				_instantiator = _expressionClass.getConstructor(new Class[]{ EOEntity.class });
62  			} catch (Exception ex) {
63  				throw new IllegalArgumentException("The expression class " + _expressionClass.getName() + " has no constructor with an entity parameter.");
64  			}
65  		}
66  		try {
67  			expr = (EOSQLExpression)_instantiator.newInstance(new Object[]{ entity });
68  		} catch (Exception ex) {
69  			throw new IllegalArgumentException("Cannot create new expression of class " + _expressionClass.getName());
70  		}
71  		return expr;
72  	}
73  
74  	public EOSQLExpression expressionForEntity(EOEntity entity) {
75  		return createExpression(entity);
76  	}
77  
78  	public EOSQLExpression deleteStatementWithQualifier(EOQualifier qualifier, EOEntity entity) {
79  		EOSQLExpression expr = createExpression(entity);
80  		expr.prepareDeleteExpressionForQualifier(qualifier);
81  		return expr;
82  	}
83  
84  	public EOSQLExpression insertStatementForRow(NSDictionary row, EOEntity entity) {
85  		EOSQLExpression expr = createExpression(entity);
86  		expr.prepareInsertExpressionWithRow(row);
87  		return expr;
88  	}
89  
90  	public EOSQLExpression selectStatementForAttributes(NSArray atts, boolean lock, EOFetchSpecification fspec, EOEntity entity) {
91  		EOSQLExpression expr = createExpression(entity);
92  		expr.prepareSelectExpressionWithAttributes(atts, lock, fspec);
93  		return expr;
94  	}
95  
96  	public EOSQLExpression updateStatementForRow(NSDictionary row, EOQualifier qualifier, EOEntity entity) {
97  		EOSQLExpression expr = createExpression(entity);
98  		expr.prepareUpdateExpressionWithRow(row, qualifier);
99  		return expr;
100 	}
101 
102 	public EOSQLExpression expressionForString(String sql) {
103 		EOSQLExpression expr = null;
104 		try {
105 			expr = (EOSQLExpression)_expressionClass.newInstance();
106 		} catch (Exception e) {
107 			return null;
108 		}
109 		expr.setStatement(sql);
110 		return expr;
111 	}
112 
113 	public Class expressionClass() {
114 		return _expressionClass;
115 	}
116 
117 }
118 /*
119  * $Log$
120  * Revision 1.2  2006/02/16 16:47:14  cgruber
121  * Move some classes in to "internal" packages and re-work imports, etc.
122  *
123  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
124  *
125  * Revision 1.1  2006/02/16 13:19:57  cgruber
126  * Check in all sources in eclipse-friendly maven-enabled packages.
127  *
128  * Revision 1.1  2003/08/13 01:04:32  chochos
129  * the SQL generation classes. EOSQLExpression still needs a lot of work, but the factory is pretty much done.
130  *
131  */