public void Listener(ListenerServiceRequestAPI listenerServiceRequestAPI) { try { SalesforceServiceSingleton.GetInstance().Listen(EmailNotifier.GetInstance(this.GetWho(), listenerServiceRequestAPI.configurationValues, "PluginSalesforceController.Listener"), this.GetWho(), listenerServiceRequestAPI); } catch (Exception exception) { throw BaseHttpUtils.GetWebException(HttpStatusCode.BadRequest, BaseHttpUtils.GetExceptionMessage(exception)); } }
public void UnregisterListener(String tenantId, String objectId, ListenerServiceRequestAPI listenerServiceRequest) { Dictionary <String, ListenerServiceRequestAPI> salesforceListenerEntries = null; if (String.IsNullOrWhiteSpace(tenantId) == true) { throw new ArgumentNullException("TenantId", "The TenantId cannot be null or blank."); } if (String.IsNullOrWhiteSpace(objectId) == true) { throw new ArgumentNullException("ObjectId", "The ObjectId cannot be null or blank."); } if (listenerServiceRequest == null) { throw new ArgumentNullException("ListenerServiceRequest", "The ListenerServiceRequest object cannot be null."); } if (String.IsNullOrWhiteSpace(listenerServiceRequest.token) == true) { throw new ArgumentNullException("ListenerServiceRequest.Token", "The ListenerServiceRequest.Token property cannot be null or blank."); } // Try to get the listener requests out for this object salesforceListenerEntries = this.GetListenerRequests(tenantId, objectId); // Check to make sure we found some listener service requests for this record if (salesforceListenerEntries != null) { // Check to see if the listener service request contains an entry for this token if (salesforceListenerEntries.ContainsKey(listenerServiceRequest.token) == true) { // Remove this listener service request from the map salesforceListenerEntries.Remove(listenerServiceRequest.token); } // If the listener service requests are now emptry, we remove the parent as well if (salesforceListenerEntries.Count == 0) { StorageUtils.RemoveStoredJson((tenantId + objectId).ToLower()); } } }
public void RegisterListener(IAuthenticatedWho authenticatedWho, ListenerServiceRequestAPI listenerServiceRequest) { Dictionary <String, ListenerServiceRequestAPI> salesforceListenerEntries = null; String objectId = null; Guid externalGuid = Guid.Empty; if (authenticatedWho == null) { throw new ArgumentNullException("AuthenticatedWho", "The AuthenticatedWho object cannot be null."); } if (listenerServiceRequest == null) { throw new ArgumentNullException("ListenerServiceRequest", "The ListenerServiceRequest object cannot be null."); } if (String.IsNullOrWhiteSpace(listenerServiceRequest.listenType) == true) { throw new ArgumentNullException("ListenerServiceRequest.ListenType", "The ListenerServiceRequest.ListenType property cannot be null or blank."); } if (String.IsNullOrWhiteSpace(listenerServiceRequest.token) == true) { throw new ArgumentNullException("ListenerServiceRequest.Token", "The ListenerServiceRequest.Token property cannot be null or blank."); } if (listenerServiceRequest.valueForListening == null) { throw new ArgumentNullException("ListenerServiceRequest.ValueForListening", "The ListenerServiceRequest.ValueForListening property cannot be null."); } if (listenerServiceRequest.valueForListening.objectData == null || listenerServiceRequest.valueForListening.objectData.Count == 0) { throw new ArgumentNullException("ListenerServiceRequest.ValueForListening.ObjectData", "The Salesforce Service cannot listen without any object records to listen to."); } if (listenerServiceRequest.valueForListening.objectData.Count > 1) { throw new ArgumentNullException("ListenerServiceRequest.ValueForListening.ObjectData", "The Salesforce Service cannot listen to more than one object record in a single request."); } if (String.IsNullOrWhiteSpace(listenerServiceRequest.valueForListening.objectData[0].externalId) == true || Guid.TryParse(listenerServiceRequest.valueForListening.objectData[0].externalId, out externalGuid) == true) { throw new ArgumentNullException("ListenerServiceRequest.ValueForListening.ObjectData", "The Salesforce Service cannot listen to an object that has not been first saved or loaded from Salesforce."); } // Assign the object id from the external id objectId = listenerServiceRequest.valueForListening.objectData[0].externalId; // Try to get the listener requests out for this object salesforceListenerEntries = this.GetListenerRequests(listenerServiceRequest.tenantId, objectId); // Check to make sure we have something, or create a new list if (salesforceListenerEntries == null) { // This is the first time we're registering a listener for this object, so we create a new map salesforceListenerEntries = new Dictionary <String, ListenerServiceRequestAPI>(); } // Now we have our list of listeners, we add this one based on the token salesforceListenerEntries[listenerServiceRequest.token.ToLower()] = listenerServiceRequest; // And we put the updated map, back into the data store StorageUtils.SetStoredJson((listenerServiceRequest.tenantId + objectId).ToLower(), JsonConvert.SerializeObject(salesforceListenerEntries)); }
private void Execute(INotifier notifier, String tenantId, String mode, WorkflowRuleNotification workflowRuleNotification) { Dictionary <String, ListenerServiceRequestAPI> salesforceListenerEntries = null; ListenerServiceResponseAPI listenerServiceResponse = null; ListenerServiceRequestAPI listenerServiceRequest = null; SforceService sforceService = null; String authenticationStrategy = null; String authenticationUrl = null; String securityToken = null; String invokeType = null; String username = null; String password = null; if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Executing listener notification."); } // Go through each object identifier in the notification if (workflowRuleNotification.ObjectIDs != null && workflowRuleNotification.ObjectIDs.Count > 0) { if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Workflow event has object identifiers."); } foreach (String objectId in workflowRuleNotification.ObjectIDs) { if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Processing object identifier: " + objectId); } // Check to see if ManyWho has asked us to listen to any of them salesforceListenerEntries = SalesforceListenerSingleton.GetInstance().GetListenerRequests(tenantId, objectId); // Check to see if we're actually listening if (salesforceListenerEntries != null && salesforceListenerEntries.Count > 0) { if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Object has listener entries."); } // If it has, send a listen notification back to the workflow engine - one by one at the moment (no bulk!) foreach (KeyValuePair <String, ListenerServiceRequestAPI> pair in salesforceListenerEntries) { // Get the listener request out listenerServiceRequest = pair.Value; if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Executing listener entry."); } // Create the service response listenerServiceResponse = new ListenerServiceResponseAPI(); listenerServiceResponse.annotations = listenerServiceRequest.annotations; listenerServiceResponse.culture = listenerServiceRequest.culture; listenerServiceResponse.tenantId = listenerServiceRequest.tenantId; listenerServiceResponse.token = listenerServiceRequest.token; // Apply the settings for the response from the request so the engine has the full details about the object listenerServiceResponse.listeningEventValue = listenerServiceRequest.valueForListening; listenerServiceResponse.listeningEventValue.contentType = listenerServiceRequest.valueForListening.contentType; listenerServiceResponse.listeningEventValue.contentValue = listenerServiceRequest.valueForListening.contentValue; listenerServiceResponse.listeningEventValue.developerName = listenerServiceRequest.valueForListening.developerName; listenerServiceResponse.listeningEventValue.typeElementDeveloperName = listenerServiceRequest.valueForListening.typeElementDeveloperName; listenerServiceResponse.listeningEventValue.typeElementId = listenerServiceRequest.valueForListening.typeElementId; listenerServiceResponse.listeningEventValue.typeElementPropertyDeveloperName = listenerServiceRequest.valueForListening.typeElementPropertyDeveloperName; listenerServiceResponse.listeningEventValue.typeElementPropertyId = listenerServiceRequest.valueForListening.typeElementPropertyId; listenerServiceResponse.listeningEventValue.valueElementId = listenerServiceRequest.valueForListening.valueElementId; // Get the configuration values out that are needed to check the voting status // TODO: we should smart cache the login info and connection authenticationUrl = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_AUTHENTICATION_URL, listenerServiceRequest.configurationValues, true); username = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_USERNAME, listenerServiceRequest.configurationValues, true); password = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_PASSWORD, listenerServiceRequest.configurationValues, true); securityToken = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_SECURITY_TOKEN, listenerServiceRequest.configurationValues, false); authenticationStrategy = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_AUTHENTICATION_STRATEGY, listenerServiceRequest.configurationValues, false); if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Logging into salesforce using: " + username); } if (String.IsNullOrWhiteSpace(authenticationStrategy) == true || authenticationStrategy.Equals(SalesforceServiceSingleton.AUTHENTICATION_STRATEGY_STANDARD, StringComparison.OrdinalIgnoreCase) == true || authenticationStrategy.Equals(SalesforceServiceSingleton.AUTHENTICATION_STRATEGY_ACTIVE_USER, StringComparison.OrdinalIgnoreCase) == true) { sforceService = SalesforceDataSingleton.GetInstance().LogUserInBasedOnSession(listenerServiceRequest.configurationValues, workflowRuleNotification.SessionID, workflowRuleNotification.SessionURL); } else if (authenticationStrategy.Equals(SalesforceServiceSingleton.AUTHENTICATION_STRATEGY_SUPER_USER, StringComparison.OrdinalIgnoreCase) == true) { // Login to salesforce using the details in the service request sforceService = SalesforceDataSingleton.GetInstance().LoginUsingCredentials(authenticationUrl, username, password, securityToken); } else { throw new ArgumentNullException("ConfigurationValues", String.Format("The provided authentication strategy is not supported: '{0}'", authenticationStrategy)); } if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Getting full sObject for: " + objectId); } // Load the latest object from salesforce so we have the data to send back listenerServiceResponse.listeningEventValue.objectData = SalesforceDataSingleton.GetInstance().LoadSObjectByIdentifier(sforceService, workflowRuleNotification.ObjectName, objectId, true); try { if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Executing event against ManyWho"); } // Dispatch a listen response to the engine as an event has occurred invokeType = RunSingleton.GetInstance().Event(notifier, null, tenantId, listenerServiceRequest.callbackUri, listenerServiceResponse); } catch (Exception exception) { if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Event execution failed with: " + BaseHttpUtils.GetExceptionMessage(exception)); } // Something went wrong - but we ignore it for now invokeType = ManyWhoConstants.INVOKE_TYPE_FORWARD; } if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Service returned invoke type of: " + invokeType); } // If the engine returns nothing, errors or returns an response other than a "WAIT", we delete the listener for now // TODO: make this a bit more intelligent so we can handle things like retry if (String.IsNullOrWhiteSpace(invokeType) == false && invokeType.IndexOf(ManyWhoConstants.INVOKE_TYPE_WAIT, StringComparison.InvariantCultureIgnoreCase) < 0) { if (SettingUtils.IsDebugging(mode)) { notifier.AddLogEntry("Removing entry from listeners for invoke type: " + invokeType); } SalesforceListenerSingleton.GetInstance().UnregisterListener(tenantId, objectId, listenerServiceRequest); } } } } } }