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.Arrays;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.log4j.Logger;
25  import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
26  import org.kuali.student.common.ui.server.gwt.MessagesRpcGwtServlet;
27  import org.kuali.student.common.ui.server.serialization.KSSerializationPolicy;
28  import org.kuali.student.common.ui.server.serialization.SerializationUtils;
29  import org.kuali.student.core.messages.dto.Message;
30  import org.kuali.student.core.messages.dto.MessageGroupKeyList;
31  import org.kuali.student.core.messages.dto.MessageList;
32  import org.kuali.student.core.messages.service.MessageService;
33  
34  import com.google.gwt.user.server.rpc.RPC;
35  
36  public class MessageRPCPreloader {
37  	final Logger LOG = Logger.getLogger(MessageRPCPreloader.class);
38      private final String MESSAGE_SERVICE_MOCK = "ks.messageServiceMock";
39  	private final String MESSAGE_SERVICE = "{http://student.kuali.org/wsdl/messages}MessageService";
40      
41      MessageService messageService;
42      
43      public MessageRPCPreloader(){
44          
45      }
46      
47      public MessageService getMessageService() {
48          if (messageService == null){
49              setMessageService((MessageService)GlobalResourceLoader.getService(MESSAGE_SERVICE_MOCK));
50              if (messageService == null){
51                  setMessageService((MessageService)GlobalResourceLoader.getService(MESSAGE_SERVICE));
52              }
53          }
54          return messageService;
55      }
56  
57      public void setMessageService(MessageService serviceImpl) {
58          this.messageService = serviceImpl;
59      }
60  
61      public String getMessagesByGroupsEncodingString(String locale, String[] keys){
62          Method serviceMethod;
63          try {
64              serviceMethod = MessagesRpcGwtServlet.class.getMethod("getMessagesByGroups", String.class,MessageGroupKeyList.class);
65              
66              MessageGroupKeyList messageGroupKeyList = new MessageGroupKeyList();
67              messageGroupKeyList.setMessageGroupKeys(Arrays.asList(keys));
68              
69              MessageList messageList = getMessageService().getMessagesByGroups(locale,messageGroupKeyList);
70  
71              Map<Class<?>, Boolean> whitelist = new HashMap<Class<?>, Boolean>();
72              whitelist.put(MessageService.class, true);
73              whitelist.put(MessageList.class, true);
74              whitelist.put(MessageGroupKeyList.class,true);
75              whitelist.put(Message.class,true);
76              whitelist.put(MessageGroupKeyList.class,true);
77              
78              KSSerializationPolicy myPolicy = new KSSerializationPolicy(whitelist);
79              
80              //String serializedData = RPC.encodeResponseForSuccess(serviceMethod, messageList,KSSerializationPolicy.getInstance());
81              String serializedData = RPC.encodeResponseForSuccess(serviceMethod, messageList,myPolicy);
82              
83              
84              return SerializationUtils.escapeForSingleQuotedJavaScriptString(serializedData);
85          } catch (Exception e) {
86              LOG.error(e);
87              return "";
88          }
89  
90      }
91      
92  }