View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.orchestra.flow.config;
20  
21  import java.io.Serializable;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.myfaces.orchestra.lib.OrchestraException;
26  
27  /**
28   * Contains static flow metadata about one particular page,
29   * including information about whether it can be called, and
30   * about which flows (if any) it can call.
31   */
32  public class FlowConfig implements Serializable
33  {
34      public static final String CONFIG_KEY = FlowConfig.class.getName() + ":config";
35  
36      // For serialization. IMPORTANT; update this when changing the
37      // binary format of this class (eg adding fields).
38      private static final long serialVersionUID = 1L;
39  
40      // Key is the outcome string
41      private Map<String, FlowCall> flowCalls = new HashMap<String, FlowCall>();
42      
43      private FlowAccept flowAccept;
44  
45      /**
46       * Constructor.
47       */
48      public FlowConfig()
49      {
50      }
51  
52      /**
53       * Check that all the properties of this object have valid values, ie
54       * whether the configuration specified by the user is valid.
55       * <p>
56       * Throws an OrchestraException if a mandatory parameter is missing.
57       */
58      public void validate()
59      {
60          if (flowAccept != null)
61          {
62              flowAccept.validate();
63          }
64          
65          for(FlowCall flowCall : flowCalls.values())
66          {
67              flowCall.validate();
68          }
69      }
70  
71      /**
72       * Return a FlowCall if this outcome for the current page should
73       * trigger a call to some logical flow service.
74       * <p>
75       * Returns null if no flow call should be triggered for this outcome.
76       */
77      public FlowCall getFlowCall(String outcome)
78      {
79          return (FlowCall) flowCalls.get(outcome);
80      }
81      
82      /**
83       * For use only during object initialization.
84       * <p>
85       * Note: the flowCall must be initialized (at least, have its outcome property set)
86       * before this method is called.
87       */
88      public void addFlowCall(FlowCall flowCall)
89      {
90          String outcome = flowCall.getOutcome();
91          if (flowCalls.containsKey(outcome))
92          {
93              throw new OrchestraException("Duplicate flowCall defined");
94          }
95          flowCalls.put(flowCall.getOutcome(), flowCall);
96      }
97  
98      /**
99       * If the associated page is an entry point for a flow, then return a
100      * FlowAccept object.
101      * <p>
102      * Otherwise, null is returned.
103      */
104     public FlowAccept getFlowAccept()
105     {
106         return flowAccept;
107     }
108 
109     /** For use only during object initialization. */
110     public void setFlowAccept(FlowAccept flowAccept)
111     {
112         if (this.flowAccept != null)
113         {
114             throw new OrchestraException("Duplicate flowAccept defined");
115         }
116         this.flowAccept = flowAccept;
117     }
118 
119     /**
120      * Custom string format to improve log messages.
121      */
122     @Override
123     public String toString()
124     {
125         StringBuffer buf = new StringBuffer();
126         buf.append("flowConfig:");
127         if (flowAccept != null)
128         {
129             buf.append("\n{");
130             buf.append(flowAccept.toString());
131             buf.append("}\n");
132         }
133         if (!flowCalls.isEmpty())
134         {
135             buf.append("\n{");
136             for(Object mapEntry : flowCalls.entrySet())
137             {
138                 buf.append(mapEntry.toString());
139                 buf.append("\n");
140             }
141             buf.append("}\n");
142         }
143         buf.append("\n");
144         return buf.toString();
145     }
146 }