/// <summary>
        /// Construct CSPro data dictionary from dictionary specs.
        /// </summary>
        /// <param name="label">Text to use for CSPro dictionary label</param>
        /// <param name="databaseName">Name of SQL server database (saved in dictionary notes)</param>
        /// <param name="levelSpecs">List of level specifications that contain mappings from SQL database columns to CSPro levels and records.</param>
        /// <param name="valueSetRetriever">Utility class to get value sets for generated dictionary items</param>
        /// <returns>CSPro data dictionary</returns>
        public DataDictionary CreateDictionary(string label, string databaseName, IEnumerable <LevelSpec> levelSpecs, ValueSetRetriever valueSetRetriever)
        {
            var dictionary = new DataDictionary();

            dictionary.Label            = label;
            dictionary.Name             = MakeName(label + "_DICT");
            dictionary.RecordTypeLength = 1;
            dictionary.RecordTypeStart  = 1;
            dictionary.Note             = String.Format("#database {0}", databaseName);

            // Keep track of id-items used at this and previous levels so that they are not repeated
            List <DatabaseColumn> usedIdItems = new List <DatabaseColumn>();

            foreach (var levelSpec in levelSpecs)
            {
                CreateLevel(dictionary.AddLevel(), levelSpec, usedIdItems, valueSetRetriever);
            }

            AdjustItemStartPositions(dictionary);

            return(dictionary);
        }
Beispiel #2
0
        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));
            }
        }