private List <ProductionDataEdit> ConvertDataEdits(TDesignName[] designNames, TDesignLayer[] layers) { //Check for any layer edits that go with design edits var matches = (from l in layers join d in designNames on new { assetId = l.FAssetID, startUTC = l.FStartTime, endUTC = l.FEndTime } equals new { assetId = d.FMachineID, startUTC = d.FStartDate, endUTC = d.FEndDate } select new { l, d }).ToList(); var layerMatches = (from m in matches select m.l).ToList(); var designMatches = (from m in matches select m.d).ToList(); //TODO: layer number from Raptor should be an int not a long. Needs fixing in Shims. List <ProductionDataEdit> dataEdits = (from m in matches select ProductionDataEdit.CreateProductionDataEdit( m.d.FMachineID, m.d.FStartDate.ToDateTime(), m.d.FEndDate.ToDateTime(), m.d.FName, m.l.FLayerID)).ToList(); var layerOnly = (from l in layers where !layerMatches.Contains(l) select ProductionDataEdit.CreateProductionDataEdit( l.FAssetID, l.FStartTime.ToDateTime(), l.FEndTime.ToDateTime(), null, l.FLayerID)).ToList(); var designOnly = (from d in designNames where !designMatches.Contains(d) select ProductionDataEdit.CreateProductionDataEdit(d.FMachineID, d.FStartDate.ToDateTime(), d.FEndDate.ToDateTime(), d.FName, null)).ToList(); dataEdits.AddRange(layerOnly); dataEdits.AddRange(designOnly); return(dataEdits); }
/// <summary> /// Validates new edit does not overlap any existing edit of the same type for the same machine. /// </summary> private void ValidateNoOverlap(List <ProductionDataEdit> existingEdits, ProductionDataEdit newEdit) { if (existingEdits != null && existingEdits.Count > 0) { var overlapEdits = (from e in existingEdits where ((!string.IsNullOrEmpty(e.onMachineDesignName) && !string.IsNullOrEmpty(newEdit.onMachineDesignName)) || (e.liftNumber.HasValue && newEdit.liftNumber.HasValue)) && e.assetId == newEdit.assetId && !(e.endUTC <= newEdit.startUTC || e.startUTC >= newEdit.endUTC) select e).ToList(); if (overlapEdits.Count > 0) { var message = string.Empty; foreach (var oe in overlapEdits) { message = $"{message}\nMachine: {oe.assetId}, Override Period: {oe.startUTC}-{oe.endUTC}, Edited Value: {(string.IsNullOrEmpty(oe.onMachineDesignName) ? oe.onMachineDesignName : (oe.liftNumber?.ToString() ?? string.Empty))}"; } throw new ServiceException(HttpStatusCode.BadRequest, new ContractExecutionResult(ContractExecutionStatesEnum.ValidationError, $"Data edit overlaps: {message}")); } } }
public void ValidateSuccessTest() { ProductionDataEdit dataEdit = ProductionDataEdit.CreateProductionDataEdit( 10538563, DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(-2), "Acme Dozer", 3); dataEdit.Validate(); }
public void ValidateFailInvalidDateRangeTest() { //startUTC > endUTC ProductionDataEdit dataEdit = ProductionDataEdit.CreateProductionDataEdit( 10538563, DateTime.UtcNow.AddDays(-2), DateTime.UtcNow.AddDays(-5), "Acme Dozer", null); Assert.ThrowsException <ServiceException>(() => dataEdit.Validate()); }
public void ValidateFailMissingDataEditTest() { //missing edit data ProductionDataEdit dataEdit = ProductionDataEdit.CreateProductionDataEdit( 10538563, DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(-2), null, null); Assert.ThrowsException <ServiceException>(() => dataEdit.Validate()); }
public void CanCreateEditDataRequestTest() { var validator = new DataAnnotationsValidator(); ProductionDataEdit dataEdit = ProductionDataEdit.CreateProductionDataEdit( 10538563, DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(-2), "Acme Dozer", 3); ICollection <ValidationResult> results; Assert.IsTrue(validator.TryValidate(dataEdit, out results)); //missing machine details dataEdit = ProductionDataEdit.CreateProductionDataEdit( 0, DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(-2), "Acme Dozer", null); Assert.IsFalse(validator.TryValidate(dataEdit, out results)); }
/// <summary> /// Validates new edit is within production data date range for the project /// </summary> private async Task ValidateDates(Guid projectUid, ProductionDataEdit dataEdit) { var projectExtents = await ProjectStatisticsHelper.GetProjectStatisticsWithProjectSsExclusions(projectUid, VelociraptorConstants.NO_PROJECT_ID, GetUserId(), CustomHeaders); if (projectExtents == null) { throw new ServiceException(HttpStatusCode.BadRequest, new ContractExecutionResult(ContractExecutionStatesEnum.ValidationError, "Cannot obtain ProjectStatistics.")); } if (dataEdit.startUTC < projectExtents.startTime || dataEdit.endUTC > projectExtents.endTime) { throw new ServiceException(HttpStatusCode.BadRequest, new ContractExecutionResult(ContractExecutionStatesEnum.ValidationError, $"Data edit outside production data date range: {projectExtents.startTime}-{projectExtents.endTime}")); } }
protected override async Task <ContractExecutionResult> ProcessAsyncEx <T>(T item) { try { var request = CastRequestObjectTo <GetEditDataRequest>(item); #if RAPTOR if (UseTRexGateway("ENABLE_TREX_GATEWAY_EDIT_DATA")) { #endif var projectIds = new ProjectIDs(request.ProjectId.Value, request.ProjectUid.Value); projectIds.Validate(); var assetUid = await GetAssetUid(projectIds, request.assetId); var parameters = new Dictionary <string, string> { { "projectUid", request.ProjectUid.Value.ToString() } } ; if (assetUid.HasValue) { parameters.Add("assetUid", assetUid.ToString()); } var queryParams = $"?{new System.Net.Http.FormUrlEncodedContent(parameters).ReadAsStringAsync().Result}"; var results = await trexCompactionDataProxy.SendDataGetRequest <TRexEditDataResult>(request.ProjectUid.Value.ToString(), $"/productiondataedit{queryParams}", customHeaders); var assetMatches = await GetAssetIds(projectIds, results.DataEdits.Select(d => d.AssetUid).ToList()); var convertedResults = from d in results.DataEdits select ProductionDataEdit.CreateProductionDataEdit(assetMatches[d.AssetUid], d.StartUtc, d.EndUtc, d.MachineDesignName, d.LiftNumber); return(EditDataResult.CreateEditDataResult(convertedResults.ToList())); #if RAPTOR } return(ProcessWithRaptor(request)); #endif } finally { ContractExecutionStates.ClearDynamic(); } }