private void processTask(
     ProjectDataSet.TaskRow taskRow,
     ProjectDataSet projectDataSet,
     ArrayList fieldsToPublish,
     SPListItem listItem,
     Hashtable hshFields,
     string taskUid,
     int pubType)
 {
     try
     {
         ProcessFieldsToPublish(taskRow, projectDataSet, fieldsToPublish, listItem);
         UpdateTaskUidAndTimeSheetId(taskRow, projectDataSet, listItem, taskUid, pubType);
         listItem.Update();
     }
     catch (SPException spException)
     {
         myLog.WriteEntry(
             $"SPException: Error processing Task ({taskRow.TASK_NAME}): {spException.Message}{spException.StackTrace}",
             EventLogEntryType.Error,
             331);
     }
     catch (Exception exception)
     {
         myLog.WriteEntry(
             $"Error processing Task ({taskRow.TASK_NAME}): {exception.Message}{exception.StackTrace}",
             EventLogEntryType.Error,
             330);
     }
 }
        private static void ProcessTaskPredecessors(
            ProjectDataSet.TaskRow taskRow,
            ProjectDataSet projectDataSet,
            SPListItem listItem,
            string wssFieldName)
        {
            Guard.ArgumentIsNotNull(wssFieldName, nameof(wssFieldName));
            Guard.ArgumentIsNotNull(listItem, nameof(listItem));
            Guard.ArgumentIsNotNull(projectDataSet, nameof(projectDataSet));
            Guard.ArgumentIsNotNull(taskRow, nameof(taskRow));

            const string LinkTypeZero  = "0";
            const string LinkTypeTwo   = "2";
            const string LinkTypeThree = "3";

            var predecessorDataSetBuilder = new StringBuilder();
            var dataRows = projectDataSet.Dependency.Select($"LINK_SUCC_UID=\'{taskRow.TASK_UID}\'");

            foreach (var dataRow in dataRows)
            {
                var drTask = (ProjectDataSet.TaskRow[])projectDataSet.Task.Select($"TASK_UID=\'{dataRow["LINK_PRED_UID"]}\'");

                if (drTask.Length > 0)
                {
                    predecessorDataSetBuilder.Append($",{drTask[0].TASK_ID}");

                    var linkType = dataRow[LinkType].ToString();

                    switch (linkType)
                    {
                    case LinkTypeZero:
                        predecessorDataSetBuilder.Append("FF");
                        break;

                    case LinkTypeTwo:
                        predecessorDataSetBuilder.Append("SF");
                        break;

                    case LinkTypeThree:
                        predecessorDataSetBuilder.Append("SS");
                        break;

                    default:
                        Trace.WriteLine($"Unexpected value : {linkType}");
                        break;
                    }
                }
            }

            var predecessorDataSet = predecessorDataSetBuilder.ToString();

            if (predecessorDataSet.Length > 1)
            {
                predecessorDataSet = predecessorDataSet.Substring(1);
            }

            listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = predecessorDataSet;
        }
        private void ProcessFilterCategoryOne(
            DataRow assignmentRow,
            ProjectDataSet projectDataSet,
            SPListItem listItem,
            ProjectDataSet.TaskRow taskRow,
            string fieldName,
            string wssFieldName,
            string assnFieldName,
            string fieldType,
            string multiplier)
        {
            Guard.ArgumentIsNotNull(multiplier, nameof(multiplier));
            Guard.ArgumentIsNotNull(fieldType, nameof(fieldType));
            Guard.ArgumentIsNotNull(assnFieldName, nameof(assnFieldName));
            Guard.ArgumentIsNotNull(wssFieldName, nameof(wssFieldName));
            Guard.ArgumentIsNotNull(fieldName, nameof(fieldName));
            Guard.ArgumentIsNotNull(taskRow, nameof(taskRow));
            Guard.ArgumentIsNotNull(listItem, nameof(listItem));
            Guard.ArgumentIsNotNull(projectDataSet, nameof(projectDataSet));
            Guard.ArgumentIsNotNull(assignmentRow, nameof(assignmentRow));

            if (fieldName == TaskPredecessors)
            {
                ProcessTaskPredecessors(taskRow, projectDataSet, listItem, wssFieldName);
            }
            else
            {
                string fieldData;

                try
                {
                    fieldData = assignmentRow[assnFieldName].ToString();
                }
                catch (Exception exception)
                {
                    Trace.WriteLine(exception);
                    fieldData = taskRow[fieldName].ToString();
                }

                if (fieldType == DateTimeText)
                {
                    if (!string.IsNullOrWhiteSpace(fieldData.Trim()))
                    {
                        listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = fieldData;
                    }
                }
                else
                {
                    if (multiplier != "1")
                    {
                        fieldData = multiplyField(fieldData, multiplier);
                    }

                    listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = fieldData;
                }
            }
        }
        private void PopulateListItem(
            int pubType,
            Guid newTransUid,
            Guid lastTransUid,
            ProjectDataSet projectDataSet,
            SPList taskCenter,
            SPListItem listItem,
            ProjectDataSet.TaskRow taskRow)
        {
            Guard.ArgumentIsNotNull(taskRow, nameof(taskRow));
            Guard.ArgumentIsNotNull(listItem, nameof(listItem));
            Guard.ArgumentIsNotNull(taskCenter, nameof(taskCenter));
            Guard.ArgumentIsNotNull(projectDataSet, nameof(projectDataSet));

            var canProcess = true;

            try
            {
                if (pubType == PubTypeTwo || pubType == PubTypeThree && lastTransUid != new Guid())
                {
                    if (listItem[TransUid].ToString() != lastTransUid.ToString())
                    {
                        canProcess = false;
                    }
                }
            }
            catch (Exception exception)
            {
                Trace.WriteLine(exception);
            }

            if (canProcess)
            {
                listItem[listItem.Fields.GetFieldByInternalName(TaskHierarchy).Id] = getHierarchy(projectDataSet, taskRow.TASK_PARENT_UID);
                const string AssignmentId = "0";
                listItem[listItem.Fields.GetFieldByInternalName(IsAssignment).Id] = AssignmentId;
                listItem[TransUid] = newTransUid.ToString();
                listItem[taskCenter.Fields.GetFieldByInternalName(Title).Id] = taskRow.TASK_NAME;

                if (!taskRow.IsTASK_WBSNull())
                {
                    listItem[taskCenter.Fields.GetFieldByInternalName(Wbs).Id] = taskRow.TASK_WBS;
                }

                listItem[taskCenter.Fields.GetFieldByInternalName(TaskUid).Id]   = taskRow.TASK_UID;
                listItem[taskCenter.Fields.GetFieldByInternalName(TaskOrder).Id] = taskRow.TASK_ID;
                listItem[Summary] = taskRow.TASK_IS_SUMMARY.ToString();

                if (!taskRow.IsTASK_NOTESNull())
                {
                    listItem[taskCenter.Fields.GetFieldByInternalName(Notes).Id] = taskRow.TASK_NOTES;
                }

                listItem[taskCenter.Fields.GetFieldByInternalName(LastPublished).Id] = DateTime.Now;
                processTask(taskRow, projectDataSet, arrFieldsToPublish, listItem, hshTaskCenterFields, taskRow.TASK_UID.ToString(), pubType);
            }
        }
        private string ProcessFieldCategoryOne(
            ProjectDataSet.TaskRow taskRow,
            ProjectDataSet projectDataSet,
            SPListItem listItem,
            string fieldName,
            string wssFieldName,
            string fieldType,
            string multiplier)
        {
            Guard.ArgumentIsNotNull(multiplier, nameof(multiplier));
            Guard.ArgumentIsNotNull(fieldType, nameof(fieldType));
            Guard.ArgumentIsNotNull(wssFieldName, nameof(wssFieldName));
            Guard.ArgumentIsNotNull(fieldName, nameof(fieldName));
            Guard.ArgumentIsNotNull(listItem, nameof(listItem));
            Guard.ArgumentIsNotNull(projectDataSet, nameof(projectDataSet));
            Guard.ArgumentIsNotNull(taskRow, nameof(taskRow));

            string fieldData = null;

            if (fieldName == TaskPredecessors)
            {
                ProcessTaskPredecessors(taskRow, projectDataSet, listItem, wssFieldName);
            }
            else if (fieldName != TaskResnames)
            {
                try
                {
                    fieldData = taskRow[fieldName].ToString();
                }
                catch (Exception exception)
                {
                    Trace.WriteLine(exception);
                }

                if (fieldType == DateTimeText)
                {
                    listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = !string.IsNullOrWhiteSpace(fieldData.Trim())
                        ? (object)DateTime.Parse(fieldData)
                        : null;
                }
                else
                {
                    if (multiplier != "1")
                    {
                        fieldData = multiplyField(fieldData, multiplier);
                    }

                    listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = fieldData;
                }
            }

            return(fieldData);
        }
        private static void MapTaskFields(Guid projectId,
                                          JiraTaskDto taskDto,
                                          ProjectDataSet.TaskRow task,
                                          ProjectDataSet.TaskCustomFieldsRow jiraProjectIdCustomFieldRow,
                                          ProjectDataSet.TaskCustomFieldsRow jiraProjectNameCustomFieldRow,
                                          ProjectDataSet.TaskCustomFieldsRow jiraTaskIdCustomFieldRow)
        {
            task.PROJ_UID         = projectId;
            task.TASK_NAME        = taskDto.Name;
            task.TASK_DUR_FMT     = (int)Task.DurationFormat.Day;
            task.TASK_START_DATE  = taskDto.Created;
            task.TASK_FINISH_DATE = taskDto.DueDate;
            task.TASK_PRIORITY    = taskDto.Priority;
            task.AddPosition      = (int)Task.AddPositionType.Last;
            if (TaskCompletedStatuses.Contains(taskDto.Status))
            {
                task.TASK_PHY_PCT_COMP = 100;
            }

            jiraProjectIdCustomFieldRow.TEXT_VALUE   = taskDto.ProjectId.ToString();
            jiraProjectNameCustomFieldRow.TEXT_VALUE = taskDto.ProjectName;
            jiraTaskIdCustomFieldRow.TEXT_VALUE      = taskDto.Id.ToString();
        }
        private void UpdateTaskUidAndTimeSheetId(
            ProjectDataSet.TaskRow taskRow,
            ProjectDataSet projectDataSet,
            SPListItem listItem,
            string taskUid,
            int pubType)
        {
            Guard.ArgumentIsNotNull(taskUid, nameof(taskUid));
            Guard.ArgumentIsNotNull(listItem, nameof(listItem));
            Guard.ArgumentIsNotNull(projectDataSet, nameof(projectDataSet));
            Guard.ArgumentIsNotNull(taskRow, nameof(taskRow));

            if (!string.IsNullOrWhiteSpace(taskUid))
            {
                listItem[TaskUid] = taskUid;
            }

            if (!string.IsNullOrWhiteSpace(strTimesheetField))
            {
                if (listItem.Fields.ContainsField(TimeSheetId))
                {
                    if (pubType == PubTypeOne)
                    {
                        listItem[TimeSheetId] = 0;
                    }
                    else
                    {
                        var drAssn = projectDataSet.TaskCustomFields.Select(
                            $"TASK_UID=\'{taskRow.TASK_UID}\' AND MD_PROP_UID=\'{strTimesheetField}\'");

                        listItem[TimeSheetId] = drAssn.Length > 0
                            ? (object)drAssn[0][FlagValue].ToString()
                            : 0;
                    }
                }
            }
        }
        private void ProcessFieldToPublish(
            ProjectDataSet.AssignmentRow assignmentRow,
            ProjectDataSet projectDataSet,
            SPListItem listItem,
            ProjectDataSet.TaskRow taskRow)
        {
            Guard.ArgumentIsNotNull(taskRow, nameof(taskRow));
            Guard.ArgumentIsNotNull(listItem, nameof(listItem));
            Guard.ArgumentIsNotNull(projectDataSet, nameof(projectDataSet));
            Guard.ArgumentIsNotNull(assignmentRow, nameof(assignmentRow));

            foreach (string field in arrFieldsToPublish)
            {
                var fieldSplit = field.Split('#');

                if (fieldSplit.Length < 8)
                {
                    throw new InvalidOperationException("Invalid fieldSplit length");
                }

                var fieldName     = fieldSplit[0];
                var wssFieldName  = fieldSplit[1];
                var assnFieldName = fieldSplit[2];
                var fieldCategory = fieldSplit[3];
                var fieldType     = fieldSplit[4];
                var multiplier    = fieldSplit[5];
                var rollDown      = fieldSplit[7];

                try
                {
                    if (fieldName == TaskResnames)
                    {
                        listItem[listItem.Fields.GetFieldByInternalName(ResourceNames).Id] = getResourceName(assignmentRow.RES_UID, projectDataSet);
                    }
                    else if (fieldName == TaskPctComp)
                    {
                        float pctWorkComplete = assignmentRow.ASSN_PCT_WORK_COMPLETE;
                        pctWorkComplete = pctWorkComplete / (float)100.00;
                        listItem[listItem.Fields.GetFieldByInternalName(PercentComplete).Id] = pctWorkComplete;
                    }
                    else
                    {
                        if (fieldCategory == FieldCategoryThree)
                        {
                            string fieldData = null;
                            var    drAssn    = projectDataSet.AssignmentCustomFields.Select(
                                $"ASSN_UID=\'{assignmentRow.ASSN_UID}\' AND MD_PROP_UID=\'{assnFieldName}\'");

                            if (drAssn.Length >= 1)
                            {
                                fieldData = GetFieldData(fieldType, drAssn, fieldName);
                            }
                            else if (rollDown == bool.TrueString)
                            {
                                var dataRows = projectDataSet.TaskCustomFields.Select(
                                    $"TASK_UID=\'{taskRow.TASK_UID}\' AND MD_PROP_ID=\'{fieldName}\'");

                                if (dataRows.Length >= 1)
                                {
                                    fieldData = GetFieldData(fieldType, dataRows, fieldName);
                                }
                            }

                            listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = fieldData;
                        }
                        else if (fieldCategory == FieldCategoryTwo)
                        {
                            var dataRows = projectDataSet.AssignmentCustomFields.Select(
                                $"ASSN_UID=\'{assignmentRow.ASSN_UID}\' AND MD_PROP_ID=\'{assnFieldName}\'");

                            if (dataRows.Length >= 1)
                            {
                                var fieldData = GetFieldData(fieldType, dataRows, fieldName);
                                listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = fieldData;
                            }
                        }
                        else if (fieldCategory == FieldCategoryOne)
                        {
                            ProcessFilterCategoryOne(
                                assignmentRow,
                                projectDataSet,
                                listItem,
                                taskRow,
                                fieldName,
                                wssFieldName,
                                assnFieldName,
                                fieldType,
                                multiplier);
                        }
                    }
                }
                catch (Exception ex)
                {
                    myLog.WriteEntry(
                        $"Error processing Assignment ({taskRow.TASK_NAME}) Field ({fieldName}): {ex.Message}{ex.StackTrace}",
                        EventLogEntryType.Error,
                        330);
                }
            }
        }
        private void PopulateResources(int pubType, ProjectDataSet projectDataSet, SPListItem listItem, ProjectDataSet.TaskRow taskRow)
        {
            Guard.ArgumentIsNotNull(taskRow, nameof(taskRow));
            Guard.ArgumentIsNotNull(listItem, nameof(listItem));
            Guard.ArgumentIsNotNull(projectDataSet, nameof(projectDataSet));

            const int PrefixLength      = 2;
            const int DefaultResourceId = 0;

            if (pubType == PubTypeOne)
            {
                listItem[AssignedTo] = string.Empty;

                if (listItem.ParentList.Fields.ContainsField(ResourceNames))
                {
                    var resourcesBuilder = new StringBuilder();

                    foreach (ProjectDataSet.AssignmentRow assignmentRow in projectDataSet.Assignment.Select($"TASK_UID=\'{taskRow.TASK_UID}\'"))
                    {
                        resourcesBuilder.Append($", {getResourceName(assignmentRow.RES_UID, projectDataSet)}");
                    }

                    var resources = resourcesBuilder.ToString();

                    if (resources.Length > PrefixLength)
                    {
                        resources = resources.Substring(PrefixLength);
                    }

                    listItem[listItem.ParentList.Fields.GetFieldByInternalName(ResourceNames).Id] = resources;
                }
            }

            if (pubType == PubTypeTwo)
            {
                var tempString       = string.Empty;
                var resourcesBuilder = new StringBuilder();

                foreach (ProjectDataSet.AssignmentRow assignmentRow in projectDataSet.Assignment.Select($"TASK_UID=\'{taskRow.TASK_UID}\'"))
                {
                    resourcesBuilder.Append($", {getResourceName(assignmentRow.RES_UID, projectDataSet)}");
                    var resId = getResourceWssId(assignmentRow.RES_UID_OWNER);

                    if (resId != DefaultResourceId)
                    {
                        tempString = $"{tempString};#{resId};#{getResourceName(assignmentRow.RES_UID_OWNER, projectDataSet)}";
                    }
                }

                if (tempString.Length > PrefixLength)
                {
                    tempString = tempString.Substring(PrefixLength);
                }

                var resources = resourcesBuilder.ToString();

                if (resources.Length > PrefixLength)
                {
                    resources = resources.Substring(PrefixLength);
                }

                listItem[AssignedTo] = tempString;

                if (listItem.ParentList.Fields.ContainsField(ResourceNames))
                {
                    listItem[listItem.ParentList.Fields.GetFieldByInternalName(ResourceNames).Id] = resources;
                }
            }

            if (pubType == PubTypeThree)
            {
                var resourceIdForTask = workspaceSynch.getResourceIdForTask(taskRow.TASK_UID, projectDataSet);

                listItem[AssignedTo] = resourceIdForTask != DefaultResourceId
                    ? (object)resourceIdForTask
                    : string.Empty;
            }
        }
        private void ProcessFieldsToPublish(
            ProjectDataSet.TaskRow taskRow,
            ProjectDataSet projectDataSet,
            ArrayList fieldsToPublish,
            SPListItem listItem)
        {
            Guard.ArgumentIsNotNull(listItem, nameof(listItem));
            Guard.ArgumentIsNotNull(fieldsToPublish, nameof(fieldsToPublish));
            Guard.ArgumentIsNotNull(projectDataSet, nameof(projectDataSet));
            Guard.ArgumentIsNotNull(taskRow, nameof(taskRow));

            const char FieldSplitChar = '#';

            foreach (string field in fieldsToPublish)
            {
                var fieldSplit = field.Split(FieldSplitChar);

                if (fieldSplit.Length < 6)
                {
                    throw new InvalidOperationException("Invalid fieldSplit count.");
                }

                var    fieldName     = fieldSplit[0];
                var    wssFieldName  = fieldSplit[1];
                var    fieldCategory = fieldSplit[3];
                var    fieldType     = fieldSplit[4];
                var    multiplier    = fieldSplit[5];
                string fieldData     = null;

                try
                {
                    if (fieldCategory == FieldCategoryThree)
                    {
                        var drAssn = projectDataSet.TaskCustomFields.Select($"TASK_UID=\'{taskRow.TASK_UID}\' AND MD_PROP_ID=\'{fieldName}\'");
                        fieldData = GetFieldData(fieldType, drAssn, fieldName);
                        listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = fieldData;
                    }
                    else if (fieldCategory == FieldCategoryTwo)
                    {
                        var drAssn = projectDataSet.TaskCustomFields.Select($"TASK_UID=\'{taskRow.TASK_UID}\' AND MD_PROP_ID=\'{fieldName}\'");

                        if (drAssn.Length >= 1)
                        {
                            fieldData = GetFieldData(fieldType, drAssn, fieldName, true);
                            listItem[listItem.Fields.GetFieldByInternalName(wssFieldName).Id] = fieldData;
                        }
                    }
                    else if (fieldCategory == FieldCategoryOne)
                    {
                        fieldData = ProcessFieldCategoryOne(taskRow, projectDataSet, listItem, fieldName, wssFieldName, fieldType, multiplier);
                    }
                }
                catch (Exception exception)
                {
                    myLog.WriteEntry(
                        $"Error setting field ({fieldName}) fieldValue ({fieldData}): {exception.Message}{exception.StackTrace}",
                        EventLogEntryType.Error,
                        335);
                }
            }
        }