//upload is done first, with the fileid returned, then we call this update which will process the uploaded file internal static void Update(UserSecurityContext user, JToken dat, HttpContext context, CancellationToken cancel) { if (dat != null) { JObject payload = dat as JObject; if (payload != null) { Guid fileId = JsonUtils.ToGuid(dat[JsonUtils.Id]); if (!Guid.Empty.Equals(fileId)) { FilestoreFile tempFile = FileStoreManager.Instance.GetProvider().Get(fileId); if (tempFile != null) { GeneralDetProcessor prov = DetProcessorManager.Instance.GetProvider(user); if (prov != null) { UpdateStatus stat = prov.Update(tempFile); PushStat(stat, context); return; } } } } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }
public FilestoreFile Make(Guid fileId) { try { string tmpPath = FilestoreFile.TmpPath(fileId); string permPath = FilestoreFile.PermPath(fileId); if (!File.Exists(tmpPath) && !File.Exists(permPath)) { DateTime init = DateTime.UtcNow; FileStream fs = File.Create(permPath); fs.Dispose(); if (Db.Insert(fileId, init, init, init, DateTime.MaxValue, false, null)) { return(Db.Open(fileId)); } else { File.Delete(permPath); //as a clean up } } } catch { } return(null); }
public FilestoreFile MakeTemp(DateTime expiration) { try { Guid id = Guid.NewGuid(); string tmpPath = FilestoreFile.TmpPath(id); string permPath = FilestoreFile.PermPath(id); if (!File.Exists(tmpPath) && !File.Exists(permPath)) { DateTime init = DateTime.UtcNow; FileStream fs = File.Create(tmpPath); fs.Dispose(); if (Db.Insert(id, init, init, init, expiration, true, null)) { return(Db.Open(id)); } else { File.Delete(tmpPath); //as a clean up } } } catch { } return(null); }
public void DeleteExpired() { DateTime cur = DateTime.UtcNow; cur = new DateTime(cur.Ticks, DateTimeKind.Utc); List <FilestoreFile> items = Db.GetExpired(cur); Db.DeleteExpired(items); //deletes the db records so nobody will come looking - makes it somewhat thread safe (unless currently locked) - all in one sql statement, which could fail if we have a ton of items (>2000 ish) foreach (FilestoreFile item in items) { try { if (item.IsTempFile && File.Exists(FilestoreFile.TmpPath(item.FileId))) { File.Delete(FilestoreFile.TmpPath(item.FileId)); } else if (File.Exists(FilestoreFile.PermPath(item.FileId))) { File.Delete(FilestoreFile.PermPath(item.FileId)); } } catch { } } }
public bool Update(FilestoreFile file) { if (file != null) { return(Db.Update(file.FileId, file.LastModified, file.LastAccessed, file.ExpiresAt, file.IsTempFile, file.FileName)); } return(false); }
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); }
public bool Delete(FilestoreFile file) { if (file != null) { file.Close(); return(Delete(file.FileId, file.IsTempFile)); } return(false); }
public bool Delete(Guid fileId) { FilestoreFile tmp = this.Get(fileId); if (tmp != null) { return(Delete(tmp.FileId, tmp.IsTempFile)); } return(false); }
public bool MakePermanent(Guid fileId) { FilestoreFile tmp = Get(fileId); if (tmp != null) { return(MakePermanent(tmp)); } return(false); }
public FilestoreFile CreateVeg(CompoundIdentity sampleEventId, EntityBundle sites, EntityBundle plotTypes, EntityBundle shrubSpecies, EntityBundle treeSpecies, EntityBundle herbSpecies, EntityBundle nonLiving, bool isPrivate) { if (!sampleEventId.IsNullOrEmpty() && sites != null && plotTypes != null && shrubSpecies != null && treeSpecies != null && herbSpecies != null && sites.DataType == BundleDataType.Site && plotTypes.DataType == BundleDataType.PlotType && shrubSpecies.DataType == BundleDataType.TaxaUnit && treeSpecies.DataType == BundleDataType.TaxaUnit && herbSpecies.DataType == BundleDataType.TaxaUnit) { if (nonLiving != null) { if (nonLiving.DataType != BundleDataType.TaxaUnit) { return(null); //if we have nonLiving, they better be taxaunits } } SampleEventMap map = DetRegistry.Instance.Get(sampleEventId); if (map == null) { map = DetRegistry.Instance.Create(sampleEventId); } else if (map.Contains(KnownDetType.Veg)) { return(null); //can't have more than 1 } VegDetProcessor wq = new VegDetProcessor(this.ctx); FilestoreFile fil = wq.Create(map, sites, plotTypes, shrubSpecies, treeSpecies, herbSpecies, nonLiving, isPrivate); //note the permission is checked in there if (fil != null) { map.Add(fil.FileId, KnownDetType.Veg, isPrivate); List <Guid> bundles = map.Get(KnownDetType.Veg).BundleIds; bundles.Add(sites.Id); bundles.Add(plotTypes.Id); bundles.Add(shrubSpecies.Id); bundles.Add(treeSpecies.Id); bundles.Add(herbSpecies.Id); if (nonLiving != null) { bundles.Add(nonLiving.Id); } DetRegistry.Instance.Update(map); SamplingEvent e = this.GetSampleEvent(map.SampleEventId); if (e != null) { string tmp = e.Name.Trim().Replace(' ', '_'); if (tmp.Length > 25) { tmp = tmp.Substring(0, 24); } fil.FileName = tmp + "_Veg.xlsx"; FileStoreManager.Instance.GetProvider().Update(fil); } } return(fil); } return(null); }
public FilestoreFile CreateFish(CompoundIdentity sampleEventId, EntityBundle sites, EntityBundle nets, EntityBundle fishSpecies, EntityBundle macroSpecies, bool isPrivate) { if (!sampleEventId.IsNullOrEmpty() && sites != null && nets != null && fishSpecies != null && sites.DataType == BundleDataType.Site && nets.DataType == BundleDataType.Instrument && fishSpecies.DataType == BundleDataType.TaxaUnit) { if (macroSpecies != null) { if (macroSpecies.DataType != BundleDataType.TaxaUnit) { return(null); //if we have macroSpecies, they better be taxaunits } } SampleEventMap map = DetRegistry.Instance.Get(sampleEventId); if (map == null) { map = DetRegistry.Instance.Create(sampleEventId); } else if (map.Contains(KnownDetType.Fish)) { return(null); //can't have more than 1 } FishDetProcessor wq = new FishDetProcessor(this.ctx); FilestoreFile fil = wq.Create(map, sites, nets, fishSpecies, macroSpecies, isPrivate); //note the permission is checked in there if (fil != null) { map.Add(fil.FileId, KnownDetType.Fish, isPrivate); List <Guid> bundles = map.Get(KnownDetType.Fish).BundleIds; bundles.Add(sites.Id); bundles.Add(nets.Id); bundles.Add(fishSpecies.Id); if (macroSpecies != null) { bundles.Add(macroSpecies.Id); } DetRegistry.Instance.Update(map); SamplingEvent e = this.GetSampleEvent(map.SampleEventId); if (e != null) { string tmp = e.Name.Trim().Replace(' ', '_'); if (tmp.Length > 25) { tmp = tmp.Substring(0, 24); } fil.FileName = tmp + "_Fish.xlsx"; FileStoreManager.Instance.GetProvider().Update(fil); } } return(fil); } return(null); }
internal static FilestoreFile Open(Guid id) { if (!Guid.Empty.Equals(id)) { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectSql + " WHERE \"Id\"=:id"; cmd.Parameters.AddWithValue("id", id); NpgsqlDataReader rdr = Db.ExecuteReader(cmd); FilestoreFile tmp = null; try { if (rdr.Read()) { DateTime cr = DbReaderUtils.GetDate(rdr, 1); DateTime u = DbReaderUtils.GetDate(rdr, 2); DateTime a = DbReaderUtils.GetDate(rdr, 3); DateTime e = DbReaderUtils.GetDate(rdr, 4); bool t = DbReaderUtils.GetBoolean(rdr, 5); string fn = DbReaderUtils.GetString(rdr, 6); if (DateTime.MinValue == e) { e = DateTime.MaxValue; } tmp = new FilestoreFile(id, cr, u, a, e, t, fn); } if (cmd.Connection.State == System.Data.ConnectionState.Open) { cmd.Connection.Close(); } } catch { } finally { cmd.Dispose(); } if (DateTime.Now >= tmp.ExpiresAt) //just handle last minute deletes { Delete(id); tmp = null; } return(tmp); } return(null); }
public UpdateStatus Update(FilestoreFile tempFileUpload) { if (tempFileUpload != null) { Guid fileId = DetProcessorBase.GetFileId(tempFileUpload); if (!Guid.Empty.Equals(fileId)) { SampleEventMap map = DetRegistry.Instance.Get(fileId); if (map != null) { SampleEventMapItem typ = map.Get(fileId); //this is to lookup the DET type this file was for if (typ != null && typ.DetType == KnownDetType.WaterQuality) { WqDetProcessor wq = new WqDetProcessor(this.ctx); UpdateStatus stat = wq.Update(tempFileUpload, map); if (stat != null && stat.Issue == UpdateIssue.AllOk) { DetRegistry.Instance.Update(map); } return(stat); } else if (typ != null && typ.DetType == KnownDetType.Fish) { FishDetProcessor f = new FishDetProcessor(this.ctx); UpdateStatus stat = f.Update(tempFileUpload, map); if (stat != null && stat.Issue == UpdateIssue.AllOk) { DetRegistry.Instance.Update(map); } return(stat); } else if (typ != null && typ.DetType == KnownDetType.Veg) { VegDetProcessor v = new VegDetProcessor(this.ctx); UpdateStatus stat = v.Update(tempFileUpload, map); if (stat != null && stat.Issue == UpdateIssue.AllOk) { DetRegistry.Instance.Update(map); } return(stat); } } } } return(null); }
public bool MakePermanent(FilestoreFile file) { if (file != null) { if (file.IsTempFile) { if (file.ToPermanent()) { return(this.Update(file)); } } else { return(true); //already permanent } } return(false); }
private Guid CreateCsvFile(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 temp file FilestoreFile deployFile = provider.MakeTemp(DateTime.UtcNow.AddHours(4)); CsvDb csv = CsvDb.Create(deployFile); CreateSampleEventCsv(csv, eventDict, orgDict, fieldTripDict, fieldActivityDict, projectDict); CreateMeasurementsCsv(csv, siteDict, eventDict, deploymentDict, measurementList); CreateSitesCsv(csv, siteDict); csv.Flush(); csv.Dispose(); deployFile.Close(); return(deployFile.FileId); }
private bool Delete(Guid fileId, bool isTmp) { if (Db.Delete(fileId)) { try { if (isTmp && File.Exists(FilestoreFile.TmpPath(fileId))) { File.Delete(FilestoreFile.TmpPath(fileId)); } else if (File.Exists(FilestoreFile.PermPath(fileId))) { File.Delete(FilestoreFile.PermPath(fileId)); } } catch { } return(true); } return(false); }
public FilestoreFile CreateWQ(CompoundIdentity sampleEventId, EntityBundle sites, EntityBundle instruments, bool isPrivate) { if (!sampleEventId.IsNullOrEmpty() && sites != null && instruments != null && sites.DataType == BundleDataType.Site && instruments.DataType == BundleDataType.Instrument) { SampleEventMap map = DetRegistry.Instance.Get(sampleEventId); if (map == null) { map = DetRegistry.Instance.Create(sampleEventId); } else if (map.Contains(KnownDetType.WaterQuality)) { return(null); //can't have more than 1 } WqDetProcessor wq = new WqDetProcessor(this.ctx); FilestoreFile fil = wq.Create(map, sites, instruments); //note the permission is checked in there if (fil != null) { map.Add(fil.FileId, KnownDetType.WaterQuality, isPrivate); List <Guid> bundles = map.Get(KnownDetType.WaterQuality).BundleIds; bundles.Add(sites.Id); bundles.Add(instruments.Id); DetRegistry.Instance.Update(map); SamplingEvent e = this.GetSampleEvent(map.SampleEventId); if (e != null) { string tmp = e.Name.Trim().Replace(' ', '_'); if (tmp.Length > 25) { tmp = tmp.Substring(0, 24); } fil.FileName = tmp + "_WQ.xlsx"; FileStoreManager.Instance.GetProvider().Update(fil); } } return(fil); } return(null); }
public static Guid GetFileId(FilestoreFile excelDet) { if (excelDet != null) { try { XlCustomProperties props = XlWorkbook.LoadProperties(excelDet); foreach (XlCustomProperty property in props) { if (property.Name == "oncorId") { string guidValue = property.Value; if (!string.IsNullOrEmpty(guidValue)) { return(Guid.Parse(guidValue)); } } } } catch { } } return(Guid.Empty); }
/// <summary> /// Replaces contents of permFile with contents of tmpFile. /// </summary> /// <param name="tmpFile">file to copy from</param> /// <param name="permFile">file to copy to</param> /// <returns></returns> public bool Replace(FilestoreFile tmpFile, FilestoreFile permFile) { if (tmpFile != null && permFile != null && !permFile.IsTempFile) { try { permFile.Release(); tmpFile.Release(); File.Delete(FilestoreFile.PermPath(permFile.FileId)); if (tmpFile.IsTempFile) { File.Copy(FilestoreFile.TmpPath(tmpFile.FileId), FilestoreFile.PermPath(permFile.FileId)); } else { File.Copy(FilestoreFile.PermPath(tmpFile.FileId), FilestoreFile.PermPath(permFile.FileId)); } return(true); } catch { } } return(false); }
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)); }
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; }
protected Guid GetId(FilestoreFile excelDet) { return(GetFileId(excelDet)); }
public abstract UpdateStatus Update(FilestoreFile tempFileUpload, SampleEventMap map);
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); }
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(); }
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); }
//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; }