View Javadoc

1   /**
2    * Copyright 2005-2012 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.kew.api.KewApiServiceLocator;
20  import org.kuali.rice.kew.api.doctype.DocumentTypeService;
21  import org.kuali.rice.kew.api.document.Document;
22  import org.kuali.rice.kew.api.document.DocumentStatus;
23  import org.kuali.rice.kew.docsearch.DocumentSearchCriteriaEbo;
24  import org.kuali.rice.kew.doctype.bo.DocumentType;
25  import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
26  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
27  import org.kuali.rice.krad.service.impl.ModuleServiceBase;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * The ModuleService for KEW
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class KEWModuleService extends ModuleServiceBase {
40  
41  	protected DocumentTypeService docTypeService = null;
42  
43  	/**
44  	 * These are the "primary" keys for the DocTypeService. We are considering both
45  	 * name and documentTypeId to be unique.
46  	 *
47  	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#listPrimaryKeyFieldNames(java.lang.Class)
48  	 */
49  	@Override
50  	public List<String> listPrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
51  		if ( DocumentTypeEBO.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
52  			List<String> pkFields = new ArrayList<String>( 1 );
53  			pkFields.add( "documentTypeId" );
54  			return pkFields;
55  		}else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectInterfaceClass )){
56  			List<String> pkFields = new ArrayList<String>( 1 );
57  			pkFields.add( "documentId" );
58  			return pkFields;
59  		}
60  		return super.listPrimaryKeyFieldNames(businessObjectInterfaceClass);
61  	}
62  
63  	/**
64  	 * This overridden method calls the DocumentTypeService instead of the underlying
65  	 * KNS service.  Allows you to search on name and docTypeId
66  	 *
67  	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
68  	 */
69  	@Override
70  	public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(
71  			Class<T> businessObjectClass, Map<String, Object> fieldValues) {
72  		if(DocumentTypeEBO.class.isAssignableFrom(businessObjectClass)){
73  			if ( fieldValues.containsKey( "name" ) ) {
74  				return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeByName((String) fieldValues.get("name")));
75  			}else if( fieldValues.containsKey( "documentTypeId" ) ){
76  				return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeById(fieldValues.get("documentTypeId").toString()));
77  			}else if (fieldValues.containsKey( "id" ) ) {
78  				// assume it's a string and convert it to a long.
79  				return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeById(fieldValues.get("id").toString()));
80  			}
81  
82  		}else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectClass )){
83  			if ( fieldValues.containsKey( "documentId" ) ) {
84  				return (T)createDocumentSearchEbo(KewApiServiceLocator.getWorkflowDocumentService().getDocument(
85                          fieldValues.get("documentId").toString()));
86  			}
87  
88  		}
89  
90  		// otherwise, use the default implementation
91  		return super.getExternalizableBusinessObject(businessObjectClass, fieldValues);
92  	}
93  
94  	/**
95  	 * @return the docTypeService
96  	 */
97  	protected synchronized DocumentTypeService getDocumentTypeService() {
98  		if(this.docTypeService == null){
99  			// the default
100 			this.docTypeService = KewApiServiceLocator.getDocumentTypeService();
101 		}
102 		return this.docTypeService;
103 	}
104 
105 	/**
106 	 * @param docTypeService the docTypeService to set
107 	 */
108 	public synchronized void setDocumentTypeService(DocumentTypeService docTypeService) {
109 		this.docTypeService = docTypeService;
110 	}
111 
112 	private DocumentSearchCriteriaEbo createDocumentSearchEbo(final Document doc){
113 		return new DocumentSearchCriteriaEbo(){
114 
115             @Override
116             public String getApplicationDocumentId() {
117                 return doc.getApplicationDocumentId();
118             }
119 
120             @Override
121             public DocumentStatus getStatus() {
122                 return doc.getStatus();
123             }
124 
125             @Override
126             public String getApplicationDocumentStatus() {
127                 return doc.getApplicationDocumentStatus();
128             }
129 
130             @Override
131             public String getTitle() {
132                 return doc.getTitle();
133             }
134 
135             @Override
136             public String getDocumentTypeName() {
137                 return doc.getDocumentTypeName();
138             }
139 
140             @Override
141             public String getInitiatorPrincipalId() {
142                 return doc.getInitiatorPrincipalId();
143             }
144 
145             @Override
146             public String getDocumentId() {
147                 return doc.getDocumentId();
148             }
149 
150             @Override
151             public DateTime getDateCreated() {
152                 return doc.getDateCreated();
153             }
154 
155             @Override
156             public void refresh() {
157                 // do nothing
158             }
159             
160         };
161 	}
162 	/**
163 	 * This overridden method rewrites the URL.
164 	 *
165 	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObjectInquiryUrl(java.lang.Class, java.util.Map)
166 	 */
167 	@Override
168 	public String getExternalizableBusinessObjectInquiryUrl(
169 			Class inquiryBusinessObjectClass, Map<String, String[]> parameters) {
170 		if ( DocumentTypeEBO.class.isAssignableFrom( inquiryBusinessObjectClass ) ) {
171 			int nonBlank = 0;
172 			boolean nameFound = false;
173 			//"name" is the only non-blank property passed in
174 			for(String key: parameters.keySet()){
175 				if("name".equals(key) && parameters.get(key) != null){
176 					nameFound=true;
177 				}else if(!"name".equals(key) && parameters.get(key) != null){
178 					nonBlank ++;
179 				}
180 			}
181 
182 			if(nonBlank == 0 && nameFound == true){
183 				parameters.clear(); // clear out other parameters, including the name pass in
184 				DocumentTypeEBO dte = (DocumentTypeEBO) DocumentType.from(getDocumentTypeService().getDocumentTypeByName(parameters.get( "name" )[0] ));
185 				String[] strArr = {dte.getDocumentTypeId().toString()};
186 				parameters.put("documentTypeId", strArr);
187 			}
188 
189 		}
190 
191 		return super.getExternalizableBusinessObjectInquiryUrl(
192 				inquiryBusinessObjectClass, parameters);
193 	}
194 	/**
195 	 * We want to be able to use name as an alternate key
196 	 *
197 	 * @see org.kuali.rice.krad.service.ModuleService#listAlternatePrimaryKeyFieldNames(java.lang.Class)
198 	 */
199 	public List<List<String>> listAlternatePrimaryKeyFieldNames(
200 			Class businessObjectInterfaceClass) {
201 		if ( DocumentTypeEBO.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
202 			ArrayList<List<String>> retList = new ArrayList<List<String>>();
203 			ArrayList<String> keyList = new ArrayList<String>();
204 
205 			keyList.add("name");
206 			retList.add(keyList);
207 			return retList;
208 		}else{
209 			return null;
210 		}
211 
212 	}
213 }
214