예제 #1
0
        private static void Get(UserSecurityContext user, HttpContext context, CancellationToken cancel)
        {
            try
            {
                InstrumentProviderBase provider = InstrumentManager.Instance.GetInstrumentProvider(user);
                InstrumentKnownArchetypeProviderBase archProvider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                if (provider != null)
                {
                    IEnumerable <Instrument> instruments = provider.Get();
                    JArray jinstruments = new JArray();
                    foreach (Instrument inst in instruments)
                    {
                        IArchetype arch = archProvider.Get(inst.Identity);
                        if (arch != null)
                        {
                            string archetypeString = archProvider.GetArchetypeType(arch.Identity);
                            switch (archetypeString)
                            {
                            case "SimpleTrapDredge":
                                jinstruments.Add(Jsonifier.ToJson(inst, arch as SimpleTrapDredge));
                                break;

                            case "StandardMeshNet":
                                jinstruments.Add(Jsonifier.ToJson(inst, arch as StandardMeshNet));
                                break;

                            case "StandardPlanktonNet":
                                jinstruments.Add(Jsonifier.ToJson(inst, arch as StandardPlanktonNet));
                                break;

                            case "WingedBagNet":
                                jinstruments.Add(Jsonifier.ToJson(inst, arch as WingedBagNet));
                                break;
                            }
                        }
                        else  //instrument has no archetype
                        {
                            jinstruments.Add(Jsonifier.ToJson(inst));
                        }
                    }

                    if (jinstruments != null)
                    {
                        RestUtils.Push(context.Response, JsonOpStatus.Ok, jinstruments.ToString());
                    }
                    else
                    {
                        RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]");
                    }
                    return;
                }

                RestUtils.Push(context.Response, JsonOpStatus.Failed);
            }
            catch
            {
                RestUtils.Push(context.Response, JsonOpStatus.Failed);
                return;
            }
        }
예제 #2
0
        private static void GetByType(CompoundIdentity typeid, UserSecurityContext user, HttpContext context, CancellationToken cancel)
        {
            try
            {
                InstrumentKnownArchetypeProviderBase provider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                if (provider != null)
                {
                    IEnumerable <CompoundIdentity> archetypes_ids = provider.ArchetypesForInstrumentType(typeid);
                    if (archetypes_ids != null && archetypes_ids.Any())
                    {
                        JArray jarchetypes_ids = JsonUtils.ToJson(archetypes_ids);
                        RestUtils.Push(context.Response, JsonOpStatus.Ok, jarchetypes_ids.ToString());
                    }
                    else
                    {
                        RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]");
                    }
                    return;
                }

                RestUtils.Push(context.Response, JsonOpStatus.Failed);
            }
            catch
            {
                RestUtils.Push(context.Response, JsonOpStatus.Failed);
                return;
            }
        }
예제 #3
0
        private static void GetForAllTypes(UserSecurityContext user, HttpContext context, CancellationToken cancel)
        {
            try
            {
                InstrumentKnownArchetypeProviderBase provider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                if (provider != null)
                {
                    IEnumerable <Tuple <CompoundIdentity, CompoundIdentity> > archetypes = provider.GetInstrumentTypeKnownArchetypes();
                    JArray jarchetypes = Jsonifier.ToJson(archetypes);

                    if (jarchetypes != null)
                    {
                        RestUtils.Push(context.Response, JsonOpStatus.Ok, jarchetypes.ToString());
                    }
                    else
                    {
                        RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]");
                    }
                    return;
                }

                RestUtils.Push(context.Response, JsonOpStatus.Failed);
            }
            catch
            {
                RestUtils.Push(context.Response, JsonOpStatus.Failed);
                return;
            }
        }
예제 #4
0
        private static void RemoveInstance(CompoundIdentity cid, UserSecurityContext user, HttpContext context, CancellationToken cancel)
        {
            try
            {
                InstrumentKnownArchetypeProviderBase archProvider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                if (cid != null && archProvider != null)
                {
                    bool result = archProvider.Delete(cid);

                    if (result == true)
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok));
                    }
                    else
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                    return;
                }

                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
            }
            catch
            {
                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                return;
            }
        }
예제 #5
0
 public override bool Delete(CompoundIdentity id)
 {
     if (!id.IsNullOrEmpty() && this.CanDelete())
     {
         try
         {
             InstrumentKnownArchetypeProviderBase archprov = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(this.Context);
             NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString);
             cmd.CommandText = Db.DeleteInstrument;
             cmd.Parameters.AddWithValue("id", id.Identity);
             Db.ExecuteNonQuery(cmd);
             archprov.Delete(id);
             return(true);
         }
         catch
         { }
     }
     return(false);
 }
예제 #6
0
        public static void Handle(UserSecurityContext user, string method, HttpContext context, CancellationToken cancel)
        {
            if (context.Request.Method == "POST")
            {
                if (method.Equals("all", StringComparison.OrdinalIgnoreCase))
                {
                    Get(user, context, cancel);
                    return;
                }
                else if (method.Equals("in", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        HashSet <CompoundIdentity> ids = JsonUtils.ToIds(JsonUtils.GetDataPayload(context.Request));
                        if (ids != null)
                        {
                            GetIds(ids, user, context, cancel);
                            return;
                        }
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                }
                else if (method.Equals("create", StringComparison.OrdinalIgnoreCase))
                {
                    CompoundIdentity       owner    = null;
                    CompoundIdentity       type     = null;
                    string                 name     = null;
                    string                 desc     = null;
                    string                 serial   = null;
                    CompoundIdentity       manf     = null;
                    InstrumentProviderBase provider = null;
                    InstrumentKnownArchetypeProviderBase archProvider = null;
                    JToken token = null;

                    try
                    {
                        //payload and provider
                        token        = JsonUtils.GetDataPayload(context.Request);
                        provider     = InstrumentManager.Instance.GetInstrumentProvider(user);
                        archProvider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                        if (provider != null && token != null)
                        {
                            //required inputs
                            name  = token["name"].ToString();
                            owner = JsonUtils.ToId(token["orgid"]);
                            type  = JsonUtils.ToId(token["typeid"]);
                            if (owner != null && type != null && !string.IsNullOrEmpty(name))
                            {
                                //optionals
                                desc   = token["desc"] != null ? token["desc"].ToString() : null;
                                serial = token["serial"] != null ? token["serial"].ToString() : null;
                                manf   = JsonUtils.ToId(token["manf"]);

                                //create
                                Instrument instrument = null;
                                instrument = provider.Create(owner, name, type, desc, serial, manf);
                                if (instrument != null)
                                {
                                    JObject jinstrument = null;

                                    //call update to persist architype info
                                    //archid and archdata are expected to be valid and consistent; non-matching properties are ignored
                                    if (token.SelectToken("archid") != null)                                     //JSON property was present
                                    {
                                        CompoundIdentity archid          = JsonUtils.ToId(token["archid"]);
                                        string           archetypeString = archProvider.GetArchetypeType(archid);
                                        JToken           archToken       = token["archdata"];
                                        switch (archetypeString)
                                        {
                                        case "SimpleTrapDredge":
                                            SimpleTrapDredge std = archProvider.AddSimpleTrapDredge(instrument.Identity);
                                            if (archToken["openarea"] != null)
                                            {
                                                std.OpenArea = double.Parse(archToken["openarea"].ToString());
                                            }
                                            archProvider.Update(std);
                                            jinstrument = Jsonifier.ToJson(instrument, std);
                                            break;

                                        case "StandardMeshNet":
                                            StandardMeshNet smn = archProvider.AddStandardMeshNet(instrument.Identity);
                                            if (archToken["length"] != null)
                                            {
                                                smn.Length = double.Parse(archToken["length"].ToString());
                                            }
                                            if (archToken["depth"] != null)
                                            {
                                                smn.Depth = double.Parse(archToken["depth"].ToString());
                                            }
                                            if (archToken["meshsize"] != null)
                                            {
                                                smn.MeshSize = double.Parse(archToken["meshsize"].ToString());
                                            }
                                            archProvider.Update(smn);
                                            jinstrument = Jsonifier.ToJson(instrument, smn);
                                            break;

                                        case "StandardPlanktonNet":
                                            StandardPlanktonNet spn = archProvider.AddStandardPlanktonNet(instrument.Identity);
                                            if (archToken["openarea"] != null)
                                            {
                                                spn.OpenArea = double.Parse(archToken["openarea"].ToString());
                                            }
                                            if (archToken["meshsize"] != null)
                                            {
                                                spn.MeshSize = double.Parse(archToken["meshsize"].ToString());
                                            }
                                            if (archToken["codsize"] != null)
                                            {
                                                spn.MeshSize = double.Parse(archToken["codsize"].ToString());
                                            }
                                            archProvider.Update(spn);
                                            jinstrument = Jsonifier.ToJson(instrument, spn);
                                            break;

                                        case "WingedBagNet":
                                            WingedBagNet wbn = archProvider.AddWingedBagNet(instrument.Identity);
                                            if (archToken["length"] != null)
                                            {
                                                wbn.Length = double.Parse(archToken["length"].ToString());
                                            }
                                            if (archToken["depth"] != null)
                                            {
                                                wbn.Depth = double.Parse(archToken["depth"].ToString());
                                            }
                                            if (archToken["meshsizewings"] != null)
                                            {
                                                wbn.Length = double.Parse(archToken["meshsizewings"].ToString());
                                            }
                                            if (archToken["meshsizebag"] != null)
                                            {
                                                wbn.Depth = double.Parse(archToken["meshsizebag"].ToString());
                                            }
                                            archProvider.Update(wbn);
                                            jinstrument = Jsonifier.ToJson(instrument, wbn);
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        jinstrument = Jsonifier.ToJson(instrument);
                                    }

                                    if (jinstrument != null)
                                    {
                                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, jinstrument.ToString()));
                                    }
                                    else
                                    {
                                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                                    }
                                    return;
                                }
                            }
                        }

                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                        return;
                    }
                }
                else if (method.Equals("update", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        //provider and token
                        JToken token = JsonUtils.GetDataPayload(context.Request);
                        InstrumentProviderBase provider = InstrumentManager.Instance.GetInstrumentProvider(user);
                        InstrumentKnownArchetypeProviderBase archProvider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                        if (provider != null && token != null)
                        {
                            //GUID must be provided
                            CompoundIdentity cid = JsonUtils.ToId(token["id"]);  //instrument ID

                            //fetch stored object
                            bool       dirty      = false;
                            bool       result     = true;
                            Instrument instrument = provider.Get(cid);
                            if (instrument != null)
                            {
                                //update archetype instance if necessary
                                //assumes inbound archtype is self-consistent and consistent with instrument type (validation should occur in provider and by requester)
                                if (token.SelectToken("archid") != null)
                                {
                                    //clear previous
                                    archProvider.Delete(instrument.Identity);

                                    //add new, if defined
                                    if (JsonUtils.ToId(token["archid"]) != null)
                                    {
                                        CompoundIdentity arch_id         = JsonUtils.ToId(token["archid"]);
                                        JToken           archToken       = token["archdata"];
                                        string           archetypeString = archProvider.GetArchetypeType(arch_id);
                                        switch (archetypeString)
                                        {
                                        case "SimpleTrapDredge":
                                            SimpleTrapDredge std = archProvider.AddSimpleTrapDredge(instrument.Identity);
                                            if (archToken["openarea"] != null)
                                            {
                                                std.OpenArea = double.Parse(archToken["openarea"].ToString());
                                            }
                                            result &= archProvider.Update(std);
                                            break;

                                        case "StandardMeshNet":
                                            StandardMeshNet smn = archProvider.AddStandardMeshNet(instrument.Identity);
                                            if (archToken["length"] != null)
                                            {
                                                smn.Length = double.Parse(archToken["length"].ToString());
                                            }
                                            if (archToken["depth"] != null)
                                            {
                                                smn.Depth = double.Parse(archToken["depth"].ToString());
                                            }
                                            if (archToken["meshsize"] != null)
                                            {
                                                smn.MeshSize = double.Parse(archToken["meshsize"].ToString());
                                            }
                                            result &= archProvider.Update(smn);
                                            break;

                                        case "StandardPlanktonNet":
                                            StandardPlanktonNet spn = archProvider.AddStandardPlanktonNet(instrument.Identity);
                                            if (archToken["openarea"] != null)
                                            {
                                                spn.OpenArea = double.Parse(archToken["openarea"].ToString());
                                            }
                                            if (archToken["meshsize"] != null)
                                            {
                                                spn.MeshSize = double.Parse(archToken["meshsize"].ToString());
                                            }
                                            if (archToken["codsize"] != null)
                                            {
                                                spn.CodSize = double.Parse(archToken["codsize"].ToString());
                                            }
                                            result &= archProvider.Update(spn);
                                            break;

                                        case "WingedBagNet":
                                            WingedBagNet wbn = archProvider.AddWingedBagNet(instrument.Identity);
                                            if (archToken["length"] != null)
                                            {
                                                wbn.Length = double.Parse(archToken["length"].ToString());
                                            }
                                            if (archToken["depth"] != null)
                                            {
                                                wbn.Depth = double.Parse(archToken["depth"].ToString());
                                            }
                                            if (archToken["meshsizewings"] != null)
                                            {
                                                wbn.MeshSizeWings = double.Parse(archToken["meshsizewings"].ToString());
                                            }
                                            if (archToken["meshsizebag"] != null)
                                            {
                                                wbn.MeshSizeBag = double.Parse(archToken["meshsizebag"].ToString());
                                            }
                                            result &= archProvider.Update(wbn);
                                            break;
                                        }
                                    }
                                }

                                if (result)
                                {
                                    //continue to check instrument
                                    //## REQUIRED ##
                                    //name
                                    if (token.SelectToken("name") != null)
                                    {
                                        string name = token["name"].ToString();
                                        if (!string.IsNullOrEmpty(name))
                                        {
                                            instrument.Name = name;
                                            dirty           = true;
                                        }
                                        else
                                        {
                                            RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //name is required and not nullable
                                            return;
                                        }
                                    }

                                    //owning org
                                    if (token.SelectToken("orgid") != null)
                                    {
                                        CompoundIdentity org_cid = JsonUtils.ToId(token["orgid"]);
                                        if (org_cid != null)
                                        {
                                            instrument.OwningOrganizationIdentity = org_cid;
                                            dirty = true;
                                        }
                                        else
                                        {
                                            RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //owning org is required and not nullable
                                            return;
                                        }
                                    }

                                    //type
                                    if (token.SelectToken("typeid") != null)

                                    {
                                        CompoundIdentity type = JsonUtils.ToId(token["typeid"]);
                                        if (type != null)
                                        {
                                            instrument.InstrumentTypeIdentity = type;
                                            dirty = true;
                                        }
                                        else
                                        {
                                            RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //intrument type is required and not nullable
                                            return;
                                        }
                                    }

                                    //## OPTIONALS ##

                                    //description
                                    if (token.SelectToken("desc") != null)
                                    {
                                        string desc = (token["desc"] != null) ? token["desc"].ToString() : null;
                                        instrument.Description = desc;
                                        dirty = true;
                                    }

                                    //serial number
                                    if (token.SelectToken("serial") != null)
                                    {
                                        string serial = token["serial"] != null ? token["serial"].ToString() : null;
                                        instrument.SerialNumber = serial;
                                        dirty = true;
                                    }

                                    //manufacturer
                                    if (token.SelectToken("manf") != null)

                                    {
                                        CompoundIdentity manf = JsonUtils.ToId(token["manf"]);
                                        instrument.ManufacturerId = manf;
                                        dirty = true;
                                    }

                                    //update instrument if necessary
                                    if (dirty)
                                    {
                                        //update
                                        result &= provider.Update(instrument);
                                        if (result == true)
                                        {
                                            RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok));
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        //return ok - no values were modified
                                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok));
                                        return;
                                    }
                                }
                            }
                        }

                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));  //default
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                        return;
                    }
                }
                else if (method.Equals("delete", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        JToken t = JsonUtils.GetDataPayload(context.Request);
                        HashSet <CompoundIdentity>           cids         = JsonUtils.ToIds(t);
                        InstrumentProviderBase               provider     = InstrumentManager.Instance.GetInstrumentProvider(user);
                        InstrumentKnownArchetypeProviderBase archProvider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                        if (provider != null && cids != null)
                        {
                            bool result = true;
                            foreach (CompoundIdentity cid in cids)
                            {
                                result &= provider.Delete(cid);
                            }

                            if (result == true)
                            {
                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok));
                            }
                            else
                            {
                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                            }
                            return;
                        }

                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                        return;
                    }
                }
            }

            context.Response.StatusCode = HttpStatusCodes.Status400BadRequest;
        }
예제 #7
0
        private static void AddInstance(CompoundIdentity cid, CompoundIdentity archid, JToken archToken, UserSecurityContext user, HttpContext context, CancellationToken cancel)
        {
            try
            {
                InstrumentKnownArchetypeProviderBase archProvider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                if (archProvider != null)
                {
                    bool   result          = true;
                    string archetypeString = archProvider.GetArchetypeType(archid);
                    switch (archetypeString)
                    {
                    case "SimpleTrapDredge":
                        SimpleTrapDredge std = archProvider.AddSimpleTrapDredge(cid);
                        if (archToken["openarea"] != null)
                        {
                            std.OpenArea = double.Parse(archToken["openarea"].ToString());
                        }
                        result &= archProvider.Update(std);
                        break;

                    case "StandardMeshNet":
                        StandardMeshNet smn = archProvider.AddStandardMeshNet(cid);
                        if (archToken["length"] != null)
                        {
                            smn.Length = double.Parse(archToken["length"].ToString());
                        }
                        if (archToken["depth"] != null)
                        {
                            smn.Depth = double.Parse(archToken["depth"].ToString());
                        }
                        if (archToken["meshsize"] != null)
                        {
                            smn.MeshSize = double.Parse(archToken["meshsize"].ToString());
                        }
                        result &= archProvider.Update(smn);
                        break;

                    case "StandardPlanktonNet":
                        StandardPlanktonNet spn = archProvider.AddStandardPlanktonNet(cid);
                        if (archToken["openarea"] != null)
                        {
                            spn.OpenArea = double.Parse(archToken["openarea"].ToString());
                        }
                        if (archToken["meshsize"] != null)
                        {
                            spn.MeshSize = double.Parse(archToken["meshsize"].ToString());
                        }
                        if (archToken["codsize"] != null)
                        {
                            spn.CodSize = double.Parse(archToken["codsize"].ToString());
                        }
                        result &= archProvider.Update(spn);
                        break;

                    case "WingedBagNet":
                        WingedBagNet wbn = archProvider.AddWingedBagNet(cid);
                        if (archToken["length"] != null)
                        {
                            wbn.Length = double.Parse(archToken["length"].ToString());
                        }
                        if (archToken["depth"] != null)
                        {
                            wbn.Depth = double.Parse(archToken["depth"].ToString());
                        }
                        if (archToken["meshsizewings"] != null)
                        {
                            wbn.MeshSizeWings = double.Parse(archToken["meshsizewings"].ToString());
                        }
                        if (archToken["meshsizebag"] != null)
                        {
                            wbn.MeshSizeBag = double.Parse(archToken["meshsizebag"].ToString());
                        }
                        result &= archProvider.Update(wbn);
                        break;
                    }

                    if (result == true)
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok));
                        return;
                    }
                }

                RestUtils.Push(context.Response, JsonOpStatus.Failed);
            }
            catch
            {
                RestUtils.Push(context.Response, JsonOpStatus.Failed);
                return;
            }
        }
예제 #8
0
        public static void Handle(UserSecurityContext user, string method, HttpContext context, CancellationToken cancel)
        {
            if (context.Request.Method == "POST")
            {
                if (method.Equals("all", StringComparison.OrdinalIgnoreCase))
                {
                    Get(user, context, cancel);
                    return;
                }
                else if (method.Equals("in", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        HashSet <CompoundIdentity> ids = JsonUtils.ToIds(JsonUtils.GetDataPayload(context.Request));
                        if (ids != null)
                        {
                            GetIds(ids, user, context, cancel);
                            return;
                        }
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                }
                else if (method.Equals("create", StringComparison.OrdinalIgnoreCase))
                {
                    string                     name      = null;
                    string                     desc      = null;
                    CompoundIdentity           family_id = null;
                    CompoundIdentity           parent_id = null;
                    HashSet <CompoundIdentity> archids   = null;
                    InstrumentTypeProviderBase provider  = null;
                    JToken                     token     = null;

                    try
                    {
                        //payload and provider
                        token    = JsonUtils.GetDataPayload(context.Request);
                        provider = InstrumentManager.Instance.GetInstrumentTypeProvider(user);
                        if (provider != null && token != null)
                        {
                            //required inputs
                            name      = token["name"].ToString();
                            family_id = JsonUtils.ToId(token["familyid"]);
                            if (family_id != null && !string.IsNullOrEmpty(name))
                            {
                                //optionals
                                desc      = token["desc"] != null ? token["desc"].ToString() : null;
                                parent_id = token["parentid"] != null?JsonUtils.ToId(token["parentid"]) : null;

                                archids = token["archid"] != null?JsonUtils.ToIds(token["archid"]) : null;

                                //create
                                InstrumentType instrumentType = null;
                                instrumentType = provider.Create(name, family_id, desc, parent_id);
                                if (instrumentType != null)
                                {
                                    bool result = true;
                                    //add archetype mappings if required
                                    InstrumentKnownArchetypeProviderBase apb = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                                    if (token.SelectToken("archid") != null && apb != null)  //user intended to set this value
                                    {
                                        foreach (CompoundIdentity c in archids)
                                        {
                                            result &= apb.AddInstrumentTypeArchetype(c, instrumentType.Identity);
                                        }
                                    }

                                    if (result == true)
                                    {
                                        JObject jinstrumentType = Jsonifier.ToJson(instrumentType);
                                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, jinstrumentType.ToString()));
                                        return;
                                    }
                                }
                            }
                        }

                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                        return;
                    }
                }
                else if (method.Equals("update", StringComparison.OrdinalIgnoreCase))
                {
                    string                     name;
                    string                     desc           = null;
                    JToken                     token          = null;
                    InstrumentType             instrumentType = null;
                    CompoundIdentity           cid            = null;
                    CompoundIdentity           family_cid     = null;
                    CompoundIdentity           parent_cid     = null;
                    HashSet <CompoundIdentity> arch_cids      = null;
                    InstrumentTypeProviderBase provider       = null;

                    try
                    {
                        token    = JsonUtils.GetDataPayload(context.Request);
                        provider = InstrumentManager.Instance.GetInstrumentTypeProvider(user);
                        if (provider != null && token != null)
                        {
                            //GUID must be provided
                            cid = JsonUtils.ToId(token["id"]);

                            //fetch stored object
                            bool dirty  = false;
                            bool result = true;
                            instrumentType = provider.Get(cid);
                            if (instrumentType != null)
                            {
                                //archetype mappings - convenience method, so don't need to set dirty flag if nothing else has changed
                                if (token.SelectToken("archid") != null)
                                {
                                    InstrumentKnownArchetypeProviderBase apb = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                                    arch_cids = token["archid"] != null?JsonUtils.ToIds(token["archid"]) : null;

                                    //reset mappings
                                    result &= apb.RemoveInstrumentType(instrumentType.Identity);

                                    //add new mappings
                                    if (arch_cids != null)
                                    {
                                        foreach (CompoundIdentity c in arch_cids)
                                        {
                                            result &= apb.AddInstrumentTypeArchetype(c, instrumentType.Identity);
                                        }
                                    }
                                }

                                if (result)
                                {
                                    //## REQUIRED ##

                                    //name
                                    if (token.SelectToken("name") != null)
                                    {
                                        name = token["name"].ToString();
                                        if (!string.IsNullOrEmpty(name))
                                        {
                                            instrumentType.Name = name;
                                            dirty = true;
                                        }
                                        else
                                        {
                                            RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //name is required and not nullable
                                            return;
                                        }
                                    }

                                    //family
                                    if (token.SelectToken("familyid") != null)
                                    {
                                        family_cid = JsonUtils.ToId(token["familyid"]);
                                        if (family_cid != null)
                                        {
                                            instrumentType.FamilyId = family_cid;
                                            dirty = true;
                                        }
                                        else
                                        {
                                            RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //family id is required and not nullable
                                            return;
                                        }
                                    }

                                    //## OPTIONALS ##

                                    //description
                                    if (token.SelectToken("desc") != null)
                                    {
                                        desc = (token["desc"] != null) ? token["desc"].ToString() : null;
                                        instrumentType.Description = desc;
                                        dirty = true;
                                    }

                                    //parent type
                                    if (token.SelectToken("parentid") != null)
                                    {
                                        parent_cid = JsonUtils.ToId(token["parentid"]);
                                        instrumentType.ParentId = parent_cid;  //could be null
                                        dirty = true;
                                    }

                                    if (dirty)
                                    {
                                        //update
                                        result &= provider.Update(instrumentType);
                                        if (result == true)
                                        {
                                            RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok));
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        //return ok - no values were modified
                                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok));
                                        return;
                                    }
                                }
                            }
                        }

                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                        return;
                    }
                }
                else if (method.Equals("delete", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        JToken t = JsonUtils.GetDataPayload(context.Request);
                        HashSet <CompoundIdentity>           cids         = JsonUtils.ToIds(t);
                        InstrumentTypeProviderBase           provider     = InstrumentManager.Instance.GetInstrumentTypeProvider(user);
                        InstrumentKnownArchetypeProviderBase archProvider = InstrumentManager.Instance.GetInstrumentKnownArchetypeProvider(user);
                        if (provider != null && cids != null && archProvider != null)
                        {
                            bool result = true;
                            foreach (CompoundIdentity cid in cids)
                            {
                                InstrumentType itype = provider.Get(cid);

                                result &= provider.Delete(cid);
                                result &= archProvider.RemoveInstrumentType(itype.Identity);  //delete related archetype mappings
                            }

                            if (result == true)
                            {
                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok));
                            }
                            else
                            {
                                RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                            }
                            return;
                        }

                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                    }
                    catch
                    {
                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed));
                        return;
                    }
                }
            }

            context.Response.StatusCode = HttpStatusCodes.Status400BadRequest;
        }