private TMSDictionary GetTMSDataFromDB()
        {
            DataTable tmsTasksFromDBDataTable = new DataTable();

            if (!Consts.TEST_MODE)
            {
                //sw.StartStopwatchAndPrintMessage("Getting TMS tasks from DB and making cache...", output);
                //Task<DataTable> execureQueryTask = DBService.ExecuteQueryAsync(DBService.GET_ALL_TMS_TASKS);
                tmsTasksFromDBDataTable           = DBService.ExecuteQuery(DBService.GET_ALL_TMS_TASKS);
                tmsTasksFromDBDataTable.TableName = "TMS_DATA";
                tmsTasksFromDBDataTable.WriteXml(Consts.FILE_NAMES.DB_TMS_CACHE);
                //sw.StopStopwatchAndPrintDoneMessageWithElapsedTime("Getting TMS tasks from DB and making cache...done", output);
            }
            else
            {
                //sw.StartStopwatchAndPrintMessage("Getting TMS tasks from cache...", output);
                DataSet ds = new DataSet();
                ds.ReadXml(Consts.FILE_NAMES.DB_TMS_CACHE, XmlReadMode.InferSchema);
                tmsTasksFromDBDataTable = ds.Tables[0];
                //sw.StopStopwatchAndPrintDoneMessageWithElapsedTime("Getting TMS tasks from cache...done", output);
            }


            //Dictionary<string, TMSItem> dbTMSDict = new Dictionary<string, TMSItem>();
            TMSDictionary dbTMSDict = new TMSDictionary();

            //List<string> team_members = TeamService.GetDEV1TeamMembersTMSLogins();

            //sw.StartStopwatchAndPrintMessage("Conversion DB items to objects...", output);
            foreach (DataRow tmsTaskFromDB in tmsTasksFromDBDataTable.Rows)
            {
                TMSItem item = new TMSItem();
                item.Source      = Consts.SRC_DB;
                item.AssignedTo  = Convert.ToString(tmsTaskFromDB["EMPLOYEE"]);
                item.TMS         = Convert.ToString(tmsTaskFromDB["TMS_ID"]);
                item.Status      = Convert.ToString(tmsTaskFromDB["STATUS"]);
                item.Urgency     = Convert.ToString(tmsTaskFromDB["URG"]);
                item.LastActText = Convert.ToString(tmsTaskFromDB["LASTACTION_TXT"]);
                item.LastActDate = Convert.ToDateTime(tmsTaskFromDB["LASTACTION_DATE"]);
                item.Desctiption = Convert.ToString(tmsTaskFromDB["TASK_DESCRIPTION"]);
                item.SDId        = Convert.ToString(tmsTaskFromDB["SOFTDEV_ID"]);
                dbTMSDict.Add(item);
            }

            //sw.StopStopwatchAndPrintDoneMessageWithElapsedTime("Conversion DB items to objects...done", output);
            return(dbTMSDict);
        }
        private static TMSDictionary GetTMSDataFromRedMine(IOutputable output)
        {
            //Stopwatch sw = new Stopwatch();
            //sw.StartStopwatchAndPrintMessage("Getting TMS data from RedMine...", output);
            NameValueCollection parameters = new NameValueCollection {
                { "status_id", "*" }
            };
            //List<TMSItem> redMineTMSList = new List<TMSItem>();

            TMSDictionary dict = new TMSDictionary();

            if (!Consts.TEST_MODE)
            {
                foreach (var issue in RMManegerService.RMManager.GetObjects <Issue>(parameters).Where(issue => issue.Project.Id == Consts.PROJECT_NAMES.TMS.MACBI.PROBLEMS))
                {
                    /*string subject = issue.Subject;
                     *
                     * TMSItem itemFromRM = new TMSItem();
                     * itemFromRM.Source = Consts.SRC_RM;
                     * itemFromRM.AssignedTo = issue.AssignedTo.TryGetName();
                     * itemFromRM.Desctiption = issue.Description;
                     *
                     * string[] subjectSplitted = issue.Subject.Split('-');
                     * itemFromRM.TMS = issue.Subject.Split('-')[0].Trim() + "-" + issue.Subject.Split('-')[1].Trim();
                     * itemFromRM.Status = issue.Status.TryGetName();
                     *
                     * //redMineTMSList.Add(itemFromRM);
                     */
                    dict.Add(new TMSItem(issue));
                }

                //redMineTMSList.SerializeTMSItemData();
                dict.SerializeTMSItemData(Consts.FILE_NAMES.RM_TMS_CACHE);
            }
            else
            {
                dict = TMSDictionary.DeserializeTMSItemData(Consts.FILE_NAMES.RM_TMS_CACHE);
            }

            // sw.StopStopwatchAndPrintDoneMessageWithElapsedTime("Getting TMS data from RedMine...done", output);
            return(dict);
        }
        public void AddMissingTMSTasksToRedmine()
        {
            sw.StartStopwatchAndPrintMessage("Getting TMS tasks from DB and making cache...", output);
            GetherSyncData();

            IdentifiableName p = IdentifiableName.Create <Project>(Consts.PROJECT_NAMES.TMS.MACBI.PROBLEMS); //to-be-changed
            //IdentifiableName u = IdentifiableName.Create<User>(USER_ID);
            //var user = RMManegerService.RMManager.GetObject<User>(USER_ID.ToString(), null);
            IdentifiableName u        = IdentifiableName.Create <IdentifiableName>(USER_ID);
            IdentifiableName priority = IdentifiableName.Create <IdentifiableName>(PRIORITY_HIGH);


            //var newIssue = new Issue { Subject = subject, Project = p, Description = details };
            //RMManegerService.RMManager.CreateObject(newIssue);

            int limit = 5;

            List <string> listOfAddedItems    = new List <string>();
            List <string> listOfNOTAddedItems = new List <string>();

            foreach (TMSItem item in dbTMSDict.GetNotClosedNotUsedAssignedToDEV1ItemList(TeamService.UsersTMSLogin))
            {
                string description = item.Desctiption;
                //check if really there is no such a TMS in RM (maybe it was already closed)
                TMSItem tmsItem = rmTMSDict.Get(item.TMS);

                //redMineTMSList.Contains()
                if (tmsItem == null)
                {
                    string firstLine = null;

                    if (description.Contains("\r\n") || description.Contains("."))
                    {
                        firstLine = description.Substring(0, description.Contains("\r\n") ? description.IndexOf("\r\n") : description.IndexOf("."));
                    }
                    else
                    {
                        firstLine = description;
                    }

                    if (firstLine.Length > 255)
                    {
                        firstLine = firstLine.Substring(0, 240);//255 is max lenght of subject.

                        int lastDotIndex = firstLine.LastIndexOf(".");
                        if (lastDotIndex > -1)
                        {
                            firstLine = firstLine.Substring(0, lastDotIndex);
                        }
                    }

                    string subject  = string.Format("{0} - {1}", item.TMS, firstLine);
                    var    newIssue = new Issue {
                        Subject = subject, Project = p, Description = description, AssignedTo = u, Priority = priority
                    };
                    RMManegerService.RMManager.CreateObject(newIssue);
                    listOfAddedItems.Add(subject);
                }
                else
                {
                    listOfNOTAddedItems.Add(string.Format("{0}", tmsItem));
                    //Console.WriteLine("Item can't be added as it already exists: {0}", tmsItem);
                }

                if (Consts.TEST_MODE)
                {
                    if (--limit == 0)
                    {
                        return;
                    }
                }
            }

            output.WriteLine("List of added items ({0})", listOfAddedItems.Count);
            output.WriteLine("List of NOT added items ({0})", listOfNOTAddedItems.Count);

            if (listOfAddedItems.Count > 0)
            {
                output.WriteLine("Cache needs to be refreshed!");
                rmTMSDict = null;
                GetherSyncData();
            }
            sw.StopStopwatchAndPrintDoneMessageWithElapsedTime(output);
        }
 public void ClearCache()
 {
     dbTMSDict = null;
     rmTMSDict = null;
     output.WriteLine("Cache cleared...");
 }
        public void GetherSyncData()
        {
            output.WriteLine("Gethering of synchronization data for {0} started...", this.client);
            bool tmsRefreshNeeded = dbTMSDict == null;
            bool rmRefreshNeeded  = rmTMSDict == null;

            Task <TMSDictionary> tmsTask = null;
            Task <TMSDictionary> rmTask  = null;


            if (dbTMSDict == null)
            {
                Stopwatch sw_tmsTask = new Stopwatch();
                sw_tmsTask.Start();
                tmsTask = Task.Factory.StartNew <TMSDictionary>(() =>
                {
                    return(GetTMSDataFromDB());
                }).ContinueWith <TMSDictionary>((x) => {
                    sw_tmsTask.Stop();
                    output.WriteLineToBuffer("Data from TMS read ({0}s)...", sw_tmsTask.Elapsed.TotalSeconds); return(x.Result);
                });
            }
            else
            {
                output.WriteLineToBuffer("Data from TMS already in cache...");
            }


            if (rmTMSDict == null)
            {
                Stopwatch sw_rmTask = new Stopwatch();
                sw_rmTask.Start();
                rmTask = Task.Factory.StartNew <TMSDictionary>(() =>
                {
                    return(GetTMSDataFromRedMine(output));
                }).ContinueWith <TMSDictionary>((x) => {
                    sw_rmTask.Stop();
                    output.WriteLineToBuffer("Data from RM read ({0}s)...", sw_rmTask.Elapsed.TotalSeconds); return(x.Result);
                });
            }
            else
            {
                output.WriteLineToBuffer("Data from RM already in cache...");
            }

            if (tmsTask != null && rmTask != null)
            {
                Task.WaitAll(new Task[] { tmsTask, rmTask });
                dbTMSDict = tmsTask.Result;
                rmTMSDict = rmTask.Result;
            }
            else if (tmsTask != null)
            {
                tmsTask.Wait();
                dbTMSDict = tmsTask.Result;
            }
            else if (rmTask != null)
            {
                rmTask.Wait();
                rmTMSDict = rmTask.Result;
            }


            #region TO BE UNCOMMENTED

            //foreach (var issue in RMManegerService.RMManager.GetObjects<Issue>(parameters).Where(issue => issue.Project.Id == TMS_PROJECT_ID))
            //{
            //    string subject = issue.Subject;

            //    TMSItem itemFromRM = new TMSItem();
            //    itemFromRM.AssignedTo = issue.AssignedTo.TryGetName();
            //    itemFromRM.Desctiption = issue.Description;

            //    string[] subjectSplitted = issue.Subject.Split('-');
            //    itemFromRM.TMS = issue.Subject.Split('-')[0].Trim() + "-" + issue.Subject.Split('-')[1].Trim();
            //    itemFromRM.Status = issue.Status.TryGetName();

            //    TMSItem dbTMSItem = null;
            //    if (dbTMSDict.TryGetValue(itemFromRM.TMS, out dbTMSItem))
            //    {
            //        Console.WriteLine("Element found\nTMS: {0}\nRM: {1}", itemFromRM.ToString(), dbTMSItem);

            //    }
            //    else
            //    {
            //        Console.WriteLine("Element not found!");
            //    }

            //    redMineTMSList.Add(itemFromRM);
            //}
            #endregion


            output.WriteLineToBuffer("Gethering of synchronization data for {0} finished...", this.client);
            output.FlushWriteLines();
            return;
        }