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/13 4:41:39
6    */
7   package org.asyrinx.joey.tapestry.components.entity;
8   
9   import java.io.Serializable;
10  
11  import org.apache.commons.beanutils.PropertyUtils;
12  import org.apache.tapestry.ApplicationRuntimeException;
13  import org.apache.tapestry.IBinding;
14  import org.apache.tapestry.IRequestCycle;
15  
16  /***
17   * @author takeshi
18   */
19  public abstract class AbstractEntityInstanceActionLink extends AbstractEntityActionLink {
20  
21      public Object getEntity() {
22          final IBinding aEntityBinding = getEntityBinding();
23          if (aEntityBinding != null)
24              return aEntityBinding.getObject();
25          else
26              return null;
27      }
28  
29      private IBinding _entityBinding = null;
30  
31      public IBinding getEntityBinding() {
32          return _entityBinding;
33      }
34  
35      public void setEntityBinding(IBinding entityBinding) {
36          _entityBinding = entityBinding;
37      }
38  
39      public abstract Class getEntityClass();
40  
41      public abstract Serializable getEntityPrimaryKey();
42  
43      public abstract String getKeyPropertyName();
44  
45      /***
46       * @param cycle
47       * @return
48       */
49      public Object deserialize(IRequestCycle cycle) {
50          final Object[] parameters = cycle.getServiceParameters();
51          final String entityClassName = (String) parameters[0];
52          final Serializable pk = (Serializable) parameters[1];
53          final Class entityClass;
54          try {
55              entityClass = Class.forName(entityClassName);
56          } catch (ClassNotFoundException e) {
57              throw new ApplicationRuntimeException("Class not found. '" + entityClassName + "'.");
58          }
59          final Object entity = getDispatcher().load(entityClass, pk);
60          return entity;
61      }
62  
63      private Serializable getPrimaryKey(Object entity) {
64          try {
65              return (Serializable) PropertyUtils.getProperty(entity, getKeyPropertyName());
66          } catch (Exception e) {
67              throw new ApplicationRuntimeException("couldn't get property value '" + getKeyPropertyName() + "'.");
68          }
69      }
70  
71      public Object[] serialize() {
72          final IBinding aEntityBinding = getEntityBinding();
73          if (aEntityBinding != null) {
74              final Object aEntity = aEntityBinding.getObject();
75              if (aEntity != null) {
76                  return new Object[] { aEntity.getClass().getName(), getPrimaryKey(aEntity) };
77              } else {
78                  return null;
79              }
80          } else if (getEntityClass() != null) {
81              if (getEntityPrimaryKey() != null) {
82                  return new Object[] { getEntityClass().getName(), getEntityPrimaryKey() };
83              } else {
84                  return null;
85              }
86          } else if (getEntityClass() == null) {
87              throw new ApplicationRuntimeException("entityClass was null");
88          } else {
89              throw new ApplicationRuntimeException("entityPrimaryKey was null");
90          }
91      }
92  
93  }