Coverage Report - org.kuali.student.core.messages.dao.impl.MessageManagementDAOImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageManagementDAOImpl
59%
48/81
83%
5/6
3
 
 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.core.messages.dao.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.persistence.EntityManager;
 22  
 import javax.persistence.PersistenceContext;
 23  
 import javax.persistence.Query;
 24  
 
 25  
 import org.kuali.student.core.messages.MessageException;
 26  
 import org.kuali.student.core.messages.dao.MessageManagementDAO;
 27  
 import org.kuali.student.core.messages.entity.MessageEntity;
 28  
 import org.slf4j.Logger;
 29  
 import org.slf4j.LoggerFactory;
 30  
 
 31  
 
 32  7
 public class MessageManagementDAOImpl implements MessageManagementDAO {
 33  
     private EntityManager entityManager;
 34  
     
 35  2
     final static Logger logger = LoggerFactory.getLogger(MessageManagementDAOImpl.class);
 36  
     
 37  
     @PersistenceContext(unitName = "MessageManagement")
 38  
     public void setEntityManager(EntityManager entityManager) {
 39  7
         this.entityManager = entityManager;
 40  7
     }
 41  
     
 42  
     public EntityManager getEntityManager(){
 43  0
         return this.entityManager; 
 44  
     }
 45  
     
 46  
     public MessageEntity addMessage(MessageEntity me){
 47  0
             MessageEntity result = null;
 48  
             try{
 49  0
                 entityManager.persist(me);
 50  0
                 result = this.getMessage(me.getLocale(), me.getGroupName(), me.getId());
 51  
             }
 52  0
                 catch(Exception e){
 53  0
                     throw new MessageException("adding message data query failed.", e);
 54  0
                 }
 55  0
                 return result;
 56  
     }
 57  
     
 58  
     public int getTotalMessages(){
 59  0
             int count = 0;
 60  
             try{
 61  0
                 Query query = entityManager.createQuery("SELECT COUNT(m) FROM MessageEntity m");
 62  0
                 count = ((Long)query.getSingleResult()).intValue();
 63  
             }
 64  0
         catch(Exception e){
 65  0
                     logger.error("count query failed.", e);
 66  0
                         throw new MessageException("count query failed.", e);
 67  0
             }
 68  0
         return count;
 69  
     }
 70  
     
 71  
         @SuppressWarnings("unchecked")
 72  
         public List<String> getLocales() {
 73  
                 List<String> locales;
 74  
             try{
 75  2
                 Query query = entityManager.createQuery("SELECT distinct m.locale FROM MessageEntity m");
 76  2
                 locales = (List<String>) query.getResultList();
 77  
             }
 78  0
         catch(Exception e){
 79  0
                     logger.error("getLocales query failed.", e);
 80  0
                         throw new MessageException("getLocales query failed.", e);
 81  2
             }
 82  2
                 return locales;
 83  
         }
 84  
         
 85  
         @SuppressWarnings("unchecked")
 86  
         public List<String> getMessageGroups() {
 87  
                 List<String> groups;
 88  
             try{
 89  2
                 Query query = entityManager.createQuery("SELECT distinct m.groupName FROM MessageEntity m");
 90  2
                 groups = (List<String>) query.getResultList();
 91  
             }
 92  0
         catch(Exception e){
 93  0
                     logger.error("getMessageGroups query failed.", e);
 94  0
                         throw new MessageException("getMessageGroups query failed.", e);
 95  2
             }
 96  2
                 return groups;
 97  
         }
 98  
         
 99  
         public MessageEntity getMessage(String locale, String groupKey, String messageKey) {
 100  
 
 101  10
                 MessageEntity entity = null;
 102  
                 try{
 103  10
                         Query query = entityManager.createQuery(
 104  
                                 "select m from MessageEntity m " +
 105  
                                 "where m.locale = :locale and " +
 106  
                                 "m.groupName = :groupKey and " +
 107  
                                 "m.messageId = :messageKey");
 108  10
                          query.setParameter("locale", locale);
 109  10
                          query.setParameter("groupKey", groupKey);
 110  10
                          query.setParameter("messageKey", messageKey);
 111  
                          
 112  10
                          if(!query.getResultList().isEmpty()){
 113  8
                             entity = (MessageEntity)query.getResultList().get(0);
 114  
                          }else{
 115  2
                             return null;
 116  
                          }
 117  
                 }
 118  0
                 catch(Exception e){
 119  0
                         logger.error("getMessage query failed.", e);
 120  0
                         throw new MessageException("getMessage query failed.", e);
 121  8
                 }
 122  8
                 return entity;
 123  
         }
 124  
         
 125  
 
 126  
         @SuppressWarnings("unchecked")
 127  
         public List<MessageEntity> getMessages(String locale, String groupKey) {
 128  
                 List<MessageEntity> list;
 129  
                 try{
 130  6
                         Query query = entityManager.createQuery(
 131  
                             "select m from MessageEntity m " +
 132  
                             "where m.locale = :locale and " +
 133  
                             "m.groupName = :groupKey ");
 134  6
                          query.setParameter("locale", locale);
 135  6
                          query.setParameter("groupKey", groupKey);
 136  6
                          list = (List<MessageEntity>)query.getResultList();
 137  
                 }
 138  0
                 catch(Exception e){
 139  0
                         logger.error("getMessages query failed.", e);
 140  0
                         throw new MessageException("getMessages query failed.", e);
 141  6
                 }
 142  6
                 return list;
 143  
         }
 144  
         
 145  
         @SuppressWarnings("unchecked")
 146  
         public List<MessageEntity> getMessagesByGroups(String locale, List<String> groupKeys) {
 147  4
                 List<MessageEntity> list = new ArrayList<MessageEntity>();
 148  
                 try{
 149  4
                         for(String currentKey: groupKeys){
 150  8
                                 Query query = entityManager.createQuery(
 151  
                                     "select m from MessageEntity m " +
 152  
                                     "where m.locale = :locale and " +
 153  
                                     "m.groupName = :groupKey ");
 154  8
                                  query.setParameter("locale", locale);
 155  8
                                  query.setParameter("groupKey", currentKey);
 156  8
                                  list.addAll((List<MessageEntity>)query.getResultList());
 157  8
                         }
 158  
                 }
 159  0
                 catch(Exception e){
 160  0
                         logger.error("getMessagesByGroups query failed.", e);
 161  0
                         throw new MessageException("getMessagesByGroups query failed.", e);
 162  4
                 }
 163  4
                 return list;
 164  
         }
 165  
         
 166  
         //assuming all keys are staying the same
 167  
         public MessageEntity updateMessage(String locale, String groupKey, String messageKey, MessageEntity updatedMessage) {
 168  2
                 MessageEntity returnValue = null;
 169  
             try{
 170  2
                     MessageEntity theEntity = this.getMessage(locale, groupKey, messageKey);
 171  2
                 if(theEntity != null){
 172  2
                         theEntity.setValue(updatedMessage.getValue());
 173  2
                         theEntity.setMessageId(updatedMessage.getMessageId());
 174  2
                         theEntity.setLocale(updatedMessage.getLocale());
 175  2
                         theEntity.setGroupName(updatedMessage.getGroupName());
 176  2
                         entityManager.merge(theEntity);
 177  2
                         returnValue = theEntity;
 178  
                 }
 179  
             }
 180  0
         catch(Exception e){
 181  0
                         throw new MessageException("updateMessage query failed.", e);
 182  2
         }
 183  2
         return returnValue;
 184  
         }
 185  
     
 186  
 
 187  
 }