001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kew.service.impl;
017
018 import org.joda.time.DateTime;
019 import org.kuali.rice.kew.api.KewApiServiceLocator;
020 import org.kuali.rice.kew.api.doctype.DocumentTypeService;
021 import org.kuali.rice.kew.api.document.Document;
022 import org.kuali.rice.kew.api.document.DocumentStatus;
023 import org.kuali.rice.kew.docsearch.DocumentSearchCriteriaEbo;
024 import org.kuali.rice.kew.doctype.bo.DocumentType;
025 import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
026 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
027 import org.kuali.rice.krad.service.impl.ModuleServiceBase;
028
029 import java.util.ArrayList;
030 import java.util.List;
031 import java.util.Map;
032
033 /**
034 * The ModuleService for KEW
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 *
038 */
039 public class KEWModuleService extends ModuleServiceBase {
040
041 protected DocumentTypeService docTypeService = null;
042
043 /**
044 * These are the "primary" keys for the DocTypeService. We are considering both
045 * name and documentTypeId to be unique.
046 *
047 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#listPrimaryKeyFieldNames(java.lang.Class)
048 */
049 @Override
050 public List<String> listPrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
051 if ( DocumentTypeEBO.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
052 List<String> pkFields = new ArrayList<String>( 1 );
053 pkFields.add( "documentTypeId" );
054 return pkFields;
055 }else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectInterfaceClass )){
056 List<String> pkFields = new ArrayList<String>( 1 );
057 pkFields.add( "documentId" );
058 return pkFields;
059 }
060 return super.listPrimaryKeyFieldNames(businessObjectInterfaceClass);
061 }
062
063 /**
064 * This overridden method calls the DocumentTypeService instead of the underlying
065 * KNS service. Allows you to search on name and docTypeId
066 *
067 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
068 */
069 @Override
070 public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(
071 Class<T> businessObjectClass, Map<String, Object> fieldValues) {
072 if(DocumentTypeEBO.class.isAssignableFrom(businessObjectClass)){
073 if ( fieldValues.containsKey( "name" ) ) {
074 return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeByName((String) fieldValues.get("name")));
075 }else if( fieldValues.containsKey( "documentTypeId" ) ){
076 return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeById(fieldValues.get("documentTypeId").toString()));
077 }else if (fieldValues.containsKey( "id" ) ) {
078 // assume it's a string and convert it to a long.
079 return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeById(fieldValues.get("id").toString()));
080 }
081
082 }else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectClass )){
083 if ( fieldValues.containsKey( "documentId" ) ) {
084 return (T)createDocumentSearchEbo(KewApiServiceLocator.getWorkflowDocumentService().getDocument(
085 fieldValues.get("documentId").toString()));
086 }
087
088 }
089
090 // otherwise, use the default implementation
091 return super.getExternalizableBusinessObject(businessObjectClass, fieldValues);
092 }
093
094 /**
095 * @return the docTypeService
096 */
097 protected synchronized DocumentTypeService getDocumentTypeService() {
098 if(this.docTypeService == null){
099 // 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