예제 #1
0
        /// <summary>
        /// Creates a .meth file from .xml
        /// </summary>
        /// <param name="xmlFilePath">The xml file path to create the method from</param>
        /// <param name="outputMethod">The file path of the to be created method</param>
        /// <param name="model">The instrument model (only TSQ)</param>
        /// <param name="version">The instrument version</param>
        public static void CreateMethod(string xmlFilePath, string outputMethod = "", string model = "", string version = "")
        {
            // Handle relative/absolute paths
            xmlFilePath = Path.GetFullPath(xmlFilePath);

            if (string.IsNullOrEmpty(outputMethod))
            {
                outputMethod = Path.ChangeExtension(xmlFilePath, ".meth");
            }
            outputMethod = Path.GetFullPath(outputMethod);

            // Get the type of instrument based on its name
            InstrumentFamily instrumentFamily = GetInstrumentFamilyFromModel(model);

            if (instrumentFamily != InstrumentFamily.TSQ)
            {
                throw new ArgumentException("You can only create methods from TSQ xml files");
            }

            using (IMethodXMLContext mxc = CreateContext(model, version))
                using (IMethodXML xmlMeth = mxc.Create())
                {
                    // Loads the method from the xml
                    xmlMeth.LoadMethodFromXMLFile(xmlFilePath);

                    // Save the in memory method to the output file
                    xmlMeth.SaveAs(outputMethod);
                }
        }
예제 #2
0
        public static void ExportMethod(string methodFile, string outputFile, string instrumentModel, string version)
        {
            methodFile = Path.GetFullPath(methodFile);

            if (string.IsNullOrEmpty(outputFile))
            {
                outputFile = Path.ChangeExtension(methodFile, ".xml");
            }
            outputFile = Path.GetFullPath(outputFile);


            // Get the type of instrument based on its name
            InstrumentFamily instrumentFamily = GetInstrumentFamilyFromModel(instrumentModel);

            if (instrumentFamily != InstrumentFamily.OrbitrapFusion)
            {
                throw new ArgumentException("You can only export Fusion/Tribrid method files");
            }

            string xml = "";

            using (IMethodXMLContext mxc = CreateContext(instrumentModel, version))
                using (IMethodXML xmlMeth = mxc.Create())
                    using (StreamWriter writer = new StreamWriter(outputFile))
                    {
                        xmlMeth.Open(methodFile);
                        xml = xmlMeth.ExportMethodToXML();
                        writer.Write(xml);
                    }
        }
예제 #3
0
 public override bool Update(InstrumentFamily org)
 {
     if (org != null && this.CanUpdate(org))
     {
         NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString);
         cmd.CommandText = Db.UpdateInstrumentFamily + Db.SelectById;
         cmd.Parameters.AddWithValue("name", org.Name);
         if (!string.IsNullOrEmpty(org.Description))
         {
             cmd.Parameters.AddWithValue("desc", org.Description);
         }
         else
         {
             cmd.Parameters.Add(NpgSqlCommandUtils.GetNullInParam("desc", NpgsqlTypes.NpgsqlDbType.Varchar));
         }
         if (org.ParentId != null)
         {
             cmd.Parameters.AddWithValue("pid", org.ParentId.Identity);
         }
         else
         {
             cmd.Parameters.Add(NpgSqlCommandUtils.GetNullInParam("pid", NpgsqlTypes.NpgsqlDbType.Uuid));
         }
         cmd.Parameters.AddWithValue("id", org.Identity.Identity);
         Db.ExecuteNonQuery(cmd);
         return(true);
     }
     return(false);
 }
 public Instrument(string newName, InstrumentFamily newFamily, AudioClip newSound, Sprite newImage)
 {
     Name   = newName;
     Family = newFamily;
     Sound  = newSound;
     Image  = newImage;
 }
예제 #5
0
 public override InstrumentFamily Get(CompoundIdentity id)
 {
     if (!id.IsNullOrEmpty() && this.CanGet())
     {
         NpgsqlCommand cmd = Db.GetCmd(Db.ConnectionString);
         cmd.CommandText = Db.SelectInstrumentFamily + Db.SelectById;
         cmd.Parameters.AddWithValue("id", id.Identity);
         NpgsqlDataReader rdr = Db.ExecuteReader(cmd);
         InstrumentFamily f   = null;
         if (rdr != null)
         {
             try
             {
                 rdr.Read();
                 f = InstrumentFamilyBuilder.Instance.Build(rdr);
                 if (cmd.Connection.State == System.Data.ConnectionState.Open)
                 {
                     cmd.Connection.Close();
                 }
             }
             catch
             { }
             finally
             {
                 cmd.Dispose();
             }
         }
         return(f);
     }
     return(null);
 }
예제 #6
0
 public Track(Composition composition, string fullname, string shortname, InstrumentFamily family, InstrumentType type, InstrumentFeature feature, string color)
 {
     this.Composition = composition;
     this.FullName = fullname;
     this.ShortName = shortname;
     this.Instrument = new Instrument(family, type, feature);
     this.Family = family;
     this.Type = type;
     this.Feature = feature;
     this.Color = color;
     this.SaveToDB();
 }
예제 #7
0
 public static JObject ToJson(InstrumentFamily instrumentFam)
 {
     if (instrumentFam != null)
     {
         JObject o = new JObject();
         o.Add(JsonUtils.Id, JsonUtils.ToJson(instrumentFam.Identity));
         o.Add(JsonUtils.Name, instrumentFam.Name);
         o.Add(JsonUtils.Description, instrumentFam.Description);
         o.Add(JsonUtils.ParentId, JsonUtils.ToJson(instrumentFam.ParentId));
         return(o);
     }
     return(null);
 }
예제 #8
0
        /// <summary>
        /// Validates a .xml file for errors in its schema
        /// </summary>
        /// <param name="xmlFilePath">The xml file to validate</param>
        /// <param name="verion">The version of xsd to validate against</param>
        /// <returns>A list of errors it detected, or an empty list if no errors are found</returns>
        public static List <string> ValidateXML(string xmlFilePath, string version = "2.0")
        {
            // http://stackoverflow.com/questions/751511/validating-an-xml-against-referenced-xsd-in-c-sharp
            var validationErrors = new List <string>();

            if (string.IsNullOrEmpty(version))
            {
                version = "3.4"; // default to 2.0
            }

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.ValidationType          = ValidationType.Schema;
            settings.ValidationFlags        |= XmlSchemaValidationFlags.ProcessIdentityConstraints;
            settings.ValidationEventHandler += (o, e) => { validationErrors.Add(string.Format("[Line {0}]: {1}", e.Exception.LineNumber, e.Message)); };

            InstrumentFamily family = GetInstrumentFamilyFromXml(xmlFilePath);

            string xsdFilePath;

            switch (family)
            {
            case InstrumentFamily.OrbitrapFusion:
                xsdFilePath = @"XSDs\Calcium\{0}\MethodModifications.xsd";
                break;

            case InstrumentFamily.TSQ:
                xsdFilePath = @"XSDs\Hyperion\{0}\HyperionMethod.xsd";
                break;

            case InstrumentFamily.Exploris:
                xsdFilePath = @"XSDs\Exploris\{0}\MethodModifications.xsd";
                break;

            default:
                throw new ArgumentException("Cannot tell how to validate the xml file: " + xmlFilePath);
            }

            xsdFilePath = string.Format(xsdFilePath, version);

            settings.Schemas.Add(null, XmlReader.Create(xsdFilePath));

            XmlReader reader = XmlReader.Create(xmlFilePath, settings);

            while (reader.Read())
            {
                ;
            }

            return(validationErrors);
        }
예제 #9
0
 public Track(Composition composition, string fullname, string shortname, InstrumentFamily family, InstrumentType type, InstrumentFeature feature, string color)
 {
     this.Id = Guid.NewGuid();
     this.Composition = composition;
     this.FullName = fullname;
     this.ShortName = shortname;
     this.Family = family.ToString();
     this.Type = type.ToString();
     this.Feature = feature.ToString();
     this.Instrument = new Instrument(family, type, feature);
     this.Color = color;
     this.Notes = new List<Note>();
     this.Save();
 }
    public Instrument GetRandomInstrumentByFamily(InstrumentFamily family)
    {
        List <Instrument> familyList = new List <Instrument>();

        // Gather all instruments part of same family
        foreach (var item in instruments)
        {
            if (item.Value.Family == family)
            {
                familyList.Add(item.Value);
            }
        }
        // Pick a random instrument to return
        return(familyList[UnityEngine.Random.Range(0, familyList.Count)]);
    }
예제 #11
0
 public void CreateTrack(Composition SelectedComposition, string FullName, string ShortName, InstrumentFamily Family, InstrumentType Type, InstrumentFeature Feature, string Color)
 {
     Track Track = new Track(SelectedComposition, FullName, ShortName, Family, Type, Feature, Color);
 }
예제 #12
0
 public void EditTrack(Track SelectedTrack, Composition NewComposition, string NewFullName, string NewShortName, InstrumentFamily NewFamily, InstrumentType NewType, InstrumentFeature NewFeature, string NewColor)
 {
     SelectedTrack.Update(
         new Track()
 {
     Composition = NewComposition,
     FullName = NewFullName,
     ShortName = NewShortName,
     Family = NewFamily.ToString(),
     Type = NewType.ToString(),
     Feature = NewFeature.ToString(),
     Instrument = new Instrument(NewFamily, NewType, NewFeature),
     Color = NewColor
 });
 }
예제 #13
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;
                    InstrumentFamilyProviderBase provider = null;
                    JToken token = null;

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

                                //create
                                InstrumentFamily instrumentFamily = null;
                                instrumentFamily = provider.Create(name, desc, parent_id);
                                if (instrumentFamily != null)
                                {
                                    JObject jinstrumentFamily = Jsonifier.ToJson(instrumentFamily);
                                    if (jinstrumentFamily != null)
                                    {
                                        RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, jinstrumentFamily.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))
                {
                    string                       name;
                    string                       desc             = null;
                    JToken                       token            = null;
                    InstrumentFamily             instrumentFamily = null;
                    CompoundIdentity             cid        = null;
                    CompoundIdentity             parent_cid = null;
                    InstrumentFamilyProviderBase provider   = null;

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

                            //fetch stored object
                            bool dirty = false;
                            instrumentFamily = provider.Get(cid);
                            if (instrumentFamily != null)
                            {
                                //## REQUIRED ##

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

                                //## OPTIONALS ##

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

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

                                if (dirty)
                                {
                                    //update
                                    bool result = provider.Update(instrumentFamily);
                                    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);
                        InstrumentFamilyProviderBase provider = InstrumentManager.Instance.GetInstrumentFamilyProvider(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;
        }
 public Instrument(InstrumentFamily family, InstrumentType type, InstrumentFeature feature)
 {
     this.Family  = family;
     this.Type    = type;
     this.Feature = feature;
 }
예제 #15
0
 public Instrument(InstrumentFamily family, InstrumentType type, InstrumentFeature feature)
 {
     this.Family = family;
     this.Type = type;
     this.Feature = feature;
 }
예제 #16
0
        /// <summary>
        /// Applies the modification XML file to the method template, to produce a modified method file
        /// </summary>
        /// <param name="methodTemplate">The method to base off of</param>
        /// <param name="methodModXML">The modifications to be applied to the template method</param>
        /// <param name="outputMethod">The file path for the generated method</param>
        /// <param name="model">The instrument model</param>
        /// <param name="version">The instrument version</param>
        /// <param name="enableValidation">Enable automatic validation on saving</param>
        public static void ModifyMethod(string methodTemplate, string methodModXML, string outputMethod = "", string model = "", string version = "", bool enableValidation = true)
        {
            if (string.IsNullOrEmpty(methodTemplate))
            {
                throw new ArgumentException("A method file path must be specified", "Method Template");
            }

            if (string.IsNullOrEmpty(methodModXML))
            {
                throw new ArgumentException("A method modification path must be specified", "Method Modification");
            }

            // Handle relative/absolute paths
            methodTemplate = Path.GetFullPath(methodTemplate);
            methodModXML   = Path.GetFullPath(methodModXML);

            // These files are required
            if (!File.Exists(methodTemplate))
            {
                throw new IOException("File Not Found: " + methodTemplate);
            }

            if (!File.Exists(methodModXML))
            {
                throw new IOException("File Not Found: " + methodModXML);
            }

            // Create output file path if not specified
            if (string.IsNullOrEmpty(outputMethod))
            {
                outputMethod = methodTemplate.Replace(".meth", "_modified.meth");
            }
            outputMethod = Path.GetFullPath(outputMethod);

            // Create output directory if doesn't exist
            if (!Directory.Exists(Path.GetDirectoryName(outputMethod)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outputMethod));
            }

            // If the instrument model is not specified, get the default installed one. (might not be the best)
            if (string.IsNullOrEmpty(model))
            {
                model = MethodXMLFactory.GetInstalledServerModel();

                if (string.IsNullOrEmpty(model))
                {
                    var instruments = MethodXMLFactory.GetInstalledInstrumentModels();

                    if (instruments.Count > 1)
                    {
                        throw new Exception(string.Format("Unable to find default installed instrument, you have {0} instruments registered", instruments.Count));
                    }
                    else if (instruments.Count == 0)
                    {
                        throw new Exception("Unable to find any installed instruments!");
                    }
                    else
                    {
                        model = instruments[0];
                    }
                }
            }

            // Get the type of instrument based on its name
            InstrumentFamily instrumentFamily = GetInstrumentFamilyFromModel(model);

            // Get the type of instrument modification based on the xml file
            InstrumentFamily xmlInstrumentFamily = GetInstrumentFamilyFromXml(methodModXML);

            // These two need to be equivalent to correctly apply the modifications
            if (instrumentFamily != xmlInstrumentFamily)
            {
                throw new ArgumentException(string.Format("The specified xml ({0}) is not compatible with the instrument model ({1}, {2})", xmlInstrumentFamily, instrumentFamily, model));
            }

            using (IMethodXMLContext mxc = CreateContext(model, version))
                using (IMethodXML xmlMeth = mxc.Create())
                {
                    // Open the template method
                    xmlMeth.Open(methodTemplate);

                    // Set the validation flag
                    xmlMeth.EnableValidation(enableValidation);

                    // Call the correct modification method based on the instrument type
                    switch (instrumentFamily)
                    {
                    case InstrumentFamily.OrbitrapFusion:
                        xmlMeth.ApplyMethodModificationsFromXMLFile(methodModXML);
                        break;

                    case InstrumentFamily.TSQ:
                        xmlMeth.ImportMassListFromXMLFile(methodModXML);
                        break;

                    default:
                        throw new ArgumentException("Unsupported instrument model:" + model);
                    }

                    // Save the in memory method to the output file
                    xmlMeth.SaveAs(outputMethod);
                }
        }
 public Instrument(string newName, InstrumentFamily newFamily)
 {
     Name   = newName;
     Family = newFamily;
 }
예제 #18
0
 public override bool CanUpdate(InstrumentFamily org)
 {
     return(this.CanUpdate());            //TODO -- add fine grained security
 }
 public Instrument(string newName, InstrumentFamily newFamily, AudioClip newSound)
 {
     Name   = newName;
     Family = newFamily;
     Sound  = newSound;
 }