View Javadoc
1   /**
2    * Copyright 2005-2016 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.element;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.kuali.rice.krad.uif.container.PageGroup;
23  import org.kuali.rice.krad.uif.lifecycle.LifecycleEventListener;
24  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
25  import org.kuali.rice.krad.uif.util.LifecycleElement;
26  import org.kuali.rice.krad.uif.view.View;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.MessageMap;
29  
30  import java.util.HashSet;
31  import java.util.Set;
32  
33  /**
34   * ValidationMessages for logic and options specific to pages.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  @BeanTag(name = "pageValidationMessages", parent = "Uif-PageValidationMessages")
39  public class PageValidationMessages extends GroupValidationMessages implements LifecycleEventListener {
40      private static final long serialVersionUID = 6387432156428507958L;
41  
42      private boolean showPageSummaryHeader;
43  
44      /**
45       * {@inheritDoc}
46       */
47      @Override
48      public void generateMessages(View view, Object model, Component parent) {
49          super.generateMessages(view, model, parent);
50  
51          ViewLifecycle viewLifecycle = ViewLifecycle.getActiveLifecycle();
52          viewLifecycle.registerLifecycleCompleteListener(view, this);
53      }
54  
55      /**
56       * Overridding to prevent the initial writing of data attributes until the view has been processed and
57       * we collection unmatched messages (through the lifecycle event).
58       *
59       * {@inheritDoc}
60       */
61      @Override
62      protected void addValidationMessageDataAttributes(Component parent) {
63          // do nothing
64      }
65  
66      /**
67       * Check for message keys that are not matched anywhere on the page, these unmatched messages must still be
68       * displayed at the page level.
69       *
70       * {@inheritDoc}
71       */
72      @Override
73      public void processEvent(ViewLifecycle.LifecycleEvent lifecycleEvent, View view, Object model,
74              LifecycleElement eventElement) {
75          View eventComponent = (View) eventElement;
76          PageGroup currentPage = eventComponent.getCurrentPage();
77  
78          Set<String> allPossibleKeys = new HashSet<String>();
79  
80          Set<String> renderedPropertyPaths = ViewLifecycle.getViewPostMetadata().getAllRenderedPropertyPaths();
81          if (renderedPropertyPaths != null) {
82              allPossibleKeys.addAll(renderedPropertyPaths);
83          }
84  
85          addNestedGroupKeys(allPossibleKeys, currentPage);
86  
87          if (getAdditionalKeysToMatch() != null) {
88              allPossibleKeys.addAll(getAdditionalKeysToMatch());
89          }
90  
91          if (StringUtils.isNotBlank(currentPage.getId())) {
92              allPossibleKeys.add(currentPage.getId());
93          }
94  
95          MessageMap messageMap = GlobalVariables.getMessageMap();
96  
97          Set<String> messageKeys = new HashSet<String>();
98  
99          messageKeys.addAll(messageMap.getAllPropertiesWithErrors());
100         messageKeys.addAll(messageMap.getAllPropertiesWithWarnings());
101         messageKeys.addAll(messageMap.getAllPropertiesWithInfo());
102 
103         messageKeys.removeAll(allPossibleKeys);
104 
105         for (String key : messageKeys) {
106             getErrors().addAll(getMessages(view, key, messageMap.getErrorMessagesForProperty(key, true)));
107             getWarnings().addAll(getMessages(view, key, messageMap.getWarningMessagesForProperty(key, true)));
108             getInfos().addAll(getMessages(view, key, messageMap.getInfoMessagesForProperty(key, true)));
109         }
110 
111         super.addValidationMessageDataAttributes(currentPage);
112     }
113 
114     /**
115      * If true, shows the page summary header (message count header message in the message block).  Otherwise, this
116      * header is not rendered.
117      *
118      * @return true if the header will show, false otherwise
119      */
120     @BeanTagAttribute
121     public boolean isShowPageSummaryHeader() {
122         return showPageSummaryHeader;
123     }
124 
125     /**
126      * Set the page summary header to show or not show.
127      *
128      * @param showPageSummaryHeader
129      */
130     public void setShowPageSummaryHeader(boolean showPageSummaryHeader) {
131         this.showPageSummaryHeader = showPageSummaryHeader;
132     }
133 }