public int AddProject(ProjectViewModel project, string userId)
 {
     Project p = new Project();
     using (ApplicationDbContext db = new ApplicationDbContext())
     {
         if (project.ID == null)
         {
             p = new Project()
             {
                 Name = project.Name,
                 IsBaseLine = project.IsBaseLine,
                 Notes = project.Notes,
                 StartDate = project.StartDate,
                 FinishDate = project.FinishDate,
                 DateUpdated = DateTime.Now,
                 UserId = userId
             };
             db.Projects.Add(p);
         }
         else
         {
             p = db.Projects.Where(x => x.ProjectId == project.ID && x.UserId == userId).FirstOrDefault();
             if (p != null)
             {
                 p.Name = project.Name;
                 p.IsBaseLine = project.IsBaseLine;
                 p.Notes = project.Notes;
                 p.StartDate = project.StartDate;
                 p.FinishDate = project.FinishDate;
                 p.DateUpdated = DateTime.Now;
             }
         }
         db.SaveChanges();
         db.Entry(p).GetDatabaseValues();
     }
     return p.ProjectId;
 }
        public ActionResult Index(HttpPostedFileBase file)
        {
            BitMiracle.Docotic.LicenseManager.AddLicenseData("5DCLC-D241C-NJ5Q1-E88HK-87873");
            try
            {
                if (file != null && file.ContentLength > 0)
                {
                    string ext = Path.GetExtension(file.FileName);
                    if (ext == ".pdf")
                    {
                        string data = String.Empty;

                        #region Download/Save file to server
                        HttpServerUtility server = System.Web.HttpContext.Current.Server;
                        foreach (string folder in new string[] { "~/App_Data", "~/App_Data/Uploads" })
                        {
                            string map = server.MapPath(folder);
                            if (!Directory.Exists(map))
                                Directory.CreateDirectory(map);
                        }

                        var fileName = Path.GetFileName(file.FileName);

                        // store the file inside ~/App_Data/Uploads folder
                        var path = Path.Combine(server.MapPath("~/App_Data/Uploads"), fileName);
                        file.SaveAs(path);
                        #endregion

                        List<string> tasks = new List<string>();
                        List<int> completes = new List<int>();
                        List<DateTime> starts = new List<DateTime>();
                        List<DateTime> ends = new List<DateTime>();
                        List<string> preds = new List<string>();
                        List<string> resources = new List<string>();
                        string task = String.Empty;
                        string start = String.Empty;
                        string end = String.Empty;
                        string pred = String.Empty;
                        bool predAdded = false;
                        string res = String.Empty;
                        bool resAdded = false;

                        PdfDocument pdf = new PdfDocument(path);
                        for (var i = 0; i < pdf.Pages.Count; i++)
                        {
                            var page = pdf.Pages[i].GetWords();
                            for (var j = 0; j < page.Count; j++)
                            {
                                PdfTextData d = page[j];

                                double l = d.Bounds.Left;
                                double r = d.Bounds.Right;

                                #region Name
                                if (l >= 58 && r < 270)
                                {
                                    if (!resAdded && tasks.Count > 0)
                                    {
                                        resources.Add(res);
                                        res = String.Empty;
                                        resAdded = true;
                                    }

                                    predAdded = false;
                                    if (task.Length > 0) task += " ";
                                    task += d.Text;

                                    if (page[j + 1].Bounds.Left >= 270 && task.Length > 0)
                                    {
                                        if (task.Contains("Task Name"))
                                            task = task.Replace("Task Name", "");
                                        else
                                        {
                                            tasks.Add(task);
                                            task = String.Empty;
                                        }
                                    }
                                }
                                #endregion

                                #region Completion
                                if (l >= 340 && r < 390 && !String.IsNullOrWhiteSpace(d.Text))
                                {
                                    int complete = 0;
                                    try
                                    {
                                        complete = Convert.ToInt32(d.Text.Substring(0, d.Text.IndexOf('%')));
                                        completes.Add(complete);
                                    }
                                    catch { }
                                }
                                #endregion

                                #region Start
                                if (l >= 400 && r < 490 && !String.IsNullOrWhiteSpace(d.Text) && d.Text != "Start")
                                {
                                    if (start.Length == 0)
                                        start += d.Text;
                                    else
                                    {
                                        start += " " + d.Text;
                                        DateTime date = DateTime.Now;

                                        foreach (string f in new string[] { "ddd M/dd/yy", "ddd M/d/yy" })
                                        {
                                            if (DateTime.TryParseExact(start, f, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                                                break;
                                        }
                                        starts.Add(date);
                                        start = String.Empty;
                                    }
                                }
                                #endregion

                                #region Finish
                                if (l >= 490 && r < 546.9 && !String.IsNullOrWhiteSpace(d.Text) && d.Text != "Finish")
                                {
                                    if (end.Length == 0)
                                        end += d.Text;
                                    else
                                    {
                                        end += " " + d.Text;
                                        DateTime date = DateTime.Now;

                                        foreach (string f in new string[] { "ddd M/dd/yy", "ddd M/d/yy" })
                                        {
                                            if (DateTime.TryParseExact(end, f, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                                                break;
                                        }
                                        ends.Add(date);
                                        end = String.Empty;
                                    }
                                }
                                #endregion

                                #region Predecessors
                                if (l >= 545 && d.Text != "Predecessors" && d.Text != "Resource")
                                {
                                    if (r < 630)
                                    {
                                        predAdded = false;
                                        if (pred.Length > 0) pred += " ";
                                        pred += d.Text;

                                        if (page[j + 1].Bounds.Left >= 630)
                                        {
                                            preds.Add(pred);
                                            pred = String.Empty;
                                            predAdded = true;
                                        }
                                    }
                                    else if (l <= 632 && !predAdded)
                                    {
                                        preds.Add("");
                                    }
                                }
                                #endregion

                                #region Resources
                                if (l >= 630 && d.Text != "Resource" && d.Text != "Names")
                                {
                                    if (res.Length > 0) res += " ";
                                    res += d.Text;
                                    resAdded = false;
                                }
                                #endregion
                            }
                        }
                        // adding blank areas
                        preds.Insert(0, "");
                        preds.Insert(61, "");
                        resources.Add(res);
                        resources.Insert(61, "");

                        ViewBag.PDF_Text = "testing"; // data;

                        int projID = 0;
                        using (ApplicationDbContext db = new ApplicationDbContext())
                        {
                            var pro = new Project() { Name = "Magellan Health Expansion", UserId = UserId, DateUpdated = DateTime.Now, StartDate = new DateTime(2015, 06, 15), FinishDate = DateTime.Now };
                            db.Projects.Add(pro);
                            db.SaveChanges();
                            db.Entry(pro).GetDatabaseValues();
                            projID = pro.ProjectId;

                            for (var i = 0; i < 85; i++)
                            {
                                int? resID = null;
                                string resName = resources[i];

                                if (!String.IsNullOrWhiteSpace(resName))
                                {
                                    var resource = db.Resources.FirstOrDefault(r => r.CompanyName == resName && r.UserId == UserId);
                                    if (resource == null)
                                    {
                                        var re = new Resource() { CompanyName = resName, UserId = UserId };
                                        db.Resources.Add(re);
                                        db.SaveChanges();
                                        db.Entry(re).GetDatabaseValues();
                                        resID = re.ResourceId;
                                    }
                                    else
                                        resID = resource.ResourceId;
                                }

                                var t = new _Task() { Order = i > 60 ? i + 2 : i + 1, ProjectId = projID, Name = tasks[i], CompletionPercentage = completes[i], StartDate = starts[i], FinishDate = ends[i], Predecessors = preds[i], ResourceId = resID, DateUpdated = DateTime.Now };
                                db.Tasks.Add(t);
                                if (i % 50 == 0)
                                    db.SaveChanges();
                            }
                            db.SaveChanges();
                        }
                        return RedirectToAction("ProjectDetails", new { id = projID });
                    }
                    else
                    {
                        ViewBag.PDF_Error = "Incorrect file format.  Please choose a '.pdf' file.";
                        return View();
                    }
                }
                else
                {
                    ViewBag.PDF_Error = "You have not chosen a file to upload.";
                    return View();
                }
            }
            catch (Exception ex)
            {
                ViewBag.PDF_Error = "Error: " + ex.Message;
            }
            return View();
        }