7ff517096e2659c5bf7d6a6855c3d670e311b06c
[utils] / mythtv / monitor / src / main / java / org / wamblee / mythtv / RecordingDatabase.java
1 /*
2  * Copyright 2006 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.wamblee.mythtv;
18
19 import java.sql.SQLException;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.hibernate.HibernateException;
27 import org.hibernate.Session;
28 import org.hibernate.criterion.Expression;
29 import org.springframework.orm.hibernate3.HibernateCallback;
30 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
31
32 /**
33  * 
34  */
35 public class RecordingDatabase extends HibernateDaoSupport {
36     
37     private static final Log LOG = LogFactory.getLog(RecordingDatabase.class);
38
39     public RecordingDatabase() { 
40         // Empty
41     }
42     
43     public void init() {  
44         /* 
45         for (Recording recording: (List<Recording>)getHibernateTemplate().loadAll(Recording.class) ) { 
46             LOG.info("Found recording " + recording);
47         }
48         LOG.info("After listing recordings");
49         */
50     }
51     
52     public Recording findRecording(final String aName) {
53         List<Recording> result = (List<Recording>) getHibernateTemplate().execute(new HibernateCallback() {
54            /* (non-Javadoc)
55              * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
56              */
57             public Object doInHibernate(Session aSession) throws HibernateException, SQLException {
58                 return aSession.createCriteria(Recording.class).add(Expression.eq("_basename", aName)).list();
59             } 
60         });
61         if ( result.size() > 1 ) { 
62             throw new RuntimeException("More than two recordings returned");
63         }
64         if ( result.size() == 0 ) { 
65             return null; 
66         }
67         return result.get(0);
68     }
69     
70     public void update(Recording aRecording) { 
71         getHibernateTemplate().update(aRecording);
72     }
73 }