View Javadoc
1   /**
2    * Copyright 2011-2013 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.academics.dao;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  import org.kuali.mobility.academics.entity.GradesPostedNotice;
21  import org.springframework.context.ApplicationContext;
22  import org.springframework.context.ApplicationContextAware;
23  import org.springframework.stereotype.Repository;
24  import org.springframework.transaction.annotation.Transactional;
25  
26  import javax.annotation.Resource;
27  import javax.persistence.*;
28  import java.sql.Timestamp;
29  import java.util.Date;
30  import java.util.List;
31  import java.util.Properties;
32  
33  /**
34   * @author Kuali Mobility Team (mobility.dev@kuali.org)
35   * @since 2.3.0
36   */
37  @Repository
38  public class GradesPostedNoticeDaoImpl implements GradesPostedNoticeDao, ApplicationContextAware {
39  
40  	private static final Logger LOG = LoggerFactory.getLogger(GradesPostedNoticeDaoImpl.class);
41  	private ApplicationContext applicationContext;
42  
43  	@PersistenceContext
44  	private EntityManager entityManager;
45  
46  	@Resource(name = "kmeProperties")
47  	private Properties kmeProperties;
48  
49  	@Resource(name = "academicsProperties")
50  	private Properties academicsProperties;
51  
52  	@SuppressWarnings("unchecked")
53  	@Transactional
54  	public boolean uploadGradesPostedNotice(List<String> gradereadylist) {
55  		LOG.debug("BEGIN: uploadGradesPostedNotice.");
56  		boolean response = true;
57  
58  		if (gradereadylist == null || gradereadylist.isEmpty()) {
59  			response = false;
60  		} else {
61  			try {
62  				LOG.debug("uploadGradesPostedNotice displaying size:" + gradereadylist.size());
63  				for (String s : gradereadylist) {
64  					LOG.debug(s);
65  					GradesPostedNotice gradespostednotice = (GradesPostedNotice) getApplicationContext().getBean("academicsGradesPostedNoticeBean");
66  					gradespostednotice.setLoginName(s);
67  					gradespostednotice.setInProcess(false);
68  					gradespostednotice.setTimestampProcessed(null);
69  					Date date = new Date();
70  					gradespostednotice.setTimestampReceived(new Timestamp(date.getTime()));
71  					if (null == saveGradesPostedNotice(gradespostednotice)) {
72  						LOG.debug("uploadGradesPostedNotice: save failed");
73  						response = false;
74  					}
75  				}
76  			} catch (OptimisticLockException oe) {
77  				LOG.error("Unable to save grade posted notice data.", oe);
78  				response = false;
79  			}
80  			LOG.debug("END: uploadGradesPostedNotice.");
81  		}
82  		return response;
83  	}
84  
85  	@Override
86  	@Transactional
87  	public Long saveGradesPostedNotice(GradesPostedNotice gradespostednotice) {
88  		Long id = null;
89  		if (gradespostednotice == null || gradespostednotice.getLoginName() == null || gradespostednotice.getLoginName().trim().isEmpty()) {
90  			id = new Long(-1);
91  		} else {
92  			LOG.debug("BEGIN: Save grades posted notice data for " + gradespostednotice.getLoginName());
93  			try {
94  				if (gradespostednotice.getId() == null) {
95  					LOG.debug("in persist");
96  					LOG.debug(gradespostednotice.getLoginName());
97  					LOG.debug(gradespostednotice.getTimestampReceived().toString());
98  					getEntityManager().persist(gradespostednotice);
99  				} 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 }