@@ -215,10 +215,12 @@ public string GetProductManufacturer(ISOProduct isoProduct)
215215 }
216216
217217
218- public IEnumerable < OperationData > PostProcessOperationData ( TaskDataMapper taskDataMapper , IEnumerable < OperationData > operationDatas )
218+ public IEnumerable < OperationData > PostProcessOperationData ( TaskDataMapper taskDataMapper , ISOTask isoTask , IEnumerable < OperationData > operationDatas )
219219 {
220220 var result = new List < OperationData > ( ) ;
221221
222+ var cropNameFromTask = GetCropName ( isoTask ) ;
223+
222224 var catalog = taskDataMapper . AdaptDataModel . Catalog ;
223225 foreach ( var operationData in operationDatas )
224226 {
@@ -244,11 +246,56 @@ public IEnumerable<OperationData> PostProcessOperationData(TaskDataMapper taskDa
244246 continue ;
245247 }
246248 }
249+
250+ if ( ! string . IsNullOrEmpty ( cropNameFromTask ) &&
251+ operationData . OperationType != OperationTypeEnum . DataCollection &&
252+ ( operationData . ProductIds == null || operationData . ProductIds . Count == 0 ) )
253+ {
254+ operationData . ProductIds = new List < int >
255+ {
256+ AddOrGetHavestCommodityProduct ( catalog , cropNameFromTask ) . Id . ReferenceId
257+ } ;
258+ }
259+
247260 result . Add ( operationData ) ;
248261 }
249262 return result ;
250263 }
251264
265+ private HarvestedCommodityProduct AddOrGetHavestCommodityProduct ( ApplicationDataModel . ADM . Catalog catalog , string cropName )
266+ {
267+ Crop adaptCrop = catalog . Crops . FirstOrDefault ( x => x . Name . EqualsIgnoreCase ( cropName ) ) ;
268+ if ( adaptCrop == null )
269+ {
270+ // Create a new one
271+ adaptCrop = new Crop
272+ {
273+ Name = cropName
274+ } ;
275+ catalog . Crops . Add ( adaptCrop ) ;
276+ }
277+
278+ var commodityProduct = catalog . Products . OfType < HarvestedCommodityProduct > ( )
279+ . FirstOrDefault ( x => x . Description . EqualsIgnoreCase ( cropName ) && x . CropId == adaptCrop . Id . ReferenceId ) ;
280+ if ( commodityProduct == null )
281+ {
282+ // New harvested commodity product
283+ commodityProduct = new HarvestedCommodityProduct
284+ {
285+ Category = CategoryEnum . Variety ,
286+ Form = ProductFormEnum . Solid ,
287+ ProductType = ProductTypeEnum . Variety ,
288+ HasHarvestCommodity = true ,
289+ Description = cropName
290+ } ;
291+
292+ commodityProduct . CropId = adaptCrop . Id . ReferenceId ;
293+
294+ catalog . Products . Add ( commodityProduct ) ;
295+ }
296+ return commodityProduct ;
297+ }
298+
252299 public void PostProcessPolygons ( List < Polygon > polygons )
253300 {
254301 var groupedPolygons = polygons . GroupBy ( x => x . ExteriorRing != null ) . ToDictionary ( x => x . Key , x => x . ToList ( ) ) ;
0 commit comments