public HttpResponseMessage SaveClientDocumentsMappings(ClientDocumentsMappings SaveRequest)
        {
            var context = new SMARTINDEXEntities();
            var ClientMappingDocTypes1 = (from c in context.ClientMappingDocTypes
                                          where c.Clientid == SaveRequest.ClientID
                                          select c).FirstOrDefault();

            if (ClientMappingDocTypes1 != null)
            {
                ClientMappingDocTypes1.Isactive     = false;
                ClientMappingDocTypes1.Modifiedby   = 1;
                ClientMappingDocTypes1.Modifieddate = DateTime.Now;
                context.SaveChanges();
            }

            ClientMappingDocType cst = new ClientMappingDocType
            {
                Clientid    = SaveRequest.ClientID,
                DocumentIDs = SaveRequest.DocumentIDs,
                Isactive    = true,
                Createdby   = 1,
                Createddate = DateTime.Now
            };

            context.ClientMappingDocTypes.Add(cst);
            context.SaveChanges();
            return(Request.CreateResponse(HttpStatusCode.OK, "Saved successfully"));
        }
        public HttpResponseMessage SaveExtractionConfig(List <PrimeCar> data)
        {
            var context = new SMARTINDEXEntities();
            List <ExtractionConfig> newCst_DocName = new List <ExtractionConfig>();

            foreach (var d in data)
            {
                //var GetdocName = (DBContext.Cst_DocName.Where(i => i.DocName == d.docName)).ToList();
                //var GetdocTypeName = (DBContext.Cst_DocTypes.Where(i => i.DocTypeName == d.docTypeName && i.Cst_DocNameID == GetdocName[0].Cst_DocNameID)).ToList();
                // if (GetdocTypeName.Count > 0) {
                var ExtractionConfig = new ExtractionConfig
                {
                    ExtractionType      = d.extractionType,
                    Pageindex           = d.pageindex,
                    FieldName           = d.fieldname,
                    FieldDataCordinates = d.fielddata,
                    Anchor = d.Anchor,
                    AnchorDataCordinates = d.AnchorCoordinates,
                    Brules        = d.bRules,
                    isactive      = true,
                    createdby     = 1,
                    createddate   = DateTime.Now,
                    Cst_DocTypeID = d.docTypeID
                };
                newCst_DocName.Add(ExtractionConfig);

                //}
            }
            context.ExtractionConfigs.AddRange(newCst_DocName);
            context.SaveChanges();
            return(Request.CreateResponse(HttpStatusCode.OK, "Saved successfully"));
        }
        public HttpResponseMessage SaveExtractionChanges(saveExtractiondata data)
        {
            var context = new SMARTINDEXEntities();

            var Batchdata = (from post in context.BatchExtractions
                             where (post.Batchid == data.batchid && post.Batchpath == data.Batchpath)
                             select post).FirstOrDefault();

            if (Batchdata != null)
            {
                Batchdata.JsonString = data.JsonString;
                context.SaveChanges();
            }
            // List<saveExtractiondata> UpdatedPages = new List<saveExtractiondata>();
            // var active = true;
            //  UpdatedPages = (from post in data.batchid select post).ToList();
            //  for (int i = 0; i < BatchExtractions.Count; i++)
            // {
            //     if (BatchExtractions != null)
            //     {
            //          var Batchdata = (from post in context.BatchExtractions
            //                               where (post.Isactive == active && post.Batchid==BatchExtractions[i].batchid && post.Batchpath==BatchExtractions[i].Batchpath)
            //                               select  post ).FirstOrDefault();;
            //         Batchdata.JsonString=BatchExtractions[i].JsonString;
            //         context.SaveChanges();
            //     }
            //}
            return(Request.CreateResponse(HttpStatusCode.OK, "Saved successfully"));
        }
        public HttpResponseMessage SaveNewDocumentName(SaveNewDocument data)
        {
            var context = new SMARTINDEXEntities();
            List <Cst_DocName> newCst_DocName = new List <Cst_DocName>();
            var NewDocName = new Cst_DocName
            {
                DocName   = data.DocName,
                Createdby = data.Createdby
            };

            newCst_DocName.Add(NewDocName);
            context.Cst_DocName.AddRange(newCst_DocName);
            context.SaveChanges();
            return(Request.CreateResponse(HttpStatusCode.OK, "Saved successfully"));
        }
        public HttpResponseMessage SaveClassifyData(List <SaveClassifyConfig> data)
        {
            var    context               = new SMARTINDEXEntities();
            string addedind              = "added";
            string modifiedind           = "Modified";
            string docNamechangeitem     = "docName";
            string docTypeNamechangeitem = "docTypeName";
            string Keywordchangeitem     = "keyword";
            //New doc Name
            var NewdocName = data.FindAll(e => (e.Ind == addedind && e.Changeitem == docNamechangeitem)).ToList();

            if (NewdocName.Count > 0)
            {
                List <Cst_DocName> new_DocName = new List <Cst_DocName>();
                foreach (var d in NewdocName)
                {
                    var cst_docName = new Cst_DocName
                    {
                        DocName  = d.docName,
                        isactive = true
                    };
                    new_DocName.Add(cst_docName);
                }
                context.Cst_DocName.AddRange(new_DocName);
                context.SaveChanges();
            }

            //New docType

            var NewdocTypeName = (from d in data where (d.Ind == addedind && d.Changeitem == docTypeNamechangeitem)select new { d.docName, d.docTypeName, d.havingPageNumbers, d.Ind, d.minPages, d.maxPages, d.pageNumberFormat, d.priority, d.addIfPrevTypeisSame, d.fileName }).ToList();

            if (NewdocTypeName.Count > 0)
            {
                var NewdocTypeNameList = (from para in NewdocTypeName
                                          join d1 in DBContext.Cst_DocName
                                          on para.docName equals d1.DocName
                                          select new { d1.Cst_DocNameID, para.docTypeName, para.havingPageNumbers, para.Ind, para.minPages, para.maxPages, para.pageNumberFormat, para.priority, para.addIfPrevTypeisSame, para.fileName }).ToList();

                List <Cst_DocTypes> new_DocTypeName = new List <Cst_DocTypes>();
                foreach (var d in NewdocTypeNameList)
                {
                    var cst_DocTypes = new Cst_DocTypes
                    {
                        DocTypeName         = d.docTypeName,
                        Cst_DocNameID       = d.Cst_DocNameID,
                        MinPages            = d.minPages,
                        MaxPages            = d.maxPages,
                        HavingPageNumbers   = d.havingPageNumbers,
                        PageNumberFormat    = d.pageNumberFormat,
                        AddIfPrevTypeisSame = d.addIfPrevTypeisSame,
                        Priority            = d.priority,
                        FileName            = d.fileName
                    };
                    new_DocTypeName.Add(cst_DocTypes);
                }
                context.Cst_DocTypes.AddRange(new_DocTypeName);
                context.SaveChanges();
            }

            //New Keyword

            var NewKeyword = (from d in data where (d.Ind == addedind && d.Changeitem == Keywordchangeitem)select new { d.docName, d.docTypeName, d.keyword, d.percentageofsimilarity, d.keywordThresholdpercetage, d.zoneArea, d.casesensitive, d.pagePosition }).ToList();

            if (NewKeyword.Count > 0)
            {
                var NewKeywordList = (from para in NewKeyword
                                      join d1 in DBContext.Cst_DocName
                                      on para.docName equals d1.DocName
                                      join d2 in DBContext.Cst_DocTypes
                                      on(d1.Cst_DocNameID.ToString() + '_' + para.docTypeName) equals(d2.Cst_DocNameID.ToString() + '_' + d2.DocTypeName)
                                      select new { d1.Cst_DocNameID, d2.Cst_DocTypeID, para.docTypeName, para.keyword, para.percentageofsimilarity, para.keywordThresholdpercetage, para.zoneArea, para.casesensitive, para.pagePosition }).ToList();

                List <Cst_Keywords> new_Keywords = new List <Cst_Keywords>();
                foreach (var d in NewKeywordList)
                {
                    var cst_Keywords = new Cst_Keywords
                    {
                        Keyword            = d.keyword,
                        Cst_DocTypeID      = d.Cst_DocTypeID,
                        Thresholdpercetage = Convert.ToDecimal(d.keywordThresholdpercetage),
                        ZoneArea           = d.zoneArea,
                        Casesensitive      = d.casesensitive == "1"?true:false,
                        PagePosition       = d.pagePosition,
                        IsActive           = 1
                                             //percentageofsimilarity = d.percentageofsimilarity
                    };
                    new_Keywords.Add(cst_Keywords);
                }
                context.Cst_Keywords.AddRange(new_Keywords);
                context.SaveChanges();
            }

            //Modified docType

            var ModdocTypeName = (from d in data where (d.Ind == modifiedind && d.Changeitem == docTypeNamechangeitem)select new { d.Changeitem, d.Ind, d.changeproperty, d.docName, d.docTypeName, d.newvalue, d.oldValue }).ToList();

            if (ModdocTypeName.Count > 0)
            {
                var ModdocTypeNameList = (from para in ModdocTypeName
                                          join d1 in DBContext.Cst_DocName
                                          on para.docName equals d1.DocName
                                          select new { d1.Cst_DocNameID, para.Changeitem, para.Ind, para.changeproperty, para.docName, para.docTypeName, para.newvalue, para.oldValue }).ToList();

                var ModdocTypeNameList1 = (from para in ModdocTypeNameList
                                           join d1 in DBContext.Cst_DocTypes
                                           on para.docTypeName + "_" + para.Cst_DocNameID.ToString() equals d1.DocTypeName + "_" + d1.Cst_DocNameID.ToString()
                                           select new { d1.Cst_DocNameID, d1.Cst_DocTypeID, para.Changeitem, para.Ind, para.changeproperty, para.docName, para.docTypeName, para.newvalue, para.oldValue }).ToList();
                Cst_DocTypes stud;
                foreach (var val in ModdocTypeNameList1)
                {
                    stud = DBContext.Cst_DocTypes.Where(s => s.Cst_DocTypeID == val.Cst_DocTypeID).FirstOrDefault <Cst_DocTypes>();
                    if (val.changeproperty == "docTypeName")
                    {
                        stud.DocTypeName = val.newvalue;
                    }
                    if (val.changeproperty == "fileName")
                    {
                        stud.FileName = val.newvalue;
                    }
                    if (val.changeproperty == "maxPages")
                    {
                        stud.MaxPages = Convert.ToInt32(val.newvalue);
                    }
                    if (val.changeproperty == "minPages")
                    {
                        stud.MinPages = Convert.ToInt32(val.newvalue);
                    }
                    if (val.changeproperty == "havingPageNumbers")
                    {
                        stud.HavingPageNumbers = val.newvalue;
                    }
                    if (val.changeproperty == "addIfPrevTypeisSame")
                    {
                        stud.AddIfPrevTypeisSame = val.newvalue;
                    }
                    if (val.changeproperty == "pageNumberFormat")
                    {
                        stud.PageNumberFormat = val.newvalue;
                    }
                    if (val.changeproperty == "priority")
                    {
                        stud.Priority = Convert.ToInt32(val.newvalue);
                    }
                    DBContext.SaveChanges();
                }
            }
            return(Request.CreateResponse(HttpStatusCode.OK, "Saved successfully"));
        }
        public HttpResponseMessage UploadFiles()
        {
            int iUploadedCnt = 0;

            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.

            string UploadedFiles   = ConfigurationManager.AppSettings["UploadedFiles"].ToString();
            string OcrProcessFiles = ConfigurationManager.AppSettings["OcrProcessFiles"].ToString();
            string OutputFiles     = ConfigurationManager.AppSettings["OutputFiles"].ToString();

            if (!System.IO.Directory.Exists(UploadedFiles))
            {
                CreateDirectoryRecursively(UploadedFiles);
            }

            if (!System.IO.Directory.Exists(OcrProcessFiles))
            {
                CreateDirectoryRecursively(OcrProcessFiles);
            }

            if (!System.IO.Directory.Exists(OutputFiles))
            {
                CreateDirectoryRecursively(OutputFiles);
            }

            System.Web.HttpFileCollection hfc = HttpContext.Current.Request.Files;

            var               context        = new SMARTINDEXEntities();
            List <Batch>      newBatches     = new List <Batch>();
            List <BatchAudit> newBatchAudits = new List <BatchAudit>();

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];
                if (hpf.ContentLength > 0)
                {
                    // SAVE THE FILES IN THE FOLDER.
                    string dattime       = DateTime.Now.ToString("yyyyMMddhhmmss");
                    string Sourcepath    = UploadedFiles + Path.GetFileNameWithoutExtension(hpf.FileName) + dattime;
                    string NewFolderpath = OutputFiles + Path.GetFileNameWithoutExtension(hpf.FileName) + dattime;
                    hpf.SaveAs(Sourcepath + Path.GetExtension(hpf.FileName));
                    System.IO.File.Copy(Sourcepath + Path.GetExtension(hpf.FileName), OcrProcessFiles + Path.GetFileNameWithoutExtension(hpf.FileName) + dattime + Path.GetExtension(hpf.FileName), true);

                    bool exists = System.IO.Directory.Exists(NewFolderpath);

                    if (!exists)
                    {
                        System.IO.Directory.CreateDirectory(NewFolderpath);
                        System.IO.Directory.CreateDirectory(NewFolderpath + "//Classified");
                        System.IO.Directory.CreateDirectory(NewFolderpath + "//UnClassified");
                        System.IO.Directory.CreateDirectory(NewFolderpath + "//Images");
                        System.IO.Directory.CreateDirectory(NewFolderpath + "//Pdfs");
                    }
                    iUploadedCnt = iUploadedCnt + 1;

                    var NewBatch = new Batch
                    {
                        Batchname     = Path.GetFileNameWithoutExtension(hpf.FileName) + dattime,
                        Createdby     = 1,
                        Createddate   = DateTime.Now,
                        Ocrengine     = "OCRengine1",
                        Statusid      = 2,
                        QueueStatusid = 8,
                        BatchPath     = UploadedFiles + Path.GetFileNameWithoutExtension(hpf.FileName) + dattime + Path.GetExtension(hpf.FileName)
                    };
                    newBatches.Add(NewBatch);

                    var NewBatchAudit = new BatchAudit
                    {
                        Batchname     = Path.GetFileNameWithoutExtension(hpf.FileName) + dattime,
                        Createdby     = 1,
                        Createddate   = DateTime.Now,
                        QueueStatusid = 8,
                        Statusid      = 2,
                        Startdate     = DateTime.Now
                    };
                    newBatchAudits.Add(NewBatchAudit);
                }
            }
            context.Batches.AddRange(newBatches);
            context.SaveChanges();
            if (iUploadedCnt > 0)
            {
                return(Request.CreateResponse(HttpStatusCode.OK, newBatches));
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }