Coverage Report - org.seasar.teeda.core.lifecycle.AbstractPhase
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPhase
66%
27/41
65%
13/20
1.889
 
 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.lifecycle;
 17  
 
 18  
 import java.util.Iterator;
 19  
 
 20  
 import javax.faces.component.EditableValueHolder;
 21  
 import javax.faces.component.UIComponent;
 22  
 import javax.faces.context.FacesContext;
 23  
 import javax.faces.event.PhaseEvent;
 24  
 import javax.faces.event.PhaseId;
 25  
 import javax.faces.event.PhaseListener;
 26  
 import javax.faces.internal.PhaseUtil;
 27  
 import javax.faces.lifecycle.Lifecycle;
 28  
 
 29  
 import org.seasar.teeda.core.util.LifecycleUtil;
 30  
 
 31  
 /**
 32  
  * @author shot
 33  
  */
 34  21
 public abstract class AbstractPhase implements Phase {
 35  
 
 36  
     public final void prePhase(FacesContext context) {
 37  1
         Lifecycle lifecycle = getLifecycle();
 38  1
         PhaseListener[] phaseListeners = lifecycle.getPhaseListeners();
 39  1
         if (phaseListeners != null) {
 40  2
             for (int i = 0; i < phaseListeners.length; i++) {
 41  1
                 PhaseListener phaseListener = phaseListeners[i];
 42  1
                 PhaseId phaseId = getCurrentPhaseId();
 43  1
                 if (isTargetListener(phaseListener, phaseId)) {
 44  1
                     PhaseEvent event = createPhaseEvent(context, phaseId,
 45  
                             lifecycle);
 46  1
                     phaseListener.beforePhase(event);
 47  
                 }
 48  
             }
 49  
         }
 50  1
     }
 51  
 
 52  
     //If need pre-prePhase or post-prePhase(and also postPhase) action, add method.
 53  
     public void execute(FacesContext context) {
 54  0
         PhaseUtil.setCurrentPhase(getCurrentPhaseId());
 55  0
         prePhase(context);
 56  
         try {
 57  0
             executePhase(context);
 58  
         } finally {
 59  0
             postPhase(context);
 60  0
         }
 61  0
     }
 62  
 
 63  
     public final void postPhase(FacesContext context) {
 64  1
         Lifecycle lifecycle = getLifecycle();
 65  1
         PhaseListener[] phaseListeners = lifecycle.getPhaseListeners();
 66  1
         if (phaseListeners != null) {
 67  3
             for (int i = phaseListeners.length - 1; i >= 0; i--) {
 68  2
                 PhaseListener phaseListener = phaseListeners[i];
 69  2
                 PhaseId phaseId = getCurrentPhaseId();
 70  2
                 if (isTargetListener(phaseListener, phaseId)) {
 71  2
                     PhaseEvent event = createPhaseEvent(context, phaseId,
 72  
                             lifecycle);
 73  2
                     phaseListener.afterPhase(event);
 74  
                 }
 75  
             }
 76  
         }
 77  1
     }
 78  
 
 79  
     protected PhaseEvent createPhaseEvent(FacesContext context,
 80  
             PhaseId phaseId, Lifecycle lifecycle) {
 81  3
         return new PhaseEvent(context, phaseId, lifecycle);
 82  
     }
 83  
 
 84  
     protected void initializeChildren(FacesContext context,
 85  
             UIComponent component) {
 86  4
         for (Iterator i = component.getFacetsAndChildren(); i.hasNext();) {
 87  0
             UIComponent child = (UIComponent) i.next();
 88  0
             if (child instanceof EditableValueHolder) {
 89  0
                 EditableValueHolder editableValueHolder = (EditableValueHolder) child;
 90  0
                 editableValueHolder.setValid(true);
 91  0
                 editableValueHolder.setSubmittedValue(null);
 92  0
                 editableValueHolder.setValue(null);
 93  0
                 editableValueHolder.setLocalValueSet(false);
 94  
             }//For each
 95  0
             initializeChildren(context, child);
 96  
         }
 97  4
     }
 98  
 
 99  
     protected boolean isTargetListener(PhaseListener listener, PhaseId phaseId) {
 100  11
         int listenerOrdinal = listener.getPhaseId().getOrdinal();
 101  11
         return listenerOrdinal == PhaseId.ANY_PHASE.getOrdinal()
 102  
                 || listenerOrdinal == phaseId.getOrdinal();
 103  
     }
 104  
 
 105  
     protected final Lifecycle getLifecycle() {
 106  3
         return LifecycleUtil.getLifecycle();
 107  
     }
 108  
 
 109  
     protected abstract void executePhase(FacesContext context);
 110  
 
 111  
     protected abstract PhaseId getCurrentPhaseId();
 112  
 }