View Javadoc
1   /**
2    * Copyright 2005-2015 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.Map;
19  
20  import javax.servlet.http.HttpServletRequest;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.krad.uif.UifConstants;
24  import org.kuali.rice.krad.uif.UifParameters;
25  import org.kuali.rice.krad.uif.component.MethodInvokerConfig;
26  import org.kuali.rice.krad.uif.view.View;
27  import org.kuali.rice.krad.util.KRADConstants;
28  import org.kuali.rice.krad.web.controller.UifControllerBase;
29  import org.kuali.rice.krad.web.form.UifFormBase;
30  import org.springframework.web.servlet.support.RequestContextUtils;
31  
32  /**
33   * Lifecycle processing task for encapsulating a view refresh. 
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   * @see ViewLifecycle#encapsulateLifecycle(View, Object, HttpServletRequest, javax.servlet.http.HttpServletResponse, Runnable)
37   * @see UifControllerBase#refresh(UifFormBase, org.springframework.validation.BindingResult, HttpServletRequest, javax.servlet.http.HttpServletResponse)
38   */
39  public class ViewLifecycleRefreshBuild implements Runnable {
40  
41      @Override
42      public void run() {
43          View view = ViewLifecycle.getView();
44          UifFormBase form = (UifFormBase) ViewLifecycle.getModel();
45          HttpServletRequest request = ViewLifecycle.getRequest();
46          
47          String flashMapSelectedLineValues = "";
48          if (RequestContextUtils.getInputFlashMap(request) != null) {
49              flashMapSelectedLineValues = (String) RequestContextUtils.getInputFlashMap(request).get(
50                      UifParameters.SELECTED_LINE_VALUES);
51          }
52  
53          String refreshCallerType = "";
54          if (request.getParameterMap().containsKey(KRADConstants.REFRESH_CALLER_TYPE)) {
55              refreshCallerType = request.getParameter(KRADConstants.REFRESH_CALLER_TYPE);
56          }
57  
58          // process multi-value lookup returns
59          if (StringUtils.equals(refreshCallerType, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP)) {
60              String lookupCollectionName = "";
61              if (request.getParameterMap().containsKey(UifParameters.LOOKUP_COLLECTION_NAME)) {
62                  lookupCollectionName = request.getParameter(UifParameters.LOOKUP_COLLECTION_NAME);
63              }
64  
65              String lookupCollectionId = "";
66              if (request.getParameterMap().containsKey(UifParameters.LOOKUP_COLLECTION_ID)) {
67                  lookupCollectionId = request.getParameter(UifParameters.LOOKUP_COLLECTION_ID);
68              }
69  
70              if (StringUtils.isBlank(lookupCollectionName)) {
71                  throw new RuntimeException(
72                          "Lookup collection name is required for processing multi-value lookup results");
73              }
74  
75              String multiValueReturnFields ="";
76              if (request.getParameterMap().containsKey(UifParameters.MULIT_VALUE_RETURN_FILEDS)) {
77                  multiValueReturnFields = request.getParameter(UifParameters.MULIT_VALUE_RETURN_FILEDS);
78              }
79  
80              String selectedLineValues = "";
81              if (request.getParameterMap().containsKey(UifParameters.SELECTED_LINE_VALUES)) {
82                  selectedLineValues = request.getParameter(UifParameters.SELECTED_LINE_VALUES);
83              }
84              if (!StringUtils.isBlank(flashMapSelectedLineValues)) {
85                  selectedLineValues = flashMapSelectedLineValues;
86              }
87  
88              // invoked view helper to populate the collection from lookup results
89              ViewLifecycle.getHelper().processMultipleValueLookupResults(form, lookupCollectionId,
90                      lookupCollectionName, multiValueReturnFields, selectedLineValues);
91          }
92  
93          // refresh references
94          if (request.getParameterMap().containsKey(KRADConstants.REFERENCES_TO_REFRESH)) {
95              String referencesToRefresh = request.getParameter(KRADConstants.REFERENCES_TO_REFRESH);
96  
97              ViewLifecycle.getHelper().refreshReferences(referencesToRefresh);
98          }
99  
100         // set focus and jump position for returning from a quickfinder
101         // check and invoke callback method
102         if (request.getParameterMap().containsKey(UifParameters.QUICKFINDER_ID)) {
103             String quickfinderId = request.getParameter(UifParameters.QUICKFINDER_ID);
104 
105             String focusId = (String) form.getViewPostMetadata().getComponentPostData(quickfinderId,
106                                 UifConstants.PostMetadata.QUICKFINDER_FOCUS_ID);
107             if (StringUtils.isNotBlank(focusId)) {
108                 form.setFocusId(focusId);
109             }
110 
111             String jumpToId = (String) form.getViewPostMetadata().getComponentPostData(quickfinderId,
112                                 UifConstants.PostMetadata.QUICKFINDER_JUMP_TO_ID);
113             if (StringUtils.isNotBlank(jumpToId)) {
114                 form.setJumpToId(jumpToId);
115             }
116 
117             // check for callback method and invoke it if present
118             String callbackMethodToCall = ( String ) form.getViewPostMetadata().getComponentPostData( quickfinderId,
119                     UifConstants.PostMetadata.QUICKFINDER_CALLBACK_METHOD_TO_CALL );
120             MethodInvokerConfig callbackMethod = ( MethodInvokerConfig ) form.getViewPostMetadata().
121                     getComponentPostData( quickfinderId, UifConstants.PostMetadata.QUICKFINDER_CALLBACK_METHOD );
122 
123             if( StringUtils.isNotBlank( callbackMethodToCall ) || callbackMethod != null ) {
124                 // if the callbackMethod is not set, then we set it
125                 if( callbackMethod == null ) {
126                     callbackMethod = new MethodInvokerConfig();
127                 }
128 
129                 // get additional parameters to be passed to the callback method
130                 Map<String, String> callbackContext = ( Map<String, String> ) form.getViewPostMetadata().
131                         getComponentPostData( quickfinderId, UifConstants.PostMetadata.QUICKFINDER_CALLBACK_CONTEXT );
132 
133                 // if target class or object not set, use view helper service
134                 if( ( callbackMethod.getTargetClass() == null ) && ( callbackMethod.getTargetObject() == null ) ) {
135                     callbackMethod.setTargetObject( ViewLifecycle.getHelper() );
136                 }
137 
138                 callbackMethod.setTargetMethod( callbackMethodToCall );
139                 Object[] arguments = new Object[3];
140                 arguments[0] = form;
141                 arguments[1] = quickfinderId;
142                 arguments[2] = callbackContext;
143                 callbackMethod.setArguments( arguments );
144 
145                 // invoke callback method
146                 try {
147                     callbackMethod.prepare();
148 
149                     Class<?> methodReturnType = callbackMethod.getPreparedMethod().getReturnType();
150                     if( StringUtils.equals( "void", methodReturnType.getName() ) ) {
151                         callbackMethod.invoke();
152                     } else {
153                         // TODO : can the return type be anything else other than void? if so, what?
154                     }
155                 } catch( Exception e ) {
156                     throw new RuntimeException( "Error invoking callback method for quickfinder: " + quickfinderId, e );
157                 }
158             }
159         }
160 
161     }
162 }