restoring head
[mir.git] / source / org / codecoop / mir / core / dao / hibernate / QueryCriteria.java
diff --git a/source/org/codecoop/mir/core/dao/hibernate/QueryCriteria.java b/source/org/codecoop/mir/core/dao/hibernate/QueryCriteria.java
new file mode 100755 (executable)
index 0000000..fa2a918
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * $Id: QueryCriteria.java,v 1.1 2004/11/06 16:20:48 idfx Exp $
+ * 
+ * Copyright (C) 2001, 2002, 2003, 2004 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+package org.codecoop.mir.core.dao.hibernate;
+
+import net.sf.hibernate.Criteria;
+import net.sf.hibernate.HibernateException;
+
+import org.codecoop.mir.core.dao.DatabaseFailure;
+import org.codecoop.mir.core.dao.IQueryCriteria;
+import org.codecoop.mir.core.dao.IQueryExpression;
+import org.codecoop.mir.core.dao.IQueryOrder;
+import org.codecoop.mir.core.dao.QueryFailure;
+
+/**
+ * QueryCriteria
+ * @author idefix
+ * @version $Revision: 1.1 $
+ */
+public class QueryCriteria implements IQueryCriteria {
+  private Criteria _criteria;
+  
+  /**
+   * @param persistentClass
+   * @throws QueryFailure
+   */
+  public QueryCriteria(Class persistentClass) throws QueryFailure {
+    try {
+      _criteria = HibernateSessionManager.getSession().createCriteria(persistentClass);
+    } catch (DatabaseFailure e) {
+      throw new QueryFailure(e);
+    }
+  }
+
+  /**
+   * @param criteria
+   */
+  private QueryCriteria(Criteria criteria) {
+    _criteria = criteria;
+  }
+
+  /**
+   * @see org.codecoop.mir.core.dao.IQueryCriteria#add(org.codecoop.mir.core.query.IQueryExpression)
+   */
+  public IQueryCriteria add(IQueryExpression expression) {
+    if(expression instanceof QueryExpression){
+      _criteria.add(((QueryExpression)expression).expression());
+    }
+    return this;
+  }
+
+  /**
+   * @see org.codecoop.mir.core.dao.IQueryCriteria#addOrder(org.codecoop.mir.core.query.IQueryOrder)
+   */
+  public IQueryCriteria addOrder(IQueryOrder order) {
+    if(order instanceof QueryOrder){
+      _criteria.addOrder(((QueryOrder)order).order());
+    }
+    return this;
+  }
+
+  /**
+   * @see org.codecoop.mir.core.dao.IQueryCriteria#setLimit(int)
+   */
+  public IQueryCriteria setLimit(int limit) {
+    _criteria.setMaxResults(limit);
+    return this;
+  }
+
+  /**
+   * @see org.codecoop.mir.core.dao.IQueryCriteria#setOffset(int)
+   */
+  public IQueryCriteria setOffset(int offset) {
+    _criteria.setFirstResult(offset);
+    return this;
+  }
+  
+  /* (non-Javadoc)
+   * @see org.codecoop.mir.core.query.IQueryCriteria#createCriteria(java.lang.String)
+   */
+  public IQueryCriteria createCriteria(String property) throws DatabaseFailure {
+    try {
+      Criteria c = _criteria.createCriteria(property);
+      QueryCriteria queryCriteria = new QueryCriteria(c);
+      return queryCriteria;
+    } catch (HibernateException e) {
+      throw new DatabaseFailure(e);
+    }
+  }
+
+  /**
+   * @return
+   */
+  public Criteria criteria() {
+    return _criteria;
+  }
+
+
+
+}