Coverage Report - org.kuali.mobility.admin.dao.AdminDaoImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AdminDaoImpl
0%
0/70
0%
0/20
3.167
 
 1  
 /**
 2  
  * Copyright 2011 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.mobility.admin.dao;
 17  
 
 18  
 import java.util.List;
 19  
 
 20  
 import javax.persistence.EntityManager;
 21  
 import javax.persistence.OptimisticLockException;
 22  
 import javax.persistence.PersistenceContext;
 23  
 import javax.persistence.Query;
 24  
 
 25  
 import org.kuali.mobility.admin.entity.HomeScreen;
 26  
 import org.kuali.mobility.admin.entity.HomeTool;
 27  
 import org.kuali.mobility.admin.entity.Tool;
 28  
 import org.springframework.stereotype.Repository;
 29  
 
 30  
 /**
 31  
  * The DAO for actually performing administrative tasks on the data store.
 32  
  * @author Kuali Mobility Team (moblitiy.collab@kuali.org)
 33  
  *
 34  
  */
 35  
 @Repository
 36  0
 public class AdminDaoImpl implements AdminDao {
 37  
 
 38  
     @PersistenceContext
 39  
     private EntityManager entityManager;
 40  
 
 41  
     public EntityManager getEntityManager() {
 42  0
         return entityManager;
 43  
     }
 44  
 
 45  
     public void setEntityManager(EntityManager entityManager) {
 46  0
         this.entityManager = entityManager;
 47  0
     }
 48  
 
 49  
     @SuppressWarnings("unchecked")
 50  
         public List<HomeScreen> getAllHomeScreens(){
 51  0
             Query query = entityManager.createQuery("select h from HomeScreen h");
 52  
         try { 
 53  0
                 return query.getResultList();
 54  0
         } catch (Exception e) {                
 55  0
                 return null;
 56  
         }
 57  
     }
 58  
     
 59  
     @Override
 60  
         public HomeScreen getHomeScreenById(long homeScreenId) {
 61  0
                 Query query = entityManager.createQuery("select h from HomeScreen h where h.homeScreenId = :id");
 62  0
         query.setParameter("id", homeScreenId);
 63  
         try {
 64  0
                 return (HomeScreen) query.getSingleResult();
 65  0
         } catch (Exception e) {
 66  0
                 return null;
 67  
         }
 68  
         }
 69  
     
 70  
     @Override
 71  
         public HomeScreen getHomeScreenByAlias(String alias) {
 72  0
                 Query query = entityManager.createQuery("select h from HomeScreen h where h.alias = :alias");
 73  0
         query.setParameter("alias", alias);
 74  
         try {
 75  0
                 return (HomeScreen) query.getSingleResult();
 76  0
         } catch (Exception e) {
 77  0
                 return null;
 78  
         }
 79  
         }
 80  
 
 81  
         @Override
 82  
         public Long saveHomeScreen(HomeScreen homeScreen) {
 83  0
                 if (homeScreen == null) {
 84  0
             return null;
 85  
         }
 86  0
         if (homeScreen.getAlias() != null) {
 87  0
                 homeScreen.setAlias(homeScreen.getAlias().trim());
 88  
         }
 89  0
         for (HomeTool ht : homeScreen.getHomeTools()) {
 90  0
                 ht.setHomeScreen(homeScreen);
 91  
         }
 92  
         try {
 93  0
                 if (homeScreen.getHomeScreenId() == null) {
 94  0
                     entityManager.persist(homeScreen);
 95  
                 } else {
 96  0
                         deleteHomeToolsByHomeScreenId(homeScreen.getHomeScreenId());
 97  0
                     entityManager.merge(homeScreen);
 98  
                 }
 99  0
         } catch (OptimisticLockException oe) {
 100  0
             return null;
 101  0
         }
 102  0
         return homeScreen.getHomeScreenId();
 103  
         }
 104  
         
 105  
         /**
 106  
          * Deletes all the HomeTool objects associated with the given HomeScreen.  This effectively removes all Tool associations from a HomeScreen.
 107  
          * @param homeScreenId
 108  
          */
 109  
         private void deleteHomeToolsByHomeScreenId(long homeScreenId) {
 110  0
                 Query query = entityManager.createQuery("delete from HomeTool ht where ht.homeScreenId = :id");
 111  0
         query.setParameter("id", homeScreenId);
 112  0
         query.executeUpdate();
 113  0
         }
 114  
 
 115  
         @Override
 116  
         public void deleteHomeScreenById(long homeScreenId) {
 117  0
                 Query query = entityManager.createQuery("delete from HomeScreen h where h.homeScreenId = :id");
 118  0
         query.setParameter("id", homeScreenId);
 119  0
         query.executeUpdate();
 120  0
         }
 121  
     
 122  
     @SuppressWarnings("unchecked")
 123  
         public List<Tool> getAllTools(){
 124  0
             Query query = entityManager.createQuery("select t from Tool t");
 125  
         try { 
 126  0
                 return query.getResultList();
 127  0
         } catch (Exception e) {                
 128  0
                 return null;
 129  
         }
 130  
     }
 131  
     
 132  
     public Tool getToolById(long toolId) {
 133  0
             Query query = entityManager.createQuery("select t from Tool t where t.toolId = :id");
 134  0
         query.setParameter("id", toolId);
 135  
         try {
 136  0
                 return (Tool) query.getSingleResult();
 137  0
         } catch (Exception e) {
 138  0
                 return null;
 139  
         }
 140  
     }
 141  
     
 142  
     public Long saveTool(Tool tool) {
 143  0
         if (tool == null) {
 144  0
             return null;
 145  
         }
 146  0
         if (tool.getTitle() != null) {
 147  0
                 tool.setTitle(tool.getTitle().trim());
 148  
         }
 149  0
         if (tool.getUrl() != null) {
 150  0
                 tool.setUrl(tool.getUrl().trim());
 151  
         }
 152  0
         if (tool.getIconUrl() != null) {
 153  0
                 tool.setIconUrl(tool.getIconUrl().trim());
 154  
         }
 155  0
         if (tool.getDescription() != null) {
 156  0
                 tool.setDescription(tool.getDescription().trim());
 157  
         }
 158  
         try {
 159  0
                 if (tool.getToolId() == null) {
 160  0
                     entityManager.persist(tool);
 161  
                 } else {
 162  0
                     entityManager.merge(tool);
 163  
                 }
 164  0
         } catch (OptimisticLockException oe) {
 165  0
             return null;
 166  0
         }
 167  0
         return tool.getToolId();
 168  
     }
 169  
     
 170  
     public void deleteToolById(Long toolId) {
 171  0
         Query query = entityManager.createQuery("delete from Tool t where t.toolId = :toolId");
 172  0
         query.setParameter("toolId", toolId);
 173  0
         query.executeUpdate();
 174  0
     }
 175  
 }