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.web.bind;
017
018import org.apache.commons.lang.ArrayUtils;
019import org.apache.commons.lang.StringUtils;
020import org.apache.commons.lang3.reflect.FieldUtils;
021import org.kuali.rice.core.framework.persistence.jta.Jta;
022import org.kuali.rice.krad.data.DataObjectService;
023import org.kuali.rice.krad.data.DataObjectWrapper;
024import org.kuali.rice.krad.data.KradDataServiceLocator;
025import org.kuali.rice.krad.data.util.Link;
026import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
027import org.kuali.rice.krad.uif.UifConstants;
028import org.kuali.rice.krad.uif.UifConstants.ViewType;
029import org.kuali.rice.krad.uif.UifParameters;
030import org.kuali.rice.krad.uif.service.ViewService;
031import org.kuali.rice.krad.uif.view.View;
032import org.kuali.rice.krad.util.KRADUtils;
033import org.kuali.rice.krad.web.form.UifFormBase;
034import org.springframework.core.annotation.AnnotationUtils;
035import org.springframework.core.convert.ConversionService;
036import org.springframework.util.Assert;
037import org.springframework.validation.AbstractPropertyBindingResult;
038import org.springframework.web.bind.ServletRequestDataBinder;
039
040import javax.servlet.ServletRequest;
041import javax.servlet.http.HttpServletRequest;
042import javax.transaction.UserTransaction;
043import java.lang.reflect.Field;
044import java.util.ArrayList;
045import java.util.Collections;
046import java.util.HashSet;
047import java.util.List;
048import java.util.Map;
049import java.util.Set;
050
051/**
052 * Override of ServletRequestDataBinder in order to hook in the UifBeanPropertyBindingResult
053 * which instantiates a custom BeanWrapperImpl, and to initialize the view.
054 *
055 * @author Kuali Rice Team (rice.collab@kuali.org)
056 */
057public class UifServletRequestDataBinder extends ServletRequestDataBinder {
058    protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(
059            UifServletRequestDataBinder.class);
060
061    private UifBeanPropertyBindingResult bindingResult;
062    private ConversionService conversionService;
063    private DataObjectService dataObjectService;
064    private boolean changeTracking = false;
065    private boolean autoLinking = true;
066
067    public UifServletRequestDataBinder(Object target) {
068        super(target);
069        this.changeTracking = determineChangeTracking(target);
070        setBindingErrorProcessor(new UifBindingErrorProcessor());
071    }
072
073    public UifServletRequestDataBinder(Object target, String name) {
074        super(target, name);
075        this.changeTracking = determineChangeTracking(target);
076        setBindingErrorProcessor(new UifBindingErrorProcessor());
077    }
078
079    /**
080     * Return true if the target of this data binder has change tracking enabled.
081     */
082    private static boolean determineChangeTracking(Object target) {
083        ChangeTracking changeTracking = AnnotationUtils.findAnnotation(target.getClass(), ChangeTracking.class);
084        if (changeTracking != null && changeTracking.enabled()) {
085            return true;
086        }
087        return false;
088    }
089
090    /**
091     * Allows for a custom binding result class.
092     *
093     * @see org.springframework.validation.DataBinder#initBeanPropertyAccess()
094     */
095    @Override
096    public void initBeanPropertyAccess() {
097        Assert.state(this.bindingResult == null,
098                "DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods");
099
100        this.bindingResult = new UifBeanPropertyBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths(),
101                getAutoGrowCollectionLimit());
102        this.bindingResult.setChangeTracking(this.changeTracking);
103
104        if (this.conversionService != null) {
105            this.bindingResult.initConversion(this.conversionService);
106        }
107
108        if (this.dataObjectService == null) {
109            this.dataObjectService = KradDataServiceLocator.getDataObjectService();
110        }
111    }
112
113    /**
114     * Allows for the setting attributes to use to find the data dictionary data from Kuali
115     *
116     * @see org.springframework.validation.DataBinder#getInternalBindingResult()
117     */
118    @Override
119    protected AbstractPropertyBindingResult getInternalBindingResult() {
120        if (this.bindingResult == null) {
121            initBeanPropertyAccess();
122        }
123
124        return this.bindingResult;
125    }
126
127    /**
128     * Disallows direct field access for Kuali
129     *
130     * @see org.springframework.validation.DataBinder#initDirectFieldAccess()
131     */
132    @Override
133    public void initDirectFieldAccess() {
134        LOG.error("Direct Field access is not allowed in UifServletRequestDataBinder.");
135        throw new RuntimeException("Direct Field access is not allowed in Kuali");
136    }
137
138    /**
139     * Helper method to facilitate calling super.bind() from {@link #bind(ServletRequest)}.
140     */
141    private void _bind(ServletRequest request) {
142        super.bind(request);
143    }
144
145    /**
146     * Calls {@link org.kuali.rice.krad.web.form.UifFormBase#preBind(HttpServletRequest)}, Performs data binding
147     * from servlet request parameters to the form, initializes view object, then calls
148     * {@link org.kuali.rice.krad.web.form.UifFormBase#postBind(javax.servlet.http.HttpServletRequest)}
149     *
150     * <p>
151     * The view is initialized by first looking for the {@code viewId} parameter in the request. If found, the view is
152     * retrieved based on this id. If the id is not present, then an attempt is made to find a view by type. In order
153     * to retrieve a view based on type, the view request parameter {@code viewTypeName} must be present. If all else
154     * fails and the viewId is populated on the form (could be populated from a previous request), this is used to
155     * retrieve the view.
156     * </p>
157     *
158     * @param request - HTTP Servlet Request instance
159     */
160    @Override
161    public void bind(ServletRequest request) {
162
163        if (LOG.isDebugEnabled()) {
164            LOG.debug("Request Parameters from getParameterMap:");
165
166            for (String key : request.getParameterMap().keySet()) {
167                LOG.debug("\t" + key + "=>" + request.getParameterMap().get(key));
168            }
169
170            LOG.debug("Request Parameters from getParameter:");
171
172            for (String name : Collections.list(request.getParameterNames())) {
173                LOG.debug("\t" + name + "=>" + request.getParameter(name));
174            }
175        }
176
177        UifFormBase form = (UifFormBase) UifServletRequestDataBinder.this.getTarget();
178
179        request.setAttribute(UifConstants.REQUEST_FORM, form);
180
181        form.preBind((HttpServletRequest) request);
182
183        _bind(request);
184
185        request.setAttribute(UifConstants.PROPERTY_EDITOR_REGISTRY, this.bindingResult.getPropertyEditorRegistry());
186
187        executeAutomaticLinking(request, form);
188
189        if (!form.isUpdateNoneRequest()) {
190            // attempt to retrieve a view by unique identifier first, either as request attribute or parameter
191            String viewId = (String) request.getAttribute(UifParameters.VIEW_ID);
192            if (StringUtils.isBlank(viewId)) {
193                viewId = request.getParameter(UifParameters.VIEW_ID);
194            }
195
196            View view = null;
197            if (StringUtils.isNotBlank(viewId)) {
198                view = getViewService().getViewById(viewId);
199            }
200
201            // attempt to get view instance by type parameters
202            if (view == null) {
203                view = getViewByType(request, form);
204            }
205
206            // if view not found attempt to find one based on the cached form
207            if (view == null) {
208                view = getViewFromPreviousModel(form);
209
210                if (view != null) {
211                    LOG.warn("Obtained viewId from cached form, this may not be safe!");
212                }
213            }
214
215            if (view != null) {
216                form.setViewId(view.getId());
217
218            } else {
219                form.setViewId(null);
220            }
221
222            form.setView(view);
223        }
224
225        // invoke form callback for custom binding
226        form.postBind((HttpServletRequest) request);
227    }
228
229    /**
230     * Performs automatic reference linking of the given form based on the properties on the form for which linking
231     * is enabled.
232     *
233     * <p>Linking will only be performed if change tracking and auto linking are enabled on this data binder.</p>
234     *
235     * @param request request instance
236     * @param form form instance against which to perform automatic linking
237     */
238    protected void executeAutomaticLinking(ServletRequest request, UifFormBase form) {
239        if (!changeTracking) {
240            LOG.info("Skip automatic linking because change tracking not enabled for this form.");
241            return;
242        }
243
244        if (!autoLinking) {
245            LOG.info("Skip automatic linking because it has been disabled for this form");
246            return;
247        }
248
249        Set<String> autoLinkingPaths = determineRootAutoLinkingPaths(form.getClass(), null, new HashSet<Class<?>>());
250        List<AutoLinkTarget> targets = extractAutoLinkTargets(autoLinkingPaths);
251
252        // perform linking for each target
253        for (AutoLinkTarget target : targets) {
254            if (!dataObjectService.supports(target.getTarget().getClass())) {
255                LOG.warn("Encountered an auto linking target that is not a valid data object: " + target.getTarget()
256                        .getClass());
257            } else {
258                DataObjectWrapper<?> wrapped = dataObjectService.wrap(target.getTarget());
259                wrapped.linkChanges(target.getModifiedPropertyPaths());
260            }
261        }
262    }
263
264    /**
265     * Determines the root property paths relative to the given root object type against which to perform automatic
266     * linking.
267     *
268     * <p>This will be determined based on the presence of {@link Link} annotations on the given root object type.
269     * This method is invoked recursively as it walks the class structure looking for Link annotations. It uses the
270     * path
271     * and scanned arguments to keep track of how deep into the structure the scanning is and to prevent infinite
272     * recursion.</p>
273     *
274     * @param rootObjectType the root object type from which to perform the scan for auto-linking paths
275     * @param path the current property path relative to the original root object type at which the scan began, if null
276     * then we are scanning from the root-most object type. Each recursive call of this method will append
277     * a new property to this path
278     * @param scanned used to track classes that have already been scanned and prevent infinite recursion
279     * @return a set of property paths that should be auto linked
280     */
281    protected Set<String> determineRootAutoLinkingPaths(Class<?> rootObjectType, String path, Set<Class<?>> scanned) {
282        Set<String> autoLinkingPaths = new HashSet<String>();
283        if (scanned.contains(rootObjectType)) {
284            return autoLinkingPaths;
285        } else {
286            scanned.add(rootObjectType);
287        }
288        Link autoLink = AnnotationUtils.findAnnotation(rootObjectType, Link.class);
289        if (autoLink != null && autoLink.cascade()) {
290            autoLinkingPaths.addAll(assembleAutoLinkingPaths(path, autoLink));
291        } else if (autoLink == null) {
292            Field[] fields = FieldUtils.getAllFields(rootObjectType);
293            for (Field field : fields) {
294                autoLink = field.getAnnotation(Link.class);
295                if (autoLink != null) {
296                    if (autoLink.cascade()) {
297                        String fieldPath = appendToPath(path, field.getName());
298                        autoLinkingPaths.addAll(assembleAutoLinkingPaths(fieldPath, autoLink));
299                    }
300                } else {
301                    autoLinkingPaths.addAll(determineRootAutoLinkingPaths(field.getType(), appendToPath(path,
302                            field.getName()), scanned));
303                }
304            }
305        }
306        return autoLinkingPaths;
307    }
308
309    /**
310     * A helper method which simply assembles a set of property paths for the given {@link Link} annotation which
311     * should
312     * be auto linked.
313     *
314     * @param path the property path from the top-most root class to where the Link annotation was found during the
315     * scan
316     * @param autoLink the Link annotation which is being processed
317     * @return a Set of auto linking paths based on the given path parameter, plus the path(s) defined on the
318     * {@link Link} annotation
319     */
320    protected Set<String> assembleAutoLinkingPaths(String path, Link autoLink) {
321        Set<String> autoLinkingPaths = new HashSet<String>();
322        if (ArrayUtils.isEmpty(autoLink.path())) {
323            autoLinkingPaths.add(path);
324        } else {
325            for (String autoLinkingPath : autoLink.path()) {
326                autoLinkingPaths.add(appendToPath(path, autoLinkingPath));
327            }
328        }
329        return autoLinkingPaths;
330    }
331
332    /**
333     * Uses the binding result on this data binder to determine the targets on the form that automatic linking should
334     * be performed against.
335     *
336     * <p>Only those property paths for which auto linking is enabled and which were actually modified during the
337     * execution of this data binding will be returned from this method.</p>
338     *
339     * @param autoLinkingPaths a set of paths relative to the form class for which auto-linking has been enabled
340     * @return a list of {@link AutoLinkTarget} objects which contain an object to be linked and which properties on
341     * that object were modified during this data binding execution
342     */
343    protected List<AutoLinkTarget> extractAutoLinkTargets(Set<String> autoLinkingPaths) {
344        List<AutoLinkTarget> targets = new ArrayList<AutoLinkTarget>();
345
346        for (String autoLinkingPath : autoLinkingPaths) {
347            Object targetObject = getInternalBindingResult().getPropertyAccessor().getPropertyValue(autoLinkingPath);
348            if (targetObject == null) {
349                continue;
350            }
351
352            if (targetObject instanceof Map) {
353                targets.addAll(extractAutoLinkMapTargets(autoLinkingPath, (Map<?, ?>) targetObject));
354
355                continue;
356            }
357
358            if (targetObject instanceof List) {
359                targets.addAll(extractAutoLinkListTargets(autoLinkingPath, (List<?>) targetObject));
360
361                continue;
362            }
363
364            Set<String> modifiedAutoLinkingPaths = new HashSet<String>();
365
366            Set<String> modifiedPaths = ((UifBeanPropertyBindingResult) getInternalBindingResult()).getModifiedPaths();
367            for (String modifiedPath : modifiedPaths) {
368                if (modifiedPath.startsWith(autoLinkingPath)) {
369                    modifiedAutoLinkingPaths.add(modifiedPath.substring(autoLinkingPath.length() + 1));
370                }
371            }
372
373            targets.add(new AutoLinkTarget(targetObject, modifiedAutoLinkingPaths));
374        }
375
376        return targets;
377    }
378
379    /**
380     * For the map object indicated for linking, iterates through the modified paths and finds paths that match
381     * entries in the map, and if found adds an auto link target.
382     *
383     * @param autoLinkingPath path configured for auto linking
384     * @param targetMap map object for the linking path
385     * @return List of auto linking targets to process
386     */
387    protected List<AutoLinkTarget> extractAutoLinkMapTargets(String autoLinkingPath, Map<?, ?> targetMap) {
388        List<AutoLinkTarget> targets = new ArrayList<AutoLinkTarget>();
389
390        Set<String> modifiedPaths = ((UifBeanPropertyBindingResult) getInternalBindingResult()).getModifiedPaths();
391
392        for (Map.Entry<?, ?> targetMapEntry : targetMap.entrySet()) {
393            Set<String> modifiedAutoLinkingPaths = new HashSet<String>();
394
395            for (String modifiedPath : modifiedPaths) {
396                String targetPathMatch = autoLinkingPath + "['" + targetMapEntry.getKey() + "']";
397
398                if (modifiedPath.startsWith(targetPathMatch)) {
399                    modifiedAutoLinkingPaths.add(modifiedPath.substring(targetPathMatch.length() + 1));
400                }
401            }
402
403            if (!modifiedAutoLinkingPaths.isEmpty()) {
404                targets.add(new AutoLinkTarget(targetMapEntry.getValue(), modifiedAutoLinkingPaths));
405            }
406        }
407
408        return targets;
409    }
410
411    /**
412     * For the list object indicated for linking, iterates through the modified paths and finds paths that match
413     * entries in the list, and if found adds an auto link target.
414     *
415     * @param autoLinkingPath path configured for auto linking
416     * @param targetList list object for the linking path
417     * @return List of auto linking targets to process
418     */
419    protected List<AutoLinkTarget> extractAutoLinkListTargets(String autoLinkingPath, List<?> targetList) {
420        List<AutoLinkTarget> targets = new ArrayList<AutoLinkTarget>();
421
422        Set<String> modifiedPaths = ((UifBeanPropertyBindingResult) getInternalBindingResult()).getModifiedPaths();
423
424        for (int i = 0; i < targetList.size(); i++) {
425            Set<String> modifiedAutoLinkingPaths = new HashSet<String>();
426
427            for (String modifiedPath : modifiedPaths) {
428                String targetPathMatch = autoLinkingPath + "[" + i + "]";
429
430                if (modifiedPath.startsWith(targetPathMatch)) {
431                    modifiedAutoLinkingPaths.add(modifiedPath.substring(targetPathMatch.length() + 1));
432                }
433            }
434
435            if (!modifiedAutoLinkingPaths.isEmpty()) {
436                targets.add(new AutoLinkTarget(targetList.get(i), modifiedAutoLinkingPaths));
437            }
438        }
439
440        return targets;
441    }
442
443    /**
444     * A utility method which appends two property paths together to create a new nested property path.
445     *
446     * <p>Handles null values for either the path or pathElement. The general output will be path.pathElement
447     * except in situations where either of the two given values are empty or null, in which case only the non-null
448     * value will be returned.</p>
449     *
450     * @param path the prefix of the property path
451     * @param pathElement the suffix of the property path to append to the given path
452     * @return an appended path, appended with a "." between the given path and pathElement (unless one of these is
453     * null)
454     */
455    private String appendToPath(String path, String pathElement) {
456        if (StringUtils.isEmpty(path)) {
457            return pathElement;
458        } else if (StringUtils.isEmpty(pathElement)) {
459            return path;
460        }
461        return path + "." + pathElement;
462    }
463
464    /**
465     * Attempts to get a view instance by looking for a view type name in the request or the form and querying
466     * that view type with the request parameters
467     *
468     * @param request request instance to pull parameters from
469     * @param form form instance to pull values from
470     * @return View instance if found or null
471     */
472    protected View getViewByType(ServletRequest request, UifFormBase form) {
473        View view = null;
474
475        String viewTypeName = request.getParameter(UifParameters.VIEW_TYPE_NAME);
476        ViewType viewType = StringUtils.isBlank(viewTypeName) ? form.getViewTypeName() : ViewType.valueOf(viewTypeName);
477
478        if (viewType != null) {
479            Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
480            view = getViewService().getViewByType(viewType, parameterMap);
481        }
482
483        return view;
484    }
485
486    /**
487     * Attempts to get a view instance based on the view id stored on the form (which might not be populated
488     * from the request but remaining from session)
489     *
490     * @param form form instance to pull view id from
491     * @return View instance associated with form's view id or null if id or view not found
492     */
493    protected View getViewFromPreviousModel(UifFormBase form) {
494        // maybe we have a view id from the session form
495        if (form.getViewId() != null) {
496            return getViewService().getViewById(form.getViewId());
497        }
498
499        return null;
500    }
501
502    public boolean isChangeTracking() {
503        return changeTracking;
504    }
505
506    public boolean isAutoLinking() {
507        return autoLinking;
508    }
509
510    public void setAutoLinking(boolean autoLinking) {
511        this.autoLinking = autoLinking;
512    }
513
514    public ViewService getViewService() {
515        return KRADServiceLocatorWeb.getViewService();
516    }
517
518    public DataObjectService getDataObjectService() {
519        return this.dataObjectService;
520    }
521
522    public void setDataObjectService(DataObjectService dataObjectService) {
523        this.dataObjectService = dataObjectService;
524    }
525
526    /**
527     * Holds an object that will have auto-linking executed against it.
528     *
529     * <p>Also contains a set of property paths (relative to the object) that were modified during the data binding
530     * execution.</p>
531     */
532    private static final class AutoLinkTarget {
533        private final Object target;
534        private final Set<String> modifiedPropertyPaths;
535
536        AutoLinkTarget(Object target, Set<String> modifiedPropertyPaths) {
537            this.target = target;
538            this.modifiedPropertyPaths = modifiedPropertyPaths;
539        }
540
541        Object getTarget() {
542            return target;
543        }
544
545        Set<String> getModifiedPropertyPaths() {
546            return Collections.unmodifiableSet(modifiedPropertyPaths);
547        }
548    }
549
550}
551
552