public CatchEffort Get(CompoundIdentity id) { if (!id.IsNullOrEmpty() && Db.DataStoreIdentity.Equals(id.DataStoreIdentity) && this.CanGet()) { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectFishCatchEfforts + Db.Where + Db.WhereId; cmd.Parameters.AddWithValue("id", id.Identity); NpgsqlDataReader rdr = Db.ExecuteReader(cmd); CatchEffort o = null; if (rdr != null) { try { rdr.Read(); o = this.builder.Build(rdr); if (cmd.Connection.State == System.Data.ConnectionState.Open) { cmd.Connection.Close(); } } catch { } finally { cmd.Dispose(); } } return(o); } return(null); }
internal bool CanDelete(CompoundIdentity catchEffortId) { if (this.CanDelete()) { if (!catchEffortId.Equals(lastCatchEffortId)) { this.lastCatchEffortId = catchEffortId; CatchEffort depl = this.Get(catchEffortId); if (depl != null) { this.lastEditPermission = this.CanDelete(depl); } else // can't get the effort, so it will always fail { this.lastEditPermission = false; } return(this.lastEditPermission); } else { return(this.lastEditPermission); //same as last check } } return(false); }
public bool CanDelete(CatchEffort item) { if (item != null && Db.DataStoreIdentity.Equals(item.Identity.DataStoreIdentity)) { if (this.CanDelete()) { UserProvider prov = UserAffilationSecurityManager.Instance.GetProvider(this.Context); if (prov != null) { return(prov.HasAffiliationForSampleEvent(item.SampleEventId, true)); } } } return(false); }
public bool Delete(CatchEffort item) { if (item != null && this.CanDelete(item)) { try { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.DeleteFishCatchEfforts; cmd.Parameters.AddWithValue("id", item.Identity.Identity); Db.ExecuteNonQuery(cmd); cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("ceid", item.Identity.Identity); //used for catches, counts, fish, hauls, etc. cmd.CommandText = Db.DeleteFishCatchMetrics + Db.WhereEffort; Db.ExecuteNonQuery(cmd); cmd.CommandText = Db.DeleteFishCount + Db.WhereEffort; Db.ExecuteNonQuery(cmd); cmd.CommandText = Db.DeleteFishNetHaulEvent + Db.WhereEffort; Db.ExecuteNonQuery(cmd); //--items sub to fish do a where fishid in (select) cmd.CommandText = Db.DeleteFishDiet + Db.WhereEffortIn; Db.ExecuteNonQuery(cmd); cmd.CommandText = Db.DeleteFishGenetics + Db.WhereEffortIn; Db.ExecuteNonQuery(cmd); cmd.CommandText = Db.DeleteFishIdTag + Db.WhereEffortIn; Db.ExecuteNonQuery(cmd); //--end all sub-items, now it's ok to remove the fish itself cmd.CommandText = Db.DeleteFishIndividual + Db.WhereEffort; Db.ExecuteNonQuery(cmd); return(true); } catch { } } return(false); }
public IEnumerable <CatchEffort> GetForSite(CompoundIdentity siteId) { if (!siteId.IsNullOrEmpty() && this.CanGet()) { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectFishCatchEfforts + Db.Where + Db.WhereSite; cmd.Parameters.AddWithValue("sitesid", siteId.DataStoreIdentity); cmd.Parameters.AddWithValue("siteid", siteId.Identity); NpgsqlDataReader rdr = Db.ExecuteReader(cmd); CatchEffort o = null; List <CatchEffort> permissions = new List <CatchEffort>(); if (rdr != null) { try { while (rdr.Read()) { o = this.builder.Build(rdr); if (o != null) { permissions.Add(o); } } if (cmd.Connection.State == System.Data.ConnectionState.Open) { cmd.Connection.Close(); } } catch { } finally { cmd.Dispose(); } } return(permissions); } return(null); }
//\"Id\", \"CatchEffortId\", \"TaxaSystemId\", \"TaxaId\", \"Count\", \"Description\" public FishCount Build(DbDataReader reader) { FishCount tmp = new FishCount(DbReaderUtils.GetGuid(reader, 0), new CompoundIdentity(Db.DataStoreIdentity, DbReaderUtils.GetGuid(reader, 1)), new CompoundIdentity(DbReaderUtils.GetGuid(reader, 2), DbReaderUtils.GetGuid(reader, 3)), (uint)DbReaderUtils.GetInt32(reader, 4), DbReaderUtils.GetString(reader, 5)); if (!seen.Contains(tmp.CatchEffortId)) { seen.Add(tmp.CatchEffortId); CatchEffort e = this.helperBuilder.Get(tmp.CatchEffortId); if (e != null) { got.Add(tmp.CatchEffortId); } } if (!got.Contains(tmp.CatchEffortId)) { return(null); } return(tmp); }
//\"Id\", \"CatchEffortId\", \"MetricType\", \"Value\", \"Description\" public CatchMetric Build(DbDataReader reader) { CatchMetric tmp = new CatchMetric(DbReaderUtils.GetGuid(reader, 0), new CompoundIdentity(Db.DataStoreIdentity, DbReaderUtils.GetGuid(reader, 1)), DbReaderUtils.GetSingle(reader, 3), DbReaderUtils.GetString(reader, 2), DbReaderUtils.GetString(reader, 4)); if (!seen.Contains(tmp.CatchEffortId)) { seen.Add(tmp.CatchEffortId); CatchEffort e = this.helperBuilder.Get(tmp.CatchEffortId); if (e != null) { got.Add(tmp.CatchEffortId); } } if (!got.Contains(tmp.CatchEffortId)) { return(null); } return(tmp); }
//\"Id\", \"CatchEffortId\", \"TaxaSystemId\", \"TaxaId\", \"LengthStandard\", \"LengthFork\", \"LengthTotal\", \"Weight\", \"AdClipped\", \"CWT\", \"Description\" public Fish Build(DbDataReader reader) { Fish tmp = new Fish(DbReaderUtils.GetGuid(reader, 0), new CompoundIdentity(Db.DataStoreIdentity, DbReaderUtils.GetGuid(reader, 1)), new CompoundIdentity(DbReaderUtils.GetGuid(reader, 2), DbReaderUtils.GetGuid(reader, 3)), DbReaderUtils.GetSingle(reader, 4), DbReaderUtils.GetSingle(reader, 5), DbReaderUtils.GetSingle(reader, 6), DbReaderUtils.GetSingle(reader, 7), DbReaderUtils.GetNullableBoolean(reader, 8), DbReaderUtils.GetNullableBoolean(reader, 9), DbReaderUtils.GetString(reader, 10)); if (!seen.Contains(tmp.CatchEffortId)) { seen.Add(tmp.CatchEffortId); CatchEffort e = this.helperBuilder.Get(tmp.CatchEffortId); if (e != null) { got.Add(tmp.CatchEffortId); } } if (!got.Contains(tmp.CatchEffortId)) { return(null); } return(tmp); }
public static JObject ToJson(CatchEffort effort) { if (effort != null) { JObject o = new JObject(); o.Add(JsonUtils.Id, JsonUtils.ToJson(effort.Identity)); o.Add("sampleeventid", JsonUtils.ToJson(effort.SampleEventId)); o.Add("siteid", JsonUtils.ToJson(effort.SiteId)); o.Add("sampledate", effort.SampleDate); if (effort.Location != null) { o.Add("location", effort.Location.ToString()); } if (effort.CatchMethod != null) { o.Add("catchmethod", effort.CatchMethod); } if (effort.Strata != null) { o.Add("strata", effort.Strata); } o.Add("depth", effort.Depth); o.Add("ph", effort.pH); o.Add("temp", effort.Temp); o.Add("do", effort.DO); o.Add("salinity", effort.Salinity); o.Add("velocity", effort.Velocity); if (effort.Description != null) { o.Add(JsonUtils.Description, effort.Description); } o.Add("isprivate", effort.IsPrivate); return(o); } return(null); }
public bool Update(CatchEffort item) { if (item != null && this.CanUpdate(item)) { try { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.UpdateFishCatchEfforts; cmd.Parameters.AddWithValue("id", item.Identity.Identity); cmd.Parameters.AddWithValue("esid", item.SampleEventId.DataStoreIdentity); cmd.Parameters.AddWithValue("eid", item.SampleEventId.Identity); Db.AddParam(cmd, "ssid", "sid", item.SiteId); Db.AddParam(cmd, "when", item.SampleDate); Db.AddParam(cmd, "loc", item.Location); Db.AddParam(cmd, "method", item.CatchMethod); Db.AddParam(cmd, "strata", item.Strata); Db.AddParam(cmd, "depth", item.Depth); Db.AddParam(cmd, "ph", item.pH); Db.AddParam(cmd, "tmp", item.Temp); Db.AddParam(cmd, "dox", item.DO); Db.AddParam(cmd, "sal", item.Salinity); Db.AddParam(cmd, "vel", item.Velocity); Db.AddParam(cmd, "d", item.Description); cmd.Parameters.AddWithValue("priv", item.IsPrivate); Db.ExecuteNonQuery(cmd); return(true); } catch { } } return(false); }
private UpdateStatus InitialLoad(CompoundIdentity seId, EntityBundle sites, EntityBundle nets, EntityBundle fishSpecies, EntityBundle macroSpecies, FishDET curdet, ICatchEffortProvider depl, ICatchHaulProvider haul, IFishProvider fish, bool isPrivate) { UpdateStatus stat = null; if (sites == null || nets == null || fishSpecies == null || seId == null || seId.IsEmpty || macroSpecies == null) { stat = new UpdateStatus(UpdateIssue.DataIssue); stat.Add(new IssueNotice("BundlesOrEvent", "Null value")); } else { //TODO -- may need to do case insensitive ops or not -- verify how excel part is implemented Dictionary <string, CompoundIdentity> deployIds = new Dictionary <string, CompoundIdentity>(); BundleElement elem; foreach (CatchEffortDTO cur in curdet.CatchEfforts.Values) { if (cur != null && !deployIds.ContainsKey(cur.CatchId)) { elem = sites.Get(cur.SiteId); if (elem != null) { CompoundIdentity siteId = elem.EntityId; Point2 <double> tmpPoint = null; if (cur.CatchX.HasValue && cur.CatchY.HasValue) { if (!(cur.CatchX.Value.IsInfiniteOrNaN() && cur.CatchY.Value.IsInfiniteOrNaN())) { tmpPoint = GeometryFactory2Double.Instance.ConstructPoint(cur.CatchX.Value, cur.CatchY.Value); } } double dep = cur.Depth.HasValue ? cur.Depth.Value : double.NaN; double dox = cur.DO.HasValue ? cur.DO.Value : double.NaN; double pH = cur.pH.HasValue ? cur.pH.Value : double.NaN; double sal = cur.Salinity.HasValue ? cur.Salinity.Value : double.NaN; double tmp = cur.Temp.HasValue ? cur.Temp.Value : double.NaN; double vel = cur.Velocity.HasValue ? cur.Velocity.Value : double.NaN; CatchEffort cat = depl.Create(seId, siteId, cur.DateTime, cur.Comments, isPrivate, cur.CatchMethod, cur.HabitatStrata, tmpPoint, (float)dep, (float)pH, (float)tmp, (float)dox, (float)sal, (float)vel); if (cat != null) { deployIds[cur.CatchId] = cat.Identity; } else { stat = Add("Create", "Deployment", UpdateIssue.DataIssue, stat); } } else { stat = Add("SiteCode", "Empty or missing", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Empty or missing", UpdateIssue.DataIssue, stat); } } foreach (CatchMetricDTO cur in curdet.CatchMetrics.Values) { if (cur != null && deployIds.ContainsKey(cur.CatchId)) { CompoundIdentity depId = deployIds[cur.CatchId]; if (depId != null) { try { CatchMetric met = haul.CreateMetric(depId, (float)cur.Value, cur.MetricType, cur.Comments); if (met == null) { stat = Add("Create", "CatchMetric", UpdateIssue.DataIssue, stat); } } catch { stat = Add("Create", "CatchMetric", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Fail to lookup in CatchMetric", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Fail to lookup in CatchMetric", UpdateIssue.DataIssue, stat); } } foreach (NetHaulEventDTO cur in curdet.NetHaulEvents.Values) { if (cur != null && deployIds.ContainsKey(cur.CatchId)) { CompoundIdentity depId = deployIds[cur.CatchId]; if (depId != null) { elem = nets.Get(cur.NetId); if (elem != null) { CompoundIdentity netId = elem.EntityId; try { double area = cur.AreaSampled.HasValue ? cur.AreaSampled.Value : double.NaN; double vol = cur.VolumeSampled.HasValue ? cur.VolumeSampled.Value : double.NaN; NetHaulEvent met = haul.CreateHaul(depId, netId, (float)area, (float)vol, cur.Comments); if (met == null) { stat = Add("Create", "NetHaulEvent", UpdateIssue.DataIssue, stat); } } catch { stat = Add("Create", "NetHaulEvent", UpdateIssue.DataIssue, stat); } } else { stat = Add("NetId", "Fail to lookup in NetHaulEvent", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Fail to lookup in NetHaulEvent", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Fail to lookup in NetHaulEvent", UpdateIssue.DataIssue, stat); } } foreach (FishCountDTO cur in curdet.FishCounts.Values) { if (cur != null && deployIds.ContainsKey(cur.CatchId)) { CompoundIdentity depId = deployIds[cur.CatchId]; if (depId != null) { elem = fishSpecies.Get(cur.SpeciesId); if (elem != null) { CompoundIdentity fishId = elem.EntityId; try { FishCount met = haul.CreateFishCount(depId, fishId, cur.Count.HasValue?cur.Count.Value:0, cur.Comments); if (met == null) { stat = Add("Create", "FishCount", UpdateIssue.DataIssue, stat); } } catch { stat = Add("Create", "FishCount", UpdateIssue.DataIssue, stat); } } else { stat = Add("TaxaId", "Fail to lookup in FishCount", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Fail to lookup in FishCount", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Fail to lookup in FishCount", UpdateIssue.DataIssue, stat); } } //keep track of all fishids as we go for lookups Dictionary <string, Guid> fishIds = new Dictionary <string, Guid>(); foreach (FishDTO cur in curdet.Fish.Values) { if (cur != null && deployIds.ContainsKey(cur.CatchId)) { CompoundIdentity depId = deployIds[cur.CatchId]; if (depId != null) { elem = fishSpecies.Get(cur.SpeciesId); if (elem != null) { CompoundIdentity fishId = elem.EntityId; try { double std = cur.LengthStandard.HasValue ? cur.LengthStandard.Value : double.NaN; double frk = cur.LengthFork.HasValue ? cur.LengthFork.Value : double.NaN; double tot = cur.LengthTotal.HasValue ? cur.LengthTotal.Value : double.NaN; double wei = cur.Mass.HasValue ? cur.Mass.Value : double.NaN; Osrs.Oncor.WellKnown.Fish.Fish met = fish.CreateFish(depId, fishId, (float)std, (float)frk, (float)tot, (float)wei, cur.AdClipped, cur.CodedWireTag, cur.Comments); fishIds[cur.FishId] = met.Identity; if (met == null) { stat = Add("Create", "Fish", UpdateIssue.DataIssue, stat); } } catch { stat = Add("Create", "Fish", UpdateIssue.DataIssue, stat); } } else { stat = Add("TaxaId", "Fail to lookup in Fish", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Fail to lookup in Fish", UpdateIssue.DataIssue, stat); } } else { stat = Add("CatchId", "Fail to lookup in Fish", UpdateIssue.DataIssue, stat); } } foreach (DietDTO cur in curdet.Diet.Values) { if (cur != null && fishIds.ContainsKey(cur.FishId)) { Guid depId = fishIds[cur.FishId]; if (!Guid.Empty.Equals(depId)) { elem = macroSpecies.Get(cur.SpeciesId); if (elem != null || string.IsNullOrEmpty(cur.SpeciesId)) //allow for empty gut { CompoundIdentity taxaId; if (elem != null) { taxaId = elem.EntityId; } else { taxaId = new CompoundIdentity(Guid.Empty, Guid.Empty); } try { double sam = cur.SampleMass.HasValue ? cur.SampleMass.Value : double.NaN; double ind = cur.IndividualMass.HasValue ? cur.IndividualMass.Value : double.NaN; uint? wh = null; if (cur.WholeAnimalsWeighed.HasValue) { wh = (uint)cur.WholeAnimalsWeighed.Value; } FishDiet met = null; if (taxaId.IsEmpty) { if (cur.Count == 0) //the only way its legal { met = fish.CreateFishDiet(depId, taxaId, cur.GutSampleId, cur.VialId, cur.LifeStage, cur.Count, (float)sam, (float)ind, wh, cur.Comments); } } else { met = fish.CreateFishDiet(depId, taxaId, cur.GutSampleId, cur.VialId, cur.LifeStage, cur.Count, (float)sam, (float)ind, wh, cur.Comments); } if (met == null) { stat = Add("Create", "FishDiet", UpdateIssue.DataIssue, stat); } } catch { stat = Add("Create", "FishDiet", UpdateIssue.DataIssue, stat); } } else { stat = Add("TaxaId", "Fail to lookup in FishDiet", UpdateIssue.DataIssue, stat); } } else { stat = Add("FishId", "Fail to lookup in FishDiet", UpdateIssue.DataIssue, stat); } } else { stat = Add("FishId", "Fail to lookup in FishDiet", UpdateIssue.DataIssue, stat); } } foreach (GeneticDTO cur in curdet.Genetics.Values) { if (cur != null && fishIds.ContainsKey(cur.FishId)) { Guid depId = fishIds[cur.FishId]; if (!Guid.Empty.Equals(depId)) { try { StockEstimates est = new StockEstimates(); if (!string.IsNullOrEmpty(cur.BestStockEstimate) && cur.ProbabilityBest.HasValue) { est[cur.BestStockEstimate] = (float)cur.ProbabilityBest.Value; } if (!string.IsNullOrEmpty(cur.SecondStockEstimate) && cur.ProbabilitySecondBest.HasValue) { est[cur.SecondStockEstimate] = (float)cur.ProbabilitySecondBest.Value; } if (!string.IsNullOrEmpty(cur.ThirdStockEstimate) && cur.ProbabilityThirdBest.HasValue) { est[cur.ThirdStockEstimate] = (float)cur.ProbabilityThirdBest.Value; } FishGenetics met = fish.CreateFishGenetics(depId, cur.GeneticSampleId, cur.LabSampleId, est, cur.Comments); if (met == null) { stat = Add("Create", "FishIdTag", UpdateIssue.DataIssue, stat); } } catch { stat = Add("Create", "FishIdTag", UpdateIssue.DataIssue, stat); } } else { stat = Add("FishId", "Fail to lookup in FishIdTag", UpdateIssue.DataIssue, stat); } } else { stat = Add("FishId", "Fail to lookup in FishIdTag", UpdateIssue.DataIssue, stat); } } foreach (IdTagDTO cur in curdet.IdTags.Values) { if (cur != null && fishIds.ContainsKey(cur.FishId)) { Guid depId = fishIds[cur.FishId]; if (!Guid.Empty.Equals(depId)) { try { FishIdTag met = fish.CreateIdTag(depId, cur.TagCode, cur.TagType, cur.TagManufacturer, cur.Comments); if (met == null) { stat = Add("Create", "FishIdTag", UpdateIssue.DataIssue, stat); } } catch { stat = Add("Create", "FishIdTag", UpdateIssue.DataIssue, stat); } } else { stat = Add("FishId", "Fail to lookup in FishIdTag", UpdateIssue.DataIssue, stat); } } else { stat = Add("FishId", "Fail to lookup in FishIdTag", UpdateIssue.DataIssue, stat); } } if (stat == null) { stat = new UpdateStatus(UpdateIssue.AllOk); stat.Add(new IssueNotice("NoIssues", "No issues")); } } return(stat); }
public IEnumerable <CatchEffort> GetForSite(CompoundIdentity siteId, DateTime start, DateTime end) { if (!siteId.IsNullOrEmpty() && this.CanGet()) { DateTime now = DateTime.UtcNow; string sql = null; if (start > FishUtils.GlobalMinDate) { sql = Db.Where + Db.WhereStart; } if (end < now) { if (sql == null) { sql = Db.Where + Db.WhereEnd; } else //already have start { sql = sql + " AND " + Db.WhereEnd; } } if (sql != null) //we need to have at least a start or end constraint that's limiting { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectFishCatchEfforts + sql + " AND " + Db.WhereSite; cmd.Parameters.AddWithValue("sitesid", siteId.DataStoreIdentity); cmd.Parameters.AddWithValue("siteid", siteId.Identity); if (start > FishUtils.GlobalMinDate) { cmd.Parameters.AddWithValue("start", start); } if (end < now) { cmd.Parameters.AddWithValue("end", end); } NpgsqlDataReader rdr = Db.ExecuteReader(cmd); CatchEffort o = null; List <CatchEffort> permissions = new List <CatchEffort>(); if (rdr != null) { try { while (rdr.Read()) { o = this.builder.Build(rdr); if (o != null) { permissions.Add(o); } } if (cmd.Connection.State == System.Data.ConnectionState.Open) { cmd.Connection.Close(); } } catch { } finally { cmd.Dispose(); } } return(permissions); } } return(null); }
public IEnumerable <CatchEffort> Get(DateTime start, DateTime end) { if (this.CanGet()) { DateTime now = DateTime.UtcNow; string sql = null; if (start > FishUtils.GlobalMinDate) { sql = Db.Where + Db.WhereStart; } if (end < now) { if (sql == null) { sql = Db.Where + Db.WhereEnd; } else //already have start { sql = sql + " AND " + Db.WhereEnd; } } if (sql != null) { NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString); cmd.CommandText = Db.SelectFishCatchEfforts + sql; if (start > FishUtils.GlobalMinDate) { cmd.Parameters.AddWithValue("start", start); } if (end < now) { cmd.Parameters.AddWithValue("end", end); } NpgsqlDataReader rdr = Db.ExecuteReader(cmd); CatchEffort o = null; List <CatchEffort> permissions = new List <CatchEffort>(); if (rdr != null) { try { while (rdr.Read()) { o = this.builder.Build(rdr); if (o != null) { permissions.Add(o); } } if (cmd.Connection.State == System.Data.ConnectionState.Open) { cmd.Connection.Close(); } } catch { } finally { cmd.Dispose(); } } return(permissions); } } return(null); }