private void handleResponseRequest(string data, string senderF, string formName, string formId, bool isEditedResponse)
 {
     Response.Clear();
     Response.ContentType = "text/plain";
     IncomingProcessor incomProc = new IncomingProcessor();
     string saveFormInstanceResult = incomProc.SaveFileResponse(data, senderF, formName, formId, isEditedResponse);
     Response.Write(saveFormInstanceResult);
 }
    public static void ProcessIncommingForms(RadButton btnProcessIncomingResponse, Literal litIncomingInfo)
    {
        bool concurrentCalls = false; //whether the process called while it is already running by the task scheduler.
        try
        {
            if (GlobalVariables.IsProcessIncomingRunning) //whether the processing is already running by the task scheduler.
            {
                if (btnProcessIncomingResponse != null)
                {
                    btnProcessIncomingResponse.Enabled = false;
                    litIncomingInfo.Text = "Processing is already Running. Please, wait.";
                }
                concurrentCalls = true;
                return;
            }

            GlobalVariables.IsProcessIncomingRunning = true;
            string[] files = Directory.GetFiles(Utility.GetResponseFilesFolderName() + "incoming");
            double step = 100.0000 / (double)files.Length;

            RadProgressContext ProgressContex = RadProgressContext.Current;
            ProgressContex.PrimaryTotal = files.Length;
            ProgressContex.PrimaryValue = 0;
            ProgressContex.PrimaryPercent = 0;

            IncomingProcessor incomProc = new IncomingProcessor();

            double ms = 0;
            Stopwatch sw = new Stopwatch();

            int formToProc = files.Length;
            if (formToProc > 10)
            {
                formToProc = 10;
            }
            for (int i = 0; i < files.Length; i++)
            {
                sw.Reset();
                sw.Start();
                string filePath = files[i];
                string fileName = Path.GetFileNameWithoutExtension(filePath);
                string senderNo = fileName.GetSubstringAfterLastChar('_');//"+" + fileName.Substring(18);
                using (StreamReader sr = File.OpenText(files[i]))
                {
                    string s = sr.ReadToEnd();
                    sr.Close();
                    incomProc.ProcessResponse(s, senderNo, fileName);
                }

                ProgressContex.CurrentOperationText = "Processing " + fileName;
                ProgressContex.PrimaryValue = (i + 1).ToString();
                ProgressContex.PrimaryPercent = (step * (i + 1)).ToString("00.##");
                sw.Stop();

                TimeSpan ts = sw.Elapsed;
                ms += ts.TotalMilliseconds;
                TimeSpan ts2 = TimeSpan.FromMilliseconds((ms / (double)(i + 1)) * (files.Length - (i + 1)));
                ts.Add(ts2);

                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}", ts2.Hours, ts2.Minutes, ts2.Seconds);
                ProgressContex.TimeEstimated = elapsedTime;
            }
            ProgressContex.CurrentOperationText = "Insert Done. Pleas Wait...";
            Thread.Sleep(2000);
            ProgressContex.CurrentOperationText = "Calculating Indexes. Pleas Wait...";
            Thread.Sleep(1100);
            incomProc.GenerateIndexesHash();
            ProgressContex.CurrentOperationText = "Calculating Server Side Calulated Fields. Please Wait...";
            Thread.Sleep(1100);
            incomProc.GenerateCalculatedField();
            ProgressContex.CurrentOperationText = "Validating User to Response Permission. Please Wait...";
            Thread.Sleep(1100);
            incomProc.GenerateUserToFormResponseAssociation();
            GlobalVariables.LastProcessFormResponsesTime = "Last Process Form Responses: " + DateTime.Now.ToString();
        }
        catch (Exception ex)
        {
            concurrentCalls = false;
            LogUtils.WriteErrorLog(ex.ToString());
        }
        finally
        {
            if (concurrentCalls == false)
            {
                GlobalVariables.IsProcessIncomingRunning = false;
                CheckProcessResponsesStatus(btnProcessIncomingResponse, litIncomingInfo);
            }
        }
    }