View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.server.messages;
17  
18  
19  import java.lang.reflect.Method;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.xml.namespace.QName;
27  
28  import org.apache.log4j.Logger;
29  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
30  import org.kuali.student.common.ui.server.gwt.MessagesRpcGwtServlet;
31  import org.kuali.student.common.ui.server.serialization.KSSerializationPolicy;
32  import org.kuali.student.common.ui.server.serialization.SerializationUtils;
33  import org.kuali.student.r2.common.dto.LocaleInfo;
34  import org.kuali.student.r2.common.messages.dto.MessageInfo;
35  import org.kuali.student.r2.common.messages.service.MessageService;
36  import org.kuali.student.r2.common.util.ContextUtils;
37  
38  import com.google.gwt.user.server.rpc.RPC;
39  
40  public class MessageRPCPreloader {
41  	final Logger LOG = Logger.getLogger(MessageRPCPreloader.class);
42      private final String MESSAGE_SERVICE_MOCK = "ks.messageService";
43  	private final String MESSAGE_SERVICE = "{http://student.kuali.org/wsdl/messages}MessageService";
44      
45      MessageService messageService;
46      
47      public MessageRPCPreloader(){
48          
49      }
50      
51      public MessageService getMessageService() {
52          if (messageService == null){
53              setMessageService((MessageService)GlobalResourceLoader.getService(MESSAGE_SERVICE_MOCK));
54              if (messageService == null){
55                  setMessageService((MessageService)GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/messages","MessageService")));
56              }
57          }
58          return messageService;
59      }
60  
61      public void setMessageService(MessageService serviceImpl) {
62          this.messageService = serviceImpl;
63      }
64  
65      public String getMessagesByGroupsEncodingString(String locale, String[] keys){
66          Method serviceMethod;
67          try {
68              serviceMethod = MessagesRpcGwtServlet.class.getMethod("getMessagesByGroups", String.class, ArrayList.class);
69              
70              LocaleInfo localeInfo = new LocaleInfo();
71              localeInfo.setLocaleLanguage(locale);
72              ArrayList<String> messageGroupKeys = new ArrayList (Arrays.asList(keys));
73              ArrayList<MessageInfo> messages = new ArrayList<MessageInfo>();
74              for (MessageInfo info : getMessageService().getMessagesByGroups(localeInfo, messageGroupKeys, ContextUtils.getContextInfo())){
75                  messages.add(info);
76              }
77              
78              Map<Class<?>, Boolean> whitelist = new HashMap<Class<?>, Boolean>();
79              whitelist.put(MessageService.class, true);
80              whitelist.put(MessageInfo.class,true);
81              whitelist.put(LocaleInfo.class,true);
82              
83              KSSerializationPolicy myPolicy = new KSSerializationPolicy(whitelist);
84              
85              //String serializedData = RPC.encodeResponseForSuccess(serviceMethod, messageList,KSSerializationPolicy.getInstance());
86              String serializedData = RPC.encodeResponseForSuccess(serviceMethod, messages, myPolicy);
87              LOG.info("preloaded " + messages.size() + " messages for " + keys);
88              return SerializationUtils.escapeForSingleQuotedJavaScriptString(serializedData);
89          } catch (Exception e) {
90              LOG.error(e);
91              return "";
92          }
93  
94      }
95      
96  }