View Javadoc

1   /*
2    * joey-rt and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/12/12 1:01:02
6    */
7   package org.asyrinx.joey.entity.service.impl;
8   
9   import java.io.Serializable;
10  import java.util.List;
11  
12  import net.sf.hibernate.exception.NestableRuntimeException;
13  
14  import org.apache.commons.beanutils.MethodUtils;
15  import org.asyrinx.brownie.core.query.exp.IExpression;
16  import org.asyrinx.joey.entity.service.EntityServiceDictionary;
17  import org.asyrinx.joey.entity.service.EntityServiceDispatcher;
18  import org.asyrinx.joey.entity.service.EntityServiceException;
19  
20  /***
21   * @author takeshi
22   */
23  public class EntityServiceDispatcherImpl implements EntityServiceDispatcher {
24  
25      /***
26       *  
27       */
28      public EntityServiceDispatcherImpl(EntityServiceDictionary serviceDictionary) {
29          super();
30          this.serviceDictionary = serviceDictionary;
31      }
32  
33      private final EntityServiceDictionary serviceDictionary;
34  
35      protected Object getService(Class entityClass) {
36          return serviceDictionary.getService(entityClass);
37      }
38  
39      public void delete(Object entity) {
40          final Object service = getService(entity.getClass());
41          try {
42              MethodUtils.invokeMethod(service, "delete", entity);
43          } catch (Exception e) {
44              throw new NestableRuntimeException(e);
45          }
46      }
47  
48      public void save(Object entity) {
49          final Object service = getService(entity.getClass());
50          try {
51              MethodUtils.invokeMethod(service, "save", entity);
52          } catch (Exception e) {
53              throw new NestableRuntimeException(e);
54          }
55      }
56  
57      public Object load(Class entityClass, Serializable key) {
58          final Object service = getService(entityClass);
59          try {
60              return MethodUtils.invokeMethod(service, "load", key);
61          } catch (Exception e) {
62              throw new NestableRuntimeException(e);
63          }
64      }
65  
66      public List select(Class entityClass, IExpression condition) {
67          final Object service = getService(entityClass);
68          try {
69              final Object result = MethodUtils.invokeMethod(service, "select", condition);
70              if (!(result instanceof List))
71                  throw new EntityServiceException("");
72              return (List) result;
73          } catch (Exception e) {
74              throw new NestableRuntimeException(e);
75          }
76      }
77  }