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.impl.service;
17  
18  import org.joda.time.DateTime;
19  import org.kuali.rice.core.api.criteria.Predicate;
20  import org.kuali.rice.core.api.criteria.PredicateUtils;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.kew.api.KewApiServiceLocator;
23  import org.kuali.rice.kew.api.doctype.DocumentTypeService;
24  import org.kuali.rice.kew.api.document.Document;
25  import org.kuali.rice.kew.api.document.DocumentStatus;
26  import org.kuali.rice.kew.docsearch.DocumentSearchCriteriaEbo;
27  import org.kuali.rice.kew.doctype.bo.DocumentType;
28  import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
29  import org.kuali.rice.kim.api.group.Group;
30  import org.kuali.rice.kim.api.group.GroupContract;
31  import org.kuali.rice.kim.api.identity.Person;
32  import org.kuali.rice.kim.api.role.Role;
33  import org.kuali.rice.kim.api.role.RoleContract;
34  import org.kuali.rice.kim.framework.group.GroupEbo;
35  import org.kuali.rice.kim.framework.role.RoleEbo;
36  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
37  import org.kuali.rice.krad.service.impl.RemoteModuleServiceBase;
38  
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.List;
42  import java.util.Map;
43  
44  public class KewRemoteModuleService extends RemoteModuleServiceBase {
45  
46      protected DocumentTypeService docTypeService = null;
47  
48      /**
49       * This overridden method calls the DocumentTypeService instead of the underlying
50       * KNS service.  Allows you to search on name and docTypeId
51       *
52       * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
53       */
54      @Override
55      public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(
56              Class<T> businessObjectClass, Map<String, Object> fieldValues) {
57          if(DocumentTypeEBO.class.isAssignableFrom(businessObjectClass)){
58              if ( fieldValues.containsKey( "name" ) ) {
59                  return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeByName((String) fieldValues.get(
60                          "name")));
61              } else if( fieldValues.containsKey( "documentTypeId" ) ){
62                  return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeById(fieldValues.get("documentTypeId").toString()));
63              } else if (fieldValues.containsKey( "id" ) ) {
64                  // assume it's a string and convert it to a long.
65                  return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeById(fieldValues.get("id").toString()));
66              }
67  
68          } else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectClass )) {
69              if ( fieldValues.containsKey( "documentId" ) ) {
70                  return (T)createDocumentSearchEbo(KewApiServiceLocator.getWorkflowDocumentService().getDocument(
71                          fieldValues.get("documentId").toString()));
72              }
73  
74          }
75  
76          return null;
77      }
78  
79      private DocumentSearchCriteriaEbo createDocumentSearchEbo(final Document doc){
80          return new DocumentSearchCriteriaEbo(){
81  
82              @Override
83              public String getApplicationDocumentId() {
84                  return doc.getApplicationDocumentId();
85              }
86  
87              @Override
88              public DocumentStatus getStatus() {
89                  return doc.getStatus();
90              }
91  
92              @Override
93              public String getApplicationDocumentStatus() {
94                  return doc.getApplicationDocumentStatus();
95              }
96  
97              @Override
98              public String getTitle() {
99                  return doc.getTitle();
100             }
101 
102             @Override
103             public String getDocumentTypeName() {
104                 return doc.getDocumentTypeName();
105             }
106 
107             @Override
108             public String getInitiatorPrincipalId() {
109                 return doc.getInitiatorPrincipalId();
110             }
111 
112             @Override
113             public String getDocumentId() {
114                 return doc.getDocumentId();
115             }
116 
117             @Override
118             public DateTime getDateCreated() {
119                 return doc.getDateCreated();
120             }
121 
122             @Override
123             public void refresh() {
124                 // do nothing
125             }
126 
127         };
128     }
129 
130     @Override
131     public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
132             Class<T> businessObjectClass, Map<String, Object> fieldValues) {
133         if (!fieldValues.isEmpty()) {
134             throw new IllegalStateException("fieldValues must be empty");
135         }
136         List<T> ebos = new ArrayList<T>();
137         if(DocumentTypeEBO.class.isAssignableFrom(businessObjectClass)){
138             List<org.kuali.rice.kew.api.doctype.DocumentType> docTypes = getDocumentTypeService().findAllDocumentTypes();
139 
140             for (org.kuali.rice.kew.api.doctype.DocumentType docType : docTypes) {
141                 ebos.add((T)DocumentType.from(docType));
142             }
143             return ebos;
144         }
145         return Collections.emptyList();
146     }
147 
148     @Override
149     public boolean isExternalizableBusinessObjectLookupable(Class boClass) {
150         return isExternalizable(boClass);
151     }
152 
153     @Override
154     public boolean isExternalizableBusinessObjectInquirable(Class boClass) {
155         return isExternalizable(boClass);
156     }
157 
158     @Override
159     public boolean isExternalizable(Class boClazz) {
160         if (boClazz == null) {
161             return false;
162         }
163         if(DocumentTypeEBO.class.isAssignableFrom(boClazz)) {
164             return true;
165         } else if(DocumentSearchCriteriaEbo.class.isAssignableFrom(boClazz)) {
166             return true;
167         }
168         return ExternalizableBusinessObject.class.isAssignableFrom(boClazz);
169     }
170 
171     @Override
172     public List<String> listPrimaryKeyFieldNames(Class boClass) {
173         if ( DocumentTypeEBO.class.isAssignableFrom( boClass ) ) {
174             List<String> pkFields = new ArrayList<String>( 1 );
175             pkFields.add( "documentTypeId" );
176             return pkFields;
177         }else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( boClass )){
178             List<String> pkFields = new ArrayList<String>( 1 );
179             pkFields.add( "documentId" );
180             return pkFields;
181         }
182         return Collections.emptyList();
183     }
184 
185     /**
186      * We want to be able to use name as an alternate key
187      *
188      * @see org.kuali.rice.krad.service.ModuleService#listAlternatePrimaryKeyFieldNames(java.lang.Class)
189      */
190     public List<List<String>> listAlternatePrimaryKeyFieldNames(
191             Class businessObjectInterfaceClass) {
192         if ( DocumentTypeEBO.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
193             ArrayList<List<String>> retList = new ArrayList<List<String>>();
194             ArrayList<String> keyList = new ArrayList<String>();
195 
196             keyList.add("name");
197             retList.add(keyList);
198             return retList;
199         }else{
200             return null;
201         }
202 
203     }
204 
205     /**
206      * @return the docTypeService
207      */
208     protected synchronized DocumentTypeService getDocumentTypeService() {
209         if(this.docTypeService == null){
210             // the default
211             this.docTypeService = KewApiServiceLocator.getDocumentTypeService();
212         }
213         return this.docTypeService;
214     }
215 }