1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.file.dao;
17
18 import org.kuali.mobility.file.entity.File;
19 import org.springframework.stereotype.Repository;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.OptimisticLockException;
23 import javax.persistence.PersistenceContext;
24 import javax.persistence.Query;
25 import java.util.List;
26
27 @Repository
28 public class FileDaoImpl implements FileDao {
29
30 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileDaoImpl.class);
31 private String ME = this.getClass().getName();
32
33 @PersistenceContext
34 private EntityManager entityManager;
35
36 public FileDaoImpl(){}
37
38 public Long saveFile(File file){
39 if(file == null){
40 return null;
41 }
42 try {
43 if(file.getId() == null){
44 entityManager.persist(file);
45 }else{
46 entityManager.merge(file);
47 }
48 } catch (OptimisticLockException e) {
49 return null;
50 }
51 return file.getId();
52 }
53
54 public boolean removeFile(File file){
55 boolean result = true;
56 if(file == null){
57 return false;
58 }
59 if(file.getId() != null){
60 try{
61 File f = entityManager.find(File.class, file.getId());
62 entityManager.remove(f);
63 }catch(Exception e){
64 LOG.info("Exception Caught: " + e.getMessage());
65 result = false;
66 }
67 }
68 return result;
69 }
70
71 @SuppressWarnings("unchecked")
72 public File findFileById(Long Id){
73 Query query = entityManager.createQuery("select f from File f where f.id = " + Id);
74 return (File) query.getSingleResult();
75 }
76
77 @SuppressWarnings("unchecked")
78 public List<File> findFilesByName(String name){
79 Query query = entityManager.createQuery("select f from File f where f.fileName like '" + name + "'");
80 return query.getResultList();
81 }
82
83 @SuppressWarnings("unchecked")
84 public List<File> findAllFiles(){
85
86 Query query = entityManager.createQuery("select f from File f order by f.postedTimestamp desc");
87 return query.getResultList();
88 }
89
90 public EntityManager getEntityManager() {
91 return entityManager;
92 }
93
94 public void setEntityManager(EntityManager entityManager) {
95 this.entityManager = entityManager;
96 }
97 }