001/** 002 * Copyright 2011-2013 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.academics.dao; 017 018import org.slf4j.Logger; 019import org.slf4j.LoggerFactory; 020import org.kuali.mobility.academics.entity.GradesPostedNotice; 021import org.springframework.context.ApplicationContext; 022import org.springframework.context.ApplicationContextAware; 023import org.springframework.stereotype.Repository; 024import org.springframework.transaction.annotation.Transactional; 025 026import javax.annotation.Resource; 027import javax.persistence.*; 028import java.sql.Timestamp; 029import java.util.Date; 030import java.util.List; 031import java.util.Properties; 032 033/** 034 * @author Kuali Mobility Team (mobility.dev@kuali.org) 035 * @since 2.3.0 036 */ 037@Repository 038public class GradesPostedNoticeDaoImpl implements GradesPostedNoticeDao, ApplicationContextAware { 039 040 private static final Logger LOG = LoggerFactory.getLogger(GradesPostedNoticeDaoImpl.class); 041 private ApplicationContext applicationContext; 042 043 @PersistenceContext 044 private EntityManager entityManager; 045 046 @Resource(name = "kmeProperties") 047 private Properties kmeProperties; 048 049 @Resource(name = "academicsProperties") 050 private Properties academicsProperties; 051 052 @SuppressWarnings("unchecked") 053 @Transactional 054 public boolean uploadGradesPostedNotice(List<String> gradereadylist) { 055 LOG.debug("BEGIN: uploadGradesPostedNotice."); 056 boolean response = true; 057 058 if (gradereadylist == null || gradereadylist.isEmpty()) { 059 response = false; 060 } else { 061 try { 062 LOG.debug("uploadGradesPostedNotice displaying size:" + gradereadylist.size()); 063 for (String s : gradereadylist) { 064 LOG.debug(s); 065 GradesPostedNotice gradespostednotice = (GradesPostedNotice) getApplicationContext().getBean("academicsGradesPostedNoticeBean"); 066 gradespostednotice.setLoginName(s); 067 gradespostednotice.setInProcess(false); 068 gradespostednotice.setTimestampProcessed(null); 069 Date date = new Date(); 070 gradespostednotice.setTimestampReceived(new Timestamp(date.getTime())); 071 if (null == saveGradesPostedNotice(gradespostednotice)) { 072 LOG.debug("uploadGradesPostedNotice: save failed"); 073 response = false; 074 } 075 } 076 } catch (OptimisticLockException oe) { 077 LOG.error("Unable to save grade posted notice data.", oe); 078 response = false; 079 } 080 LOG.debug("END: uploadGradesPostedNotice."); 081 } 082 return response; 083 } 084 085 @Override 086 @Transactional 087 public Long saveGradesPostedNotice(GradesPostedNotice gradespostednotice) { 088 Long id = null; 089 if (gradespostednotice == null || gradespostednotice.getLoginName() == null || gradespostednotice.getLoginName().trim().isEmpty()) { 090 id = new Long(-1); 091 } else { 092 LOG.debug("BEGIN: Save grades posted notice data for " + gradespostednotice.getLoginName()); 093 try { 094 if (gradespostednotice.getId() == null) { 095 LOG.debug("in persist"); 096 LOG.debug(gradespostednotice.getLoginName()); 097 LOG.debug(gradespostednotice.getTimestampReceived().toString()); 098 getEntityManager().persist(gradespostednotice); 099 } else { 100 getEntityManager().merge(gradespostednotice); 101 } 102 id = gradespostednotice.getId(); 103 } catch (OptimisticLockException oe) { 104 LOG.error("Unable to save grade alerts posted notice for " + gradespostednotice.getLoginName(), oe); 105 id = new Long(-1); 106 } 107 LOG.debug("END: Save grades posted notice data " + gradespostednotice.getLoginName()); 108 } 109 return id; 110 } 111 112 @Override 113 public GradesPostedNotice loadGradesPostedNotice(Long id) { 114 LOG.debug("Looking up grade notice for "+id); 115 Query query = getEntityManager().createNamedQuery("GradesPostedNotice.findById"); 116 query.setParameter("id",id); 117 return (GradesPostedNotice)query.getSingleResult(); 118 } 119 120 @Override 121 public Long countUnsentGradeNotices() { 122 Query query = getEntityManager().createNamedQuery("GradesPostedNotice.countUnsentNotices"); 123 Long count = (Long) query.getSingleResult(); 124 return count; 125 } 126 127 @Transactional 128 public List<? extends GradesPostedNotice> getGradesToProcess(boolean getAll) { 129 List<GradesPostedNotice> notices = null; 130 try { 131 Query query = getEntityManager().createNamedQuery("GradesPostedNotice.getUnsent"); 132 if (!getAll) { 133 query.setMaxResults(Integer.parseInt(getAcademicsProperties().getProperty("academics.grade.alert.batch.size", "10"))); 134 } 135 query.setLockMode(LockModeType.PESSIMISTIC_WRITE); 136 notices = query.getResultList(); 137 138 for( GradesPostedNotice notice : notices ) { 139 notice.setInProcess(true); 140 getEntityManager().merge(notice); 141 } 142 143 144 } 145 catch (OptimisticLockException oe) { 146 LOG.error( oe.toString() ); 147 } 148 catch (Exception e) { 149 LOG.error( e.toString() ); 150 } 151 152 return notices; 153 } 154 155 /** 156 * @return the applicationContext 157 */ 158 public ApplicationContext getApplicationContext() { 159 return applicationContext; 160 } 161 162 /** 163 * @param applicationContext the applicationContext to set 164 */ 165 public void setApplicationContext(ApplicationContext applicationContext) { 166 this.applicationContext = applicationContext; 167 } 168 169 public EntityManager getEntityManager() { 170 return entityManager; 171 } 172 173 public void setEntityManager(EntityManager entityManager) { 174 this.entityManager = entityManager; 175 } 176 177 public Properties getKmeProperties() { 178 return kmeProperties; 179 } 180 181 public void setKmeProperties(Properties kmeProperties) { 182 this.kmeProperties = kmeProperties; 183 } 184 185 public Properties getAcademicsProperties() { 186 return academicsProperties; 187 } 188 189 public void setAcademicsProperties(Properties academicsProperties) { 190 this.academicsProperties = academicsProperties; 191 } 192}