View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * The ModuleService for KEW
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   *
40   */
41  public class KEWModuleService extends ModuleServiceBase {
42  
43  	protected DocumentTypeService docTypeService = null;
44  
45  	/**
46  	 * These are the "primary" keys for the DocTypeService. We are considering both
47  	 * name and documentTypeId to be unique.
48  	 *
49  	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#listPrimaryKeyFieldNames(java.lang.Class)
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  	 * This overridden method calls the DocumentTypeService instead of the underlying
67  	 * KNS service.  Allows you to search on name and docTypeId
68  	 *
69  	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
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  				// assume it's a string and convert it to a long.
85                  fetchedDocumentType = getDocumentTypeService().getDocumentTypeById(fieldValues.get("id").toString());
86  			}
87  
88              if (fetchedDocumentType != null) {
89                  // convert to EBO
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 		// otherwise, use the default implementation
104 		return super.getExternalizableBusinessObject(businessObjectClass, fieldValues);
105 	}
106 
107 	/**
108 	 * @return the docTypeService
109 	 */
110 	protected synchronized DocumentTypeService getDocumentTypeService() {
111 		if(this.docTypeService == null){
112 			// the default
113 			this.docTypeService = KewApiServiceLocator.getDocumentTypeService();
114 		}
115 		return this.docTypeService;
116 	}
117 
118 	/**
119 	 * @param docTypeService the docTypeService to set
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                 // do nothing
171             }
172             
173         };
174 	}
175 	/**
176 	 * This overridden method rewrites the URL.
177 	 *
178 	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObjectInquiryUrl(java.lang.Class, java.util.Map)
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 			//"name" is the only non-blank property passed in
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(); // clear out other parameters, including the name pass in
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 	 * We want to be able to use name as an alternate key
210 	 *
211 	 * @see org.kuali.rice.krad.service.ModuleService#listAlternatePrimaryKeyFieldNames(java.lang.Class)
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