|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Xml; |
| 4 | + |
| 5 | +using PCAxis.Serializers.Util.Metaid; |
| 6 | + |
| 7 | +namespace PCAxis.Serializers.Util.MetaId |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Class that encapsulates a metaid.config file containing link-definitions to metadata systems, and has methodsw that resolves a "raw" meta-id string to Links. |
| 11 | + /// </summary> |
| 12 | + public static class MetaIdResolverStatic |
| 13 | + { |
| 14 | + |
| 15 | + #region "Private fields" |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Configuration file |
| 19 | + /// </summary> |
| 20 | + private static XmlDocument _xdoc; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Logging to Log4Net |
| 24 | + /// </summary> |
| 25 | + private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(MetaIdResolverStatic)); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Character that separates the systems within a META-ID |
| 29 | + /// </summary> |
| 30 | + private static readonly char[] _systemSeparator = { ',', ' ' }; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Character that separates the parameters within a system META-ID |
| 34 | + /// </summary> |
| 35 | + private static readonly char[] _paramSeparator = { ':' }; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Holds all data loaded from config, reshuffled so using it easy. |
| 39 | + /// </summary> |
| 40 | + private static readonly LinkTemplatesHolder holder = LoadConfiguration("metaid.config"); |
| 41 | + |
| 42 | + #endregion |
| 43 | + |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Load metadata.config file |
| 47 | + /// </summary> |
| 48 | + /// <param name="configurationFile">Path to the configuration file</param> |
| 49 | + /// <returns>True if the configuration file was successfully loaded, else false</returns> |
| 50 | + private static LinkTemplatesHolder LoadConfiguration(string configurationFile) |
| 51 | + { |
| 52 | + //It is ok to not use metaid |
| 53 | + if (!System.IO.File.Exists(configurationFile)) |
| 54 | + { |
| 55 | + _logger.WarnFormat("Metaid configuration file '{0}' does not exist in the folder where the dlls are ...", configurationFile); |
| 56 | + return new LinkTemplatesHolder(); |
| 57 | + } |
| 58 | + |
| 59 | + _xdoc = new XmlDocument(); |
| 60 | + _xdoc.Load(configurationFile); |
| 61 | + |
| 62 | + LinkTemplatesHolder myOut = new LinkTemplatesHolder(); |
| 63 | + |
| 64 | + // Table-level |
| 65 | + LoadConfigurationSection(AttachmentLevel.onTable, myOut); |
| 66 | + |
| 67 | + // Variable-level |
| 68 | + LoadConfigurationSection(AttachmentLevel.onVariable, myOut); |
| 69 | + |
| 70 | + // Value-level |
| 71 | + LoadConfigurationSection(AttachmentLevel.onValue, myOut); |
| 72 | + |
| 73 | + return myOut; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Load sub section of the configuration file |
| 78 | + /// </summary> |
| 79 | + /// <param name="section">Name of the section</param> |
| 80 | + /// <param name="dictionary">Dictionary to store section data in</param> |
| 81 | + /// <returns></returns> |
| 82 | + private static void LoadConfigurationSection(AttachmentLevel attachmentLevel, LinkTemplatesHolder holder) |
| 83 | + { |
| 84 | + // Find all metaSystem nodes in section |
| 85 | + string section = attachmentLevel.ToString(); |
| 86 | + |
| 87 | + string xpath = "/metaId/" + section + "/metaSystem"; |
| 88 | + XmlNodeList xmlnodes = _xdoc.SelectNodes(xpath); |
| 89 | + |
| 90 | + foreach (XmlNode sysNode in xmlnodes) |
| 91 | + { |
| 92 | + string sysId = sysNode.Attributes["id"].Value; |
| 93 | + |
| 94 | + // Find all language nodes for the system |
| 95 | + xpath = ".//relationalGroup"; |
| 96 | + XmlNodeList relationalGroupNodes = sysNode.SelectNodes(xpath); |
| 97 | + |
| 98 | + foreach (XmlNode relationalGroupNode in relationalGroupNodes) |
| 99 | + { |
| 100 | + string linkType = relationalGroupNode.Attributes["type"].Value; |
| 101 | + string linkRelation = relationalGroupNode.Attributes["relation"].Value; |
| 102 | + |
| 103 | + // Find all language nodes for the system |
| 104 | + xpath = ".//link"; |
| 105 | + XmlNodeList linkNodes = relationalGroupNode.SelectNodes(xpath); |
| 106 | + |
| 107 | + foreach (XmlNode linkNode in linkNodes) |
| 108 | + { |
| 109 | + string pxLang = linkNode.Attributes["pxLang"].Value; |
| 110 | + string labelStringFormat = linkNode.Attributes["labelStringFormat"].Value; |
| 111 | + string urlStringFormat = linkNode.Attributes["urlStringFormat"].Value; |
| 112 | + |
| 113 | + LinkTemplate temp = new LinkTemplate(sysId, labelStringFormat, urlStringFormat, linkType, linkRelation); |
| 114 | + holder.Add(attachmentLevel, pxLang, temp); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Create links |
| 123 | + /// </summary> |
| 124 | + /// <param name="metaIdList">META-ID one or more</param> |
| 125 | + /// <param name="templates">templates for Language and attachmentlevel</param> |
| 126 | + /// <param name="textParams">0,1 or 2 text parameters, depending on attachmentleve</param> |
| 127 | + /// <returns></returns> |
| 128 | + private static List<Link> GetLinks(string metaIdList, List<LinkTemplate> templates, string[] textParams) |
| 129 | + { |
| 130 | + List<Link> myOut = new List<Link>(); |
| 131 | + |
| 132 | + if (templates.Count == 0) |
| 133 | + { |
| 134 | + return myOut; |
| 135 | + } |
| 136 | + |
| 137 | + string[] metaIds = metaIdList.Split(_systemSeparator, StringSplitOptions.RemoveEmptyEntries); |
| 138 | + |
| 139 | + foreach (string metaId in metaIds) |
| 140 | + { |
| 141 | + foreach (LinkTemplate template in templates) |
| 142 | + { |
| 143 | + if (!template.Match(metaId)) |
| 144 | + { |
| 145 | + break; |
| 146 | + } |
| 147 | + |
| 148 | + string rawParamsString = metaId.Replace(template.MetaSysId, ""); |
| 149 | + string[] linkParams = rawParamsString.Split(_paramSeparator, StringSplitOptions.RemoveEmptyEntries); |
| 150 | + |
| 151 | + myOut.Add(template.GetFormattedLink(textParams, linkParams)); |
| 152 | + |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return myOut; |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + #region "Implementation of IMetaIdProvider" |
| 162 | + |
| 163 | + |
| 164 | + public static List<Link> GetTableLinks(string metaIdField, string language) |
| 165 | + { |
| 166 | + string[] textParams = new string[] { }; |
| 167 | + return GetLinks(metaIdField, holder.GetTemplates(AttachmentLevel.onTable, language), textParams); |
| 168 | + } |
| 169 | + |
| 170 | + public static List<Link> GetVariableLinks(string metaIdField, string language, string variableLabel) |
| 171 | + { |
| 172 | + string[] textParams = new string[] { variableLabel }; |
| 173 | + return GetLinks(metaIdField, holder.GetTemplates(AttachmentLevel.onVariable, language), textParams); |
| 174 | + } |
| 175 | + |
| 176 | + public static List<Link> GetValueLinks(string metaIdField, string language, string variableLabel, string valueLabel) |
| 177 | + { |
| 178 | + string[] textParams = new string[] { variableLabel, valueLabel }; |
| 179 | + return GetLinks(metaIdField, holder.GetTemplates(AttachmentLevel.onValue, language), textParams); |
| 180 | + } |
| 181 | + #endregion |
| 182 | + |
| 183 | + } |
| 184 | +} |
0 commit comments