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 java.util.List;
19  import java.util.Map;
20  
21  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22  import org.kuali.rice.krad.uif.UifConstants;
23  import org.kuali.rice.krad.uif.container.PageGroup;
24  import org.kuali.rice.krad.uif.service.ViewHelperService;
25  import org.kuali.rice.krad.uif.util.ProcessLogger;
26  import org.kuali.rice.krad.uif.view.View;
27  import org.kuali.rice.krad.uif.view.ViewModel;
28  import org.kuali.rice.krad.web.form.UifFormBase;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Perform the lifecycle process for the view or a component.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class ViewLifecycleBuild implements Runnable {
38      private static final Logger LOG = LoggerFactory.getLogger(ViewLifecycleBuild.class);
39  
40      private final Map<String, String> parameters;
41      private final Map<String, List<String>> refreshPathMappings;
42  
43      /**
44       * Constructor.
45       *
46       * @param parameters Map of key values pairs that provide configuration for the view, this is generally comes from
47       * the request and can be the request parameter Map itself. Any parameters not valid for the View will be
48       * filtered out
49       * @param refreshPathMappings
50       */
51      public ViewLifecycleBuild(Map<String, String> parameters, Map<String, List<String>> refreshPathMappings) {
52          this.parameters = parameters;
53          this.refreshPathMappings = refreshPathMappings;
54      }
55  
56      /**
57       * Runs the three lifecycle phases and performs post finalize processing.
58       */
59      @Override
60      public void run() {
61          View view = ViewLifecycle.getView();
62  
63          ProcessLogger.trace("begin-view-lifecycle:" + view.getId());
64  
65          populateViewRequestParameters();
66  
67          runInitializePhase();
68  
69          runApplyModelPhase();
70  
71          runFinalizePhase();
72  
73          // remove view so default values are only applied once
74          ((ViewModel) ViewLifecycle.getModel()).setApplyDefaultValues(false);
75  
76          // build script for generating growl messages
77          String growlScript = ViewLifecycle.getHelper().buildGrowlScript();
78          ((ViewModel) ViewLifecycle.getModel()).setGrowlScript(growlScript);
79  
80          // on component refreshes regenerate server message content for page
81          if (ViewLifecycle.isRefreshLifecycle()) {
82              PageGroup page = view.getCurrentPage();
83              page.getValidationMessages().generateMessages(view, ViewLifecycle.getModel(), page);
84          }
85  
86          LifecycleRefreshPathBuilder.processLifecycleElements();
87  
88          ViewLifecycle.getViewPostMetadata().cleanAfterLifecycle();
89  
90          ProcessLogger.trace("finalize:" + view.getId());
91      }
92  
93      /**
94       * Invokes the view helper to populate view attributes from request parameters, then makes a back up of the
95       * view request parameters on the form.
96       */
97      protected void populateViewRequestParameters() {
98          View view = ViewLifecycle.getView();
99          ViewHelperService helper = ViewLifecycle.getHelper();
100         UifFormBase model = (UifFormBase) ViewLifecycle.getModel();
101 
102         // populate view from request parameters. In case of refresh, the parameters will be stored on the
103         // form from the initial build
104         Map<String, String> parametersToPopulate = parameters;
105         if (ViewLifecycle.isRefreshLifecycle()) {
106             parametersToPopulate = model.getViewRequestParameters();
107         }
108 
109         helper.populateViewFromRequestParameters(parametersToPopulate);
110 
111         // backup view request parameters on form for refreshes
112         model.setViewRequestParameters(view.getViewRequestParameters());
113     }
114 
115     /**
116      * Runs the initialize lifecycle phase.
117      *
118      * <p>First the view helper is invoked to perform any custom processing, then the processor is invoked
119      * to perform any tasks for this phase.</p>
120      */
121     protected void runInitializePhase() {
122         ViewLifecycleProcessor processor = ViewLifecycle.getProcessor();
123 
124         List<String> refreshPaths = null;
125         if (refreshPathMappings != null) {
126             refreshPaths = refreshPathMappings.get(UifConstants.ViewPhases.INITIALIZE);
127         }
128 
129         ViewLifecyclePhase phase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase(
130                 ViewLifecycle.getView(), UifConstants.ViewPhases.INITIALIZE, refreshPaths);
131 
132         View view = ViewLifecycle.getView();
133         ViewHelperService helper = ViewLifecycle.getHelper();
134         UifFormBase model = (UifFormBase) ViewLifecycle.getModel();
135 
136         ViewLifecycle.getExpressionEvaluator().initializeEvaluationContext(model);
137 
138         if (LOG.isInfoEnabled()) {
139             LOG.info("performing initialize phase for view: " + view.getId());
140         }
141 
142         helper.performCustomViewInitialization(model);
143 
144         processor.performPhase(phase);
145 
146         ProcessLogger.trace("initialize:" + view.getId());
147     }
148 
149     /**
150      * Runs the apply model lifecycle phase.
151      *
152      * <p>Default values are applied and context is setup for expression evaluation. Then the processor is invoked
153      * to perform any tasks for this phase. </p>
154      */
155     protected void runApplyModelPhase() {
156         ViewLifecycleProcessor processor = ViewLifecycle.getProcessor();
157 
158         List<String> refreshPaths = null;
159         if (refreshPathMappings != null) {
160             refreshPaths = refreshPathMappings.get(UifConstants.ViewPhases.APPLY_MODEL);
161         }
162 
163         ViewLifecyclePhase phase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase(
164                 ViewLifecycle.getView(), UifConstants.ViewPhases.APPLY_MODEL, refreshPaths);
165 
166         View view = ViewLifecycle.getView();
167         ViewHelperService helper = ViewLifecycle.getHelper();
168         UifFormBase model = (UifFormBase) ViewLifecycle.getModel();
169 
170         if (LOG.isInfoEnabled()) {
171             LOG.info("performing apply model phase for view: " + view.getId());
172         }
173 
174         // apply default values if view in list
175 
176         if(model.isApplyDefaultValues()) {
177             helper.applyDefaultValues(view);
178 
179             //ensure default values are only set once
180             model.setApplyDefaultValues(false);
181         }
182 
183         // get action flag and edit modes from authorizer/presentation controller
184         helper.retrieveEditModesAndActionFlags();
185 
186         // set view context for conditional expressions
187         helper.setViewContext();
188 
189         processor.performPhase(phase);
190 
191         ProcessLogger.trace("apply-model:" + view.getId());
192     }
193 
194     /**
195      * Runs the finalize lifecycle phase.
196      *
197      * <p>Processor is invoked to perform any tasks for this phase.</p>
198      */
199     protected void runFinalizePhase() {
200         ViewLifecycleProcessor processor = ViewLifecycle.getProcessor();
201 
202         List<String> refreshPaths = null;
203         if (refreshPathMappings != null) {
204             refreshPaths = refreshPathMappings.get(UifConstants.ViewPhases.FINALIZE);
205         }
206 
207         ViewLifecyclePhase phase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase(
208                 ViewLifecycle.getView(), UifConstants.ViewPhases.FINALIZE, refreshPaths);
209 
210         View view = ViewLifecycle.getView();
211         if (LOG.isInfoEnabled()) {
212             LOG.info("performing finalize phase for view: " + view.getId());
213         }
214 
215         processor.performPhase(phase);
216     }
217 
218 }