Ejemplo n.º 1
0
        private bool UpdateRecord(string recordId, Dictionary <string, object> result)
        {
            if (result.Count > 0)
            {
                var url  = UpdateRecordUrl + ListId + "&id=" + recordId;
                var auth = Workflow.GetWorkiomAccessToken();
                var json = JsonConvert.SerializeObject(result);

                var updateTask = WorkiomHelper.Put(url, auth, json);
                updateTask.Wait();
                var response        = updateTask.Result;
                var responseSuccess = (bool)JObject.Parse(response).SelectToken("success");

                if (responseSuccess)
                {
                    Info("Record " + recordId + " updated.");
                    return(true);
                }
                else
                {
                    ErrorFormat("An error occured while updating the record {0}: {1}", recordId, response);
                    return(false);
                }
            }
            else
            {
                Info("The mapping resulted in an empty payload.");
            }

            return(false);
        }
Ejemplo n.º 2
0
        public override TaskStatus Run()
        {
            Info("Creating Workiom record ...");

            bool success = true;

            try
            {
                InfoFormat("Mapping: {0}", Mapping);

                // Retrieve payload
                var trigger = new Trigger {
                    Payload = JsonConvert.DeserializeObject <Dictionary <string, object> >(Workflow.RestParams["Payload"])
                };

                // Retrieve mapping
                var jArray  = JArray.Parse(Mapping);
                var mapping = new Dictionary <string, MappingValue>();

                foreach (var item in jArray)
                {
                    var field = item.Value <string>("Field");
                    var val   = item.Value <object>("Value");
                    var type  = item.Value <string>("Type");

                    mapping.Add(field, new MappingValue {
                        Value = val, MappingType = type.ToLower() == "field" ? MappingType.Dynamic : MappingType.Static
                    });
                }

                // Genereate result
                var result = WorkiomHelper.Map(trigger, mapping);

                // Create record from result
                if (result.Count > 0)
                {
                    var url  = CreateRecordUrl + ListId;
                    var auth = Workflow.GetWorkiomAccessToken();
                    var json = JsonConvert.SerializeObject(result);
                    InfoFormat("Payload: {0}", json);

                    var createTask = WorkiomHelper.Post(url, auth, json);
                    createTask.Wait();
                    var response        = createTask.Result;
                    var responseSuccess = (bool)JObject.Parse(response).SelectToken("success");

                    if (responseSuccess)
                    {
                        Info("Record created.");
                    }
                    else
                    {
                        ErrorFormat("An error occured while creating the record: {0}", response);
                        success = false;
                    }
                }
                else
                {
                    Info("The mapping resulted in an empty payload.");
                }
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception e)
            {
                ErrorFormat("An error occured while creating Workiom record. Error: {0}", e.Message);
                success = false;
            }

            var status = Status.Success;

            if (!success)
            {
                status = Status.Error;
            }

            Info("Task finished.");
            return(new TaskStatus(status));
        }
Ejemplo n.º 3
0
        public override TaskStatus Run()
        {
            Info("Sending email ...");

            bool success = true;

            try
            {
                var to      = string.Empty;
                var cc      = string.Empty;
                var bcc     = string.Empty;
                var subject = string.Empty;
                var body    = string.Empty;

                var trigger = new Trigger {
                    Payload = JsonConvert.DeserializeObject <Dictionary <string, object> >(Workflow.RestParams["Payload"])
                };

                // toMapping
                InfoFormat("toMapping: {0}", ToMapping);
                var jArray  = JArray.Parse(ToMapping);
                var mapping = new Dictionary <string, MappingValue>();

                foreach (var item in jArray)
                {
                    var field = item.Value <string>("Field");
                    var val   = item.Value <object>("Value");
                    var type  = item.Value <string>("Type");

                    mapping.Add(field, new MappingValue {
                        Value = val, MappingType = type.ToLower() == "field" ? MappingType.Dynamic : MappingType.Static
                    });
                }

                // Genereate result
                var result = WorkiomHelper.Map(trigger, mapping);

                if (result.Count > 0)
                {
                    to = result.Values.First().ToString();
                    InfoFormat("to: {0}", to);
                }
                else
                {
                    Info("toMapping: The mapping resulted in an empty payload.");
                }

                // ccMapping
                InfoFormat("ccMapping: {0}", CcMapping);
                jArray  = JArray.Parse(CcMapping);
                mapping = new Dictionary <string, MappingValue>();

                foreach (var item in jArray)
                {
                    var field = item.Value <string>("Field");
                    var val   = item.Value <object>("Value");
                    var type  = item.Value <string>("Type");

                    mapping.Add(field, new MappingValue {
                        Value = val, MappingType = type.ToLower() == "field" ? MappingType.Dynamic : MappingType.Static
                    });
                }

                // Genereate result
                result = WorkiomHelper.Map(trigger, mapping);

                if (result.Count > 0)
                {
                    cc = result.Values.First().ToString();
                    InfoFormat("cc: {0}", cc);
                }
                else
                {
                    Info("ccMapping: The mapping resulted in an empty payload.");
                }

                // bccMapping
                InfoFormat("BccMapping: {0}", BccMapping);
                jArray  = JArray.Parse(BccMapping);
                mapping = new Dictionary <string, MappingValue>();

                foreach (var item in jArray)
                {
                    var field = item.Value <string>("Field");
                    var val   = item.Value <object>("Value");
                    var type  = item.Value <string>("Type");

                    mapping.Add(field, new MappingValue {
                        Value = val, MappingType = type.ToLower() == "field" ? MappingType.Dynamic : MappingType.Static
                    });
                }

                // Genereate result
                result = WorkiomHelper.Map(trigger, mapping);

                if (result.Count > 0)
                {
                    bcc = result.Values.First().ToString();
                    InfoFormat("bcc: {0}", bcc);
                }
                else
                {
                    Info("bccMapping: The mapping resulted in an empty payload.");
                }

                // subject
                InfoFormat("subjectMapping: {0}", SubjectMapping);
                jArray  = JArray.Parse(SubjectMapping);
                mapping = new Dictionary <string, MappingValue>();

                foreach (var item in jArray)
                {
                    var field = item.Value <string>("Field");
                    var val   = item.Value <object>("Value");
                    var type  = item.Value <string>("Type");

                    mapping.Add(field, new MappingValue {
                        Value = val, MappingType = type.ToLower() == "field" ? MappingType.Dynamic : MappingType.Static
                    });
                }

                // Genereate result
                result = WorkiomHelper.Map(trigger, mapping);

                if (result.Count > 0)
                {
                    subject = result.Values.First().ToString();
                    InfoFormat("subject: {0}", subject);
                }
                else
                {
                    Info("subjectMapping: The mapping resulted in an empty payload.");
                }

                // body
                InfoFormat("bodyMapping: {0}", BodyMapping);
                jArray  = JArray.Parse(BodyMapping);
                mapping = new Dictionary <string, MappingValue>();

                foreach (var item in jArray)
                {
                    var field = item.Value <string>("Field");
                    var val   = item.Value <object>("Value");
                    var type  = item.Value <string>("Type");

                    mapping.Add(field, new MappingValue {
                        Value = val, MappingType = type.ToLower() == "field" ? MappingType.Dynamic : MappingType.Static
                    });
                }

                // Genereate result
                result = WorkiomHelper.Map(trigger, mapping);

                if (result.Count > 0)
                {
                    body = result.Values.First().ToString();
                    InfoFormat("body: {0}", body);
                }
                else
                {
                    Info("bodyMapping: The mapping resulted in an empty payload.");
                }

                // Send email
                var tos  = to.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).Where(s => s != string.Empty).ToArray();
                var ccs  = cc.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).Where(s => s != string.Empty).ToArray();
                var bccs = bcc.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).Where(s => s != string.Empty).ToArray();
                Send(Host, Port, EnableSsl, User, Password, IsBodyHtml, tos, ccs, bccs, subject, body);
                Info("Mail sent.");
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception e)
            {
                ErrorFormat("An error occured while sending email. Error: {0}", e.Message);
                success = false;
            }

            var status = Status.Success;

            if (!success)
            {
                status = Status.Error;
            }

            Info("Task finished.");
            return(new TaskStatus(status));
        }
Ejemplo n.º 4
0
        public override TaskStatus Run()
        {
            Info("Updating Workiom record ...");

            //Thread.Sleep(10 * 1000); // To test queuing

            bool success = true;

            try
            {
                InfoFormat("Mapping: {0}", Mapping);

                // Retrieve trigger
                var trigger = new Trigger {
                    Payload = JsonConvert.DeserializeObject <Dictionary <string, object> >(Workflow.RestParams["Payload"])
                };


                // Retrieve mapping
                var jArray  = JArray.Parse(Mapping);
                var mapping = new Dictionary <string, MappingValue>();

                foreach (var item in jArray)
                {
                    var field = item.Value <string>("Field");
                    var val   = item.Value <object>("Value");
                    var type  = item.Value <string>("Type");

                    mapping.Add(field, new MappingValue {
                        Value = val, MappingType = type.ToLower() == "field" ? MappingType.Dynamic : MappingType.Static
                    });
                }

                // Genereate result
                var result = WorkiomHelper.Map(trigger, mapping);
                InfoFormat("Payload: {0}", JsonConvert.SerializeObject(result));

                // Retrieve recordId
                var recordId           = string.Empty;
                var recordIdSourceObj  = JObject.Parse(RecordIdSource);
                var recordIdSourceType = recordIdSourceObj.Value <string>("type");
                if (recordIdSourceType.ToLower() == "static")
                {
                    recordId = recordIdSourceObj.Value <string>("id");

                    if (string.IsNullOrEmpty(recordId))
                    {
                        Info("RecordId not found.");
                    }
                    else
                    {
                        InfoFormat("RecordId: {0}", recordId);
                    }

                    // Update record from result
                    success &= UpdateRecord(recordId, result);
                }
                else
                {
                    var linkedFieldId = recordIdSourceObj.Value <string>("linkedFieldId");

                    foreach (var kvp in trigger.Payload)
                    {
                        if (kvp.Key == linkedFieldId)
                        {
                            if (linkedFieldId == "_id")
                            {
                                var rId = kvp.Value.ToString();
                                success &= UpdateRecord(rId, result);
                            }
                            else
                            {
                                var linkedField = JArray.Parse(kvp.Value.ToString());
                                foreach (var recordField in linkedField)
                                {
                                    var rId = recordField.Value <string>("_id");
                                    success &= UpdateRecord(rId, result);
                                }
                            }

                            break;
                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception e)
            {
                ErrorFormat("An error occured while updating Workiom record. Error: {0}", e.Message);
                success = false;
            }

            var status = Status.Success;

            if (!success)
            {
                status = Status.Error;
            }

            Info("Task finished.");
            return(new TaskStatus(status));
        }