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.lifecycle;
17  
18  import java.io.Serializable;
19  import java.text.MessageFormat;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.krad.messages.MessageService;
23  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24  import org.kuali.rice.krad.uif.UifConstants;
25  import org.kuali.rice.krad.uif.util.ScriptUtils;
26  import org.kuali.rice.krad.uif.view.View;
27  import org.kuali.rice.krad.uif.view.ViewModel;
28  import org.kuali.rice.krad.util.ErrorMessage;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  import org.kuali.rice.krad.util.GrowlMessage;
31  import org.kuali.rice.krad.util.KRADConstants;
32  import org.kuali.rice.krad.util.MessageMap;
33  
34  /**
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class LifecycleBase implements Serializable {
38      private static final long serialVersionUID = 9150578453981086356L;
39  
40      public static void performPostLifecycleProcessing(View view, Object model, ViewPostMetadata viewPostMetadata) {
41          String growlScript = buildGrowlScript(view.isGrowlMessagingEnabled());
42          ((ViewModel) model).setGrowlScript(growlScript);
43  
44      }
45  
46      /**
47       * Builds JS script that will invoke the show growl method to display a growl message when the
48       * page is rendered.
49       *
50       * <p>A growl call will be created for any explicit growl messages added to the message map.</p>
51       *
52       * @param growlMessagingEnabled indicates whether growls have been enabled, if not any growl messages are
53       * written as info messages
54       * @return JS script string for generated growl messages
55       */
56      protected static String buildGrowlScript(boolean growlMessagingEnabled) {
57          StringBuilder growlScript = new StringBuilder();
58  
59          MessageService messageService = KRADServiceLocatorWeb.getMessageService();
60  
61          MessageMap messageMap = GlobalVariables.getMessageMap();
62          for (GrowlMessage growl : messageMap.getGrowlMessages()) {
63              if (growlMessagingEnabled) {
64                  String message = messageService.getMessageText(growl.getNamespaceCode(), growl.getComponentCode(),
65                          growl.getMessageKey());
66  
67                  if (StringUtils.isBlank(message)) {
68                      continue;
69                  }
70  
71                  if (growl.getMessageParameters() != null) {
72                      message = message.replace("'", "''");
73                      message = MessageFormat.format(message, (Object[]) growl.getMessageParameters());
74                  }
75  
76                  // escape single quotes in message or title since that will cause problem with plugin
77                  message = message.replace("'", "\\'");
78  
79                  String title = growl.getTitle();
80                  if (StringUtils.isNotBlank(growl.getTitleKey())) {
81                      title = messageService.getMessageText(growl.getNamespaceCode(), growl.getComponentCode(),
82                              growl.getTitleKey());
83                  }
84                  title = title.replace("'", "\\'");
85  
86                  growlScript.append(ScriptUtils.buildFunctionCall(UifConstants.JsFunctions.SHOW_GROWL, message, title,
87                          growl.getTheme()));
88  
89              } else {
90                  ErrorMessage infoMessage = new ErrorMessage(growl.getMessageKey(), growl.getMessageParameters());
91                  infoMessage.setNamespaceCode(growl.getNamespaceCode());
92                  infoMessage.setComponentCode(growl.getComponentCode());
93  
94                  messageMap.putInfoForSectionId(KRADConstants.GLOBAL_INFO, infoMessage);
95              }
96          }
97  
98          return growlScript.toString();
99      }
100 
101 }