Coverage Report - org.seasar.teeda.core.application.NavigationHandlerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
NavigationHandlerImpl
97%
56/58
77%
20/26
2.9
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.application;
 17  
 
 18  
 import java.util.Iterator;
 19  
 import java.util.List;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.faces.application.NavigationHandler;
 23  
 import javax.faces.application.ViewHandler;
 24  
 import javax.faces.component.UIViewRoot;
 25  
 import javax.faces.context.ExternalContext;
 26  
 import javax.faces.context.FacesContext;
 27  
 
 28  
 import org.seasar.framework.util.AssertionUtil;
 29  
 import org.seasar.teeda.core.application.navigation.NavigationCaseContext;
 30  
 import org.seasar.teeda.core.application.navigation.NavigationContext;
 31  
 import org.seasar.teeda.core.application.navigation.NavigationResource;
 32  
 import org.seasar.teeda.core.util.IteratorUtil;
 33  
 import org.seasar.teeda.core.util.NavigationHandlerUtil;
 34  
 import org.seasar.teeda.core.util.PortletUtil;
 35  
 
 36  
 /**
 37  
  * @author shot
 38  
  * @author higa
 39  
  */
 40  11
 public class NavigationHandlerImpl extends NavigationHandler {
 41  
 
 42  
     public void handleNavigation(FacesContext context, String fromAction,
 43  
             String outcome) {
 44  7
         AssertionUtil.assertNotNull("context is null.", context);
 45  7
         if (outcome == null) {
 46  0
             return;
 47  
         }
 48  7
         ExternalContext externalContext = context.getExternalContext();
 49  7
         String viewId = context.getViewRoot().getViewId();
 50  7
         NavigationCaseContext navigationCaseContext = getNavigationCaseContext(
 51  
                 fromAction, outcome, viewId);
 52  7
         if (navigationCaseContext != null) {
 53  7
             ViewHandler viewHandler = context.getApplication().getViewHandler();
 54  7
             String newViewId = navigationCaseContext.getToViewId();
 55  7
             if (isRedirect(context, navigationCaseContext)) {
 56  1
                 String redirectPath = getRedirectActionPath(context,
 57  
                         viewHandler, newViewId);
 58  1
                 redirect(context, externalContext, redirectPath, newViewId);
 59  
             } else {
 60  6
                 render(context, viewHandler, newViewId);
 61  
             }
 62  
         }
 63  7
     }
 64  
 
 65  
     protected String getRedirectActionPath(FacesContext context,
 66  
             ViewHandler viewHandler, String newViewId) {
 67  1
         return viewHandler.getActionURL(context, newViewId);
 68  
     }
 69  
 
 70  
     protected void redirect(FacesContext context,
 71  
             ExternalContext externalContext, String redirectPath,
 72  
             String newViewId) {
 73  1
         NavigationHandlerUtil.redirect(context, redirectPath);
 74  1
     }
 75  
 
 76  
     protected void render(FacesContext context, ViewHandler viewHandler,
 77  
             String newViewId) {
 78  6
         UIViewRoot viewRoot = viewHandler.createView(context, newViewId);
 79  6
         viewRoot.setViewId(newViewId);
 80  6
         context.setViewRoot(viewRoot);
 81  6
         context.renderResponse();
 82  6
     }
 83  
 
 84  
     protected boolean isRedirect(FacesContext context,
 85  
             NavigationCaseContext caseContext) {
 86  
         // PortletSupport: JSR 127 7.4.2
 87  7
         if (PortletUtil.isPortlet(context)) {
 88  1
             return false;
 89  
         }
 90  6
         return caseContext.isRedirect();
 91  
     }
 92  
 
 93  
     protected NavigationCaseContext getNavigationCaseContext(String fromAction,
 94  
             String outcome, String viewId) {
 95  
         //exact match
 96  11
         List navigationList = getExactMatchNavigationCases(viewId);
 97  11
         NavigationCaseContext navigationCaseContext = getNavigationCaseContextInternal(
 98  
                 navigationList, fromAction, outcome);
 99  
         //wildcard match
 100  11
         if (navigationCaseContext == null) {
 101  3
             navigationList = getWildCardMatchNavigationCases(viewId);
 102  3
             navigationCaseContext = getNavigationCaseContextInternal(
 103  
                     navigationList, fromAction, outcome);
 104  
         }
 105  
         //and * match
 106  11
         if (navigationCaseContext == null) {
 107  2
             navigationList = getDefaultNavigationCases(viewId);
 108  2
             navigationCaseContext = getNavigationCaseContextInternal(
 109  
                     navigationList, fromAction, outcome);
 110  
         }
 111  11
         return navigationCaseContext;
 112  
     }
 113  
 
 114  
     private NavigationCaseContext getNavigationCaseContextInternal(
 115  
             List navigationList, String fromAction, String outcome) {
 116  16
         for (Iterator itr = IteratorUtil.getIterator(navigationList); itr
 117  16
                 .hasNext();) {
 118  11
             NavigationContext navContext = (NavigationContext) itr.next();
 119  11
             NavigationCaseContext caseContext = navContext.getNavigationCase(
 120  
                     fromAction, outcome);
 121  11
             if (caseContext != null) {
 122  11
                 return caseContext;
 123  
             }
 124  
         }
 125  5
         return null;
 126  
     }
 127  
 
 128  
     protected List getExactMatchNavigationCases(String viewId) {
 129  11
         Map map = NavigationResource.getNavigationContexts();
 130  11
         if (map != null) {
 131  8
             return (List) map.get(viewId);
 132  
         }
 133  3
         return null;
 134  
     }
 135  
 
 136  
     protected List getWildCardMatchNavigationCases(String viewId) {
 137  3
         Map map = NavigationResource.getWildCardMatchNavigationContexts();
 138  3
         if (map != null) {
 139  1
             for (Iterator itr = map.keySet().iterator(); itr.hasNext();) {
 140  1
                 String key = (String) itr.next();
 141  1
                 key = key.substring(0, key.lastIndexOf("*"));
 142  1
                 if (viewId.startsWith(key)) {
 143  1
                     return (List) map.get(key + "*");
 144  
                 }
 145  
             }
 146  
         }
 147  2
         return null;
 148  
     }
 149  
 
 150  
     protected List getDefaultNavigationCases(String viewId) {
 151  2
         Map map = NavigationResource.getDefaultMatchNavigationContexts();
 152  2
         if (map != null) {
 153  2
             return (List) map.get("*");
 154  
         }
 155  0
         return null;
 156  
     }
 157  
 }