private List <WaterQualityMeasurement> GetMeasurements(IWQMeasurementProvider provider, DateTime?start, DateTime?end, IEnumerable <WaterQualityDeployment> deployments) { List <WaterQualityMeasurement> measurements = new List <WaterQualityMeasurement>(); if (start != null || end != null) { DateTime queryStart; DateTime queryEnd; if (start == null) { queryStart = WQUtils.GlobalMinDate; } else { queryStart = start.Value; } if (end == null) { queryEnd = DateTime.UtcNow; } else { queryEnd = end.Value; } foreach (WaterQualityDeployment dep in deployments) { measurements.AddRange(provider.Get(dep.Identity, queryStart, queryEnd)); } } else { foreach (WaterQualityDeployment dep in deployments) { measurements.AddRange(provider.Get(dep.Identity)); } } return(measurements); }
public override void Handle(HttpContext context, CancellationToken cancel) { if (context != null) { UserIdentityBase user = Security.Session.GetUser(context); if (user != null) { UserSecurityContext ctx = new UserSecurityContext(user); string localUrl = RestUtils.LocalUrl(this, context.Request); string meth = RestUtils.StripLocal(this.BaseUrl, localUrl); meth = meth.Substring(1); if (!string.IsNullOrEmpty(meth)) { if (context.Request.Method == "POST") { if (meth.Equals("deployments", StringComparison.OrdinalIgnoreCase)) { try { HashSet <CompoundIdentity> eventIds = null; HashSet <CompoundIdentity> deploymentIds = null; HashSet <CompoundIdentity> siteIds = null; DateTime?start = null; DateTime?end = null; JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { if (token["events"] != null) { eventIds = JsonUtils.ToIds(token["events"]); } if (token["deployments"] != null) { deploymentIds = JsonUtils.ToIds(token["deployments"]); } if (token["sites"] != null) { siteIds = JsonUtils.ToIds(token["sites"]); } if (token["start"] != null) { start = JsonUtils.ToDate(token["start"]); } if (token["end"] != null) { end = JsonUtils.ToDate(token["end"]); } } IWQDeploymentProvider provider = WaterQualityManager.Instance.GetDeploymentProvider(ctx); //filters WQ deployments by user context if (provider != null) { IEnumerable <WaterQualityDeployment> deployments = GetDeployments(provider, eventIds, deploymentIds, siteIds, start, end); JArray jdeployments = Jsonifier.ToJson(deployments); if (jdeployments != null) { RestUtils.Push(context.Response, JsonOpStatus.Ok, jdeployments.ToString()); return; } else { RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]"); return; } } } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (meth.Equals("measurements", StringComparison.OrdinalIgnoreCase)) { try { //TODO: ask about iterative calls for deployments (each deployment is a separate db call) IWQMeasurementProvider provider = WaterQualityManager.Instance.GetMeasurementProvider(ctx); if (provider != null) { HashSet <CompoundIdentity> eventIds = null; HashSet <CompoundIdentity> deploymentIds = null; HashSet <CompoundIdentity> siteIds = null; DateTime?start = null; DateTime?end = null; JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { if (token["events"] != null) { eventIds = JsonUtils.ToIds(token["events"]); } if (token["deployments"] != null) { deploymentIds = JsonUtils.ToIds(token["deployments"]); } if (token["sites"] != null) { siteIds = JsonUtils.ToIds(token["sites"]); } if (token["start"] != null) { start = JsonUtils.ToDate(token["start"]); } if (token["end"] != null) { end = JsonUtils.ToDate(token["end"]); } } IWQDeploymentProvider depProvider = WaterQualityManager.Instance.GetDeploymentProvider(ctx); //provider will autofilter WQ deployments by user context IEnumerable <WaterQualityDeployment> deployments = GetDeployments(depProvider, eventIds, deploymentIds, siteIds, null, null); List <WaterQualityMeasurement> measurements = new List <WaterQualityMeasurement>(); if (start != null || end != null) { DateTime queryStart; DateTime queryEnd; if (start == null) { queryStart = WQUtils.GlobalMinDate; } else { queryStart = start.Value; } if (end == null) { queryEnd = DateTime.UtcNow; } else { queryEnd = end.Value; } foreach (WaterQualityDeployment dep in deployments) { measurements.AddRange(provider.Get(dep.Identity, queryStart, queryEnd)); } } else { foreach (WaterQualityDeployment dep in deployments) { measurements.AddRange(provider.Get(dep.Identity)); } } JArray jmeasurements = Jsonifier.ToJson(measurements); if (jmeasurements != null) { RestUtils.Push(context.Response, JsonOpStatus.Ok, jmeasurements.ToString()); } else { RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]"); } return; } RestUtils.Push(context.Response, JsonOpStatus.Failed); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (meth.Equals("export", StringComparison.OrdinalIgnoreCase)) { try { HashSet <CompoundIdentity> eventIds = null; HashSet <CompoundIdentity> deploymentIds = null; HashSet <CompoundIdentity> siteIds = null; DateTime?start = null; DateTime?end = null; JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { if (token["events"] != null) { eventIds = JsonUtils.ToIds(token["events"]); } if (token["deployments"] != null) { deploymentIds = JsonUtils.ToIds(token["deployments"]); } if (token["sites"] != null) { siteIds = JsonUtils.ToIds(token["sites"]); } if (token["start"] != null) { start = JsonUtils.ToDate(token["start"]); } if (token["end"] != null) { end = JsonUtils.ToDate(token["end"]); } } IWQDeploymentProvider depProvider = WaterQualityManager.Instance.GetDeploymentProvider(ctx); IWQMeasurementProvider measProvider = WaterQualityManager.Instance.GetMeasurementProvider(ctx); ISiteProvider siteProvider = SiteManager.Instance.GetSiteProvider(ctx); ISampleEventProvider sampProvider = FieldActivityManager.Instance.GetSampleEventProvider(ctx); IOrganizationProvider orgProvider = OrganizationManager.Instance.GetOrganizationProvider(ctx); IFieldTripProvider fieldTripProvider = FieldActivityManager.Instance.GetFieldTripProvider(ctx); IFieldActivityProvider fieldActivityProvider = FieldActivityManager.Instance.GetFieldActivityProvider(ctx); IProjectProvider projectProvider = ProjectManager.Instance.GetProvider(ctx); if (depProvider != null && measProvider != null && siteProvider != null && sampProvider != null && orgProvider != null && fieldTripProvider != null && fieldActivityProvider != null && projectProvider != null) { IEnumerable <WaterQualityDeployment> deployments = GetDeployments(depProvider, eventIds, deploymentIds, siteIds, null, null); //on export, time filters apply to measurements only IEnumerable <WaterQualityMeasurement> measurements = GetMeasurements(measProvider, start, end, deployments); //Get sites and sample events List <CompoundIdentity> selected_siteIds = deployments.Select(x => x.SiteId).ToList(); List <CompoundIdentity> selected_eventIds = deployments.Select(x => x.SampleEventId).ToList(); IEnumerable <Site> sitesData = GetSites(siteProvider, selected_siteIds); IEnumerable <SamplingEvent> eventsData = sampProvider.Get(selected_eventIds); //Get orgs and field trips List <CompoundIdentity> selected_orgIds = eventsData.Select(x => x.PrincipalOrgId).ToList(); List <CompoundIdentity> selected_fieldTripIds = eventsData.Select(x => x.FieldTripId).ToList(); IEnumerable <Organization> orgData = orgProvider.Get(selected_orgIds); IEnumerable <FieldTrip> fieldTripData = fieldTripProvider.Get(selected_fieldTripIds); //Get field activities List <CompoundIdentity> selected_fieldActivityIds = fieldTripData.Select(x => x.FieldActivityId).ToList(); IEnumerable <FieldActivity> fieldActivityData = fieldActivityProvider.Get(selected_fieldActivityIds); //Get projects List <CompoundIdentity> selected_projectIds = fieldActivityData.Select(x => x.ProjectId).ToList(); IEnumerable <Project> projectData = projectProvider.Get(selected_projectIds); Guid fileId = CreateDeploymentFile(eventsData, deployments, measurements, sitesData, orgData, fieldTripData, fieldActivityData, projectData); if (fileId != null) { JObject o = new JObject(); o.Add("fileid", fileId.ToString()); o.Add("fileext", fileExtension); RestUtils.Push(context.Response, JsonOpStatus.Ok, o); } else { RestUtils.Push(context.Response, JsonOpStatus.Failed); } return; } RestUtils.Push(context.Response, JsonOpStatus.Failed); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } } } } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }