// REMOVE -- symbol-name private void ProcessRemove(string[] commands) { DataDictionaryObject symbol = GetSymbolFromName(commands[1], SymbolTypes.Record | SymbolTypes.Item | SymbolTypes.ValueSet); DataDictionary dictionary = null; if (symbol is Record) { Record record = (Record)symbol; Level level = record.ParentLevel; dictionary = record.ParentDictionary; if (level.IDs == record) { throw new Exception(Messages.RemoveIDsDisabled); } level.Records.Remove(record); SetOutputSuccess(String.Format(Messages.RemoveRecord, record.Name, record.Items.Count)); } else if (symbol is Item) { Item item = (Item)symbol; Record record = item.ParentRecord; dictionary = item.ParentDictionary; record.Items.Remove(item); // delete any subitems associated with the item foreach (Item subitem in item.Subitems) { record.Items.Remove(subitem); } DataDictionaryWorker.CalculateItemPositions(dictionary); DataDictionaryWorker.CalculateRecordLengths(dictionary); DataDictionaryWorker.RemoveBrokenValueSetLinks(dictionary); SetOutputSuccess(String.Format(Messages.RemoveItem, item.Name, record.Name)); } else // ValueSet { ValueSet vs = (ValueSet)symbol; Item item = vs.ParentItem; dictionary = vs.ParentDictionary; item.ValueSets.Remove(vs); DataDictionaryWorker.RemoveBrokenValueSetLinks(dictionary); SetOutputSuccess(String.Format(Messages.RemoveValueSet, vs.Name, item.Name)); } LoadDictionarySymbols(dictionary); RefreshSymbolParents(); }
// UNCHECKBOXIZE -- item-name private void ProcessUncheckboxize(string[] commands) { const string UncheckboxizeNameFormat = "{0}_{1}"; const string UncheckboxizeValueSetSuffix = "_VS1"; Item item = (Item)GetSymbolFromName(commands[1], SymbolTypes.Item); if (!item.Alpha) { throw new Exception(String.Format(Messages.UncheckboxizeNotAlpha, item.Name)); } if (item.ValueSets.Count == 0) { throw new Exception(String.Format(Messages.UncheckboxizedNoValueSet, item.Name)); } List <Value> checkboxValues = item.ValueSets[0].Values; int maxValueWidth = 0; foreach (Value value in checkboxValues) { maxValueWidth = Math.Max(maxValueWidth, value.Pairs[0].From.Trim().Length); } if (maxValueWidth == 0 || item.Length % maxValueWidth != 0) { throw new Exception(String.Format(Messages.UncheckboxizedNotValidCheckbox, item.Name, item.ValueSets[0].Name, maxValueWidth, item.Length)); } int numCheckboxes = item.Length / maxValueWidth; // make sure that all of the names (to be created) are valid and available for (int occ = 0; occ < numCheckboxes; occ++) { string newName = null; try { newName = String.Format(UncheckboxizeNameFormat, item.Name, occ + 1); CheckIfValidNewName(item.ParentDictionary, newName); newName = newName + UncheckboxizeValueSetSuffix; CheckIfValidNewName(item.ParentDictionary, newName); } catch (Exception) { throw new Exception(String.Format(Messages.UnsubitemizeNameInvalid, item.Name, newName)); } } // remove the old item int itemIndex = item.ParentRecord.Items.IndexOf(item); item.ParentRecord.Items.RemoveAt(itemIndex); // create the new items ValueSet baseVS = null; for (int occ = 0; occ < numCheckboxes; occ++) { Item newItem = new Item(item.ParentRecord); newItem.ParentRecord.Items.Insert(itemIndex + occ, newItem); newItem.Name = String.Format(UncheckboxizeNameFormat, item.Name, occ + 1); newItem.Label = occ < checkboxValues.Count ? String.Format("{0} ({1})", item.Label, checkboxValues[occ].Label) : item.Label; newItem.Start = item.Start + maxValueWidth * occ; newItem.Occurrences = 1; newItem.Note = item.Note; newItem.Length = maxValueWidth; newItem.Numeric = true; newItem.Subitem = item.Subitem; newItem.ZeroFill = item.ParentDictionary.ZeroFillDefault; ValueSet vs = null; // add the value set if (baseVS == null) { Value uncheckedValue = new Value(); uncheckedValue.Label = "Unchecked"; uncheckedValue.Pairs.Add(new ValuePair("0")); Value checkedValue = new Value(); checkedValue.Label = "Checked"; checkedValue.Pairs.Add(new ValuePair("1")); baseVS = new ValueSet(newItem); baseVS.AddValue(uncheckedValue); baseVS.AddValue(checkedValue); vs = baseVS; } else { if (baseVS.LinkID == Int32.MinValue) { baseVS.LinkID = DataDictionaryWorker.GetNewValueSetLinkID(item.ParentDictionary); } vs = new ValueSet(newItem); vs.EstablishValueSetLink(baseVS); } vs.Name = String.Format(UncheckboxizeNameFormat, item.Name, occ + 1) + UncheckboxizeValueSetSuffix; vs.Label = newItem.Label; newItem.AddValueSet(vs); } SetOutputSuccess(String.Format(Messages.UncheckboxizedSuccess, item.Name, numCheckboxes)); }
// FLATTEN -- [UNSUBITEMIZE] item-name public void ProcessFlatten(string[] commands) { const string FlattenNameFormat = "{0}_{1}"; const string FlattenLabelFormat = "{0} ({1})"; bool createItem = true; if (commands.Length == 3) { if (!commands[1].Equals("UNSUBITEMIZE", StringComparison.InvariantCultureIgnoreCase)) { throw new Exception(String.Format(Messages.ProcessorUnrecognizedCommandSpecify, commands[1])); } createItem = false; } Item item = (Item)GetSymbolFromName(commands[createItem ? 1: 2], SymbolTypes.Item); if (!createItem) { CheckCanUnsubitemize(item); } if (item.Occurrences == 1) { throw new Exception(String.Format(Messages.FlattenMustHaveOccurrences, item.Name)); } // make sure that all of the names (to be created) are valid and available for (int i = createItem ? -1 : 0; i < item.Subitems.Count; i++) { Item thisItem = (i == -1) ? item : item.Subitems[i]; for (int occ = 0; occ < item.Occurrences; occ++) { string newName = null; try { newName = String.Format(FlattenNameFormat, thisItem.Name, occ + 1); CheckIfValidNewName(item.ParentDictionary, newName); foreach (ValueSet vs in thisItem.ValueSets) { newName = String.Format(FlattenNameFormat, vs.Name, occ + 1); CheckIfValidNewName(item.ParentDictionary, newName); } } catch (Exception) { throw new Exception(String.Format(Messages.FlattenNameInvalid, item.Name, newName)); } } } List <Item> recordItems = item.ParentRecord.Items; int itemInsertionIndex = recordItems.IndexOf(item); recordItems.RemoveAt(itemInsertionIndex); // remove any subitems associated with the just removed item for (int i = 0; i < item.Subitems.Count; i++) { recordItems.RemoveAt(itemInsertionIndex); } int itemsAdded = 0; for (int occ = 0; occ < item.Occurrences; occ++) { Item firstItem = null; for (int i = createItem ? -1 : 0; i < item.Subitems.Count; i++) { Item thisItem = (i == -1) ? item : item.Subitems[i]; Item newItem = new Item(thisItem.ParentRecord); if (createItem) { if (firstItem == null) { firstItem = newItem; } else { firstItem.Subitems.Add(newItem); } } newItem.Name = String.Format(FlattenNameFormat, thisItem.Name, occ + 1); // add an occurrence number (or label if it exists) newItem.Label = String.Format(FlattenLabelFormat, thisItem.Label, (occ < item.OccurrenceLabels.Count) ? item.OccurrenceLabels[occ] : (occ + 1).ToString()); newItem.Start = thisItem.Start + item.Length * occ; newItem.Occurrences = 1; // most of the item is a direct copy newItem.Note = thisItem.Note; newItem.Length = thisItem.Length; newItem.Numeric = thisItem.Numeric; newItem.Subitem = createItem ? thisItem.Subitem : false; newItem.Decimal = thisItem.Decimal; newItem.DecChar = thisItem.DecChar; newItem.ZeroFill = thisItem.ZeroFill; // create linked value sets for each of the value sets foreach (ValueSet vs in thisItem.ValueSets) { ValueSet newValueSet = new ValueSet(newItem); newValueSet.Name = String.Format(FlattenNameFormat, vs.Name, occ + 1); newValueSet.Label = String.Format(FlattenLabelFormat, vs.Label, (occ < item.OccurrenceLabels.Count) ? item.OccurrenceLabels[occ] : (occ + 1).ToString()); // create a new linked value set if it is not already linked if (vs.LinkID == Int32.MinValue) { vs.LinkID = DataDictionaryWorker.GetNewValueSetLinkID(item.ParentDictionary); } newValueSet.EstablishValueSetLink(vs); newItem.ValueSets.Add(newValueSet); } recordItems.Insert(itemInsertionIndex++, newItem); itemsAdded++; } } LoadDictionarySymbols(item.ParentDictionary); RefreshSymbolParents(); SetOutputSuccess(String.Format(Messages.FlattenSuccess, item.Name, item.Occurrences, itemsAdded)); }
private void buttonConvert_Click(object sender, EventArgs e) { if (_codebook == null) { MessageBox.Show(Messages.Convert_OpenCodebook); return; } if (_dictFilename == null) { MessageBox.Show(Messages.Convert_SelectDictionary); return; } if (!_codebook.Records.ContainsKey("H")) { MessageBox.Show(Messages.Convert_NoHousingRecord); return; } // create a CSPro dictionary from the IPUMS codebook DataDictionary dict = new DataDictionary(); dict.Name = "IPUMS_DICT"; dict.Label = _codebook.Description; List <IPUMS.Variable> housingRecord = _codebook.Records["H"]; // go through the housing record, see if there is a record type, and set up all the ID fields Level level = new Level(dict, 1); // IPUMS dictionaries will always be one-level dict.AddLevel(level); level.Name = "IPUMS_LEVEL"; level.Label = _codebook.Description; foreach (IPUMS.Variable variable in housingRecord) { if (variable.RecordType) { dict.RecTypeStart = variable.StartPosition; dict.RecTypeLength = variable.Length; } else if (variable.ID) { level.IDs.AddItem(CreateItem(variable, level)); } } // now go through and create each record Record thisRecord = null; foreach (var kp in _codebook.Records) { string recLabel = kp.Key == "H" ? "Housing" : kp.Key == "P" ? "Population" : kp.Key; if (thisRecord == null || dict.RecTypeLength > 0) // if there is a record type, we will create a new CSPro record for every IPUMS record { thisRecord = new Record(level); level.Records.Add(thisRecord); thisRecord.Name = recLabel.ToUpper() + "_REC"; thisRecord.Label = recLabel; bool isHousingRecord = (kp.Key == "H"); thisRecord.Required = isHousingRecord; // only require housing records if (dict.RecTypeLength > 0) { thisRecord.RecordType = kp.Key; } // 100 is assumed to be the maximum number of population occurrences thisRecord.MaxOccs = (isHousingRecord && _codebook.FileType != IPUMS.FileType.Rectangular) ? 1 : 100; } else // this is a record that has multiple types on one record (e.g., housing and population) { thisRecord.Label = thisRecord.Label + " + " + recLabel; } foreach (IPUMS.Variable variable in kp.Value) { if (!variable.RecordType && !variable.ID) { thisRecord.AddItem(CreateItem(variable, level)); } } } try { DataDictionaryWorker.CalculateRecordLengths(dict); DataDictionaryWriter.Save(dict, _dictFilename); MessageBox.Show(Messages.Convert_Success); } catch (Exception exception) { MessageBox.Show(String.Format(Messages.Convert_Failure, exception.Message)); } }