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); }
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); }