View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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  
17  package org.gwe.persistence.dao;
18  
19  import java.util.List;
20  
21  import org.gwe.persistence.model.OrderInfo;
22  import org.hibernate.Criteria;
23  import org.hibernate.HibernateException;
24  import org.hibernate.Query;
25  import org.hibernate.Session;
26  import org.hibernate.criterion.Restrictions;
27  import org.springframework.orm.hibernate3.HibernateCallback;
28  
29  /**
30   * @author Marco Ruiz
31   * @since Aug 13, 2007
32   */
33  public class OrderInfoDAO extends BaseInfoDAO<OrderInfo, Integer> {
34  	
35  	public List<OrderInfo> getByDescription(final String description) {
36  		return (List<OrderInfo>) getHibernateTemplate().execute(new HibernateCallback() {
37  			public Object doInHibernate(Session session) throws HibernateException {
38  				Criteria criteria = session.createCriteria(OrderInfo.class);
39  				if (description != null) criteria = criteria.add(Restrictions.eq("description", description));
40  				return criteria.list();
41  			}
42  		});
43  	}
44  
45  	public OrderInfo getOrder(int orderId, boolean includeJobs) {
46  		OrderInfo result = get(orderId);
47  		if (includeJobs && result != null) result.getJobs().size();
48  		return result;
49  	}
50  	
51  	public Integer save(OrderInfo order) {
52  		Integer result = super.save(order);
53  		order.setPriority(order.getId());
54  		return result;
55  	}
56  
57  	public List<OrderInfo> getList() {
58  		return (List<OrderInfo>) getHibernateTemplate().execute(new HibernateCallback() {
59  			public Object doInHibernate(Session session) throws HibernateException {
60  				return session.createCriteria(OrderInfo.class)
61  					.add(Restrictions.eq("deleted", false))
62  //					.addOrder(Order.asc("priority"))
63  					.list();
64  			}
65  		});
66  	}
67  
68  	public void swapPriorities(final int orderId1, final int offset) {
69  		final int priority = get(orderId1).getPriority();
70  		if (priority < 1) return;
71  /*
72  		getHibernateTemplate().execute(new HibernateCallback() {
73  			public Object doInHibernate(Session session) throws HibernateException {
74  	            Query query = session.createQuery("from OrderInfo where priority = :priority");
75  		        query.setInteger("priority", priority);
76  	            List<OrderInfo> orders = query.list();
77  	            int otherId = -1;
78  				if (!orders.isEmpty()) otherId = orders.get(0).getId();
79  
80  	            
81  				setPriority(session, orderId2, priority);
82  				setPriority(session, orderId1, priority + offset);
83  		        return null;
84  			}
85  
86  			private void setPriority(Session session, final int orderId1, final int priority) {
87  	            Query query = session.createQuery("update OrderInfo set priority = :priority where id = :id");
88  		        query.setInteger("id", orderId1);
89  		        query.setInteger("priority", priority);
90  		        query.executeUpdate();
91              }
92  		});
93  */
94      }
95  	
96  	public void markAsDeleted(final int orderId) {
97  		getHibernateTemplate().execute(new HibernateCallback() {
98  			public Object doInHibernate(Session session) throws HibernateException {
99  		        Query query = session.createQuery("update OrderInfo set deleted = true, paused = true where id = :id");
100 		        query.setInteger("id", orderId);
101 		        return query.executeUpdate();
102 			}
103 		});
104     }
105 
106 	public void setPause(final int orderId, final boolean pause) {
107 		getHibernateTemplate().execute(new HibernateCallback() {
108 			public Object doInHibernate(Session session) throws HibernateException {
109 		        Query query = session.createQuery("update OrderInfo set paused = :pause where id = :id");
110 		        query.setInteger("id", orderId);
111 		        query.setBoolean("pause", pause);
112 		        return query.executeUpdate();
113 			}
114 		});
115     }
116 }
117