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