Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/sbugs/dao/state/StateMachineDAO.java


1   /*
2    * This program is free software; you can redistribute it and/or
3    * modify it under the terms of the GNU General Public License
4    * as published by the Free Software Foundation;  version 2 only.
5     *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program; if not, write to the Free Software
13   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
14   */
15  package org.sbugs.dao.state;
16  
17  import java.sql.*;
18  import java.util.*;
19  
20  import org.sbugs.logic.state.*;
21  import org.sbugs.model.attributes.Attribute;
22  import org.sbugs.dao.DAO;
23  
24  public class StateMachineDAO extends DAO
25  {
26    protected static StateMachineDAO instance = new StateMachineDAO();
27    
28    protected StateMachineDAO() {}
29    
30    public static StateMachineDAO getInstance() { return instance; }
31    
32    public void loadStateMachine( DefectStateMachine stateMachine,
33                    Collection stateList,
34                    Connection connection )
35                    throws SQLException
36    {
37      stateMachine.setMaxStateId( getMaxId( stateList ) );
38      loadTransitions( stateMachine, connection );
39    }
40    
41    /**
42    Assumes all ids are > 0 and attributeList != null
43    */
44    protected int getMaxId( Collection attributeList )
45    {
46      int currMaxId = 0;
47      for( Iterator i = attributeList.iterator(); i.hasNext(); )
48      {
49        Attribute currAttr = (Attribute)i.next();
50        if( currAttr.getId() > currMaxId )
51        {
52          currMaxId = currAttr.getId();
53        }
54      }
55      return currMaxId;
56    }
57    
58    /**
59    Loads all transitions and transition handlers in the system.
60    */
61    protected void loadTransitions( DefectStateMachine stateMachine,
62                    Connection connection )
63                    throws SQLException
64    {
65      String sql = "select begin_state_id, end_state_id, transition_id, " +
66              "action, description " +
67              "from transition";
68      PreparedStatement statement = null;
69      try
70      {
71        statement = connection.prepareStatement( sql );
72        ResultSet results = statement.executeQuery();
73        while( results.next() )
74        {
75          int beginStateId = results.getInt( "begin_state_id" );
76          int endStateId = results.getInt( "end_state_id" );
77          Transition transition = new Transition( results.getInt( "transition_id" ),
78                              results.getString( "action" ),
79                              results.getString( "description" ) );
80          //loadHandlers( transition, connection ); temp for testing
81          stateMachine.addTransition( beginStateId, endStateId, transition );
82        }
83      }
84      finally
85      {
86        closeStatement( statement );
87      }
88    }
89    
90    protected void loadHandlers( Transition transition, Connection connection )
91            throws SQLException
92    {
93      String sql = "select handler_class " +
94              "from transition_handler " +
95              "where transition_id = ? " +
96              "order by priority";
97      PreparedStatement statement = null;
98      try
99      {
100       statement = connection.prepareStatement( sql );
101       statement.setInt( 1, transition.getId() );
102       ResultSet results = statement.executeQuery();
103       
104       while( results.next() )
105       {
106         String handlerClass = results.getString( "handler_class" );
107         transition.addHandler( initializeHandler( handlerClass ) );
108       }
109     }
110     finally
111     {
112       closeStatement( statement );
113     }
114   }
115   
116   protected TransitionHandler initializeHandler( String className )
117   {
118     try
119     {
120       Class targetClass = Class.forName( className );
121       Object instance = targetClass.newInstance();
122       
123       return (TransitionHandler)instance;
124     }
125     //Exception handling here is really messy, but I don't feel up to doing 
126     //it cleanly right now... this should really never happen other than
127     //during development/setup anyway...  I'll fix it later
128     //TODO: add clean exception handling
129     catch( Exception e )
130     {
131       e.printStackTrace();
132       throw new RuntimeException( e.getMessage() );
133     }
134   }
135 }