1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.kuali.rice.kew.edl.extract.dao.impl;
19
20 import java.util.List;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.NoResultException;
24 import javax.persistence.PersistenceContext;
25
26 import org.kuali.rice.core.jpa.criteria.Criteria;
27 import org.kuali.rice.core.jpa.criteria.QueryByCriteria;
28 import org.kuali.rice.core.util.OrmUtils;
29 import org.kuali.rice.kew.edl.extract.Dump;
30 import org.kuali.rice.kew.edl.extract.Fields;
31 import org.kuali.rice.kew.edl.extract.dao.ExtractDAO;
32 import org.kuali.rice.kew.notes.Note;
33
34 public class ExtractDAOJpaImpl implements ExtractDAO {
35
36 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExtractDAOJpaImpl.class);
37
38 @PersistenceContext(unitName = "kew-unit")
39 private EntityManager entityManager;
40
41 public Dump getDumpByRouteHeaderId(Long docId) {
42 LOG.debug("finding Document Extract by routeHeaderId " + docId);
43 Criteria crit = new Criteria(Dump.class.getName());
44 crit.eq("docId", docId);
45 try {
46 return (Dump) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
47 } catch (NoResultException e) {
48 return null;
49 }
50 }
51
52 public List<Fields> getFieldsByRouteHeaderId(Long docId) {
53 LOG.debug("finding Extract Fileds by routeHeaderId " + docId);
54 Criteria crit = new Criteria(Fields.class.getName());
55 crit.eq("routeHeaderId", docId);
56 crit.orderBy("docId", true);
57
58 return (List<Fields>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
59 }
60
61 public void saveDump(Dump dump) {
62 LOG.debug("check for null values in Extract document");
63 checkNull(dump.getDocId(), "Document ID");
64 checkNull(dump.getDocCreationDate(), "Creation Date");
65 checkNull(dump.getDocCurrentNodeName(), "Current Node Name");
66 checkNull(dump.getDocModificationDate(), "Modification Date");
67 checkNull(dump.getDocRouteStatusCode(), "Route Status Code");
68 checkNull(dump.getDocInitiatorId(), "Initiator ID");
69 checkNull(dump.getDocTypeName(), "Doc Type Name");
70 LOG.debug("saving EDocLite document: routeHeader " + dump.getDocId());
71 if (dump.getDocId() == null) {
72 entityManager.persist(dump);
73 } else {
74 OrmUtils.merge(entityManager, dump);
75 }
76 }
77
78 public void saveField(Fields field) {
79 LOG.debug("saving EDocLite Extract fields");
80 checkNull(field.getDocId(), "Document ID");
81 checkNull(field.getFieldValue(), "Field Value");
82 checkNull(field.getFiledName(), "Field Name");
83 LOG.debug("saving Fields: routeHeader " + field.getFieldId());
84
85 if (field.getFieldId() == null) {
86 entityManager.persist(field);
87 } else {
88 OrmUtils.merge(entityManager, field);
89 }
90 }
91
92 private void checkNull(Object value, String valueName) throws RuntimeException {
93 if (value == null) {
94 throw new RuntimeException("Null value for " + valueName);
95 }
96 }
97
98 public void deleteDump(Long routeHeaderId) {
99 LOG.debug("deleting record form Extract Dump table");
100 entityManager.remove(entityManager.find(Note.class, routeHeaderId));
101 }
102
103 public EntityManager getEntityManager() {
104 return this.entityManager;
105 }
106
107 public void setEntityManager(EntityManager entityManager) {
108 this.entityManager = entityManager;
109 }
110 }