View Javadoc

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