public override TaxaUnit GetParent(CompoundIdentity taxaUnitId)
 {
     if (!taxaUnitId.IsNullOrEmpty() && this.CanGet())
     {
         NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString);
         cmd.CommandText = Db.SelectTaxaUnit + " WHERE \"Id\"=(SELECT \"ParentId\" FROM oncor.\"TaxaUnits\" WHERE \"Id\"=:id)";
         cmd.Parameters.AddWithValue("id", taxaUnitId.Identity);
         NpgsqlDataReader rdr = Db.ExecuteReader(cmd);
         TaxaUnit         t   = null;
         if (rdr != null)
         {
             try
             {
                 rdr.Read();
                 t = TaxaUnitBuilder.Instance.Build(rdr);
                 if (cmd.Connection.State == System.Data.ConnectionState.Open)
                 {
                     cmd.Connection.Close();
                 }
             }
             catch
             { }
             finally
             {
                 cmd.Dispose();
             }
             return(t);
         }
     }
     return(null);
 }
 //Methods for TaxaCommonNameTaxa
 public override IEnumerable <TaxaCommonName> GetCommonNamesByTaxa(TaxaUnit taxa)
 {
     if (taxa != null && this.CanGet())
     {
         NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString);
         cmd.CommandText = Db.SelectCommonName + " WHERE \"Id\" IN (SELECT \"TaxaCommonNameId\" FROM oncor.\"TaxaCommonNamesTaxa\" WHERE \"TaxaUnitId\"=:tuid)";
         cmd.Parameters.AddWithValue("tuid", taxa.Identity.Identity);
         NpgsqlDataReader      rdr         = Db.ExecuteReader(cmd);
         List <TaxaCommonName> commonNames = new List <TaxaCommonName>();
         try
         {
             TaxaCommonName t;
             while (rdr.Read())
             {
                 t = TaxaCommonNameBuilder.Instance.Build(rdr);
                 if (t != null)
                 {
                     commonNames.Add(t);
                 }
             }
             if (cmd.Connection.State == System.Data.ConnectionState.Open)
             {
                 cmd.Connection.Close();
             }
         }
         catch
         { }
         finally
         {
             cmd.Dispose();
         }
         return(commonNames);
     }
     return(null);
 }
 public override bool HasParent(TaxaUnit taxaUnit)
 {
     if (taxaUnit != null && this.CanGet())
     {
         NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString);
         cmd.CommandText = Db.CountTaxaUnit + Db.SelectTaxaUnitById;
         cmd.Parameters.AddWithValue("id", taxaUnit.ParentId.Identity);
         return(Db.Exists(cmd));
     }
     return(false);
 }
        public static void Handle(UserSecurityContext user, string method, HttpContext context, CancellationToken cancel)
        {
            if (context.Request.Method == "POST")
            {
                if (method.Equals("find", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        TaxaCommonNameProviderBase cnprovider = TaxonomyManager.Instance.GetTaxaCommonNameProvider(user);
                        TaxaUnitProviderBase       uprovider  = TaxonomyManager.Instance.GetTaxaUnitProvider(user);
                        JToken token        = JsonUtils.GetDataPayload(context.Request);
                        JArray jcommonNames = null;

                        if (cnprovider != null && uprovider != null && token != null)
                        {
                            CompoundIdentity unitId = JsonUtils.ToId(token["unitid"]);
                            TaxaUnit         unit   = uprovider.Get(unitId);
                            if (unit != null)
                            {
                                IEnumerable <TaxaCommonName> names = cnprovider.GetCommonNamesByTaxa(unit);
                                jcommonNames = Jsonifier.ToJson(names);
                            }

                            if (jcommonNames != null)
                            {
                                RestUtils.Push(context.Response, JsonOpStatus.Ok, jcommonNames.ToString());
                            }
                            else
                            {
                                RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]");
                            }
                            return;
                        }
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                        return;
                    }
                    RestUtils.Push(context.Response, JsonOpStatus.Failed);
                    return;
                }
            }
            context.Response.StatusCode = HttpStatusCodes.Status400BadRequest;
        }
Exemple #5
0
 public static JObject ToJson(TaxaUnit unit)
 {
     if (unit != null)
     {
         JObject o = new JObject();
         o.Add(JsonUtils.Id, JsonUtils.ToJson(unit.Identity));
         o.Add(JsonUtils.Name, unit.Name);
         o.Add(JsonUtils.Description, unit.Description);
         if (unit.ParentId != null)
         {
             o.Add(JsonUtils.ParentId, JsonUtils.ToJson(unit.ParentId));
         }
         o.Add("domainid", JsonUtils.ToJson(unit.TaxaDomainId));
         o.Add("taxonomyid", JsonUtils.ToJson(unit.TaxonomyId));
         o.Add("unittypeid", JsonUtils.ToJson(unit.TaxaUnitTypeId));
         return(o);
     }
     return(null);
 }
        public override bool Update(TaxaUnit taxaUnit)
        {
            if (taxaUnit != null && this.CanUpdate())
            {
                try
                {
                    NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString);
                    cmd.CommandText = Db.UpdateTaxaUnit + Db.SelectById;
                    cmd.Parameters.AddWithValue("name", taxaUnit.Name);
                    if (string.IsNullOrEmpty(taxaUnit.Description))
                    {
                        cmd.Parameters.Add(NpgSqlCommandUtils.GetNullInParam("desc", NpgsqlTypes.NpgsqlDbType.Varchar));
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("desc", taxaUnit.Description);
                    }
                    if (taxaUnit.ParentId == null)
                    {
                        cmd.Parameters.Add(NpgSqlCommandUtils.GetNullInParam("pid", NpgsqlTypes.NpgsqlDbType.Uuid));
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("pid", taxaUnit.ParentId.Identity);
                    }
                    cmd.Parameters.AddWithValue("did", taxaUnit.TaxaDomainId);
                    cmd.Parameters.AddWithValue("tid", taxaUnit.TaxonomyId);
                    cmd.Parameters.AddWithValue("utid", taxaUnit.TaxaUnitTypeId);
                    cmd.Parameters.AddWithValue("id", taxaUnit.Identity.Identity);
                    Db.ExecuteNonQuery(cmd);

                    return(true);
                }
                catch
                { }
            }
            return(false);
        }
 public override bool CanUpdate(TaxaUnit taxaUnit)
 {
     return(this.CanUpdate());            //TODO -- add fine grained security
 }