1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.dao.impl;
17
18 import org.apache.log4j.Logger;
19 import org.apache.ojb.broker.query.Criteria;
20 import org.apache.ojb.broker.query.QueryByCriteria;
21 import org.apache.ojb.broker.query.QueryFactory;
22 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
23 import org.kuali.rice.kns.service.KNSServiceLocator;
24 import org.kuali.rice.krad.dao.BusinessObjectDao;
25 import org.kuali.rice.krad.dao.DocumentDao;
26 import org.kuali.rice.krad.document.Document;
27 import org.kuali.rice.krad.service.DocumentAdHocService;
28 import org.kuali.rice.krad.service.util.OjbCollectionAware;
29 import org.kuali.rice.krad.service.util.OjbCollectionHelper;
30 import org.kuali.rice.krad.util.KRADPropertyConstants;
31 import org.springframework.dao.DataAccessException;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36
37
38
39
40
41 public class DocumentDaoOjb extends PlatformAwareDaoBaseOjb implements DocumentDao, OjbCollectionAware {
42 private static final Logger LOG = Logger.getLogger(DocumentDaoOjb.class);
43
44 protected BusinessObjectDao businessObjectDao;
45 protected DocumentAdHocService documentAdHocService;
46 private OjbCollectionHelper ojbCollectionHelper;
47
48
49 public DocumentDaoOjb(BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) {
50 super();
51 this.businessObjectDao = businessObjectDao;
52 this.documentAdHocService = documentAdHocService;
53 }
54
55 @Override
56 public <T extends Document> T save(T document) throws DataAccessException {
57 if ( LOG.isDebugEnabled() ) {
58 LOG.debug( "About to store document: " + document, new Throwable() );
59 }
60 Document retrievedDocument = findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
61 getOjbCollectionHelper().processCollections(this, document, retrievedDocument);
62 this.getPersistenceBrokerTemplate().store(document);
63 return document;
64 }
65
66
67
68
69
70
71
72
73 @Override
74 public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
75 List<String> idList = new ArrayList<String>();
76 idList.add(id);
77
78 List<T> documentList = findByDocumentHeaderIds(clazz, idList);
79
80 T document = null;
81 if ((null != documentList) && (documentList.size() > 0)) {
82 document = documentList.get(0);
83 }
84
85 return document;
86 }
87
88
89
90
91
92
93
94
95 @Override
96 public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
97 Criteria criteria = new Criteria();
98 criteria.addIn(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
99
100 QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
101
102
103 @SuppressWarnings("unchecked")
104 List<T> tempList = new ArrayList<T>(this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
105
106 for (T doc : tempList) {
107 documentAdHocService.addAdHocs(doc);
108 }
109 return tempList;
110 }
111
112
113
114
115
116
117 @Override
118 public BusinessObjectDao getBusinessObjectDao() {
119 return businessObjectDao;
120 }
121
122
123
124
125
126 public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
127 this.businessObjectDao = businessObjectDao;
128 }
129
130
131
132
133 @Override
134 public DocumentAdHocService getDocumentAdHocService() {
135 return this.documentAdHocService;
136 }
137
138
139
140
141
142 public void setDocumentAdHocService(DocumentAdHocService dahs) {
143 this.documentAdHocService = dahs;
144 }
145
146 protected OjbCollectionHelper getOjbCollectionHelper() {
147 if (ojbCollectionHelper == null) {
148 ojbCollectionHelper = KNSServiceLocator.getOjbCollectionHelper();
149 }
150
151 return ojbCollectionHelper;
152 }
153
154 public void setOjbCollectionHelper(OjbCollectionHelper ojbCollectionHelper) {
155 this.ojbCollectionHelper = ojbCollectionHelper;
156 }
157 }