KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > patterns > flow > DefaultFlow


1 package org.mr.core.util.patterns.flow;
2
3 import org.w3c.dom.Element JavaDoc;
4 import org.mr.core.util.exceptions.CreationException;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 /*
12  * Copyright 2002 by
13  * <a HREF="http://www.coridan.com">Coridan</a>
14  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
15  *
16  * The contents of this file are subject to the Mozilla Public License Version
17  * 1.1 (the "License"); you may not use this file except in compliance with the
18  * License. You may obtain a copy of the License at
19  * http://www.mozilla.org/MPL/
20  *
21  * Software distributed under the License is distributed on an "AS IS" basis,
22  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
23  * for the specific language governing rights and limitations under the
24  * License.
25  *
26  * The Original Code is "MantaRay" (TM).
27  *
28  * The Initial Developer of the Original Code is Coridan.
29  * Portions created by the Initial Developer are Copyright (C) 2006
30  * Coridan Inc. All Rights Reserved.
31  *
32  * Contributor(s): all the names of the contributors are added in the source
33  * code where applicable.
34  *
35  * Alternatively, the contents of this file may be used under the terms of the
36  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
37  * provisions of LGPL are applicable instead of those above. If you wish to
38  * allow use of your version of this file only under the terms of the LGPL
39  * License and not to allow others to use your version of this file under
40  * the MPL, indicate your decision by deleting the provisions above and
41  * replace them with the notice and other provisions required by the LGPL.
42  * If you do not delete the provisions above, a recipient may use your version
43  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
44  
45  *
46  * This library is free software; you can redistribute it and/or modify it
47  * under the terms of the MPL as stated above or under the terms of the GNU
48  * Lesser General Public License as published by the Free Software Foundation;
49  * either version 2.1 of the License, or any later version.
50  *
51  * This library is distributed in the hope that it will be useful, but WITHOUT
52  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
54  * License for more details.
55  */

56
57  /**
58  * User: Moti Tal
59  * Date: Mar 13, 2005
60  * Time: 12:09:34 PM
61  *
62  * A default flow,
63  * In this implementation entry point to the flow represent as a NULL Declaration at the “from” attribute.
64  */

65 public class DefaultFlow implements Flow{
66
67 // private static final String ENTRY_POINT_KEY = "ENTRY_POINT_KEY";
68

69     //holds the possible entry steps.
70
protected List JavaDoc m_entryDeclarations = null;
71
72     //holds the steps
73
HashMap JavaDoc m_steps = null;
74
75     public DefaultFlow() {
76         m_steps = new HashMap JavaDoc();
77         m_entryDeclarations = new ArrayList JavaDoc();
78     }
79
80     public void addStep(String JavaDoc i_id, Declaration i_from, Declaration i_to, Condition i_conditions){
81         //set an entry point
82
if(i_from == null){
83             i_from = new EntryPointDeclaration();
84             m_entryDeclarations.add(i_from);
85         }
86
87         List JavaDoc steps = (List JavaDoc)m_steps.get(i_from);
88         if(steps == null){
89             steps = new ArrayList JavaDoc();
90         }
91         // add step to the flow
92
Step step = new Step(i_id, i_from,i_to);
93 // if(i_conditions != null && i_conditions.length > 0)
94
step.addConditions(i_conditions);
95         steps.add(step);
96         m_steps.put(i_from, steps);
97     }
98
99     public boolean hasMoreSteps(Declaration i_current, Map JavaDoc i_parameters){
100        List JavaDoc steps = (List JavaDoc)m_steps.get(i_current);
101         if(steps != null && !steps.isEmpty())
102             return true;
103         return false;
104     }
105
106     public Declaration getNextStep(Declaration i_current, Map JavaDoc i_parameters){
107         //if its the first entry
108
if(i_current == null){
109             for(int i = 0 ; i < m_entryDeclarations.size() ; i++){
110                 Declaration declaration = search((Declaration)m_entryDeclarations.get(i),i_parameters);
111                 if(declaration != null)
112                     return declaration;
113             }
114             return null;
115         }else{
116             //otherwise look for other step/s apply to the current declaration
117
return search(i_current, i_parameters);
118         }
119
120     }
121
122     /**
123      * Search for possible steps
124      * @param i_declaration the current declaration
125      * @param i_parameters the current parameters passed from the previous step
126      * @return true, if has more steps applies to that Declaration; false otherwise
127      */

128     private Declaration search(Declaration i_declaration, Map JavaDoc i_parameters){
129         List JavaDoc steps = (List JavaDoc)m_steps.get(i_declaration);
130         if(steps == null)
131             return null;
132         for(int i = 0; i < steps.size() ; i++){
133             Step step = (Step)steps.get(i);
134             if(step.isMetStepCondition(i_parameters))
135                 return step.getNextStep();
136         }
137         return null;
138     }
139
140     /**
141      * This class represent step in the flow
142      */

143     private class Step{
144         private Declaration m_to;
145         private Condition m_conditions = null;
146
147         public Step(String JavaDoc i_id, Declaration i_from, Declaration i_to) {
148            m_to = i_to;
149             m_conditions = null;
150         }
151
152         public void addConditions(Condition i_conditions) {
153             m_conditions = i_conditions;
154         }
155
156         public Declaration getNextStep() {
157             return m_to;
158         }
159
160         public boolean isMetStepCondition(Map JavaDoc i_params){
161             /*for(int i = 0; i < m_conditions.size() ; i++){
162                 FlowCondition condition = (FlowCondition)m_conditions.get(i);
163                 if(!condition.metCondition(i_params))
164                     return false;
165             }
166             return true;*/

167             return m_conditions.metCondition(i_params);
168         }
169     }
170
171     /**
172      * This class represent an entry point to the flow
173      */

174     private class EntryPointDeclaration implements Declaration{
175
176         public Map JavaDoc execute(Map JavaDoc i_params) {
177             return i_params;
178         }
179
180         public void configure(Element JavaDoc i_element) throws CreationException {
181         }
182     }
183 }
184
Popular Tags