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.reporting.dao;
017
018import java.util.List;
019
020import javax.persistence.EntityManager;
021import javax.persistence.NoResultException;
022import javax.persistence.OptimisticLockException;
023import javax.persistence.PersistenceContext;
024import javax.persistence.Query;
025
026import org.kuali.mobility.file.entity.File;
027
028//import org.kuali.mobility.reporting.entity.File;
029import org.kuali.mobility.reporting.entity.Submission;
030import org.springframework.stereotype.Repository;
031
032/**
033 * @author Kuali Mobility Team (mobility.collab@kuali.org)
034 */
035@Repository
036public class ReportingDaoImpl implements ReportingDao {
037
038        @PersistenceContext
039        private EntityManager entityManager;
040
041        @SuppressWarnings("unchecked")
042        public List<Submission> findAllSubmissions() {
043                try {
044                        Query query = entityManager.createQuery("select s from Submission s");
045                        return query.getResultList();
046                } catch (NoResultException e) {
047                        return null;
048                }
049        }
050
051        @SuppressWarnings("unchecked")
052        public List<Submission> findAllSubmissionsByParentId(Long id) {
053                try {
054                        Query query = entityManager.createQuery("select s from Submission s where s.parentId = :id or s.id = :id order by s.revisionNumber desc");
055                        query.setParameter("id", id);
056                        return query.getResultList();
057                } catch (NoResultException e) {
058                        return null;
059                }
060        }
061
062        public Submission findSubmissionById(Long id) {
063                try {
064                        Query query = entityManager.createQuery("select s from Submission s where s.id = :id");
065                        query.setParameter("id", id);
066                        return (Submission) query.getSingleResult();
067                } catch (Exception e) {
068                        return null;
069                }
070        }
071
072        public Long saveSubmission(Submission submission) {
073                if (submission == null) {
074                        return null;
075                }
076                try {
077                        if (submission.getId() == null) {
078                                entityManager.persist(submission);
079                        } else {
080                                entityManager.merge(submission);
081                        }
082                } catch (OptimisticLockException oe) {
083                        return null;
084                }
085                return submission.getId();
086        }
087
088        public Long saveAttachment(File file) {
089                if (file == null) {
090                        return null;
091                }
092                try {
093                        if (file.getId() == null) {
094                                entityManager.persist(file);
095                        } else {
096                                entityManager.merge(file);
097                        }
098                } catch (OptimisticLockException oe) {
099                        return null;
100                }
101                return file.getId();
102        }
103
104        public EntityManager getEntityManager() {
105                return entityManager;
106        }
107
108        public void setEntityManager(EntityManager entityManager) {
109                this.entityManager = entityManager;
110        }
111
112}