View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.lifecycle;
17  
18  import static org.kuali.rice.krad.uif.UifConstants.ViewPhases.APPLY_MODEL;
19  import static org.kuali.rice.krad.uif.UifConstants.ViewPhases.FINALIZE;
20  import static org.kuali.rice.krad.uif.UifConstants.ViewPhases.INITIALIZE;
21  
22  import org.kuali.rice.krad.uif.component.Component;
23  import org.kuali.rice.krad.uif.util.LifecycleElement;
24  
25  /**
26   * Default phase builder implementation.
27   * 
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class ViewLifecyclePhaseBuilderBase implements ViewLifecyclePhaseBuilder {
31  
32      /**
33       * Return the previous view phase, for automatic phase spawning.
34       * 
35       * @param viewPhase view phase
36       * @return previous view phase
37       */
38      private static String getPreviousViewPhase(ViewLifecyclePhase phase) {
39          String viewPhase = phase.getViewPhase();
40          if (FINALIZE.equals(viewPhase) && !phase.getElement().isModelApplied()) {
41              return APPLY_MODEL;
42          }
43  
44          if (APPLY_MODEL.equals(viewPhase) && !phase.getElement().isInitialized()) {
45              return INITIALIZE;
46          }
47  
48          return null;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public ViewLifecyclePhase buildPhase(String viewPhase) {
56          ViewLifecyclePhase phase = LifecyclePhaseFactory.buildPhase(viewPhase);
57          phase.prepareView();
58          return finishBuildPhase(phase);
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public ViewLifecyclePhase buildPhase(String viewPhase, LifecycleElement element, Component parent, String parentPath) {
66          ViewLifecyclePhase phase = LifecyclePhaseFactory.buildPhase(viewPhase);
67          phase.prepareElement(element, parent, parentPath);
68          return finishBuildPhase(phase);
69      }
70      
71      private ViewLifecyclePhase finishBuildPhase(ViewLifecyclePhase phase) {
72          String previousViewPhase = getPreviousViewPhase(phase);
73          while (previousViewPhase != null) {
74              
75              ViewLifecyclePhase prevPhase = LifecyclePhaseFactory.buildPhase(previousViewPhase);
76              if (phase.getParent() == null) {
77                  prevPhase.prepareView();
78              } else {
79                  prevPhase.prepareElement(phase.getElement(), phase.getParent(), phase.getParentPath());
80              }
81              
82              prevPhase.setNextPhase(phase);
83              phase = prevPhase;
84              
85              previousViewPhase = getPreviousViewPhase(phase);
86          }
87  
88          return phase;
89      }
90  
91  }