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.finalize;
17  
18  import java.util.Map;
19  
20  import org.kuali.rice.core.api.CoreApiServiceLocator;
21  import org.kuali.rice.krad.uif.UifConstants;
22  import org.kuali.rice.krad.uif.lifecycle.FinalizeComponentPhase;
23  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
24  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
25  import org.kuali.rice.krad.uif.util.ScriptUtils;
26  import org.kuali.rice.krad.uif.view.View;
27  import org.kuali.rice.krad.uif.view.ViewModel;
28  import org.kuali.rice.krad.util.KRADConstants;
29  
30  /**
31   * Perform custom finalize behavior for the component defined by the helper.
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class FinalizeViewTask extends ViewLifecycleTaskBase<View> {
36  
37      /**
38       * Default constructor.
39       */
40      public FinalizeViewTask() {
41          super(View.class);
42      }
43  
44      /**
45       * {@inheritDoc}
46       */
47      @Override
48      public FinalizeComponentPhase getElementState() {
49          return (FinalizeComponentPhase) super.getElementState();
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      @Override
56      protected void performLifecycleTask() {
57          View view = (View) getElementState().getElement();
58          assert view == ViewLifecycle.getView();
59          Object model = ViewLifecycle.getModel();
60  
61          view.setPreLoadScript(ScriptUtils.appendScript(
62                  view.getPreLoadScript(), buildClientSideStateScript(model)));
63      }
64  
65      /**
66       * Builds script that will initialize configuration parameters and component state on the client
67       * 
68       * <p>
69       * Here client side state is initialized along with configuration variables that need exposed to
70       * script
71       * </p>
72       * 
73       * @param model model containing the client side state map.
74       * @return The client side state script associated with this model.
75       */
76      protected String buildClientSideStateScript(Object model) {
77          Map<String, Object> clientSideState = ((ViewModel) model).getClientStateForSyncing();
78  
79          // script for initializing client side state on load
80          String clientStateScript = "";
81          if (!clientSideState.isEmpty()) {
82              clientStateScript = ScriptUtils.buildFunctionCall(UifConstants.JsFunctions.INITIALIZE_VIEW_STATE,
83                      clientSideState);
84          }
85  
86          // add necessary configuration parameters
87          String kradImageLocation = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
88                  UifConstants.ConfigProperties.KRAD_IMAGES_URL);
89          clientStateScript += ScriptUtils.buildFunctionCall(UifConstants.JsFunctions.SET_CONFIG_PARM,
90                  UifConstants.ClientSideVariables.KRAD_IMAGE_LOCATION, kradImageLocation);
91  
92          String kradURL = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
93                  UifConstants.ConfigProperties.KRAD_URL);
94          clientStateScript += ScriptUtils.buildFunctionCall(UifConstants.JsFunctions.SET_CONFIG_PARM,
95                  UifConstants.ClientSideVariables.KRAD_URL, kradURL);
96  
97          String applicationURL = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
98                  KRADConstants.ConfigParameters.APPLICATION_URL);
99          clientStateScript += ScriptUtils.buildFunctionCall(UifConstants.JsFunctions.SET_CONFIG_PARM,
100                 UifConstants.ClientSideVariables.APPLICATION_URL, applicationURL);
101 
102         String scriptCleanup = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
103                 KRADConstants.ConfigParameters.KRAD_SCRIPT_CLEANUP);
104         clientStateScript += ScriptUtils.buildFunctionCall(UifConstants.JsFunctions.SET_CONFIG_PARM,
105                 UifConstants.ClientSideVariables.KRAD_SCRIPT_CLEANUP, scriptCleanup);
106 
107         return clientStateScript;
108     }
109 
110 }