View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core;
20  
21  //import org.caleigo.core.*;
22  import org.caleigo.core.exception.*;
23  import org.caleigo.toolkit.log.*;
24  
25  
26  /*** <Description for AbstractDataAction>
27   *
28   * @author  Dennis Zikovic
29   * @version 1.00
30   *
31   *//*
32   *
33   * WHEN        WHO               WHY & WHAT
34   * -----------------------------------------------------------------------------
35   * 2002-09-12  Dennis Zikovic    Creation
36   */
37  public abstract class AbstractTransactionEntityAction extends AbstractEntityAction implements ITransactionEntityAction
38  {
39      // Constants ---------------------------------------------------------------
40      
41      // Data members ------------------------------------------------------------
42      
43      // Constructors ------------------------------------------------------------
44      
45      /*** Creates a new instance of AbstractDataAction.
46       */
47      public AbstractTransactionEntityAction(IEntityDescriptor entityDescriptor, String codeName, String displayName)
48      {
49          super(entityDescriptor, codeName, displayName);
50      }
51      
52      /*** Creates a new instance of AbstractDataAction.
53       */
54      public AbstractTransactionEntityAction(IEntityDescriptor entityDescriptor, String codeName, String displayName, String displayHint)
55      {
56          super(entityDescriptor, codeName, displayName, displayHint);
57      }
58  
59      // Abstract methods --------------------------------------------------------
60  
61      /*** Prepare method for the action where the action is expected to add 
62       * the data operations needed to perform the action.
63       */
64      public abstract void prepareTransaction(IDataBundle data, IDataTransaction transaction);
65      
66  
67      /*** This method is called by the action objects constructor once and only 
68       * once. The method is expected to define the action data requriments
69       * (IDataBundleDescriptor) using the diferent add methods for descriptors
70       * defined in this class.
71       */
72      public abstract void defineDataDescriptor();
73      
74      // Superclass overrides ----------------------------------------------------
75      
76      /*** Performs the actions logic using the provided IActionData object.
77       * This method is defined by the IAction interface. 
78       */
79      public final void perform(IDataBundle data)
80      {
81          try
82          {
83              IDataSource dataSource = this.getDataSource();
84              if(dataSource==null)
85                  throw new DataServiceNotFoundException("No default data source has been set for: "+this.getEntityDescriptor().getDataSourceDescriptor().getCodeName());
86  
87              IDataTransaction transaction = dataSource.getDataService().newTransaction();
88              this.prepareTransaction(data, transaction);
89              transaction.commit();
90              this.cleanUpTransaction(data, transaction);
91          }
92          catch(DataServiceException e)
93          {
94              Log.printError(this, "Transaction failed in Action "+this.getEntityDescriptor()+"."+this.getCodeName(), e);
95              // Some error feedback should be handled here.
96          }
97      }
98      
99      // Help methods ------------------------------------------------------------
100     
101     /*** Help method used to access the data source used by the perform method
102      * deined in the IEntityAction interface. Does by default return the default
103      * data source object registered in the the data source descripor that the
104      * action object belongs to.
105      */
106     public IDataSource getDataSource()
107     {
108         return this.getEntityDescriptor().getDataSourceDescriptor().getDefaultDataSource();
109     }
110     
111     /*** Clean up method for the action where the action can clean up data
112      * operations, if necessary via an overriding method in an implementing
113      * class,  which were performed by the action. E.g. resetting flags etc.
114      */
115     public void cleanUpTransaction(IDataBundle data, IDataTransaction transaction)
116     {
117     }
118 }