001/** 002 * Copyright 2011 The Kuali Foundation Licensed under the 003 * Educational Community License, Version 2.0 (the "License"); you may 004 * not use this file except in compliance with the License. You may 005 * obtain a copy of the License at 006 * 007 * http://www.osedu.org/licenses/ECL-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, 010 * software distributed under the License is distributed on an "AS IS" 011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 012 * or implied. See the License for the specific language governing 013 * permissions and limitations under the License. 014 */ 015 016package org.kuali.mobility.news.dao; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import javax.persistence.EntityManager; 022import javax.persistence.OptimisticLockException; 023import javax.persistence.PersistenceContext; 024import javax.persistence.Query; 025 026import org.apache.log4j.Logger; 027import org.kuali.mobility.news.entity.NewsSource; 028import org.kuali.mobility.util.mapper.DataMapperImpl; 029import org.springframework.context.ApplicationContext; 030import org.springframework.stereotype.Repository; 031import org.springframework.transaction.annotation.Transactional; 032 033/** 034 * DAO for actually persisting and retrieving NewsSource objects 035 * 036 * @author Kuali Mobility Team (mobility.dev@kuali.org) 037 * @see org.kuali.mobility.news.dao.NewsDao 038 */ 039@Repository 040public class NewsDaoDBImpl implements NewsDao { 041 042 public static Logger LOG = Logger.getLogger( NewsDaoDBImpl.class ); 043 044 private ApplicationContext applicationContext; 045 private NewsCache cache; 046 047 @PersistenceContext 048 private EntityManager entityManager; 049 050 @SuppressWarnings("unchecked") 051 @Override 052 @Transactional 053 public List<NewsSource> findAllActiveNewsSources() { 054 Query query = entityManager.createQuery("select s from NewsSource s where s.active = :active order by s.order"); 055 query.setParameter("active", true); 056 try { 057 return query.getResultList(); 058 } catch (Exception e) { 059 return new ArrayList<NewsSource>(); 060 } 061 } 062 063 @SuppressWarnings("unchecked") 064 @Override 065 @Transactional 066 public List<NewsSource> findAllNewsSources() { 067 Query query = entityManager.createQuery("select s from NewsSource s order by s.order"); 068 try { 069 return query.getResultList(); 070 } catch (Exception e) { 071 return new ArrayList<NewsSource>(); 072 } 073 } 074 075 @Override 076 @Transactional 077 public NewsSource lookup(Long id) { 078 Query query = entityManager.createQuery("select s from NewsSource s where s.id = :id"); 079 query.setParameter("id", id); 080 try { 081 NewsSource s = (NewsSource)query.getSingleResult(); 082 return s; 083 } catch (Exception e) { 084 return null; 085 } 086 } 087 088 @Override 089 @Transactional 090 public NewsSource save(NewsSource newsSource) { 091 if (newsSource == null) { 092 return null; 093 } 094 try { 095 if (newsSource.getId() == null) { 096 entityManager.persist(newsSource); 097 } else { 098 entityManager.merge(newsSource); 099 } 100 } catch (OptimisticLockException oe) { 101 return null; 102 } 103 return newsSource; 104 } 105 106 @Override 107 @Transactional 108 public NewsSource delete(NewsSource newsSource) { 109 Query query = entityManager.createQuery("delete from NewsSource ns where ns.id = :id"); 110 query.setParameter("id", newsSource.getId()); 111 query.executeUpdate(); 112 return newsSource; 113 } 114 115 public ApplicationContext getApplicationContext() { 116 return applicationContext; 117 } 118 119 public void setApplicationContext(ApplicationContext applicationContext) { 120 this.applicationContext = applicationContext; 121 } 122 123 public NewsCache getCache() { 124 return cache; 125 } 126 127 public void setCache(NewsCache cache) { 128 this.cache = cache; 129 } 130 131 public EntityManager getEntityManager() { 132 return entityManager; 133 } 134 135 public void setEntityManager(EntityManager entityManager) { 136 this.entityManager = entityManager; 137 } 138 139 @Override 140 public List<NewsSource> findNewsSources(Long parentId, Boolean isActive) { 141 // TODO Auto-generated method stub 142 return null; 143 } 144 145 @Override 146 public List<NewsSource> findAllActiveNewsSources(Long parentId) { 147 // TODO Auto-generated method stub 148 return null; 149 } 150 151 @Override 152 public List<NewsSource> findAllNewsSources(Long parentId) { 153 // TODO Auto-generated method stub 154 return null; 155 } 156 157}