Example #1
0
        // 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));
        }
Example #2
0
        // 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));
        }