public static void Save(DataDictionary dictionary,string filename) { DataDictionaryWriter ddw = new DataDictionaryWriter(); ddw._filename = filename; using( ddw._tw = IO.CreateStreamWriterForCSProFile(filename,dictionary.Version) ) { // save the header ddw._tw.WriteLine(DataDictionaryElements.DICT_HEADER); ddw.SaveOption(DataDictionaryElements.HEADER_VERSION,String.Format(CultureInfo.InvariantCulture,"{0} {1:F1}", DataDictionaryElements.HEADER_VERSION_CSPRO,dictionary.Version)); ddw.SaveSharedComponents(dictionary); ddw.SaveOption(DataDictionaryElements.HEADER_RECSTART,dictionary.RecTypeStart); ddw.SaveOption(DataDictionaryElements.HEADER_RECLEN,dictionary.RecTypeLength); ddw.SaveOption(DataDictionaryElements.HEADER_POSITIONS, dictionary.RelativePositioning ? DataDictionaryElements.HEADER_RELATIVE : DataDictionaryElements.HEADER_ABSOLUTE); ddw.SaveOption(DataDictionaryElements.HEADER_ZEROFILL,dictionary.ZeroFillDefault); ddw.SaveOption(DataDictionaryElements.HEADER_DECCHAR,dictionary.DecimalCharDefault); if( dictionary.UsingValueSetImages ) ddw.SaveOption(DataDictionaryElements.HEADER_VALUESETIMAGES,dictionary.UsingValueSetImages); ddw.SaveLanguages(dictionary); foreach( Level level in dictionary.Levels ) ddw.SaveLevel(level); foreach( Relation relation in dictionary.Relations ) ddw.SaveRelation(relation); } }
public static List<ValueSet> GetAllValueSets(DataDictionary dictionary) { List<ValueSet> valueSets = new List<ValueSet>(); foreach( Item item in GetAllItems(dictionary) ) valueSets.AddRange(item.ValueSets); return valueSets; }
public static void CalculateRecordLengths(DataDictionary dictionary) { foreach( Level level in dictionary.Levels ) { int maxLengthIDs = CalculateMaxLengthFromItems(level.IDs.Items); foreach( Record record in level.Records ) record.Length = Math.Max(maxLengthIDs,CalculateMaxLengthFromItems(record.Items)); } }
public void CheckIfValidNewName(DataDictionary dictionary,string newName) { if( !Names.IsValid(newName) ) throw new Exception(String.Format(Messages.NameCheckInvalid,newName)); // check that a dictionary with the same name has not been loaded if( _dictionaries.ContainsKey(newName) ) throw new Exception(String.Format(Messages.NameCheckUsedAsDictionary,newName)); if( _dictionarySymbols[dictionary.Name].ContainsKey(newName) ) throw new Exception(String.Format(Messages.NameCheckUsedWithinDictionary,newName,dictionary.Name)); }
public static List<Item> GetAllItems(DataDictionary dictionary) { List<Item> items = new List<Item>(); foreach( Level level in dictionary.Levels ) { for( int i = -1; i < level.Records.Count; i++ ) { Record record = ( i == -1 ) ? level.IDs : level.Records[i]; items.AddRange(record.Items); } } return items; }
public static void CalculateItemPositions(DataDictionary dictionary) { if( dictionary.AbsolutePositioning ) return; int lengthIDs = 0; foreach( Level level in dictionary.Levels ) { int position = dictionary.RecTypeLength + 1; foreach( Item item in level.IDs.Items ) { item.Start = position; position += item.Length; lengthIDs += item.Length; } foreach( Record record in level.Records ) { int lastItemOffset = 0; position = dictionary.RecTypeLength + 1 + lengthIDs; foreach( Item item in record.Items ) { if( item.Subitem ) item.Start += lastItemOffset; else { lastItemOffset = position - item.Start; item.Start = position; position += item.Length * item.Occurrences; } } } } }
private void PrefixItem(DataDictionary dict, string prefix, Item i) { PrefixDictObjectName(dict, prefix, i); i.ValueSets.ForEach(vs => PrefixDictObjectName(dict, prefix, vs)); }
private void PrefixDictObjectName(DataDictionary dict, string prefix, DataDictionaryObject ddo) { string oldName = ddo.Name; string newName = prefix + ddo.Name; CheckIfValidNewName(dict, newName); // change the name ddo.Name = newName; }
private void LoadDictionarySymbols(DataDictionary dictionary) { Dictionary<string,object> symbols = new Dictionary<string,object>(); foreach( Level level in dictionary.Levels ) { AddSymbol(symbols,level.Name,level); LoadRecordSymbols(symbols,level.IDs); foreach( Record record in level.Records ) LoadRecordSymbols(symbols,record); } foreach( Relation relation in dictionary.Relations ) AddSymbol(symbols,relation.Name,relation); _dictionarySymbols[dictionary.Name] = symbols; }
private string LoadDictionary() { _dictionary = new DataDictionary(); string line; while( ( line = ReadTrimmedLine() ) != null && !IsHeader(line) ) { string argument,value; ParseLine(line,out argument,out value); if( AssignSharedComponents(_dictionary,argument,value) ) { } else if( argument.Equals(DataDictionaryElements.HEADER_VERSION,StringComparison.InvariantCultureIgnoreCase) ) { if( value.IndexOf(DataDictionaryElements.HEADER_VERSION_CSPRO,StringComparison.InvariantCultureIgnoreCase) != 0 ) throw new DataDictionaryReaderException(line); _dictionary.Version = Double.Parse(value.Substring(DataDictionaryElements.HEADER_VERSION_CSPRO.Length),CultureInfo.InvariantCulture); } else if( argument.Equals(DataDictionaryElements.HEADER_RECSTART,StringComparison.InvariantCultureIgnoreCase) ) _dictionary.RecTypeStart = Int32.Parse(value,CultureInfo.InvariantCulture); else if( argument.Equals(DataDictionaryElements.HEADER_RECLEN,StringComparison.InvariantCultureIgnoreCase) ) _dictionary.RecTypeLength = Int32.Parse(value,CultureInfo.InvariantCulture); else if( argument.Equals(DataDictionaryElements.HEADER_POSITIONS,StringComparison.InvariantCultureIgnoreCase) ) _dictionary.RelativePositioning = value.Equals(DataDictionaryElements.HEADER_RELATIVE,StringComparison.InvariantCultureIgnoreCase); else if( argument.Equals(DataDictionaryElements.HEADER_ZEROFILL,StringComparison.InvariantCultureIgnoreCase) ) _dictionary.ZeroFillDefault = value.Equals(DataDictionaryElements.DICT_YES,StringComparison.InvariantCultureIgnoreCase); else if( argument.Equals(DataDictionaryElements.HEADER_DECCHAR,StringComparison.InvariantCultureIgnoreCase) ) _dictionary.DecimalCharDefault = value.Equals(DataDictionaryElements.DICT_YES,StringComparison.InvariantCultureIgnoreCase); else if( argument.Equals(DataDictionaryElements.HEADER_VALUESETIMAGES,StringComparison.InvariantCultureIgnoreCase) ) _dictionary.UsingValueSetImages = value.Equals(DataDictionaryElements.DICT_YES,StringComparison.InvariantCultureIgnoreCase); else throw new DataDictionaryReaderException(line); } return line; }
public Level(DataDictionary parent,int levelNum) { _parent = parent; _records = new List<Record>(); LevelNum = levelNum; _ids = new Record(this); _ids.Name = String.Format("_IDS{0}",LevelNum - 1); _ids.Label = "(Id Items)"; }
private void SaveLanguages(DataDictionary dictionary) { if( dictionary.Languages.Count > 1 ) { _tw.WriteLine(); _tw.WriteLine(DataDictionaryElements.DICT_LANGUAGES); foreach( Language language in dictionary.Languages ) SaveOption(language.Name,language.Label); } }
public static int GetNewValueSetLinkID(DataDictionary dictionary) { // create a listing of used IDs HashSet<int> usedIDs = new HashSet<int>(); foreach( ValueSet vs in GetAllValueSets(dictionary) ) { if( vs.LinkID != Int32.MinValue && !usedIDs.Contains(vs.LinkID) ) usedIDs.Add(vs.LinkID); } // return a linked value over 10000000 for( int linkID = 10000001; ; linkID++ ) { if( !usedIDs.Contains(linkID) ) return linkID; } }
public static void RemoveBrokenValueSetLinks(DataDictionary dictionary) { // the ValueSet will be set to null if more than one value set exists Dictionary<int,ValueSet> links = new Dictionary<int,ValueSet>(); foreach( ValueSet vs in GetAllValueSets(dictionary) ) { if( vs.LinkID != Int32.MinValue ) links[vs.LinkID] = links.ContainsKey(vs.LinkID) ? null : vs; } // remove the broken links foreach( var kp in links ) { if( kp.Value != null ) kp.Value.LinkID = Int32.MinValue; } }
private void PrefixLevel(DataDictionary dict, string prefix, Level l) { PrefixDictObjectName(dict, prefix, l); l.IDs.Items.ForEach(i => PrefixItem(dict, prefix, i)); l.Records.ForEach(r => PrefixRecord(dict, prefix, r)); }
private void PrefixRecord(DataDictionary dict, string prefix, Record r) { PrefixDictObjectName(dict, prefix, r); r.Items.ForEach(i => PrefixItem(dict, prefix, i)); }
private void buttonOpenDictionary_Click(object sender,EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "CSPro Dictionary (*.dcf)|*.dcf"; if( ofd.ShowDialog() == DialogResult.OK ) { try { dictionary = DataDictionaryReader.Load(ofd.FileName); if( dictionary.Levels.Count != 1 ) throw new Exception(Messages.Dictionary_TwoLevelError); else if( dictionary.RecTypeLength == 0 ) throw new Exception(Messages.Dictionary_NoRecordType); labelDictionary.Text = dictionary.Label; SetupInitialRecordTypes(); casesRead = 0; recordsRead = 0; recordsWritten = 0; UpdateStats(false); } catch( Exception exception ) { dictionary = null; labelDictionary.Text = ""; MessageBox.Show(String.Format(Messages.Dictionary_LoadError,exception.Message)); } } EnableCleanButton(); }