View Javadoc

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