Example #1
0
        public ServiceResponseAPI InvokeCreateEvent(INotifier notifier, IAuthenticatedWho authenticatedWho, ServiceRequestAPI serviceRequest)
        {
            List <ObjectDataTypePropertyAPI> objectDataTypeProperties = null;
            ServiceResponseAPI serviceResponse   = null;
            DateTime           whenDate          = DateTime.Now;
            List <ObjectAPI>   eventObjects      = null;
            ObjectAPI          eventObject       = null;
            String             authenticationUrl = null;
            String             username          = null;
            String             password          = null;
            String             securityToken     = null;
            String             adminEmail        = null;
            String             when        = null;
            String             duration    = null;
            String             description = null;
            String             subject     = null;
            String             eventId     = null;

            // Grab the configuration values from the service request
            authenticationUrl = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_AUTHENTICATION_URL, serviceRequest.configurationValues, true);
            username          = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_USERNAME, serviceRequest.configurationValues, true);
            password          = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_PASSWORD, serviceRequest.configurationValues, true);
            securityToken     = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_SECURITY_TOKEN, serviceRequest.configurationValues, false);
            adminEmail        = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_ADMIN_EMAIL, serviceRequest.configurationValues, true);

            if (serviceRequest.authorization != null)
            {
                // Get the message from the inputs
                when        = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_INPUT_WHEN, serviceRequest.inputs, true);
                duration    = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_INPUT_DURATION, serviceRequest.inputs, true);
                description = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_INPUT_DESCRIPTION, serviceRequest.inputs, true);
                subject     = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_INPUT_SUBJECT, serviceRequest.inputs, true);

                // Get the when date for the provided command
                whenDate = DateUtils.CreateDateFromWhenCommand(notifier, authenticatedWho, when, adminEmail);
                // Set the calendar event for a day in the week at 10am
                whenDate = DateUtils.GetDayInWeek(whenDate, 10);

                // Add the link to the flow in the description
                description += "  Link to Flow: " + serviceRequest.joinPlayerUri;

                // Create a event object to save back to the system
                eventObject = new ObjectAPI();
                eventObject.developerName = "Event";
                eventObject.properties    = new List <PropertyAPI>();
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "ActivityDateTime", contentValue = whenDate.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffZ")
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "Description", contentValue = description
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "DurationInMinutes", contentValue = duration
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "Subject", contentValue = subject
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsAllDayEvent", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsArchived", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsChild", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsGroupEvent", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsPrivate", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsRecurrence", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsReminderSet", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsVisibleInSelfService", contentValue = "false"
                });

                // Add the object to the list of objects to save
                eventObjects = new List <ObjectAPI>();
                eventObjects.Add(eventObject);

                // Create the object data type properties for this object so the system knows what we're selecting
                objectDataTypeProperties = new List <ObjectDataTypePropertyAPI>();
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "ActivityDateTime"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "Description"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "DurationInMinutes"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "Subject"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsAllDayEvent"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsArchived"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsChild"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsGroupEvent"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsPrivate"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsRecurrence"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsReminderSet"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsVisibleInSelfService"
                });

                // Save the event object to salesforce
                eventObjects = SalesforceDataSingleton.GetInstance().Save(notifier, authenticatedWho, serviceRequest.configurationValues, objectDataTypeProperties, eventObjects);

                // Check to see if anything came back as part of the save - it should unless there was a fault
                if (eventObjects != null &&
                    eventObjects.Count > 0)
                {
                    // Grab the first object from the returned event objects
                    eventObject = eventObjects[0];

                    // Grab the id from that object - this needs to be returned in our outputs
                    eventId = eventObject.externalId;
                }
                else
                {
                    // If we didn't get any objects back, we need to throw an error
                    String errorMessage = "Event could not be created for an unknown reason.";

                    ErrorUtils.SendAlert(notifier, authenticatedWho, ErrorUtils.ALERT_TYPE_FAULT, errorMessage);

                    throw new ArgumentNullException("BadRequest", errorMessage);
                }
            }
            else
            {
                // Alert the admin that no one is in the authorization context
                ErrorUtils.SendAlert(notifier, authenticatedWho, ErrorUtils.ALERT_TYPE_WARNING, "The service request does not have an authorization context, so there's no one to notify.");
            }

            // Construct the service response
            serviceResponse            = new ServiceResponseAPI();
            serviceResponse.invokeType = ManyWhoConstants.INVOKE_TYPE_FORWARD;
            serviceResponse.token      = serviceRequest.token;
            serviceResponse.outputs    = new List <EngineValueAPI>();
            serviceResponse.outputs.Add(new EngineValueAPI()
            {
                contentType = ManyWhoConstants.CONTENT_TYPE_STRING, contentValue = eventId, developerName = SalesforceServiceSingleton.SERVICE_OUTPUT_ID
            });

            return(serviceResponse);
        }