001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.impl.service;
017
018import org.joda.time.DateTime;
019import org.kuali.rice.core.api.criteria.Predicate;
020import org.kuali.rice.core.api.criteria.PredicateUtils;
021import org.kuali.rice.core.api.criteria.QueryByCriteria;
022import org.kuali.rice.kew.api.KewApiServiceLocator;
023import org.kuali.rice.kew.api.doctype.DocumentTypeService;
024import org.kuali.rice.kew.api.document.Document;
025import org.kuali.rice.kew.api.document.DocumentStatus;
026import org.kuali.rice.kew.docsearch.DocumentSearchCriteriaEbo;
027import org.kuali.rice.kew.doctype.bo.DocumentType;
028import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
029import org.kuali.rice.kim.api.group.Group;
030import org.kuali.rice.kim.api.group.GroupContract;
031import org.kuali.rice.kim.api.identity.Person;
032import org.kuali.rice.kim.api.role.Role;
033import org.kuali.rice.kim.api.role.RoleContract;
034import org.kuali.rice.kim.framework.group.GroupEbo;
035import org.kuali.rice.kim.framework.role.RoleEbo;
036import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
037import org.kuali.rice.krad.service.impl.RemoteModuleServiceBase;
038
039import java.util.ArrayList;
040import java.util.Collections;
041import java.util.List;
042import java.util.Map;
043
044public class KewRemoteModuleService extends RemoteModuleServiceBase {
045
046    protected DocumentTypeService docTypeService = null;
047
048    /**
049     * This overridden method calls the DocumentTypeService instead of the underlying
050     * KNS service.  Allows you to search on name and docTypeId
051     *
052     * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
053     */
054    @Override
055    public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(
056            Class<T> businessObjectClass, Map<String, Object> fieldValues) {
057        if(DocumentTypeEBO.class.isAssignableFrom(businessObjectClass)){
058            if ( fieldValues.containsKey( "name" ) ) {
059                return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeByName((String) fieldValues.get(
060                        "name")));
061            } else if( fieldValues.containsKey( "documentTypeId" ) ){
062                return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeById(fieldValues.get("documentTypeId").toString()));
063            } else if (fieldValues.containsKey( "id" ) ) {
064                // assume it's a string and convert it to a long.
065                return (T) DocumentType.from(getDocumentTypeService().getDocumentTypeById(fieldValues.get("id").toString()));
066            }
067
068        } else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectClass )) {
069            if ( fieldValues.containsKey( "documentId" ) ) {
070                return (T)createDocumentSearchEbo(KewApiServiceLocator.getWorkflowDocumentService().getDocument(
071                        fieldValues.get("documentId").toString()));
072            }
073
074        }
075
076        return null;
077    }
078
079    private DocumentSearchCriteriaEbo createDocumentSearchEbo(final Document doc){
080        return new DocumentSearchCriteriaEbo(){
081
082            @Override
083            public String getApplicationDocumentId() {
084                return doc.getApplicationDocumentId();
085            }
086
087            @Override
088            public DocumentStatus getStatus() {
089                return doc.getStatus();
090            }
091
092            @Override
093            public String getApplicationDocumentStatus() {
094                return doc.getApplicationDocumentStatus();
095            }
096
097            @Override
098            public String getTitle() {
099                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}