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.element;
017
018import org.kuali.rice.krad.datadictionary.parse.BeanTag;
019import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
020import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
021import org.kuali.rice.krad.uif.UifConstants;
022import org.kuali.rice.krad.uif.component.Component;
023import org.kuali.rice.krad.uif.util.ScriptUtils;
024import org.kuali.rice.krad.uif.view.View;
025
026import java.util.HashMap;
027import java.util.Map;
028
029/**
030 * ValidationMessages for logic and options specific to fields.
031 *
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034@BeanTag(name = "fieldValidationMessages", parent = "Uif-FieldValidationMessages")
035public class FieldValidationMessages extends ValidationMessages {
036
037    private boolean useTooltip;
038    private boolean showIcons;
039
040    /**
041     * Calls super and add dataAttributes that are appropriate for field level validationMessages
042     * data.  This data is used by the validation framework clientside.
043     *
044     * @see krad.validate.js
045     */
046    @Override
047    public void generateMessages(View view, Object model, Component parent) {
048        super.generateMessages(view, model, parent);
049
050        boolean hasMessages = false;
051        if (!this.getErrors().isEmpty() || !this.getWarnings().isEmpty() || !this.getInfos().isEmpty()) {
052            hasMessages = true;
053        }
054        HashMap<String, Object> validationMessagesDataAttributes = new HashMap<String, Object>();
055
056        Map<String, String> dataDefaults =
057                (Map<String, String>) (KRADServiceLocatorWeb.getDataDictionaryService().getDictionaryBean(
058                        "Uif-FieldValidationMessages-DataDefaults"));
059
060        //display
061        this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "displayMessages",
062                this.isDisplayMessages());
063
064        //options
065        this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "useTooltip", useTooltip);
066        this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "messagingEnabled",
067                this.isDisplayMessages());
068        this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "hasOwnMessages",
069                hasMessages);
070        this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "showIcons", showIcons);
071
072        //add property directly for selections
073        if (hasMessages) {
074            parent.addDataAttribute(UifConstants.DataAttributes.HAS_MESSAGES, Boolean.toString(hasMessages));
075        }
076
077        //server messages
078        this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "serverErrors",
079                ScriptUtils.escapeHtml(this.getErrors()));
080        this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "serverWarnings",
081                ScriptUtils.escapeHtml(this.getWarnings()));
082        this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "serverInfo",
083                ScriptUtils.escapeHtml(this.getInfos()));
084
085        if (!validationMessagesDataAttributes.isEmpty()) {
086            parent.addScriptDataAttribute(UifConstants.DataAttributes.VALIDATION_MESSAGES, ScriptUtils.translateValue(
087                    validationMessagesDataAttributes));
088        }
089    }
090
091    /**
092     * When true, use the tooltip on fields to display their relevant messages.  When false, these messages
093     * will appear directly below the control.
094     *
095     * @return true if using tooltips for messages, false to display below control
096     */
097    @BeanTagAttribute
098    public boolean isUseTooltip() {
099        return useTooltip;
100    }
101
102    /**
103     * Set the useTooltip flag
104     *
105     * @param useTooltip if true, show tooltip, otherwise show messages below field control
106     */
107    public void setUseTooltip(boolean useTooltip) {
108        this.useTooltip = useTooltip;
109    }
110
111    /**
112     * If true, display dynamic icons next to fields which have messages.  Otherwise, do not render these icons.
113     *
114     * @return true if icons will be displayed, false otherwise
115     */
116    @BeanTagAttribute
117    public boolean isShowIcons() {
118        return showIcons;
119    }
120
121    /**
122     * Set whether field validation icons should display or not.
123     *
124     * @param showIcons
125     */
126    public void setShowIcons(boolean showIcons) {
127        this.showIcons = showIcons;
128    }
129}