Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            ConfigurationManager.Instance.Bootstrap();
            ConfigurationManager.Instance.Initialize();
            ConfigurationManager.Instance.Start();
            Console.WriteLine("ConfigurationManager: " + ConfigurationManager.Instance.State.ToString());

            LogManager.Instance.Bootstrap();
            LogManager.Instance.Initialize();
            LogManager.Instance.Start();
            Console.WriteLine("LogManager: " + LogManager.Instance.State.ToString());

            FileStoreManager.Instance.Initialize();
            FileStoreManager.Instance.Start();
            Console.WriteLine("FileStore: " + FileStoreManager.Instance.State.ToString());

            IFileStoreProvider prov = FileStoreManager.Instance.GetProvider();

            Console.WriteLine("Prov: " + (prov == null).ToString());

            if (prov != null)
            {
                Guid          id  = Guid.NewGuid();
                FilestoreFile fil = prov.Make(id);
                if (fil != null)
                {
                    Console.WriteLine("Got file");
                    string     s  = "Hello from testing";
                    TextWriter wr = new StreamWriter(fil);
                    for (int i = 0; i < 100; i++)
                    {
                        wr.WriteLine(s);
                    }
                    wr.Flush();
                    wr.Close();
                    fil.Dispose();
                    Console.WriteLine("Done");

                    fil = prov.Get(id);
                    if (fil != null)
                    {
                        Console.WriteLine("Opened file");
                        wr = new StreamWriter(fil);
                        for (int i = 0; i < 100; i++)
                        {
                            wr.WriteLine(s);
                        }
                        wr.Flush();
                        wr.Close();
                        fil.Dispose();
                        Console.WriteLine("Wrote");
                        prov.Delete(id);
                        Console.WriteLine("Done");
                    }
                }

                prov.DeleteExpired();
            }

            Console.WriteLine("Enter to exit");
            Console.ReadLine();
        }
Exemplo n.º 3
0
        public override UpdateStatus Update(FilestoreFile tempFileUpload, SampleEventMap map)
        {
            if (tempFileUpload != null)
            {
                FilestoreFile oldFile = this.Get(map);
                if (oldFile != null)
                {
                    if (this.CanModify(map))
                    {
                        FishDET      curdet   = new FishDET();
                        ExcelFishDET curexcel = new ExcelFishDET(curdet);
                        curexcel.Load(tempFileUpload);
                        ValidationIssues curIssues = curdet.ValidationIssues;
                        if (curIssues.Count < 1)
                        {
                            FishDET      olddet = new FishDET();
                            ExcelFishDET excel  = new ExcelFishDET(olddet);
                            excel.Load(oldFile);
                            ValidationIssues issues = olddet.ValidationIssues;
                            if (issues.Count < 1) //should be this is the old file
                            {
                                UpdateStatus status = null;
                                if (olddet.CatchEfforts.Count < 1)     //old file has no data - bare det
                                {
                                    if (curdet.CatchEfforts.Count < 1) //nothing to do really, new file has no data either
                                    {
                                        olddet = null;
                                        excel  = null;
                                        oldFile.Dispose();
                                        curdet   = null;
                                        curexcel = null;
                                        FileStoreManager.Instance.GetProvider().Delete(tempFileUpload);

                                        status = new UpdateStatus(UpdateIssue.AllOk);
                                        status.Add(new IssueNotice("NoDataInEither", "nothing to do"));
                                        return(status);
                                    }

                                    SampleEventMapItem item = map.Get(KnownDetType.Fish);
                                    if (item != null)
                                    {
                                        olddet = null; //release old reference
                                        excel  = null; //release old reference

                                        //ok, no data in old file, but data in new file -- we have work to do -- handle first-time data insertion
                                        ICatchEffortProvider depl = FishManager.Instance.GetCatchEffortProvider(this.Context);
                                        ICatchHaulProvider   meas = FishManager.Instance.GetCatchHaulProvider(this.Context);
                                        IFishProvider        fi   = FishManager.Instance.GetFishProvider(this.Context);
                                        if (depl != null && meas != null && fi != null)
                                        {
                                            if (depl.CanCreate())
                                            {
                                                List <EntityBundle> bundles = this.GetBundles(map, KnownDetType.Fish);
                                                if (bundles != null && bundles.Count >= 3)
                                                {
                                                    EntityBundle mac = null;
                                                    if (bundles.Count > 3)
                                                    {
                                                        mac = bundles[3];
                                                    }
                                                    status = this.InitialLoad(map.SampleEventId, bundles[0], bundles[1], bundles[2], mac, curdet, depl, meas, fi, item.IsPrivate);
                                                    if (status != null && status.Issue == UpdateIssue.AllOk) //success, so we overwrite the file
                                                    {
                                                        curdet   = null;
                                                        curexcel = null;
                                                        olddet   = null;
                                                        excel    = null;
                                                        IFileStoreProvider ip = FileStoreManager.Instance.GetProvider();
                                                        if (ip.Replace(tempFileUpload, oldFile)) //overwrite the existing file with the new one
                                                        {
                                                            tempFileUpload.Dispose();
                                                            oldFile.Dispose();
                                                            //ip.Delete(tempFileUpload); //may still be references so delete can fail
                                                            return(status); //we're done here
                                                        }
                                                        else
                                                        {
                                                            status = new UpdateStatus(UpdateIssue.DataIssue);
                                                            status.Add(new IssueNotice("File", "Failed to replace file"));
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    status = new UpdateStatus(UpdateIssue.DataIssue);
                                                    status.Add(new IssueNotice("Bundles", "Failed to get bundles"));
                                                }
                                            }
                                            else
                                            {
                                                status = new UpdateStatus(UpdateIssue.Security);
                                                status.Add(new IssueNotice("Permissions", "create denied"));
                                            }
                                        }
                                        else
                                        {
                                            status = new UpdateStatus(UpdateIssue.SystemIssue);
                                            status.Add(new IssueNotice("Failure", "Failed to get provider"));
                                        }
                                    }
                                    else
                                    {
                                        status = new UpdateStatus(UpdateIssue.NoExistingFile);
                                        status.Add(new IssueNotice("NoMapEntry", "Failed to find map reference"));
                                    }
                                    return(status);
                                }
                                else //crap -- this is an update where existing file already had data
                                {
                                    ICatchEffortProvider depl = FishManager.Instance.GetCatchEffortProvider(this.Context);
                                    ICatchHaulProvider   meas = FishManager.Instance.GetCatchHaulProvider(this.Context);
                                    IFishProvider        fi   = FishManager.Instance.GetFishProvider(this.Context);
                                    if (depl != null && meas != null && fi != null)
                                    {
                                        if (depl.CanCreate())
                                        {
                                            SampleEventMapItem  item    = map.Get(KnownDetType.Fish);
                                            List <EntityBundle> bundles = this.GetBundles(map, KnownDetType.Fish);
                                            if (item != null && bundles != null && bundles.Count >= 3)
                                            {
                                                EntityBundle mac = null;
                                                if (bundles.Count > 3)
                                                {
                                                    mac = bundles[3];
                                                }
                                                status = this.DeltaLoad(map.SampleEventId, bundles[0], bundles[1], bundles[2], mac, curdet, olddet, depl, meas, fi, map);
                                                if (status != null && status.Issue == UpdateIssue.AllOk) //success, so we overwrite the file
                                                {
                                                    curdet   = null;
                                                    curexcel = null;
                                                    olddet   = null;
                                                    excel    = null;
                                                    //NOTE -- making new file the permanent file and removing old file, then updating map for new file
                                                    IFileStoreProvider ip = FileStoreManager.Instance.GetProvider();
                                                    if (ip.Replace(tempFileUpload, oldFile)) //overwrite the existing file with the new one
                                                    {
                                                        tempFileUpload.Dispose();
                                                        oldFile.Dispose();
                                                        //ip.Delete(tempFileUpload); //may still be references so delete can fail
                                                        return(status); //we're done here
                                                    }
                                                    else
                                                    {
                                                        status = new UpdateStatus(UpdateIssue.DataIssue);
                                                        status.Add(new IssueNotice("File", "Failed to replace file"));
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                status = new UpdateStatus(UpdateIssue.DataIssue);
                                                status.Add(new IssueNotice("Bundles", "Failed to get bundles"));
                                            }
                                        }
                                        else
                                        {
                                            status = new UpdateStatus(UpdateIssue.Security);
                                            status.Add(new IssueNotice("Permissions", "create denied"));
                                        }
                                    }
                                    else
                                    {
                                        status = new UpdateStatus(UpdateIssue.SystemIssue);
                                        status.Add(new IssueNotice("Failure", "Failed to get provider"));
                                    }
                                    return(status);
                                }
                            }
                            else
                            {
                                UpdateStatus stat = new UpdateStatus(UpdateIssue.NoExistingFile);
                                foreach (ValidationIssue cur in issues)
                                {
                                    stat.Add(new IssueNotice(cur.IssueCode.ToString(), cur.IssueMessage));
                                }
                                return(stat);
                            }
                        }
                        else
                        {
                            UpdateStatus stat = new UpdateStatus(UpdateIssue.FileValidationIssues);
                            foreach (ValidationIssue cur in curIssues)
                            {
                                stat.Add(new IssueNotice(cur.IssueCode.ToString(), cur.IssueMessage));
                            }
                            return(stat);
                        }
                    }
                    else
                    {
                        UpdateStatus status = new UpdateStatus(UpdateIssue.Security);
                        status.Add(new IssueNotice("Permissions", "create denied"));
                        return(status);
                    }
                }
                return(new UpdateStatus(UpdateIssue.NoExistingFile));
            }
            return(new UpdateStatus(UpdateIssue.NoFilePosted));
        }
Exemplo n.º 4
0
        //to support mutliple det types in a single request (which all share the same sample event), we have a collection of items as follows:
        //{id:<fieldtripid>, name:<sampleeventname - to be>, desc:<sampleeventdesc - to be>, princorgid: <id>, start:<date>, end:<date>, dets:[<det element>,...]}
        //each det element is an object as follows:
        //{type:<dettype>, privacy:<bool>, <<entity bundle ids specific to det>>}   (example is sites:{id:bundleid,include:[key list]})    optional "exclude" instead of include would allow removing elements from bundle in det
        //returns fileid of the file to download
        //      note that the name is required, desc is optional - name
        //for all types, the entity bundle format is:  {id:<entitybundleid>, keys:[<id>, <id>, ...]}
        //      note that keys are optional - if not provided it means "all items in bundle"
        //      note that the bundle should be of the type expected for the parameter - e.g. if the provided bundle is for "sites", that bundle should be of sites
        internal static void Create(UserSecurityContext user, JToken dat, HttpContext context, CancellationToken cancel)
        {
            if (dat != null)
            {
                JObject payload = dat as JObject;
                if (payload != null)
                {
                    //these are requried for any and all DETs to create
                    CompoundIdentity ftId    = JsonUtils.ToId(payload[JsonUtils.Id]);
                    CompoundIdentity prOrgId = JsonUtils.ToId(payload[JsonUtils.OwnerId]);

                    if (HasAffiliation(user, prOrgId))
                    {
                        string name = JsonUtils.ToString(payload[JsonUtils.Name]);
                        string desc = JsonUtils.ToString(payload[JsonUtils.Description]);

                        ValueRange <DateTime> range = JsonUtils.ToRange(payload, JsonUtils.Start, JsonUtils.Finish);

                        if (ftId != null && prOrgId != null && !string.IsNullOrEmpty(name))
                        {
                            JToken dets = payload["dets"];
                            if (dets != null && dets is JArray)
                            {
                                JArray detItems = dets as JArray;
                                if (detItems != null)
                                {
                                    List <DetPayload> items = JsonDetExtractor.ExtractAll(detItems);
                                    if (items != null && items.Count > 0) //we actually have things to create
                                    {
                                        GeneralDetProcessor prov = DetProcessorManager.Instance.GetProvider(user);
                                        if (prov != null)
                                        {
                                            CompoundIdentity     seId  = prov.CreateSampleEvent(ftId, prOrgId, name, desc, range);
                                            EntityBundleProvider eProv = EntityBundleManager.Instance.GetProvider(user);
                                            if (eProv != null)
                                            {
                                                if (seId != null)
                                                {
                                                    Dictionary <string, Guid> fileIds = new Dictionary <string, Guid>();
                                                    foreach (DetPayload cur in items)
                                                    {
                                                        if (cur.DetTypeName == KnownDets.Instance.WQ)
                                                        {
                                                            EntityBundle siteBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Sites]);
                                                            EntityBundle instBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Instruments]);
                                                            if (siteBundle != null && instBundle != null)
                                                            {
                                                                FilestoreFile fil = prov.CreateWQ(seId, siteBundle, instBundle, cur.IsPrivate);
                                                                if (fil != null)
                                                                {
                                                                    fileIds.Add(cur.DetTypeName, fil.FileId);
                                                                    fil.Dispose();
                                                                    fil = null;
                                                                }
                                                            }
                                                        }
                                                        else if (cur.DetTypeName == KnownDets.Instance.Fish)
                                                        {
                                                            EntityBundle siteBundle  = eProv.Get(cur.EntityBundles[JsonDetExtractor.Sites]);
                                                            EntityBundle instBundle  = eProv.Get(cur.EntityBundles[JsonDetExtractor.Nets]);
                                                            EntityBundle fishBundle  = eProv.Get(cur.EntityBundles[JsonDetExtractor.Fish]);
                                                            EntityBundle macroBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Macro]);

                                                            if (siteBundle != null && instBundle != null && fishBundle != null && macroBundle != null)
                                                            {
                                                                FilestoreFile fil = prov.CreateFish(seId, siteBundle, instBundle, fishBundle, macroBundle, cur.IsPrivate);
                                                                if (fil != null)
                                                                {
                                                                    fileIds.Add(cur.DetTypeName, fil.FileId);
                                                                    fil.Dispose();
                                                                    fil = null;
                                                                }
                                                            }
                                                        }
                                                        else if (cur.DetTypeName == KnownDets.Instance.Veg)
                                                        {
                                                            EntityBundle siteBundle  = eProv.Get(cur.EntityBundles[JsonDetExtractor.Sites]);
                                                            EntityBundle treeBundle  = eProv.Get(cur.EntityBundles[JsonDetExtractor.Tree]);
                                                            EntityBundle shrubBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Shrub]);
                                                            EntityBundle herbBundle  = eProv.Get(cur.EntityBundles[JsonDetExtractor.Herb]);
                                                            EntityBundle plotBundle  = eProv.Get(cur.EntityBundles[JsonDetExtractor.Plots]);

                                                            EntityBundle nlBundle = null;
                                                            if (JsonDetExtractor.NonLiving != null)
                                                            {
                                                                nlBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.NonLiving]);
                                                            }


                                                            if (siteBundle != null && treeBundle != null && shrubBundle != null && herbBundle != null && plotBundle != null)
                                                            {
                                                                FilestoreFile fil = prov.CreateVeg(seId, siteBundle, plotBundle, shrubBundle, treeBundle, herbBundle, nlBundle, cur.IsPrivate);
                                                                if (fil != null)
                                                                {
                                                                    fileIds.Add(cur.DetTypeName, fil.FileId);
                                                                    fil.Dispose();
                                                                    fil = null;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    //NOTE - we may want to deal with partial successes in case we have multiple DETs and some work, some don't
                                                    //ok, now we do the response with a list of fileIds
                                                    if (fileIds.Count > 0)
                                                    {
                                                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, JsonUtils.ToJson(fileIds).ToString()));
                                                        return;
                                                    }
                                                    else
                                                    {
                                                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed, new JValue("\"No items succeeded\"").ToString()));
                                                        return;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        context.Response.StatusCode = HttpStatusCodes.Status401Unauthorized; //in this case, by way of affiliation
                        return;
                    }
                }
            }
            context.Response.StatusCode = HttpStatusCodes.Status400BadRequest;
        }