1
2
3
4
5
6
7
8
9
10
11
12
13
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
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 }