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.service.impl;
017
018import org.kuali.rice.krad.bo.AdHocRoutePerson;
019import org.kuali.rice.krad.bo.AdHocRouteWorkgroup;
020import org.kuali.rice.krad.rules.rule.event.AddAdHocRoutePersonEvent;
021import org.kuali.rice.krad.rules.rule.event.AddAdHocRouteWorkgroupEvent;
022import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
023import org.kuali.rice.krad.service.KualiRuleService;
024import org.kuali.rice.krad.uif.view.ViewModel;
025import org.kuali.rice.krad.web.form.DocumentFormBase;
026
027/**
028 * View helper extension for document views.
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class DocumentViewHelperServiceImpl extends ViewHelperServiceImpl {
033    private static final long serialVersionUID = 5311653907592800785L;
034
035    private transient KualiRuleService kualiRuleService;
036
037    /**
038     * Performs validation on the new collection line before it is added to the corresponding collection.
039     *
040     * @param addLine new line instance to validate
041     * @param model object instance that contains the views data
042     * @return true if the line is valid and it should be added to the
043     * collection, false if it was not valid and should not be added to
044     * the collection
045     */
046    @Override
047    protected boolean performAddLineValidation(ViewModel model, Object addLine, String collectionId,
048            String collectionPath) {
049        boolean isValidLine = super.performAddLineValidation(model, addLine, collectionId, collectionPath);
050
051        if (model instanceof DocumentFormBase && addLine instanceof AdHocRoutePerson) {
052            DocumentFormBase form = (DocumentFormBase) model;
053            isValidLine &= getKualiRuleService().applyRules(new AddAdHocRoutePersonEvent(form.getDocument(),
054                    (AdHocRoutePerson) addLine));
055        } else if (model instanceof DocumentFormBase && addLine instanceof AdHocRouteWorkgroup) {
056            DocumentFormBase form = (DocumentFormBase) model;
057            isValidLine &= getKualiRuleService().applyRules(new AddAdHocRouteWorkgroupEvent(form.getDocument(),
058                    (AdHocRouteWorkgroup) addLine));
059        }
060
061        return isValidLine;
062    }
063
064    /**
065     * Retrieves an instance of the rule service.
066     *
067     * @return Kuali rule service
068     */
069    protected KualiRuleService getKualiRuleService() {
070        if (kualiRuleService == null) {
071            kualiRuleService = KRADServiceLocatorWeb.getKualiRuleService();
072        }
073
074        return this.kualiRuleService;
075    }
076}