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.api;
017
018import javax.xml.namespace.QName;
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.core.api.config.module.RunMode;
021import org.kuali.rice.core.api.config.property.ConfigContext;
022import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
023import org.kuali.rice.kew.api.action.ActionInvocationQueue;
024import org.kuali.rice.kew.api.action.RolePokerQueue;
025import org.kuali.rice.kew.api.action.WorkflowDocumentActionsService;
026import org.kuali.rice.kew.api.actionlist.ActionListService;
027import org.kuali.rice.kew.api.doctype.DocumentTypeService;
028import org.kuali.rice.kew.api.document.DocumentOrchestrationQueue;
029import org.kuali.rice.kew.api.document.DocumentProcessingQueue;
030import org.kuali.rice.kew.api.document.DocumentRefreshQueue;
031import org.kuali.rice.kew.api.document.WorkflowDocumentService;
032import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
033import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
034import org.kuali.rice.kew.api.group.GroupMembershipChangeQueue;
035import org.kuali.rice.kew.api.mail.ImmediateEmailReminderQueue;
036import org.kuali.rice.kew.api.note.NoteService;
037import org.kuali.rice.kew.api.peopleflow.PeopleFlowService;
038import org.kuali.rice.kew.api.preferences.PreferencesService;
039import org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService;
040import org.kuali.rice.kew.api.responsibility.ResponsibilityChangeQueue;
041import org.kuali.rice.kew.api.rule.RuleService;
042import org.kuali.rice.ksb.api.KsbApiServiceLocator;
043
044/**
045 * A static service locator which aids in locating the various services that
046 * form the Kuali Service Bus API.
047 */
048public class KewApiServiceLocator {
049
050    // Rice 2.0 - TODO - should these be using the QName versions instead?  How else will they be fetched in "remote" mode?
051        public static final String WORKFLOW_DOCUMENT_ACTIONS_SERVICE = "rice.kew.workflowDocumentActionsService";
052        public static final String WORKFLOW_DOCUMENT_SERVICE = "rice.kew.workflowDocumentService";
053    public static final String ACTION_LIST_SERVICE = "rice.kew.actionListService";
054        public static final String DOCUMENT_TYPE_SERVICE = "rice.kew.documentTypeService";
055        public static final String NOTE_SERVICE = "rice.kew.noteService";
056    public static final String EXTENSION_REPOSITORY_SERVICE = "rice.kew.extensionRepositoryService";
057    public static final String RULE_SERVICE = "rice.kew.ruleService";
058    public static final String KEW_TYPE_REPOSITORY_SERVICE = "rice.kew.kewTypeRepositoryService";
059    public static final String PEOPLE_FLOW_SERVICE = "rice.kew.peopleFlowService";
060    public static final String PREFERENCES_SERVICE = "rice.kew.preferencesService";
061    public static final String KEW_RUN_MODE_PROPERTY = "kew.mode";
062    public static final String STANDALONE_APPLICATION_ID = "standalone.application.id";
063    public static final QName DOCUMENT_ATTRIBUTE_INDEXING_QUEUE_NAME = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "documentAttributeIndexingQueue");
064    public static final QName GROUP_MEMBERSHIP_CHANGE_QUEUE_NAME = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "groupMembershipChangeQueue");
065    public static final QName IMMEDIATE_EMAIL_REMINDER_QUEUE = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "immediateEmailReminderQueue");
066    public static final QName RESPONSIBILITY_CHANGE_QUEUE = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "responsibilityChangeQueue");
067    public static final QName DOCUMENT_REFRESH_QUEUE = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "documentRefreshQueue");
068    public static final QName ROLE_POKER_QUEUE = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "rolePokerQueue");
069    public static final QName DOCUMENT_PROCESSING_QUEUE = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "documentProcessingQueue");
070    public static final QName DOCUMENT_ORCHESTRATION_QUEUE = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "documentOrchestrationQueue");
071    public static final QName ACTION_INVOCATION_QUEUE = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "actionInvocationQueue");
072
073    static <T> T getService(String serviceName) {
074        return GlobalResourceLoader.<T>getService(serviceName);
075    }
076
077    public static WorkflowDocumentActionsService getWorkflowDocumentActionsService() {
078        RunMode kewRunMode = RunMode.valueOf(ConfigContext.getCurrentContextConfig().getProperty(KEW_RUN_MODE_PROPERTY));
079        if (kewRunMode == RunMode.REMOTE || kewRunMode == RunMode.THIN) {
080            String standaloneApplicationId = ConfigContext.getCurrentContextConfig().getProperty(STANDALONE_APPLICATION_ID);
081            return getWorkflowDocumentActionsService(standaloneApplicationId);
082        } else { 
083            return getService(WORKFLOW_DOCUMENT_ACTIONS_SERVICE);
084        }
085    }
086    
087    public static WorkflowDocumentActionsService getWorkflowDocumentActionsService(String applicationId){
088        if(!StringUtils.isEmpty(applicationId)){//Need to find out proper remote endpoint in this case
089            QName qN = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, KewApiConstants.ServiceNames.WORKFLOW_DOCUMENT_ACTIONS_SERVICE_SOAP);
090            //http://rice.kuali.org/kew/v2_0}workflowDocumentActionsService
091            return (WorkflowDocumentActionsService)KsbApiServiceLocator.getServiceBus().getService(qN, applicationId);
092        }else{//we can use the default internal service here since we dont have an applicationId
093            return getWorkflowDocumentActionsService();
094        }
095    }
096    
097    public static WorkflowDocumentService getWorkflowDocumentService() {
098        return getService(WORKFLOW_DOCUMENT_SERVICE);
099    }
100
101    public static ActionListService getActionListService() {
102        return getService(ACTION_LIST_SERVICE);
103    }
104    
105    public static DocumentTypeService getDocumentTypeService() {
106        return getService(DOCUMENT_TYPE_SERVICE);
107    }
108    
109    public static NoteService getNoteService() {
110        return getService(NOTE_SERVICE);
111    }
112
113    public static RuleService getRuleService() {
114        return getService(RULE_SERVICE);
115    }
116
117    public static ExtensionRepositoryService getExtensionRepositoryService() {
118        return getService(EXTENSION_REPOSITORY_SERVICE);
119    }
120
121    public static KewTypeRepositoryService getKewTypeRepositoryService() {
122        return getService(KEW_TYPE_REPOSITORY_SERVICE);
123    }
124
125    public static PeopleFlowService getPeopleFlowService() {
126        return getService(PEOPLE_FLOW_SERVICE);
127    }
128
129    public static DocumentAttributeIndexingQueue getDocumentAttributeIndexingQueue() {
130        return getDocumentAttributeIndexingQueue(null);
131    }
132
133    /**
134     * For accessing common asynchronous services.
135     */
136
137    public static DocumentAttributeIndexingQueue getDocumentAttributeIndexingQueue(String applicationId) {
138        return (DocumentAttributeIndexingQueue)KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(DOCUMENT_ATTRIBUTE_INDEXING_QUEUE_NAME, applicationId);
139    }
140    
141    public static GroupMembershipChangeQueue getGroupMembershipChangeQueue() {
142        return (GroupMembershipChangeQueue)KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(GROUP_MEMBERSHIP_CHANGE_QUEUE_NAME);
143    }
144
145    public static ResponsibilityChangeQueue getResponsibilityChangeQueue() {
146        return (ResponsibilityChangeQueue)KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(
147                RESPONSIBILITY_CHANGE_QUEUE);
148    }
149    
150    public static ImmediateEmailReminderQueue getImmediateEmailReminderQueue() {
151        return KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(IMMEDIATE_EMAIL_REMINDER_QUEUE);
152    }
153
154    public static PreferencesService getPreferencesService() {
155        return getService(PREFERENCES_SERVICE);
156    }
157
158    public static DocumentProcessingQueue getDocumentProcessingQueue(String documentId, String applicationId) {
159        return (DocumentProcessingQueue)getServiceAsynchronously(DOCUMENT_PROCESSING_QUEUE, documentId, applicationId);
160    }
161
162    public static ActionInvocationQueue getActionInvocationProcessorService(String documentId, String applicationId) {
163        return (ActionInvocationQueue) getServiceAsynchronously(ACTION_INVOCATION_QUEUE, documentId, applicationId);
164    }
165
166    public static DocumentOrchestrationQueue getDocumentOrchestrationQueue(String documentId, String applicationId) {
167        return (DocumentOrchestrationQueue) getServiceAsynchronously(DOCUMENT_ORCHESTRATION_QUEUE, documentId, applicationId);
168    }
169
170    public static RolePokerQueue getRolePokerQueue(String documentId, String applicationId) {
171        return (RolePokerQueue) getServiceAsynchronously(ROLE_POKER_QUEUE, documentId, applicationId);
172    }
173
174    public static DocumentRefreshQueue getDocumentRequeuerService(String applicationId, String documentId, long waitTime) {
175        if (waitTime > 0) {
176            return (DocumentRefreshQueue) getDelayedServiceAsynchronously(DOCUMENT_REFRESH_QUEUE, documentId, waitTime, applicationId);
177        }
178        return (DocumentRefreshQueue) getServiceAsynchronously(DOCUMENT_REFRESH_QUEUE, documentId, applicationId);
179    }
180
181    private static Object getDelayedServiceAsynchronously(QName serviceName, String documentId, long waitTime, String applicationId) {
182        return KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(serviceName, applicationId, null, (documentId == null ? null : documentId.toString()), null, waitTime);
183    }
184
185    private static Object getServiceAsynchronously(QName serviceName, String documentId, String applicationId) {
186        return KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(serviceName, applicationId, null, null, (documentId == null ? null : documentId.toString()), null);
187    }
188}