1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.service.impl;
17
18 import org.joda.time.DateTime;
19 import org.kuali.rice.core.api.config.module.RunMode;
20 import org.kuali.rice.kew.api.KewApiConstants;
21 import org.kuali.rice.kew.api.KewApiServiceLocator;
22 import org.kuali.rice.kew.api.doctype.DocumentTypeService;
23 import org.kuali.rice.kew.api.document.Document;
24 import org.kuali.rice.kew.api.document.DocumentStatus;
25 import org.kuali.rice.kew.docsearch.DocumentSearchCriteriaEbo;
26 import org.kuali.rice.kew.doctype.bo.DocumentType;
27 import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
28 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
29 import org.kuali.rice.krad.service.impl.ModuleServiceBase;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Map;
34
35
36
37
38
39
40
41 public class KEWModuleService extends ModuleServiceBase {
42
43 protected DocumentTypeService docTypeService = null;
44
45
46
47
48
49
50
51 @Override
52 public List<String> listPrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
53 if ( DocumentTypeEBO.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
54 List<String> pkFields = new ArrayList<String>( 1 );
55 pkFields.add( "documentTypeId" );
56 return pkFields;
57 }else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectInterfaceClass )){
58 List<String> pkFields = new ArrayList<String>( 1 );
59 pkFields.add( "documentId" );
60 return pkFields;
61 }
62 return super.listPrimaryKeyFieldNames(businessObjectInterfaceClass);
63 }
64
65
66
67
68
69
70
71 @Override
72 public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(
73 Class<T> businessObjectClass, Map<String, Object> fieldValues) {
74
75 if(DocumentTypeEBO.class.isAssignableFrom(businessObjectClass)){
76
77 org.kuali.rice.kew.api.doctype.DocumentType fetchedDocumentType = null;
78
79 if ( fieldValues.containsKey( "name" ) ) {
80 fetchedDocumentType = getDocumentTypeService().getDocumentTypeByName((String) fieldValues.get("name"));
81 }else if( fieldValues.containsKey( "documentTypeId" ) ){
82 fetchedDocumentType = getDocumentTypeService().getDocumentTypeById(fieldValues.get("documentTypeId").toString());
83 }else if (fieldValues.containsKey( "id" ) ) {
84
85 fetchedDocumentType = getDocumentTypeService().getDocumentTypeById(fieldValues.get("id").toString());
86 }
87
88 if (fetchedDocumentType != null) {
89
90 return (T) DocumentType.from(fetchedDocumentType);
91 } else {
92 return null;
93 }
94
95 }else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectClass )){
96 if ( fieldValues.containsKey( "documentId" ) ) {
97 return (T)createDocumentSearchEbo(KewApiServiceLocator.getWorkflowDocumentService().getDocument(
98 fieldValues.get("documentId").toString()));
99 }
100
101 }
102
103
104 return super.getExternalizableBusinessObject(businessObjectClass, fieldValues);
105 }
106
107
108
109
110 protected synchronized DocumentTypeService getDocumentTypeService() {
111 if(this.docTypeService == null){
112
113 this.docTypeService = KewApiServiceLocator.getDocumentTypeService();
114 }
115 return this.docTypeService;
116 }
117
118
119
120
121 public synchronized void setDocumentTypeService(DocumentTypeService docTypeService) {
122 this.docTypeService = docTypeService;
123 }
124
125 private DocumentSearchCriteriaEbo createDocumentSearchEbo(final Document doc){
126 return new DocumentSearchCriteriaEbo(){
127
128 @Override
129 public String getApplicationDocumentId() {
130 return doc.getApplicationDocumentId();
131 }
132
133 @Override
134 public DocumentStatus getStatus() {
135 return doc.getStatus();
136 }
137
138 @Override
139 public String getApplicationDocumentStatus() {
140 return doc.getApplicationDocumentStatus();
141 }
142
143 @Override
144 public String getTitle() {
145 return doc.getTitle();
146 }
147
148 @Override
149 public String getDocumentTypeName() {
150 return doc.getDocumentTypeName();
151 }
152
153 @Override
154 public String getInitiatorPrincipalId() {
155 return doc.getInitiatorPrincipalId();
156 }
157
158 @Override
159 public String getDocumentId() {
160 return doc.getDocumentId();
161 }
162
163 @Override
164 public DateTime getDateCreated() {
165 return doc.getDateCreated();
166 }
167
168 @Override
169 public void refresh() {
170
171 }
172
173 };
174 }
175
176
177
178
179
180 @Override
181 public String getExternalizableBusinessObjectInquiryUrl(
182 Class inquiryBusinessObjectClass, Map<String, String[]> parameters) {
183 if ( DocumentTypeEBO.class.isAssignableFrom( inquiryBusinessObjectClass ) ) {
184 int nonBlank = 0;
185 boolean nameFound = false;
186
187 for(String key: parameters.keySet()){
188 if("name".equals(key) && parameters.get(key) != null){
189 nameFound=true;
190 }else if(!"name".equals(key) && parameters.get(key) != null){
191 nonBlank ++;
192 }
193 }
194
195 if(nonBlank == 0 && nameFound == true){
196 parameters.clear();
197 DocumentTypeEBO dte = (DocumentTypeEBO) DocumentType.from(getDocumentTypeService().getDocumentTypeByName(parameters.get( "name" )[0] ));
198 String[] strArr = {dte.getDocumentTypeId().toString()};
199 parameters.put("documentTypeId", strArr);
200 }
201
202 }
203
204 return super.getExternalizableBusinessObjectInquiryUrl(
205 inquiryBusinessObjectClass, parameters);
206 }
207
208
209
210
211
212
213 public List<List<String>> listAlternatePrimaryKeyFieldNames(
214 Class businessObjectInterfaceClass) {
215 if ( DocumentTypeEBO.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
216 ArrayList<List<String>> retList = new ArrayList<List<String>>();
217 ArrayList<String> keyList = new ArrayList<String>();
218
219 keyList.add("name");
220 retList.add(keyList);
221 return retList;
222 }else{
223 return null;
224 }
225
226 }
227
228 @Override
229 public boolean goToCentralRiceForInquiry() {
230 RunMode runMode = getRunMode(KewApiConstants.Namespaces.MODULE_NAME);
231
232 if (RunMode.EMBEDDED.equals(runMode)) {
233 return true;
234 } else {
235 return false;
236 }
237 }
238 }
239