Example #1
0
        public FilestoreFile Create(SampleEventMap map, EntityBundle sites, EntityBundle instruments)
        {
            if (this.CanModify(map))
            {
                Guid            id  = Guid.NewGuid();
                WaterQualityDET det = new WaterQualityDET();
                det.Id    = id;
                det.Owner = "originator:" + sites.PrincipalOrgId.Identity.ToString() + ":" + instruments.PrincipalOrgId.Identity.ToString();

                int ct = 0;
                foreach (BundleElement cur in sites.Elements)
                {
                    SiteDTO tmp = new SiteDTO();
                    tmp.Key  = cur.LocalKey;
                    tmp.Name = cur.DisplayName;
                    det.Sites.Add(tmp);
                    ct++;
                }
                if (ct > 0) //has to be elements in the collection
                {
                    ct = 0;

                    foreach (BundleElement cur in instruments.Elements)
                    {
                        InstrumentDTO tmp = new InstrumentDTO();
                        tmp.Key  = cur.LocalKey;
                        tmp.Name = cur.DisplayName;
                        det.Instruments.Add(tmp);
                        ct++;
                    }

                    if (ct > 0)
                    {
                        det.Validate();
                        if (det.ValidationIssues.Count == 0)
                        {
                            IFileStoreProvider prov = this.FileStore;

                            if (prov != null)
                            {
                                FilestoreFile fil = prov.Make(id);
                                if (fil != null)
                                {
                                    ExcelWaterQualityDET excel = new ExcelWaterQualityDET(det);
                                    excel.Save(fil);
                                    fil.Flush();
                                    fil.Seek(0, System.IO.SeekOrigin.Begin);
                                    return(fil);
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
Example #2
0
        private Guid CreateExcelFile(Dictionary <CompoundIdentity, Tuple <int, Site> > siteDict, Dictionary <CompoundIdentity, Tuple <int, SamplingEvent> > eventDict,
                                     Dictionary <CompoundIdentity, Tuple <int, WaterQualityDeployment> > deploymentDict, List <Tuple <int, WaterQualityMeasurement> > measurementList,
                                     Dictionary <CompoundIdentity, Organization> orgDict, Dictionary <CompoundIdentity, FieldTrip> fieldTripDict,
                                     Dictionary <CompoundIdentity, FieldActivity> fieldActivityDict, Dictionary <CompoundIdentity, Project> projectDict)
        {
            IFileStoreProvider provider = FileStoreManager.Instance.GetProvider();

            //Setting up file and Excel Workbook
            FilestoreFile deployFile = provider.MakeTemp(DateTime.UtcNow.AddHours(4));
            XlWorkbook    book       = new XlWorkbook();
            XlWorksheets  sheets     = book.Worksheets;

            //Generating Sampling Event Sheet
            XlSchema    eventSchema = GetSampleEventSchema();
            XlWorksheet eventSheet  = sheets.AddWorksheet("SamplingEvents", XlColor.White, eventSchema);
            XlRows      eventRows   = eventSheet.Rows;

            var orderedEvents = eventDict.OrderBy(x => x.Value.Item1);

            foreach (var evt in orderedEvents)
            {
                string orgName = na;
                if (orgDict.ContainsKey(evt.Value.Item2.PrincipalOrgId))
                {
                    orgName = orgDict[evt.Value.Item2.PrincipalOrgId].Name;
                }
                string ftripName     = na;
                string factivityName = na;
                string projName      = na;
                if (fieldTripDict.ContainsKey(evt.Value.Item2.FieldTripId))
                {
                    FieldTrip ftrip = fieldTripDict[evt.Value.Item2.FieldTripId];
                    ftripName = ftrip.Name;
                    if (fieldActivityDict.ContainsKey(ftrip.FieldActivityId))
                    {
                        FieldActivity factivity = fieldActivityDict[ftrip.FieldActivityId];
                        factivityName = factivity.Name;
                        if (projectDict.ContainsKey(factivity.ProjectId))
                        {
                            projName = projectDict[factivity.ProjectId].Name;
                        }
                    }
                }

                List <string> evtItems = new List <string>();
                evtItems.Add(orgName);
                evtItems.Add(projName);
                evtItems.Add(factivityName);
                evtItems.Add(ftripName);
                evtItems.Add(evt.Value.Item2.Name);
                evtItems.Add(evt.Value.Item1.ToString());
                evtItems.Add(evt.Value.Item2.Description);
                //evtItems.Add(evt.Value.Item2.DateRange.Min.ToString());
                //evtItems.Add(evt.Value.Item2.DateRange.Max.ToString());

                SchemaRowData row = new SchemaRowData(eventSchema, evtItems);
                eventRows.AddRow(row);
            }

            //Generating Deployment/Measurement Sheet
            XlSchema    measSchema = GetDeployMeasurementSchema();
            XlWorksheet measSheet  = sheets.AddWorksheet("WaterQualityMeasurements", XlColor.White, measSchema);
            XlRows      measRows   = measSheet.Rows;

            var orderedMeasurements = measurementList.OrderBy(x => x.Item1);

            foreach (var meas in orderedMeasurements)
            {
                WaterQualityDeployment  deploy      = deploymentDict[meas.Item2.DeploymentId].Item2;
                WaterQualityMeasurement measurement = meas.Item2;
                int eventIndex = eventDict[deploy.SampleEventId].Item1;

                //deploy.SiteId could be a dangling reference
                string siteFK = na;
                if (siteDict.ContainsKey(deploy.SiteId))
                {
                    siteFK = siteDict[deploy.SiteId].Item1.ToString();
                }

                List <string> measItems = new List <string>();
                measItems.Add(meas.Item1.ToString());
                measItems.Add(deploy.Name);
                measItems.Add(deploy.Description);
                measItems.Add(eventIndex.ToString());
                measItems.Add(siteFK);
                measItems.Add(deploy.Range.StartDate.ToString());
                measItems.Add(deploy.Range.EndDate.ToString());
                measItems.Add(measurement.SampleDate.ToString());
                measItems.Add(measurement.SurfaceElevation.ToString());
                measItems.Add(measurement.Temperature.ToString());
                measItems.Add(measurement.pH.ToString());
                measItems.Add(measurement.DissolvedOxygen.ToString());
                measItems.Add(measurement.Conductivity.ToString());
                measItems.Add(measurement.Salinity.ToString());
                measItems.Add(measurement.Velocity.ToString());
                SchemaRowData row = new SchemaRowData(measSchema, measItems);
                measRows.AddRow(row);
            }

            //Generating Site Sheet
            XlSchema    siteSchema = GetSiteSchema();
            XlWorksheet siteSheet  = sheets.AddWorksheet("Sites", XlColor.White, siteSchema);
            XlRows      siteRows   = siteSheet.Rows;

            var orderedSites = siteDict.OrderBy(x => x.Value.Item1);

            foreach (var site in orderedSites)
            {
                Site          s         = site.Value.Item2;
                List <string> siteItems = new List <string>();
                siteItems.Add(site.Value.Item1.ToString());
                siteItems.Add(s.Name);
                siteItems.Add(s.Description);
                IGeometry2 <double> geom = s.Location;
                if (geom != null)
                {
                    if (geom is PolygonBag2 <double> )
                    {
                        siteItems.Add(WktUtils.ToWkt(geom as PolygonBag2 <double>).ToString());
                    }
                    else if (geom is Polygon2 <double> )
                    {
                        siteItems.Add(WktUtils.ToWkt(geom as Polygon2 <double>).ToString());
                    }
                    else if (geom is Polyline2 <double> )
                    {
                        siteItems.Add(WktUtils.ToWkt(geom as Polyline2 <double>).ToString());
                    }
                    else if (geom is PolylineBag2 <double> )
                    {
                        siteItems.Add(WktUtils.ToWkt(geom as PolylineBag2 <double>).ToString());
                    }
                    else if (geom is Point2 <double> )
                    {
                        siteItems.Add(WktUtils.ToWkt(geom as Point2 <double>).ToString());
                    }
                }
                else
                {
                    siteItems.Add("");
                }

                Point2 <double> geom2 = s.LocationMark;
                if (geom2 != null)
                {
                    siteItems.Add(WktUtils.ToWkt(geom2 as Point2 <double>).ToString());
                }
                else
                {
                    siteItems.Add("");
                }

                SchemaRowData row = new SchemaRowData(siteSchema, siteItems);
                siteRows.AddRow(row);
            }

            book.Save(deployFile);
            deployFile.Flush();
            deployFile.Close();
            deployFile.Dispose();

            return(deployFile.FileId);
        }
Example #3
0
        public FilestoreFile Create(SampleEventMap map, EntityBundle sites, EntityBundle plotTypes, EntityBundle shrubSpecies, EntityBundle treeSpecies, EntityBundle herbSpecies, EntityBundle nonLiving, bool isPrivate)
        {
            if (this.CanModify(map))
            {
                Guid   id  = Guid.NewGuid();
                VegDET det = new VegDET();
                det.Id    = id;
                det.Owner = "originator:" + sites.PrincipalOrgId.Identity.ToString() + ":" + plotTypes.PrincipalOrgId.Identity.ToString();

                int ct = 0;
                foreach (BundleElement cur in sites.Elements)
                {
                    SiteDTO tmp = new SiteDTO();
                    tmp.Key  = cur.LocalKey;
                    tmp.Name = cur.DisplayName;
                    det.Sites.Add(tmp);
                    ct++;
                }
                if (ct > 0) //has to be elements in the collection
                {
                    ct = 0;

                    foreach (BundleElement cur in plotTypes.Elements)
                    {
                        PlotTypeDTO tmp = new PlotTypeDTO();
                        tmp.Key  = cur.LocalKey;
                        tmp.Name = cur.DisplayName;
                        det.PlotTypes.Add(tmp);
                        ct++;
                    }

                    if (ct > 0)
                    {
                        ct = 0;

                        foreach (BundleElement cur in shrubSpecies.Elements)
                        {
                            SpeciesDTO tmp = new SpeciesDTO();
                            tmp.Key  = cur.LocalKey;
                            tmp.Name = cur.DisplayName;
                            det.ShrubSpecies.Add(tmp);
                            ct++;
                        }

                        if (treeSpecies != null)
                        {
                            foreach (BundleElement cur in treeSpecies.Elements)
                            {
                                SpeciesDTO tmp = new SpeciesDTO();
                                tmp.Key  = cur.LocalKey;
                                tmp.Name = cur.DisplayName;
                                det.TreeSpecies.Add(tmp);
                                ct++;
                            }
                        }

                        if (herbSpecies != null)
                        {
                            foreach (BundleElement cur in herbSpecies.Elements)
                            {
                                SpeciesDTO tmp = new SpeciesDTO();
                                tmp.Key  = cur.LocalKey;
                                tmp.Name = cur.DisplayName;
                                det.HerbSpecies.Add(tmp);
                                ct++;
                            }
                        }

                        if (nonLiving != null)
                        {
                            foreach (BundleElement cur in nonLiving.Elements)
                            {
                                SpeciesDTO tmp = new SpeciesDTO();
                                tmp.Key  = cur.LocalKey;
                                tmp.Name = cur.DisplayName;
                                det.NonLiving.Add(tmp);
                            }
                        }

                        if (ct > 0) //have to have at least one of herb, tree, shrub to POSSIBLY be valid
                        {
                            det.Validate();
                            if (det.ValidationIssues.Count == 0)
                            {
                                IFileStoreProvider prov = this.FileStore;

                                if (prov != null)
                                {
                                    FilestoreFile fil = prov.Make(id);
                                    if (fil != null)
                                    {
                                        ExcelVegDET excel = new ExcelVegDET(det);
                                        excel.Save(fil);
                                        fil.Flush();
                                        fil.Seek(0, System.IO.SeekOrigin.Begin);
                                        return(fil);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
Example #4
0
        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);

                    if (!string.IsNullOrEmpty(meth))
                    {
                        if (meth.StartsWith(Upload, StringComparison.OrdinalIgnoreCase))
                        {
                            string err = null;
                            try
                            {
                                string fName = MetaInfo.GetFileName(context);             //TODO -- add origin filename handling
                                if (fName != null && MetaInfo.SupportedUploadType(fName)) //this might be a bogus way to go
                                {
                                    if (prov == null)
                                    {
                                        prov = FileStoreManager.Instance.GetProvider();
                                    }

                                    if (prov != null)
                                    {
                                        Stream input = context.Request.Body;
                                        if (input != null)
                                        {
                                            FilestoreFile fil = prov.MakeTemp();
                                            if (fil != null)
                                            {
                                                input.CopyTo(fil);
                                                fil.Flush();
                                                fil.Close();
                                                fil.FileName = fName;
                                                prov.Update(fil);
                                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, "\"" + fil.FileId.ToString() + "\""));
                                                return;
                                            }
                                            else
                                            {
                                                err = "failed to make";
                                            }
                                        }
                                        else
                                        {
                                            err = "no file received";
                                        }
                                    }
                                    else
                                    {
                                        err = "no provider";
                                    }
                                }
                                else
                                {
                                    err = "unsupported file extension";  //this might be a bogus way to go
                                }
                            }
                            catch
                            { err = "unknown error"; }

                            if (err == null)
                            {
                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                            }
                            else
                            {
                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed, "\"" + err + "\""));
                            }
                            return;
                        }
                        if (meth.StartsWith(Download, StringComparison.OrdinalIgnoreCase))
                        {
                            string err = null;
                            try
                            {
                                //this should be a Guid string
                                string filename = Uri.UnescapeDataString(meth.Substring(Download.Length));
                                Guid   fileId;

                                if (Guid.TryParse(filename, out fileId))
                                {
                                    if (prov == null)
                                    {
                                        prov = FileStoreManager.Instance.GetProvider();
                                    }

                                    if (prov != null)
                                    {
                                        FilestoreFile fil = prov.Get(fileId);
                                        if (fil != null)
                                        {
                                            if (fil.Length > -1)
                                            {
                                                //send the bytes of a file in the response body
                                                context.Response.Headers.Add("Content-Disposition", "attachment ; filename=\"" + fil.FileName + "\"");
                                                context.Response.StatusCode  = HttpStatusCodes.Status200OK;
                                                context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                                                fil.CopyTo(context.Response.Body);
                                                return;
                                            }
                                            else
                                            {
                                                err = "no stream";
                                            }
                                        }
                                        else
                                        {
                                            err = "no such file";
                                        }
                                    }
                                    else
                                    {
                                        err = "no provider";
                                    }
                                }
                                else
                                {
                                    err = "fileid is not legal";
                                }
                            }
                            catch
                            { err = "unknown error"; }

                            if (err == null)
                            {
                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                            }
                            else
                            {
                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed, "\"" + err + "\""));
                            }
                            return;
                        }
                    }
                }
                else
                {
                    context.Response.StatusCode = HttpStatusCodes.Status401Unauthorized;
                    return;
                }
            }
            context.Response.StatusCode = HttpStatusCodes.Status400BadRequest;
        }