Пример #1
0
        public ISO11783_TaskData Export(ApplicationDataModel.ADM.ApplicationDataModel adm)
        {
            AdaptDataModel = adm;

            //TaskData
            ISOTaskData = new ISO11783_TaskData();
            ISOTaskData.VersionMajor = 4;
            ISOTaskData.VersionMinor = 0;
            ISOTaskData.ManagementSoftwareManufacturer = "AgGateway";
            ISOTaskData.ManagementSoftwareVersion      = "1.0";
            ISOTaskData.DataTransferOrigin             = ISOEnumerations.ISOTaskDataTransferOrigin.FMIS;
            ISOTaskData.TaskControllerManufacturer     = "";
            ISOTaskData.TaskControllerVersion          = "";

            //LinkList
            ISOTaskData.LinkList = new ISO11783_LinkList();
            ISOTaskData.LinkList.VersionMajor = 4;
            ISOTaskData.LinkList.VersionMinor = 0;
            ISOTaskData.LinkList.ManagementSoftwareManufacturer = "AgGateway";
            ISOTaskData.LinkList.ManagementSoftwareVersion      = "1.0";
            ISOTaskData.LinkList.DataTransferOrigin             = ISOEnumerations.ISOTaskDataTransferOrigin.FMIS;
            ISOTaskData.LinkList.TaskControllerManufacturer     = "";
            ISOTaskData.LinkList.TaskControllerVersion          = "";
            ISOTaskData.LinkList.FileVersion = "";
            UniqueIDMapper = new UniqueIdMapper(ISOTaskData.LinkList);

            //Crops
            if (adm.Catalog.Crops != null)
            {
                CropTypeMapper            cropMapper = new CropTypeMapper(this, ProductGroupMapper);
                IEnumerable <ISOCropType> crops      = cropMapper.ExportCropTypes(adm.Catalog.Crops);
                ISOTaskData.ChildElements.AddRange(crops);
            }

            //Products
            if (adm.Catalog.Products != null)
            {
                IEnumerable <Product> products = AdaptDataModel.Catalog.Products;
                if (products.Any())
                {
                    ProductMapper            productMapper = new ProductMapper(this, ProductGroupMapper);
                    IEnumerable <ISOProduct> isoProducts   = productMapper.ExportProducts(products);
                    ISOTaskData.ChildElements.AddRange(isoProducts);
                }
            }

            //Growers
            if (adm.Catalog.Growers != null)
            {
                CustomerMapper            customerMapper = new CustomerMapper(this);
                IEnumerable <ISOCustomer> customers      = customerMapper.Export(adm.Catalog.Growers);
                ISOTaskData.ChildElements.AddRange(customers);
            }

            //Farms
            if (adm.Catalog.Farms != null)
            {
                FarmMapper            farmMapper = new FarmMapper(this);
                IEnumerable <ISOFarm> farms      = farmMapper.Export(adm.Catalog.Farms);
                ISOTaskData.ChildElements.AddRange(farms);
            }

            //Fields & Cropzones
            if (adm.Catalog.Fields != null)
            {
                PartfieldMapper fieldMapper = new PartfieldMapper(this);
                foreach (Field field in adm.Catalog.Fields)
                {
                    IEnumerable <CropZone> fieldCropZones = adm.Catalog.CropZones.Where(c => c.FieldId == field.Id.ReferenceId);
                    if (fieldCropZones.Count() == 0)
                    {
                        //Export Field
                        ISOPartfield isoField = fieldMapper.ExportField(field);
                        ISOTaskData.ChildElements.Add(isoField);
                    }
                    else if (fieldCropZones.Count() == 1)
                    {
                        //Export Cropzone to retain the crop reference
                        ISOPartfield isoField = fieldMapper.ExportCropZone(fieldCropZones.First());
                        ISOTaskData.ChildElements.Add(isoField);
                    }
                    else
                    {
                        //Export both
                        ISOPartfield isoField = fieldMapper.ExportField(field);
                        ISOTaskData.ChildElements.Add(isoField);
                        foreach (CropZone cropZone in fieldCropZones)
                        {
                            ISOPartfield isoCropField = fieldMapper.ExportCropZone(cropZone);
                            ISOTaskData.ChildElements.Add(isoCropField);
                        }
                    }
                }
            }

            //Workers
            if (adm.Catalog.Persons != null)
            {
                WorkerMapper            workerMapper = new WorkerMapper(this);
                IEnumerable <ISOWorker> workers      = workerMapper.Export(adm.Catalog.Persons);
                ISOTaskData.ChildElements.AddRange(workers);
            }

            //Devices
            if (adm.Catalog.DeviceModels.Any())
            {
                DeviceMapper            dvcMapper = new DeviceMapper(this);
                IEnumerable <ISODevice> devices   = dvcMapper.ExportDevices(adm.Catalog.DeviceModels);
                ISOTaskData.ChildElements.AddRange(devices);
            }

            //Tasks
            if (AdaptDataModel.Documents.WorkItems.Any() || AdaptDataModel.Documents.LoggedData.Any())
            {
                TaskMapper taskMapper = new TaskMapper(this);
                if (AdaptDataModel.Documents.WorkItems != null)
                {
                    //Prescriptions
                    int gridType = 1;
                    if (Properties != null)
                    {
                        Int32.TryParse(Properties.GetProperty(ISOGrid.GridTypeProperty), out gridType);
                    }
                    if (gridType == 1 || gridType == 2)
                    {
                        IEnumerable <ISOTask> plannedTasks = taskMapper.Export(AdaptDataModel.Documents.WorkItems, gridType);
                        ISOTaskData.ChildElements.AddRange(plannedTasks);
                    }
                    else
                    {
                        AddError($"Invalid Grid Type {gridType}.  WorkItems will not be exported", null, "TaskDataMapper");
                    }
                }

                if (AdaptDataModel.Documents.LoggedData != null)
                {
                    //LoggedData
                    IEnumerable <ISOTask> loggedTasks = taskMapper.Export(AdaptDataModel.Documents.LoggedData);
                    ISOTaskData.ChildElements.AddRange(loggedTasks.Where(t => !ISOTaskData.ChildElements.OfType <ISOTask>().Contains(t)));
                }
            }

            //Add Comments
            ISOTaskData.ChildElements.AddRange(CommentMapper.ExportedComments);

            //Add LinkList Attached File Reference
            if (ISOTaskData.LinkList.LinkGroups.Any())
            {
                ISOAttachedFile afe = new ISOAttachedFile();
                afe.FilenamewithExtension = "LINKLIST.XML";
                afe.Preserve        = ISOEnumerations.ISOAttachedFilePreserve.Preserve;
                afe.ManufacturerGLN = string.Empty;
                afe.FileType        = 1;
                ISOTaskData.ChildElements.Add(afe);
            }

            return(ISOTaskData);
        }
Пример #2
0
        public ApplicationDataModel.ADM.ApplicationDataModel Import(ISO11783_TaskData taskData)
        {
            ISOTaskData    = taskData;
            UniqueIDMapper = new UniqueIdMapper(ISOTaskData.LinkList);

            AdaptDataModel         = new ApplicationDataModel.ADM.ApplicationDataModel();
            AdaptDataModel.Catalog = new Catalog()
            {
                Description = taskData.FilePath
            };
            AdaptDataModel.Documents = new Documents();

            //Comments
            CodedCommentListMapper commentListMapper = new CodedCommentListMapper(this);
            CodedCommentMapper     commentMapper     = new CodedCommentMapper(this, commentListMapper);

            //Crops - several dependencies require import prior to Products
            IEnumerable <ISOCropType> crops = taskData.ChildElements.OfType <ISOCropType>();

            if (crops.Any())
            {
                CropTypeMapper cropMapper = new CropTypeMapper(this, ProductGroupMapper);
                AdaptDataModel.Catalog.Crops.AddRange(cropMapper.ImportCropTypes(crops));
            }

            //Products
            IEnumerable <ISOProduct> products = taskData.ChildElements.OfType <ISOProduct>();

            if (products.Any())
            {
                ProductMapper         productMapper = new ProductMapper(this, ProductGroupMapper);
                IEnumerable <Product> adaptProducts = productMapper.ImportProducts(products);
                AdaptDataModel.Catalog.Products.AddRange(adaptProducts.Where(p => !AdaptDataModel.Catalog.Products.Contains(p)));
            }

            //Growers
            IEnumerable <ISOCustomer> customers = taskData.ChildElements.OfType <ISOCustomer>();

            if (customers.Any())
            {
                CustomerMapper customerMapper = new CustomerMapper(this);
                AdaptDataModel.Catalog.Growers.AddRange(customerMapper.Import(customers));
            }

            //Farms
            IEnumerable <ISOFarm> farms = taskData.ChildElements.OfType <ISOFarm>();

            if (farms.Any())
            {
                FarmMapper farmMapper = new FarmMapper(this);
                AdaptDataModel.Catalog.Farms.AddRange(farmMapper.Import(farms));
            }

            //Fields & Cropzones
            IEnumerable <ISOPartfield> partFields = taskData.ChildElements.OfType <ISOPartfield>();

            if (partFields.Any())
            {
                PartfieldMapper partFieldMapper = new PartfieldMapper(this);
                AdaptDataModel.Catalog.Fields.AddRange(partFieldMapper.ImportFields(partFields));
                AdaptDataModel.Catalog.CropZones.AddRange(partFieldMapper.ImportCropZones(partFields));
            }

            //Devices
            IEnumerable <ISODevice> devices = taskData.ChildElements.OfType <ISODevice>();

            if (devices.Any())
            {
                DeviceElementHierarchies = new DeviceElementHierarchies(devices, RepresentationMapper);

                DeviceMapper deviceMapper = new DeviceMapper(this);
                AdaptDataModel.Catalog.DeviceModels.AddRange(deviceMapper.ImportDevices(devices));
            }

            //Workers
            IEnumerable <ISOWorker> workers = taskData.ChildElements.OfType <ISOWorker>();

            if (workers.Any())
            {
                WorkerMapper workerMapper = new WorkerMapper(this);
                AdaptDataModel.Catalog.Persons.AddRange(workerMapper.Import(workers));
            }


            //Cultural Practices
            IEnumerable <ISOCulturalPractice> practices = taskData.ChildElements.OfType <ISOCulturalPractice>();

            if (practices.Any())
            {
                foreach (ISOCulturalPractice cpc in practices)
                {
                    (AdaptDataModel.Documents.WorkOrders as List <WorkOrder>).Add(new WorkOrder()
                    {
                        Description = cpc.CulturalPracticeDesignator
                    });
                }
            }

            //OperationTechniques
            IEnumerable <ISOOperationTechnique> techniques = taskData.ChildElements.OfType <ISOOperationTechnique>();

            if (techniques.Any())
            {
                foreach (ISOOperationTechnique otq in techniques)
                {
                    (AdaptDataModel.Documents.WorkOrders as List <WorkOrder>).Add(new WorkOrder()
                    {
                        Description = otq.OperationTechniqueDesignator
                    });
                }
            }

            IEnumerable <ISOTask> prescribedTasks = taskData.ChildElements.OfType <ISOTask>().Where(t => t.IsWorkItemTask);
            IEnumerable <ISOTask> loggedTasks     = taskData.ChildElements.OfType <ISOTask>().Where(t => t.IsLoggedDataTask || t.TimeLogs.Any());

            if (prescribedTasks.Any() || loggedTasks.Any())
            {
                TaskMapper taskMapper = new TaskMapper(this);
                if (prescribedTasks.Any())
                {
                    //Prescribed Tasks
                    IEnumerable <WorkItem> workItems = taskMapper.ImportWorkItems(prescribedTasks);
                    AdaptDataModel.Documents.WorkItems = workItems;
                }

                if (loggedTasks.Any())
                {
                    //Logged Tasks
                    IEnumerable <LoggedData> loggedDatas = taskMapper.ImportLoggedDatas(loggedTasks);
                    AdaptDataModel.Documents.LoggedData = loggedDatas;

                    //Create Work Records for Logged Tasks
                    List <WorkRecord> workRecords = new List <WorkRecord>();
                    foreach (LoggedData data in loggedDatas)
                    {
                        WorkRecord record = new WorkRecord();
                        record.LoggedDataIds.Add(data.Id.ReferenceId);
                        if (data.SummaryId.HasValue)
                        {
                            record.SummariesIds.Add(data.SummaryId.Value);
                            Summary summary = AdaptDataModel.Documents.Summaries.FirstOrDefault(s => s.Id.ReferenceId == data.SummaryId);
                            if (summary != null)
                            {
                                summary.WorkRecordId = record.Id.ReferenceId;
                            }
                        }
                        workRecords.Add(record);
                    }
                    AdaptDataModel.Documents.WorkRecords = workRecords;
                }
            }

            return(AdaptDataModel);
        }
Пример #3
0
        public ApplicationDataModel.ADM.ApplicationDataModel Import(ISO11783_TaskData taskData)
        {
            if (Properties == null)
            {
                Properties = new Properties();
            }

            ISOTaskData    = taskData;
            UniqueIDMapper = new UniqueIdMapper(ISOTaskData.LinkList);

            AdaptDataModel         = new ApplicationDataModel.ADM.ApplicationDataModel();
            AdaptDataModel.Catalog = new Catalog()
            {
                Description = taskData.FilePath
            };
            AdaptDataModel.Documents = new Documents();

            //Comments
            CodedCommentListMapper commentListMapper = new CodedCommentListMapper(this);
            CodedCommentMapper     commentMapper     = new CodedCommentMapper(this, commentListMapper);

            //Crops - several dependencies require import prior to Products
            IEnumerable <ISOCropType> crops = taskData.ChildElements.OfType <ISOCropType>();

            if (crops.Any())
            {
                CropTypeMapper cropMapper = new CropTypeMapper(this, ProductGroupMapper);
                AdaptDataModel.Catalog.Crops.AddRange(cropMapper.ImportCropTypes(crops));
            }

            //Products
            IEnumerable <ISOProduct> products = taskData.ChildElements.OfType <ISOProduct>();

            if (products.Any())
            {
                ProductMapper         productMapper = new ProductMapper(this, ProductGroupMapper);
                IEnumerable <Product> adaptProducts = productMapper.ImportProducts(products);
                AdaptDataModel.Catalog.Products.AddRange(adaptProducts.Where(p => !AdaptDataModel.Catalog.Products.Contains(p)));
            }

            //Growers
            IEnumerable <ISOCustomer> customers = taskData.ChildElements.OfType <ISOCustomer>();

            if (customers.Any())
            {
                CustomerMapper customerMapper = new CustomerMapper(this);
                AdaptDataModel.Catalog.Growers.AddRange(customerMapper.Import(customers));
            }

            //Farms
            IEnumerable <ISOFarm> farms = taskData.ChildElements.OfType <ISOFarm>();

            if (farms.Any())
            {
                FarmMapper farmMapper = new FarmMapper(this);
                AdaptDataModel.Catalog.Farms.AddRange(farmMapper.Import(farms));
            }

            //Fields & Cropzones
            IEnumerable <ISOPartfield> partFields = taskData.ChildElements.OfType <ISOPartfield>();

            if (partFields.Any())
            {
                PartfieldMapper partFieldMapper = new PartfieldMapper(this);
                AdaptDataModel.Catalog.Fields.AddRange(partFieldMapper.ImportFields(partFields));
                AdaptDataModel.Catalog.CropZones.AddRange(partFieldMapper.ImportCropZones(partFields, crops));
            }

            //Devices

            IEnumerable <ISODevice> devices = taskData.ChildElements.OfType <ISODevice>();

            if (devices.Any())
            {
                //See explanation of MergeSingleBinsIntoBoom in DeviceElementHierarchy
                bool mergeBins;
                if (Properties == null || !bool.TryParse(Properties.GetProperty(MergeSingleBinsIntoBoom), out mergeBins))
                {
                    mergeBins = true;
                }

                //Load the internal objects modeling hierarchies of DETs per DVC
                DeviceElementHierarchies = new DeviceElementHierarchies(devices,
                                                                        RepresentationMapper,
                                                                        mergeBins,
                                                                        taskData.ChildElements.OfType <ISOTask>().SelectMany(t => t.TimeLogs),
                                                                        BaseFolder);

                //Import the ISO DVC & DET data into the actual ADAPT models.
                //During DET import, we use the DeviceElementHierarchies from above to map the actual hierarchies and fill in details.
                DeviceMapper deviceMapper = new DeviceMapper(this);
                AdaptDataModel.Catalog.DeviceModels.AddRange(deviceMapper.ImportDevices(devices));
            }

            //Workers
            IEnumerable <ISOWorker> workers = taskData.ChildElements.OfType <ISOWorker>();

            if (workers.Any())
            {
                WorkerMapper workerMapper = new WorkerMapper(this);
                AdaptDataModel.Catalog.Persons.AddRange(workerMapper.Import(workers));
            }


            //Cultural Practices
            IEnumerable <ISOCulturalPractice> practices = taskData.ChildElements.OfType <ISOCulturalPractice>();

            if (practices.Any())
            {
                foreach (ISOCulturalPractice cpc in practices)
                {
                    (AdaptDataModel.Documents.WorkOrders as List <WorkOrder>).Add(new WorkOrder()
                    {
                        Description = cpc.CulturalPracticeDesignator
                    });
                }
            }

            //OperationTechniques
            IEnumerable <ISOOperationTechnique> techniques = taskData.ChildElements.OfType <ISOOperationTechnique>();

            if (techniques.Any())
            {
                foreach (ISOOperationTechnique otq in techniques)
                {
                    (AdaptDataModel.Documents.WorkOrders as List <WorkOrder>).Add(new WorkOrder()
                    {
                        Description = otq.OperationTechniqueDesignator
                    });
                }
            }

            IEnumerable <ISOTask> prescribedTasks = taskData.ChildElements.OfType <ISOTask>().Where(t => t.IsWorkItemTask);
            IEnumerable <ISOTask> loggedTasks     = taskData.ChildElements.OfType <ISOTask>().Where(t => t.IsLoggedDataTask || t.TimeLogs.Any());

            if (prescribedTasks.Any() || loggedTasks.Any())
            {
                TaskMapper taskMapper = new TaskMapper(this);
                if (prescribedTasks.Any())
                {
                    //Prescribed Tasks
                    IEnumerable <WorkItem> workItems = taskMapper.ImportWorkItems(prescribedTasks);
                    AdaptDataModel.Documents.WorkItems = workItems;
                }

                if (loggedTasks.Any())
                {
                    //Logged Tasks
                    IEnumerable <LoggedData> loggedDatas = taskMapper.ImportLoggedDatas(loggedTasks);
                    AdaptDataModel.Documents.LoggedData = loggedDatas;

                    //Create Work Records for Logged Tasks
                    List <WorkRecord> workRecords = new List <WorkRecord>();
                    foreach (LoggedData data in loggedDatas)
                    {
                        WorkRecord record = new WorkRecord();
                        record.LoggedDataIds.Add(data.Id.ReferenceId);
                        if (data.SummaryId.HasValue)
                        {
                            record.SummariesIds.Add(data.SummaryId.Value);
                            Summary summary = AdaptDataModel.Documents.Summaries.FirstOrDefault(s => s.Id.ReferenceId == data.SummaryId);
                            if (summary != null)
                            {
                                summary.WorkRecordId = record.Id.ReferenceId;
                            }
                        }

                        //Export Derived UTC Delta as a ContextItem
                        //The value will be as accurate as the clock settings and thus terming "Delta" vs "Offset" to underscore this is not an accurate UTC offset for the local timezone
                        //Not rounding value so that relatively accurate GPS UTC values can be reverse calculated from this value.
                        string      offset      = GPSToLocalDelta.HasValue ? GPSToLocalDelta.Value.ToString(CultureInfo.InvariantCulture) : "no data";
                        ContextItem contextItem = new ContextItem()
                        {
                            Code = "GPSUTC_Local_Delta", Value = offset, ValueUOM = "hr"
                        };
                        record.ContextItems.Add(contextItem);

                        workRecords.Add(record);
                    }
                    AdaptDataModel.Documents.WorkRecords = workRecords;
                }
            }

            return(AdaptDataModel);
        }