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.flow.util;
20  
21  import java.util.List;
22  import javax.faces.context.FacesContext;
23  import javax.faces.flow.Flow;
24  import org.apache.myfaces.flow.FlowHandlerImpl;
25  import org.apache.myfaces.flow.FlowReference;
26  
27  /**
28   *
29   */
30  public class FlowUtils
31  {
32      public static String getFlowMapKey(FacesContext facesContext, FlowReference flowReference)
33      {
34          Flow flow = null;
35          if (flowReference.getDocumentId() == null)
36          {
37              flow = facesContext.getApplication().getFlowHandler().getFlow(
38                  facesContext, "", flowReference.getId());
39          }
40          else
41          {
42              flow = facesContext.getApplication().getFlowHandler().getFlow(
43                  facesContext, flowReference.getDocumentId(), flowReference.getId());
44          }
45          if (flow != null)
46          {
47              return FlowUtils.getFlowMapKey(facesContext, flow);
48          }
49          return null;
50      }    
51      
52      public static String getFlowMapKey(FacesContext facesContext, Flow flow)
53      {
54          String flowMapKey = flow.getClientWindowFlowId(
55              facesContext.getExternalContext().getClientWindow());
56          int flowIndex = getFlowIndex(facesContext, flow);
57          if (flowIndex > 0)
58          {
59              flowMapKey = flowMapKey + "_" + flowIndex;
60          }
61          return flowMapKey;
62      }
63      
64      private static int getFlowIndex(FacesContext facesContext, Flow flow)
65      {
66          List<Flow> list = FlowHandlerImpl.getActiveFlows(facesContext, 
67                  facesContext.getApplication().getFlowHandler());
68          FlowReference flowRef = new FlowReference(flow.getDefiningDocumentId(), flow.getId());
69          int flowIndex = 0;
70          for (Flow f : list)
71          {
72              FlowReference fr = new FlowReference(f.getDefiningDocumentId(), f.getId());
73              if (flowRef.equals(fr))
74              {
75                  flowIndex++;
76              }
77          }
78          return flowIndex;
79      }
80  
81  }