OSDN Git Service

4eabc3ab3a9b7a895c67bb3bfdba740ec7c2fcce
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / dao / jdo / GenericDaoJdo.java
1 /*
2    Copyright 2009 senju@users.sourceforge.jp
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 jp.sourceforge.rabbitBTS.dao.jdo;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.LinkedHashSet;
23 import java.util.List;
24
25 import jp.sourceforge.rabbitBTS.dao.GenericDao;
26
27 import org.springframework.orm.ObjectRetrievalFailureException;
28 import org.springframework.orm.jdo.support.JdoDaoSupport;
29
30 import com.google.appengine.api.datastore.Key;
31
32 public class GenericDaoJdo<T, PK extends Serializable> extends JdoDaoSupport
33                 implements GenericDao<T, PK> {
34         public final Class<T> persistentClass;
35
36         /**
37          * Constructor that takes in a class for easy creation of DAO
38          */
39         public GenericDaoJdo(Class<T> persistentClass) {
40                 this.persistentClass = persistentClass;
41         }
42
43         public boolean exists(PK id) {
44                 try {
45                         getJdoTemplate().getObjectById(this.persistentClass, id);
46                         return true;
47                 } catch (final ObjectRetrievalFailureException e) {
48                         return false;
49                 }
50         }
51
52         @SuppressWarnings("unchecked")
53         public T get(PK id) {
54                 final T entity = (T) getJdoTemplate().getObjectById(
55                                 this.persistentClass, id);
56                 return entity;
57         }
58
59         @SuppressWarnings("unchecked")
60         public T getByKey(Key id) {
61                 final T entity = (T) getJdoTemplate().getObjectById(
62                                 this.persistentClass, id);
63                 return entity;
64         }
65
66         public List<T> getAll() {
67                 return new ArrayList<T>(getJdoTemplate().find(this.persistentClass));
68         }
69
70         public List<T> getAllDistinct() {
71                 final Collection result = new LinkedHashSet(getAll());
72                 return new ArrayList(result);
73         }
74
75         public void remove(PK id) {
76                 getJdoTemplate().deletePersistent(this.get(id));
77         }
78
79         public T save(T object) {
80                 return (T) getJdoTemplate().makePersistent(object);
81         }
82
83 }