1 /** 2 * Copyright 2005-2013 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 java.util.HashSet; 19 import java.util.Set; 20 21 import org.kuali.rice.krad.uif.component.Component; 22 import org.kuali.rice.krad.uif.util.RecycleUtils; 23 24 /** 25 * Responsible for creating lifecycle phases. 26 * 27 * <p> 28 * This factory recycles completed phases to reduce object creation during the lifecycle. 29 * </p> 30 * 31 * @author Kuali Rice Team (rice.collab@kuali.org) 32 */ 33 public final class LifecyclePhaseFactory { 34 35 /** 36 * Creates a new lifecycle phase processing task for performing initialization on a component. 37 * 38 * @param component The component. 39 * @param model The model 40 * @return lifecycle processing task for processing the initialize phase on the component 41 */ 42 public static InitializeComponentPhase initialize(Component component, Object model) { 43 return initialize(component, model, 0, null, null); 44 } 45 46 /** 47 * Creates a new lifecycle phase processing task for performing initialization on a component. 48 * 49 * @param component The component. 50 * @param model The model 51 * @param parent The parent component. 52 * @param index The index of the phase within the nested component list. 53 * @param nextPhase The applyModel phase to spawn after the successful completion of the 54 * initialize phase. 55 * @return lifecycle processing task for processing the initialize phase on the component 56 */ 57 public static InitializeComponentPhase initialize(Component component, Object model, 58 int index, Component parent, ApplyModelComponentPhase nextPhase) { 59 InitializeComponentPhase initializePhase = RecycleUtils.getInstance(InitializeComponentPhase.class); 60 initializePhase.prepare(component, model, index, parent, nextPhase); 61 return initializePhase; 62 } 63 64 /** 65 * Creates a new lifecycle phase processing task for applying the model to a component. 66 * 67 * @param component The component. 68 * @param model The model 69 * @return lifecycle processing task for processing the apply model phase on the component 70 */ 71 public static ApplyModelComponentPhase applyModel(Component component, Object model) { 72 return applyModel(component, model, 0, null, null, new HashSet<String>()); 73 } 74 75 /** 76 * Creates a new lifecycle phase processing task for applying the model to a component. 77 * 78 * @param component The component. 79 * @param model The model 80 * @param parent The component. 81 * @return lifecycle processing task for processing the apply model phase on the component 82 */ 83 public static ApplyModelComponentPhase applyModel(Component component, Object model, Component parent) { 84 return applyModel(component, model, 0, parent, null, new HashSet<String>()); 85 } 86 87 /** 88 * Creates a new lifecycle phase processing task for applying the model to a component. 89 * 90 * @param component The component. 91 * @param model The model 92 * @param parent The parent component. 93 * @param index The index of the phase within the nested component list. 94 * @param nextPhase The applyModel phase to spawn after the successful completion of the 95 * initialize phase. 96 * @param visitedIds The set of visited IDs to track while applying model. 97 * @return lifecycle processing task for processing the apply model phase on the component 98 */ 99 public static ApplyModelComponentPhase applyModel(Component component, Object model, 100 int index, Component parent, FinalizeComponentPhase nextPhase, Set<String> visitedIds) { 101 ApplyModelComponentPhase applyModelPhase = RecycleUtils.getInstance(ApplyModelComponentPhase.class); 102 applyModelPhase.prepare(component, model, index, parent, nextPhase, visitedIds); 103 return applyModelPhase; 104 } 105 106 /** 107 * Creates a new lifecycle phase processing task for finalizing a nested component. 108 * 109 * @param component The component. 110 * @param model The model 111 * @return lifecycle processing task for processing the finalize phase on the component 112 */ 113 public static FinalizeComponentPhase finalize(Component component, Object model) { 114 return finalize(component, model, 0, null); 115 } 116 117 /** 118 * Creates a new lifecycle phase processing task for finalizing a nested component. 119 * 120 * @param component The component. 121 * @param model The model 122 * @param parent The parent component. 123 * @return lifecycle processing task for processing the finalize phase on the component 124 */ 125 public static FinalizeComponentPhase finalize(Component component, Object model, Component parent) { 126 return finalize(component, model, 0, parent); 127 } 128 129 /** 130 * Creates a new lifecycle phase processing task for finalizing a nested component. 131 * 132 * @param component The component. 133 * @param model The model 134 * @param parent The parent component. 135 * @param index The index of the phase within the nested component list. 136 * @return lifecycle processing task for processing the finalize phase on the component 137 */ 138 public static FinalizeComponentPhase finalize(Component component, Object model, 139 int index, Component parent) { 140 FinalizeComponentPhase finalizePhase = RecycleUtils.getInstance(FinalizeComponentPhase.class); 141 finalizePhase.prepare(component, model, index, parent); 142 return finalizePhase; 143 } 144 145 /** 146 * Creates a new lifecycle phase processing task for rendering a component. 147 * 148 * @param component The component to render. 149 * @param model The model associated with the component. 150 * @param index The position of the associated finalize phase's within its predecessor's 151 * successor queue. 152 * @return lifecycle processing task for processing the render phase on the component 153 */ 154 public static RenderComponentPhase render(Component component, Object model, int index) { 155 RenderComponentPhase renderPhase = RecycleUtils.getInstance(RenderComponentPhase.class); 156 renderPhase.prepare(component, model, index, null, 0); 157 return renderPhase; 158 } 159 160 /** 161 * Creates a new lifecycle phase processing task for rendering a component. 162 * 163 * @param finalizePhase The finalize component phase associated with this rendering phase. 164 * @param parent The rendering phase for the parent of the component associated with this phase. 165 * @param pendingChildren The number of child phases to expect to be queued with this phase as 166 * their rendering parent. 167 * @return lifecycle processing task for processing the render phase on the component 168 */ 169 public static RenderComponentPhase render( 170 FinalizeComponentPhase finalizePhase, RenderComponentPhase parent, int pendingChildren) { 171 RenderComponentPhase renderPhase = RecycleUtils.getInstance(RenderComponentPhase.class); 172 renderPhase.prepare(finalizePhase.getComponent(), finalizePhase.getModel(), 173 finalizePhase.getIndex(), parent, pendingChildren); 174 return renderPhase; 175 } 176 177 /** 178 * Recycles a task instance after processing. 179 * 180 * @param phase The task to recycle. 181 */ 182 static void recycle(ViewLifecyclePhaseBase phase) { 183 phase.recycle(); 184 RecycleUtils.recycle(phase); 185 } 186 187 }