private FormResponseResource ReadRootResponseResource(IResponseContext responseContext)
        {
            try
            {
                var rootResponseId         = responseContext.RootResponseId ?? responseContext.ResponseId;
                var rootFormCollectionName = responseContext.RootFormName ?? GetRootFormName(responseContext.RootFormId);
                var rootFormCollectionUri  = GetCollectionUri(rootFormCollectionName);
                // Set some common query options
                FeedOptions queryOptions = new FeedOptions {
                    MaxItemCount = 1
                };

                var query = Client.CreateDocumentQuery(rootFormCollectionUri,
                                                       SELECT
                                                       + AssembleSelect(rootFormCollectionName, "*")
                                                       + FROM + rootFormCollectionName
                                                       + WHERE
                                                       + AssembleExpressions(rootFormCollectionName, Expression("id", EQ, rootResponseId),
                                                                             And_Expression(FRP_RecStatus, NE, RecordStatus.Deleted))
                                                       , queryOptions);

                FormResponseResource formResponseResource = query.AsEnumerable().FirstOrDefault();
                return(formResponseResource);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return(null);
        }
        /// <summary>
        /// RestoreLastResponseSnapshot
        /// </summary>
        /// <param name="snapshotFormResponseResource"></param>
        /// <returns></returns>
        public bool RestoreLastResponseSnapshot(FormResponseResource snapshotFormResponseResource)
        {
            var    snapshotFormResponseProperties = snapshotFormResponseResource.FormResponseProperties;
            string rootFormName          = snapshotFormResponseProperties.FormName;
            Uri    rootFormCollectionUri = GetCollectionUri(rootFormName);
            var    documentResponse      = ExecuteWithFollowOnAction(() => Client.UpsertDocumentAsync(rootFormCollectionUri, snapshotFormResponseResource));
            var    isSuccessful          = documentResponse != null;

            return(isSuccessful);
        }
 private async Task <bool> PhysicallyDeleteResponse(FormResponseResource formResponseResource, FormResponseProperties formResponseProperties)
 {
     if (formResponseProperties.IsRootResponse)
     {
         var result = await Client.DeleteDocumentAsync(formResponseResource.SelfLink).ConfigureAwait(false);
     }
     else // if (formResponseProperties.IsChildResponse)
     {
         var result = await Client.UpsertDocumentAsync(formResponseResource.SelfLink, formResponseResource, null).ConfigureAwait(false);
     }
     return(await Task.FromResult <bool>(false).ConfigureAwait(false));
 }
 private static void ResolveMissingContext(FormResponseResource formResponseResource, IResponseContext responseContext)
 {
     // verify that the response context is fully resolved.
     FormResponseResource.ChildResponseContext responseDirectory = null;
     if (formResponseResource.ChildResponseContexts.TryGetValue(responseContext.ResponseId, out responseDirectory))
     {
         responseContext.FormId           = responseDirectory.FormId;
         responseContext.FormName         = responseDirectory.FormName;
         responseContext.ParentFormId     = responseDirectory.ParentFormId;
         responseContext.ParentFormName   = responseDirectory.ParentFormName;
         responseContext.ParentResponseId = responseDirectory.ParentResponseId;
     }
 }
        /// <summary>
        /// UpdateAttachment
        /// </summary>
        /// <param name="responseContext"></param>
        /// <param name="responseStatus"></param>
        /// <param name="userId"></param>
        /// <param name="newResponseStatus"></param>
        /// <returns></returns>
        public async Task <bool> UpdateAttachment(IResponseContext responseContext, int responseStatus, int userId = 0, int newResponseStatus = 0)
        {
            newResponseStatus = responseStatus;
            bool isSuccessful   = false;
            bool deleteResponse = false;

            try
            {
                Uri rootFormCollectionUri = GetCollectionUri(responseContext.RootFormName);
                FormResponseResource formResponseResource = ReadRootResponseResource(responseContext);
                if (formResponseResource != null)
                {
                    var formResponseProperties = formResponseResource.FormResponseProperties;
                    //Is status is Saved and check if attachment is existed or not.If attachment is null and delete attachment
                    if (newResponseStatus == RecordStatus.Saved)
                    {
                        Attachment attachment = ReadResponseAttachment(responseContext, AttachmentId);
                        if (attachment != null)
                        {
                            deleteResponse = DeleteAttachment(attachment);
                        }

                        formResponseResource.CascadeThroughChildren(formResponseProperties, f => { if (f.RecStatus != RecordStatus.Deleted)
                                                                                                   {
                                                                                                       f.RecStatus = RecordStatus.Saved;
                                                                                                   }
                                                                    });
                    }
                    if (newResponseStatus != formResponseProperties.RecStatus)
                    {
                        switch (newResponseStatus)
                        {
                        case RecordStatus.Saved:
                            formResponseProperties.IsNewRecord = false;
                            formResponseProperties.RecStatus   = RecordStatus.Saved;
                            var formResponseSave = await Client.UpsertDocumentAsync(rootFormCollectionUri, formResponseResource).ConfigureAwait(false);

                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(isSuccessful);
        }
Ejemplo n.º 6
0
        public FormResponseResource RetrieveResponseAttachment(Attachment attachmentInfo)
        {
            try
            {
                var attachmentResponse = attachmentInfo.GetPropertyValue <string>("SurveyDocument") ?? attachmentInfo.GetPropertyValue <string>("SurveyDocumnet");

                FormResponseResource formResponseResource = attachmentResponse != null
                    ? JsonConvert.DeserializeObject <FormResponseResource>(attachmentResponse)
                    : null;

                return(formResponseResource);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        private async Task <bool> LogicallyDeleteResponse(FormResponseResource formResponseResource, FormResponseProperties formResponseProperties)
        {
            if (formResponseProperties.IsRootResponse)
            {
                formResponseResource.LogicalCascadeDeleteChildren(formResponseProperties);
            }
            else // if (formResponseProperties.IsChildResponse)
            {
                formResponseResource.LogicalCascadeDeleteChildren(formResponseProperties);
            }


            return(await Task.FromResult <bool>(true).ConfigureAwait(false));

            //if (formResponseProperties.RecStatus != RecordStatus.Deleted)
            //{
            //    formResponseResource.LogicalCascadeDelete(formResponseProperties);
            //    var result = await Client.UpsertDocumentAsync(formResponseResource.SelfLink, formResponseResource, null).ConfigureAwait(false);
            //}
        }
        /// <summary>
        /// This method will save form properties
        /// and also used for delete operation.Ex:RecStatus=0
        /// </summary>
        /// <param name="formResponsePropertiesList"></param>
        /// <returns></returns>
        public async Task <ResourceResponse <Document> > SaveFormResponsePropertiesAsync(List <FormResponseProperties> formResponsePropertiesList)
        {
            var now = DateTime.UtcNow;
            var rootFormResponseProperties     = formResponsePropertiesList[0];
            ResourceResponse <Document> result = null;
            var formResponseResource           = new FormResponseResource();
            var rootFormCollectionUri          = GetCollectionUri(rootFormResponseProperties.RootFormName);

            try
            {
                //Verify that the Root Response Id exists
                formResponseResource = ReadRootResponseResource((IResponseContext)rootFormResponseProperties);
                if (formResponseResource == null)
                {
                    if (rootFormResponseProperties.IsRootResponse)
                    {
                        formResponseResource = new FormResponseResource
                        {
                            Id = rootFormResponseProperties.RootResponseId,
                            FormResponseProperties = rootFormResponseProperties
                        };
                    }
                    else // if (responseContext.IsChildResponse)
                    {
                        throw new Exception("Can't add a child response without an existing root response");
                    }
                }
                var existingFormResponseProperties = formResponseResource.FormResponseProperties;
                foreach (var formResponseProperties in formResponsePropertiesList)
                {
                    formResponseProperties.LastSaveTime = now;

                    if (formResponseProperties.IsRootResponse)
                    {
                        formResponseProperties.FirstSaveTime      = existingFormResponseProperties.FirstSaveTime;
                        formResponseProperties.FirstSaveLogonName = existingFormResponseProperties.FirstSaveLogonName;

                        //Create attachment if transitioning from Saved to InProcess
                        if (existingFormResponseProperties.RecStatus == RecordStatus.Saved && formResponseProperties.RecStatus == RecordStatus.InProcess)
                        {
                            Attachment attachment           = null;
                            var        existingResponseJson = JsonConvert.SerializeObject(formResponseResource);
                            attachment = CreateResponseAttachment(formResponseResource.SelfLink, (IResponseContext)formResponseProperties, AttachmentId, existingResponseJson);
                        }

                        formResponseResource.FormResponseProperties = formResponseProperties;
                    }
                    else /* if (responseContext.IsChildResponse) */
                    {
                        formResponseResource.AddOrReplaceChildResponse(formResponseProperties);
                    }
                }

                // Update the root response and its children.
                result = await Client.UpsertDocumentAsync(rootFormCollectionUri, formResponseResource).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return(result);
        }
        /// <summary>
        /// ToHierarchicalFormResponseDetail
        /// </summary>
        /// <param name="formResponseProperties"></param>
        /// <param name="formResponseResource"></param>
        /// <returns></returns>
        public static FormResponseDetail ToHierarchicalFormResponseDetail(this FormResponseProperties formResponseProperties, FormResponseResource formResponseResource)
        {
            FormResponseDetail        formResponseDetail = formResponseProperties.ToFormResponseDetail();
            List <FormResponseDetail> flattened          = formResponseDetail.FlattenHierarchy();

            formResponseResource.CascadeThroughChildren(formResponseProperties,
                                                        frp =>
            {
                var frd    = frp.ToFormResponseDetail();
                var parent = flattened.Where(f => f.ResponseId == frp.ParentResponseId).SingleOrDefault();
                if (parent != null)
                {
                    parent.ChildFormResponseDetailList.Add(frd);
                }
                else
                {
                    throw new KeyNotFoundException();
                }
                flattened.Add(frd);
            });
            return(formResponseDetail);
        }