View Javadoc

1   package org.kuali.student.admin.messages.service.impl;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.List;
9   import java.util.Map;
10  
11  import javax.jws.WebService;
12  import javax.jws.soap.SOAPBinding;
13  
14  import org.kuali.rice.core.util.MaxAgeSoftReference;
15  import org.kuali.rice.core.util.MaxSizeMap;
16  import org.kuali.rice.kns.service.BusinessObjectService;
17  import org.kuali.rice.kns.service.KNSServiceLocator;
18  import org.kuali.student.core.enumerationmanagement.bo.EnumeratedValue;
19  import org.kuali.student.core.messages.bo.MessageEntity;
20  import org.kuali.student.core.messages.dto.LocaleKeyList;
21  import org.kuali.student.core.messages.dto.Message;
22  import org.kuali.student.core.messages.dto.MessageGroupKeyList;
23  import org.kuali.student.core.messages.dto.MessageList;
24  import org.kuali.student.core.messages.service.MessageService;
25  import org.springframework.beans.factory.InitializingBean;
26  
27  @WebService(endpointInterface = "org.kuali.student.core.messages.service.MessageService", serviceName = "MessageService", portName = "MessageService", targetNamespace = "http://student.kuali.org/wsdl/messages")
28  @SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
29  public class MessageServiceImpl implements MessageService, InitializingBean {
30  
31  	protected boolean cachingEnabled = false;
32  	protected int msgsCacheMaxSize = 20;
33  	protected int msgsCacheMaxAgeSeconds = 90;
34  	protected Map<String,MaxAgeSoftReference<MessageList>> msgsCache;
35  	
36      private BusinessObjectService businessObjectService;
37  
38      public Message addMessage(Message messageInfo) {
39          MessageEntity entity = toMessageEntity(messageInfo);
40          getBusinessObjectService().save(entity);
41  
42          return toMessage(entity);
43      }
44  
45  	@SuppressWarnings("unchecked")
46      public LocaleKeyList getLocales() {
47  	    List<String> keyList = new ArrayList<String>();
48  	    Map<String, Object> criteria = new HashMap<String,Object>();
49          
50          criteria.put(EnumeratedValue.ENUMERATION_KEY, MessageEntity.LOCALE_ENUMERATION);
51          BusinessObjectService boService = KNSServiceLocator.getBusinessObjectService();
52          Collection<EnumeratedValue> values = boService.findMatching(EnumeratedValue.class, criteria);
53          
54          Iterator<EnumeratedValue> iterator = values.iterator(); 
55          while(iterator.hasNext()) {
56              EnumeratedValue value = iterator.next();
57              keyList.add(value.getCode());
58          }
59      	
60      	LocaleKeyList localeKeyList = new LocaleKeyList();
61      	localeKeyList.setLocales(keyList);
62      	
63          return localeKeyList;
64      }
65  
66      public Message getMessage(String localeKey, String messageGroupKey, String messageKey) {
67          Map<String, Object> queryMap = new HashMap<String, Object>();
68          queryMap.put("locale", localeKey);
69          queryMap.put("groupName", messageGroupKey);
70          queryMap.put("messageId", messageKey);
71          
72          MessageEntity entity = (MessageEntity) getBusinessObjectService().findByPrimaryKey(MessageEntity.class, queryMap);
73          
74          return toMessage(entity);
75      }
76  
77      @SuppressWarnings("unchecked")
78      public MessageGroupKeyList getMessageGroups() {
79          List<String> keyList = new ArrayList<String>();
80          Map<String, Object> criteria = new HashMap<String,Object>();
81          
82          criteria.put(EnumeratedValue.ENUMERATION_KEY, MessageEntity.GROUP_NAME_ENUMERATION);
83          BusinessObjectService boService = KNSServiceLocator.getBusinessObjectService();
84          Collection<EnumeratedValue> values = boService.findMatching(EnumeratedValue.class, criteria);
85          
86          Iterator<EnumeratedValue> iterator = values.iterator(); 
87          while(iterator.hasNext()) {
88              EnumeratedValue value = iterator.next();
89              keyList.add(value.getCode());
90          }
91      	
92      	MessageGroupKeyList messageGroupKeyList = new MessageGroupKeyList();
93      	messageGroupKeyList.setMessageGroupKeys(keyList);
94      	
95          return messageGroupKeyList;
96      }
97  
98      @SuppressWarnings("unchecked")
99      public MessageList getMessages(String localeKey, String messageGroupKey) {
100         
101     	if(cachingEnabled){
102     		//Get From Cache
103     		MaxAgeSoftReference<MessageList> ref = msgsCache.get("localeKey="+localeKey+", messageGroupKey="+messageGroupKey);
104     		if ( ref != null ) {
105     			MessageList messageList = ref.get();
106     			if(messageList!=null){
107     				return messageList;
108     			}
109     		}
110 		}
111     	
112     	Map<String,String> fieldValues = new HashMap<String,String>();
113     	fieldValues.put("locale", localeKey);
114     	fieldValues.put("groupName", messageGroupKey);
115     	
116     	List<MessageEntity> messageEntityList = (List<MessageEntity>) getBusinessObjectService().findMatching(MessageEntity.class, fieldValues);
117     	
118     	List<Message> messages = new ArrayList<Message>();
119     	
120     	for(MessageEntity messageEntity: messageEntityList){
121     		messages.add(toMessage(messageEntity));
122     	}
123     	
124     	MessageList messageList = new MessageList();
125     	messageList.setMessages(messages);
126     	
127     	if(cachingEnabled){
128     		//Store to cache
129     		msgsCache.put("localeKey="+localeKey+", messageGroupKey="+messageGroupKey, new MaxAgeSoftReference<MessageList>( msgsCacheMaxAgeSeconds, messageList) );
130     	}
131     	
132     	return messageList;
133     	
134     }
135 
136     public MessageList getMessagesByGroups(String localeKey, MessageGroupKeyList messageGroupKeyList) {
137     	
138     	List<Message> messages = new ArrayList<Message>();
139     	
140     	for(String messageGroupKey: messageGroupKeyList.getMessageGroupKeys()){
141     		messages.addAll(getMessages(localeKey, messageGroupKey).getMessages());
142     	}
143     	
144     	MessageList messageList = new MessageList();
145     	messageList.setMessages(messages);
146     	
147     	return messageList;
148 
149     }
150 
151     public Message updateMessage(String localeKey, String messageGroupKey, String messageKey, Message messageInfo) {
152         
153     	Map<String,String> primaryKeys = new HashMap<String,String>();
154     	primaryKeys.put("locale", localeKey);
155     	primaryKeys.put("groupName", messageGroupKey);
156     	primaryKeys.put("messageId", messageKey);
157     	
158     	MessageEntity entity = (MessageEntity) getBusinessObjectService().findByPrimaryKey(MessageEntity.class, primaryKeys);
159     	
160     	entity.setMessageId(messageInfo.getId());
161     	entity.setLocale(messageInfo.getLocale());
162     	entity.setGroupName(messageInfo.getGroupName());
163     	entity.setValue(messageInfo.getValue());
164     	
165     	getBusinessObjectService().save(entity);
166     	
167     	return toMessage(entity);
168     	
169     }
170 
171     protected MessageEntity toMessageEntity(Message message) {
172         MessageEntity result = new MessageEntity();
173 
174         result.setMessageId(message.getId());
175         result.setLocale(message.getLocale());
176         result.setGroupName(message.getGroupName());
177         result.setValue(message.getValue());
178 
179         return result;
180     }
181 
182     protected Message toMessage(MessageEntity entity) {
183         Message result = new Message();
184 
185         result.setGroupName(entity.getGroupName());
186         result.setId(entity.getMessageId());
187         result.setLocale(entity.getLocale());
188         result.setValue(entity.getValue());
189 
190         return result;
191     }
192 
193     protected BusinessObjectService getBusinessObjectService() {
194         if (businessObjectService == null) {
195             businessObjectService = KNSServiceLocator.getBusinessObjectService();
196         }
197         return businessObjectService;
198     }
199 
200 	@Override
201 	public void afterPropertiesSet() throws Exception {
202 		if(cachingEnabled){
203 			msgsCache = Collections.synchronizedMap( new MaxSizeMap<String,MaxAgeSoftReference<MessageList>>( msgsCacheMaxSize ) );
204 		}
205 	}
206 
207 	public void setCachingEnabled(boolean cachingEnabled) {
208 		this.cachingEnabled = cachingEnabled;
209 	}
210 	
211 }