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.krad.dao.BusinessObjectDao;
24 import org.kuali.rice.krad.dao.DocumentDao;
25 import org.kuali.rice.krad.document.Document;
26 import org.kuali.rice.krad.service.DocumentAdHocService;
27 import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
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
56
57
58 @Override
59 public <T extends Document> T save(T document) throws DataAccessException {
60 if ( LOG.isDebugEnabled() ) {
61 LOG.debug( "About to store document: " + document, new Throwable() );
62 }
63 Document retrievedDocument = findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
64 getOjbCollectionHelper().processCollections(this, document, retrievedDocument);
65 this.getPersistenceBrokerTemplate().store(document);
66 return document;
67 }
68
69
70
71
72
73
74
75
76 @Override
77 public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
78 List<String> idList = new ArrayList<String>();
79 idList.add(id);
80
81 List<T> documentList = findByDocumentHeaderIds(clazz, idList);
82
83 T document = null;
84 if ((null != documentList) && (documentList.size() > 0)) {
85 document = documentList.get(0);
86 }
87
88 return document;
89 }
90
91
92
93
94
95
96
97
98 @Override
99 public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
100 Criteria criteria = new Criteria();
101 criteria.addIn(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
102
103 QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
104
105
106 @SuppressWarnings("unchecked")
107 List<T> tempList = new ArrayList<T>(this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
108
109 for (T doc : tempList) {
110 documentAdHocService.addAdHocs(doc);
111 }
112 return tempList;
113 }
114
115
116
117
118
119
120 @Override
121 public BusinessObjectDao getBusinessObjectDao() {
122 return businessObjectDao;
123 }
124
125
126
127
128
129 public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
130 this.businessObjectDao = businessObjectDao;
131 }
132
133
134
135
136 @Override
137 public DocumentAdHocService getDocumentAdHocService() {
138 return this.documentAdHocService;
139 }
140
141
142
143
144
145 public void setDocumentAdHocService(DocumentAdHocService dahs) {
146 this.documentAdHocService = dahs;
147 }
148
149 protected OjbCollectionHelper getOjbCollectionHelper() {
150 if (ojbCollectionHelper == null) {
151 ojbCollectionHelper = KRADServiceLocatorInternal.getOjbCollectionHelper();
152 }
153
154 return ojbCollectionHelper;
155 }
156
157 public void setOjbCollectionHelper(OjbCollectionHelper ojbCollectionHelper) {
158 this.ojbCollectionHelper = ojbCollectionHelper;
159 }
160 }