Пример #1
0
        public ISOTask ExportPrescription(WorkItem workItem, int gridType, Prescription prescription)
        {
            ISOTask task = new ISOTask();

            //Task ID
            string taskID = workItem.Id.FindIsoId() ?? GenerateId();

            task.TaskID = taskID;
            ExportIDs(workItem.Id, taskID);

            //Designator
            task.TaskDesignator = prescription.Description;

            //Customer Ref
            if (workItem.GrowerId.HasValue)
            {
                task.CustomerIdRef = TaskDataMapper.InstanceIDMap.GetISOID(workItem.GrowerId.Value);
            }

            //Farm Ref
            if (workItem.FarmId.HasValue)
            {
                task.FarmIdRef = TaskDataMapper.InstanceIDMap.GetISOID(workItem.FarmId.Value);
            }

            //Partfield Ref
            if (workItem.CropZoneId.HasValue)
            {
                task.PartFieldIdRef = TaskDataMapper.InstanceIDMap.GetISOID(workItem.CropZoneId.Value);
            }
            else if (workItem.FieldId.HasValue)
            {
                task.PartFieldIdRef = TaskDataMapper.InstanceIDMap.GetISOID(workItem.FieldId.Value);
            }

            //Comments
            if (workItem.Notes.Any())
            {
                CommentAllocationMapper canMapper = new CommentAllocationMapper(TaskDataMapper);
                task.CommentAllocations = canMapper.ExportCommentAllocations(workItem.Notes).ToList();
            }

            //Worker Allocations
            if (workItem.PeopleRoleIds.Any())
            {
                WorkerAllocationMapper workerAllocationMapper = new WorkerAllocationMapper(TaskDataMapper);
                List <PersonRole>      personRoles            = new List <PersonRole>();
                foreach (int id in workItem.PeopleRoleIds)
                {
                    PersonRole personRole = DataModel.Catalog.PersonRoles.FirstOrDefault(p => p.Id.ReferenceId == id);
                    if (personRole != null)
                    {
                        personRoles.Add(personRole);
                    }
                }
                task.WorkerAllocations = workerAllocationMapper.ExportWorkerAllocations(personRoles).ToList();
            }

            //Guidance Allocations
            if (workItem.GuidanceAllocationIds.Any())
            {
                GuidanceAllocationMapper  guidanceAllocationMapper = new GuidanceAllocationMapper(TaskDataMapper);
                List <GuidanceAllocation> allocations = new List <GuidanceAllocation>();
                foreach (int id in workItem.GuidanceAllocationIds)
                {
                    GuidanceAllocation allocation = DataModel.Documents.GuidanceAllocations.FirstOrDefault(p => p.Id.ReferenceId == id);
                    if (allocation != null)
                    {
                        allocations.Add(allocation);
                    }
                }
                task.GuidanceAllocations = guidanceAllocationMapper.ExportGuidanceAllocations(allocations).ToList();
            }

            //Connections
            if (workItem.EquipmentConfigurationGroup != null)
            {
                task.Connections = _connectionMapper.ExportConnections(workItem.Id.ReferenceId, workItem.EquipmentConfigurationGroup.EquipmentConfigurations).ToList();
            }

            //Status
            if (workItem.StatusUpdates == null || workItem.StatusUpdates.Count == 0)
            {
                task.TaskStatus = ISOEnumerations.ISOTaskStatus.Planned;
            }
            else if (workItem.StatusUpdates.Count == 1)
            {
                StatusUpdate lastStatus = workItem.StatusUpdates.OrderByDescending(su => su.TimeStamp).Last();
                task.TaskStatus = ExportStatus(lastStatus.Status);
            }

            //Prescription
            if (prescription is RasterGridPrescription)
            {
                ExportRasterPrescription(task, prescription as RasterGridPrescription, gridType);
            }
            else if (prescription is VectorPrescription)
            {
                ExportVectorPrescription(task, prescription as VectorPrescription);
            }
            else if (prescription is ManualPrescription)
            {
                ExportManualPresciption(task, prescription as ManualPrescription);
            }

            return(task);
        }
Пример #2
0
        private WorkItem ImportWorkItem(ISOTask isoPrescribedTask)
        {
            WorkItem workItem = new WorkItem();

            //Task ID
            ImportIDs(workItem.Id, isoPrescribedTask.TaskID);

            //Grower ID
            workItem.GrowerId = TaskDataMapper.InstanceIDMap.GetADAPTID(isoPrescribedTask.CustomerIdRef);

            //Farm ID
            workItem.FarmId = TaskDataMapper.InstanceIDMap.GetADAPTID(isoPrescribedTask.FarmIdRef);

            //Field/CropZone
            int?pfdID = TaskDataMapper.InstanceIDMap.GetADAPTID(isoPrescribedTask.PartFieldIdRef);

            if (pfdID.HasValue)
            {
                if (DataModel.Catalog.CropZones.Any(c => c.Id.ReferenceId == pfdID.Value))
                {
                    workItem.CropZoneId = pfdID.Value;
                }
                else
                {
                    workItem.FieldId = pfdID.Value;
                    if (DataModel.Catalog.CropZones.Count(c => c.FieldId == pfdID) == 1)
                    {
                        //There is a single cropZone for the field.
                        workItem.CropZoneId = DataModel.Catalog.CropZones.Single(c => c.FieldId == pfdID).Id.ReferenceId;
                    }
                }
            }

            //Status
            workItem.StatusUpdates = new List <StatusUpdate>()
            {
                new StatusUpdate()
                {
                    Status = ImportStatus(isoPrescribedTask.TaskStatus)
                }
            };

            //Responsible Worker
            if (!string.IsNullOrEmpty(isoPrescribedTask.ResponsibleWorkerIdRef))
            {
                ISOWorker worker   = ISOTaskData.ChildElements.OfType <ISOWorker>().FirstOrDefault(w => w.WorkerId == isoPrescribedTask.ResponsibleWorkerIdRef);
                int?      personID = TaskDataMapper.InstanceIDMap.GetADAPTID(isoPrescribedTask.ResponsibleWorkerIdRef);
                if (personID.HasValue)
                {
                    //Create a Role
                    PersonRole role = new PersonRole()
                    {
                        PersonId = personID.Value
                    };

                    //Add to Catalog
                    DataModel.Catalog.PersonRoles.Add(role);
                    if (workItem.PeopleRoleIds == null)
                    {
                        workItem.PeopleRoleIds = new List <int>();
                    }

                    //Add to Task
                    workItem.PeopleRoleIds.Add(role.Id.ReferenceId);
                }
            }

            //Worker Allocation
            if (isoPrescribedTask.WorkerAllocations.Any())
            {
                WorkerAllocationMapper wanMapper   = new WorkerAllocationMapper(TaskDataMapper);
                List <PersonRole>      personRoles = wanMapper.ImportWorkerAllocations(isoPrescribedTask.WorkerAllocations).ToList();

                //Add to Catalog
                DataModel.Catalog.PersonRoles.AddRange(personRoles);

                //Add to Task
                if (workItem.PeopleRoleIds == null)
                {
                    workItem.PeopleRoleIds = new List <int>();
                }
                workItem.PeopleRoleIds.AddRange(personRoles.Select(p => p.Id.ReferenceId));
            }

            if (isoPrescribedTask.GuidanceAllocations.Any())
            {
                GuidanceAllocationMapper  ganMapper   = new GuidanceAllocationMapper(TaskDataMapper);
                List <GuidanceAllocation> allocations = ganMapper.ImportGuidanceAllocations(isoPrescribedTask.GuidanceAllocations).ToList();

                //Add to Catalog
                List <GuidanceAllocation> guidanceAllocations = DataModel.Documents.GuidanceAllocations as List <GuidanceAllocation>;
                if (guidanceAllocations != null)
                {
                    guidanceAllocations.AddRange(allocations);
                }

                //Add to Task
                if (workItem.GuidanceAllocationIds == null)
                {
                    workItem.GuidanceAllocationIds = new List <int>();
                }
                workItem.GuidanceAllocationIds.AddRange(allocations.Select(p => p.Id.ReferenceId));
            }

            //Comments
            if (isoPrescribedTask.CommentAllocations.Any())
            {
                CommentAllocationMapper canMapper = new CommentAllocationMapper(TaskDataMapper);
                workItem.Notes = canMapper.ImportCommentAllocations(isoPrescribedTask.CommentAllocations).ToList();
            }

            //Prescription
            if (isoPrescribedTask.HasPrescription)
            {
                Prescription rx = PrescriptionMapper.ImportPrescription(isoPrescribedTask, workItem);

                if (rx == null)
                {
                    return(workItem);
                }
                //Add to the Prescription the Catalog
                List <Prescription> prescriptions = DataModel.Catalog.Prescriptions as List <Prescription>;
                prescriptions?.Add(rx);

                //Add A WorkItemOperation
                WorkItemOperation operation = new WorkItemOperation();
                operation.PrescriptionId = rx.Id.ReferenceId;

                //Add the operation to the Documents and reference on the WorkItem
                List <WorkItemOperation> operations =
                    DataModel.Documents.WorkItemOperations as List <WorkItemOperation>;
                operations?.Add(operation);

                workItem.WorkItemOperationIds.Add(operation.Id.ReferenceId);

                //Track any prescription IDs to map to any completed TimeLog data
                _rxIDsByTask.Add(isoPrescribedTask.TaskID, rx.Id.ReferenceId);
            }

            return(workItem);
        }
Пример #3
0
        private LoggedData ImportLoggedData(ISOTask isoLoggedTask)
        {
            LoggedData loggedData = new LoggedData();

            loggedData.OperationData = new List <OperationData>();

            //Task ID
            if (!ImportIDs(loggedData.Id, isoLoggedTask.TaskID))
            {
                //In the case where a TSK contains both TZN and TLG data, we'll store the LoggedData as the mapped Task.
                //The Prescription ID will be assigned to the OperationData objects by means of the dictionary in this class.
                TaskDataMapper.InstanceIDMap.ReplaceADAPTID(isoLoggedTask.TaskID, loggedData.Id.ReferenceId);
            }

            //Task Name
            loggedData.Description = isoLoggedTask.TaskDesignator;

            //Grower ID
            loggedData.GrowerId = TaskDataMapper.InstanceIDMap.GetADAPTID(isoLoggedTask.CustomerIdRef);

            //Farm ID
            loggedData.FarmId = TaskDataMapper.InstanceIDMap.GetADAPTID(isoLoggedTask.FarmIdRef);

            //Field ID
            int?pfdID = TaskDataMapper.InstanceIDMap.GetADAPTID(isoLoggedTask.PartFieldIdRef);

            if (pfdID.HasValue)
            {
                if (DataModel.Catalog.CropZones.Any(c => c.Id.ReferenceId == pfdID.Value))
                {
                    loggedData.CropZoneId = pfdID.Value;
                }
                else
                {
                    loggedData.FieldId = pfdID.Value;
                    if (DataModel.Catalog.CropZones.Count(c => c.FieldId == pfdID) == 1)
                    {
                        //There is a single cropZone for the field.
                        loggedData.CropZoneId = DataModel.Catalog.CropZones.Single(c => c.FieldId == pfdID).Id.ReferenceId;
                    }
                }
            }

            //Responsible Worker
            if (!string.IsNullOrEmpty(isoLoggedTask.ResponsibleWorkerIdRef))
            {
                ISOWorker worker   = ISOTaskData.ChildElements.OfType <ISOWorker>().FirstOrDefault(w => w.WorkerId == isoLoggedTask.ResponsibleWorkerIdRef);
                int?      personID = TaskDataMapper.InstanceIDMap.GetADAPTID(isoLoggedTask.ResponsibleWorkerIdRef);
                if (personID.HasValue)
                {
                    //Create a Role
                    PersonRole role = new PersonRole()
                    {
                        PersonId = personID.Value
                    };

                    //Add to Catalog
                    DataModel.Catalog.PersonRoles.Add(role);
                    if (loggedData.PersonRoleIds == null)
                    {
                        loggedData.PersonRoleIds = new List <int>();
                    }

                    //Add to Task
                    loggedData.PersonRoleIds.Add(role.Id.ReferenceId);
                }
            }

            //Worker Allocations
            if (isoLoggedTask.WorkerAllocations.Any())
            {
                WorkerAllocationMapper wanMapper   = new WorkerAllocationMapper(TaskDataMapper);
                List <PersonRole>      personRoles = wanMapper.ImportWorkerAllocations(isoLoggedTask.WorkerAllocations).ToList();

                //Add to Catalog
                DataModel.Catalog.PersonRoles.AddRange(personRoles);
                if (loggedData.PersonRoleIds == null)
                {
                    loggedData.PersonRoleIds = new List <int>();
                }

                //Add to Task
                loggedData.PersonRoleIds.AddRange(personRoles.Select(p => p.Id.ReferenceId));
            }

            //Guidance Allocations
            if (isoLoggedTask.GuidanceAllocations.Any())
            {
                GuidanceAllocationMapper  ganMapper   = new GuidanceAllocationMapper(TaskDataMapper);
                List <GuidanceAllocation> allocations = ganMapper.ImportGuidanceAllocations(isoLoggedTask.GuidanceAllocations).ToList();

                //Add to Catalog
                List <GuidanceAllocation> guidanceAllocations = DataModel.Documents.GuidanceAllocations as List <GuidanceAllocation>;
                if (guidanceAllocations != null)
                {
                    guidanceAllocations.AddRange(allocations);
                }

                //Add to Task
                if (loggedData.GuidanceAllocationIds == null)
                {
                    loggedData.GuidanceAllocationIds = new List <int>();
                }
                loggedData.GuidanceAllocationIds.AddRange(allocations.Select(p => p.Id.ReferenceId));
            }

            //Comments
            if (isoLoggedTask.CommentAllocations.Any())
            {
                CommentAllocationMapper canMapper = new CommentAllocationMapper(TaskDataMapper);
                loggedData.Notes = canMapper.ImportCommentAllocations(isoLoggedTask.CommentAllocations).ToList();
            }

            //Summaries
            if (isoLoggedTask.Times.Any(t => t.HasStart && t.HasType)) //Nothing added without a Start & Type attribute
            {
                //An ADAPT LoggedData has exactly one summary.   This is what necessitates that ISO Task maps to LoggedData and ISO TimeLog maps to one or more Operation Data objects
                Summary summary = ImportSummary(isoLoggedTask, loggedData);
                if (DataModel.Documents.Summaries == null)
                {
                    DataModel.Documents.Summaries = new List <Summary>();
                }
                (DataModel.Documents.Summaries as List <Summary>).Add(summary);
                loggedData.SummaryId = summary.Id.ReferenceId;
            }

            //Operation Data
            if (isoLoggedTask.TimeLogs.Any())
            {
                //Find ID for any Prescription that may also be tied to this task
                int?rxID = null;
                if (_rxIDsByTask.ContainsKey(isoLoggedTask.TaskID))
                {
                    rxID = _rxIDsByTask[isoLoggedTask.TaskID];
                }

                loggedData.OperationData = TimeLogMapper.ImportTimeLogs(isoLoggedTask, rxID);
            }

            //Connections
            if (isoLoggedTask.Connections.Any())
            {
                IEnumerable <EquipmentConfiguration> equipConfigs = ConnectionMapper.ImportConnections(isoLoggedTask);

                loggedData.EquipmentConfigurationGroup = new EquipmentConfigurationGroup();
                loggedData.EquipmentConfigurationGroup.EquipmentConfigurations = equipConfigs.ToList();

                //Make a reference to the IDs on the OperationData
                foreach (OperationData operationData in loggedData.OperationData)
                {
                    operationData.EquipmentConfigurationIds.AddRange(equipConfigs.Select(e => e.Id.ReferenceId));
                }

                DataModel.Catalog.EquipmentConfigurations.AddRange(equipConfigs);
            }

            return(loggedData);
        }
Пример #4
0
        private ISOTask Export(LoggedData loggedData)
        {
            ISOTask task = null;

            //Try to map to a pre-existing Work Item task where appropriate
            if (loggedData.OperationData.All(o => o.PrescriptionId.HasValue) &&
                loggedData.OperationData.Select(o => o.PrescriptionId.Value).Distinct().Count() == 1)
            {
                int rxID = loggedData.OperationData.First().PrescriptionId.Value;
                if (_taskIDsByPrescription.ContainsKey(rxID))
                {
                    task = ISOTaskData.ChildElements.OfType <ISOTask>().FirstOrDefault(t => t.TaskID == _taskIDsByPrescription[rxID]);
                }
            }

            if (task == null)
            {
                task = new ISOTask();

                //Task ID
                string taskID = loggedData.Id.FindIsoId() ?? GenerateId();
                task.TaskID = taskID;
            }


            if (!ExportIDs(loggedData.Id, task.TaskID))
            {
                //Update the mapping to represent the completed task
                TaskDataMapper.InstanceIDMap.ReplaceADAPTID(task.TaskID, loggedData.Id.ReferenceId);
            }

            //Task Designator
            task.TaskDesignator = loggedData.Description;

            //Customer Ref
            if (loggedData.GrowerId.HasValue)
            {
                task.CustomerIdRef = TaskDataMapper.InstanceIDMap.GetISOID(loggedData.GrowerId.Value);
            }

            //Farm Ref
            if (loggedData.FarmId.HasValue)
            {
                task.FarmIdRef = TaskDataMapper.InstanceIDMap.GetISOID(loggedData.FarmId.Value);
            }

            //Partfield Ref
            if (loggedData.CropZoneId.HasValue)
            {
                task.PartFieldIdRef = TaskDataMapper.InstanceIDMap.GetISOID(loggedData.CropZoneId.Value);
            }
            else if (loggedData.FieldId.HasValue)
            {
                task.PartFieldIdRef = TaskDataMapper.InstanceIDMap.GetISOID(loggedData.FieldId.Value);
            }

            //Status
            task.TaskStatus = ISOEnumerations.ISOTaskStatus.Completed;

            if (loggedData.OperationData.Any())
            {
                //Time Logs
                task.TimeLogs = TimeLogMapper.ExportTimeLogs(loggedData.OperationData, TaskDataPath).ToList();

                //Connections
                IEnumerable <int> taskEquipmentConfigIDs = loggedData.OperationData.SelectMany(o => o.EquipmentConfigurationIds);
                if (taskEquipmentConfigIDs.Any())
                {
                    IEnumerable <EquipmentConfiguration> taskEquipmentConfigs = DataModel.Catalog.EquipmentConfigurations.Where(d => taskEquipmentConfigIDs.Contains(d.Id.ReferenceId));
                    task.Connections = ConnectionMapper.ExportConnections(loggedData.Id.ReferenceId, taskEquipmentConfigs).ToList();
                }
            }

            //Summaries
            if (loggedData.SummaryId.HasValue)
            {
                Summary summary = DataModel.Documents.Summaries.FirstOrDefault(s => s.Id.ReferenceId == loggedData.SummaryId.Value);
                if (summary != null)
                {
                    task.Times.AddRange(ExportSummary(summary));
                }

                List <ISOProductAllocation> productAllocations = GetProductAllocationsForSummary(summary);
                if (productAllocations != null)
                {
                    task.ProductAllocations.AddRange(productAllocations);
                }
            }

            //Comments
            if (loggedData.Notes.Any())
            {
                CommentAllocationMapper canMapper = new CommentAllocationMapper(TaskDataMapper);
                task.CommentAllocations = canMapper.ExportCommentAllocations(loggedData.Notes).ToList();
            }

            //Worker Allocations
            if (loggedData.PersonRoleIds.Any())
            {
                WorkerAllocationMapper workerAllocationMapper = new WorkerAllocationMapper(TaskDataMapper);
                List <PersonRole>      personRoles            = new List <PersonRole>();
                foreach (int id in loggedData.PersonRoleIds)
                {
                    PersonRole personRole = DataModel.Catalog.PersonRoles.FirstOrDefault(p => p.Id.ReferenceId == id);
                    if (personRole != null)
                    {
                        personRoles.Add(personRole);
                    }
                }
                task.WorkerAllocations = workerAllocationMapper.ExportWorkerAllocations(personRoles).ToList();
            }

            //Guidance Allocations
            if (loggedData.GuidanceAllocationIds.Any())
            {
                GuidanceAllocationMapper  guidanceAllocationMapper = new GuidanceAllocationMapper(TaskDataMapper);
                List <GuidanceAllocation> allocations = new List <GuidanceAllocation>();
                foreach (int id in loggedData.GuidanceAllocationIds)
                {
                    GuidanceAllocation allocation = DataModel.Documents.GuidanceAllocations.FirstOrDefault(p => p.Id.ReferenceId == id);
                    if (allocation != null)
                    {
                        allocations.Add(allocation);
                    }
                }
                task.GuidanceAllocations = guidanceAllocationMapper.ExportGuidanceAllocations(allocations).ToList();
            }

            return(task);
        }