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  
20  package org.apache.myfaces.orchestra.viewController;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.myfaces.orchestra.annotation.AnnotationInfo;
24  import org.apache.myfaces.orchestra.annotation.AnnotationInfoManager;
25  import org.apache.myfaces.orchestra.conversation.ConversationManager;
26  import org.apache.myfaces.orchestra.conversation.annotations.ConversationRequire;
27  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
28  import org.apache.myfaces.orchestra.lib.OrchestraException;
29  
30  import java.io.IOException;
31  
32  /**
33   * A {@link org.apache.myfaces.orchestra.viewController.ViewControllerManager} implementation which uses
34   * annotations on backing beans to determine the beans responsible for a given view and execute
35   * the appropriate annotated methods.
36   *
37   * <p>When using Spring, every bean declaration in the spring config files is checked to see if the
38   * referenced class has annotations, and if so that information is cached. Here, that information is
39   * then used to locate a bean which has a ViewController annotation that references the current view.</p>
40   *
41   * <p>See also org.apache.myfaces.orchestra.viewController.annotations.*.</p>
42   *
43   * @see org.apache.myfaces.orchestra.viewController.ViewControllerManager
44   */
45  public abstract class AbstractAnnotationsViewControllerManager extends AbstractViewControllerManager
46  {
47      private AnnotationInfoManager annotationInfoManager;
48  
49      public AbstractAnnotationsViewControllerManager()
50      {
51      }
52  
53      public abstract void initManager();
54  
55      public void setAnnotationInfoManager(AnnotationInfoManager annotationInfoManager)
56      {
57          this.annotationInfoManager = annotationInfoManager;
58      }
59  
60      public AnnotationInfoManager getAnnotationInfoManager()
61      {
62          return annotationInfoManager;
63      }
64  
65      public void assertConversationState(String viewId)
66      {
67          String beanName = getViewControllerNameMapper().mapViewId(viewId);
68          if (beanName == null)
69          {
70              return;
71          }
72  
73          AnnotationInfo annotationInfo = annotationInfoManager.getAnnotationInfoByBeanName(beanName);
74          if (annotationInfo == null)
75          {
76              return;
77          }
78  
79          ConversationRequire conversationRequire = annotationInfo.getConversationRequire();
80          if (conversationRequire == null)
81          {
82              return;
83          }
84  
85          // check if we are on an ignored view
86          String[] entryPoints = conversationRequire.entryPointViewIds();
87          if (entryPoints != null)
88          {
89              for(String ignoredView: entryPoints)
90              {
91                  if (!StringUtils.isEmpty(ignoredView) && ignoredView.equals(viewId))
92                  {
93                      return;
94                  }
95              }
96          }
97  
98          // check that all the required conversations already exist
99          ConversationManager manager = ConversationManager.getInstance();
100         String[] conversationNames = conversationRequire.conversationNames();
101         if (conversationNames != null) 
102         {
103             for(String conversationName: conversationNames)
104             {
105                 if (!manager.hasConversation(conversationName))
106                 {
107                     reportInactive(manager, conversationName, conversationRequire);
108                     return;
109                 }
110             }
111         }
112     }
113     
114     private void reportInactive(ConversationManager manager, String conversationName,
115             ConversationRequire conversationRequire)
116     {
117         // oops..handle failure
118         if (manager.getMessager() != null)
119         {
120             manager.getMessager().setConversationNotActive(conversationName);
121         }
122 
123         if (!StringUtils.isEmpty(conversationRequire.redirect()))
124         {
125             try
126             {
127                 FrameworkAdapter.getCurrentInstance().redirect(conversationRequire.redirect());
128             }
129             catch (IOException e)
130             {
131                 throw new OrchestraException(e);
132             }
133         }
134         else if (!StringUtils.isEmpty(conversationRequire.navigationAction()))
135         {
136             try
137             {
138                 String dst = conversationRequire.navigationAction();
139                 FrameworkAdapter.getCurrentInstance().invokeNavigation(dst);
140             }
141             catch (IOException e)
142             {
143                 throw new OrchestraException(e);
144             }
145         }
146     }
147 }