001/** 002 * Copyright 2005-2018 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.uif.lifecycle; 017 018import java.util.Map; 019 020import javax.servlet.http.HttpServletRequest; 021 022import org.apache.commons.lang.StringUtils; 023import org.kuali.rice.krad.uif.UifConstants; 024import org.kuali.rice.krad.uif.UifParameters; 025import org.kuali.rice.krad.uif.component.MethodInvokerConfig; 026import org.kuali.rice.krad.uif.view.View; 027import org.kuali.rice.krad.util.KRADConstants; 028import org.kuali.rice.krad.web.controller.UifControllerBase; 029import org.kuali.rice.krad.web.form.UifFormBase; 030import org.springframework.web.servlet.support.RequestContextUtils; 031 032/** 033 * Lifecycle processing task for encapsulating a view refresh. 034 * 035 * @author Kuali Rice Team (rice.collab@kuali.org) 036 * @see ViewLifecycle#encapsulateLifecycle(View, Object, HttpServletRequest, javax.servlet.http.HttpServletResponse, Runnable) 037 * @see UifControllerBase#refresh(UifFormBase, org.springframework.validation.BindingResult, HttpServletRequest, javax.servlet.http.HttpServletResponse) 038 */ 039public class ViewLifecycleRefreshBuild implements Runnable { 040 041 @Override 042 public void run() { 043 View view = ViewLifecycle.getView(); 044 UifFormBase form = (UifFormBase) ViewLifecycle.getModel(); 045 HttpServletRequest request = ViewLifecycle.getRequest(); 046 047 String flashMapSelectedLineValues = ""; 048 if (RequestContextUtils.getInputFlashMap(request) != null) { 049 flashMapSelectedLineValues = (String) RequestContextUtils.getInputFlashMap(request).get( 050 UifParameters.SELECTED_LINE_VALUES); 051 } 052 053 String refreshCallerType = ""; 054 if (request.getParameterMap().containsKey(KRADConstants.REFRESH_CALLER_TYPE)) { 055 refreshCallerType = request.getParameter(KRADConstants.REFRESH_CALLER_TYPE); 056 } 057 058 // process multi-value lookup returns 059 if (StringUtils.equals(refreshCallerType, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP)) { 060 String lookupCollectionName = ""; 061 if (request.getParameterMap().containsKey(UifParameters.LOOKUP_COLLECTION_NAME)) { 062 lookupCollectionName = request.getParameter(UifParameters.LOOKUP_COLLECTION_NAME); 063 } 064 065 String lookupCollectionId = ""; 066 if (request.getParameterMap().containsKey(UifParameters.LOOKUP_COLLECTION_ID)) { 067 lookupCollectionId = request.getParameter(UifParameters.LOOKUP_COLLECTION_ID); 068 } 069 070 if (StringUtils.isBlank(lookupCollectionName)) { 071 throw new RuntimeException( 072 "Lookup collection name is required for processing multi-value lookup results"); 073 } 074 075 String multiValueReturnFields =""; 076 if (request.getParameterMap().containsKey(UifParameters.MULIT_VALUE_RETURN_FILEDS)) { 077 multiValueReturnFields = request.getParameter(UifParameters.MULIT_VALUE_RETURN_FILEDS); 078 } 079 080 String selectedLineValues = ""; 081 if (request.getParameterMap().containsKey(UifParameters.SELECTED_LINE_VALUES)) { 082 selectedLineValues = request.getParameter(UifParameters.SELECTED_LINE_VALUES); 083 } 084 if (!StringUtils.isBlank(flashMapSelectedLineValues)) { 085 selectedLineValues = flashMapSelectedLineValues; 086 } 087 088 // invoked view helper to populate the collection from lookup results 089 ViewLifecycle.getHelper().processMultipleValueLookupResults(form, lookupCollectionId, 090 lookupCollectionName, multiValueReturnFields, selectedLineValues); 091 } 092 093 // refresh references 094 if (request.getParameterMap().containsKey(KRADConstants.REFERENCES_TO_REFRESH)) { 095 String referencesToRefresh = request.getParameter(KRADConstants.REFERENCES_TO_REFRESH); 096 097 ViewLifecycle.getHelper().refreshReferences(referencesToRefresh); 098 } 099 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}