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