public void TestParseOneSimpleProperty()
 {
     var items = new IndicatorMetadataTextParser().Parse(Join("2", "a"));
     Assert.AreEqual(1, items.Count);
     Assert.AreEqual(2, items.First().PropertyId);
     Assert.AreEqual("a", items.First().Text);
 }
        public void TestParseTwoSimpleProperties()
        {
            var items = new IndicatorMetadataTextParser().Parse(Join("2", "a", "3", "b"));
            Assert.AreEqual(2, items.Count);

            var item = items[0];
            Assert.AreEqual(2, item.PropertyId);
            Assert.AreEqual("a", item.Text);

            item = items[1];
            Assert.AreEqual(3, item.PropertyId);
            Assert.AreEqual("b", item.Text);
        }
 public void TestIsOverriddenParsedCorrectly()
 {
     var items = new IndicatorMetadataTextParser().Parse(Join("2", "a", "3o", "b"));
     Assert.IsFalse(items[0].IsOverridden);
     Assert.IsTrue(items[1].IsOverridden);
 }
        public static void UpdateIndicatorTextValues(int indicatorId, int? groupId, string userMTVChanges,
            IList<IndicatorMetadataTextProperty> properties, DateTime timeOfChange, string userName, int? profileId, ProfileRepository profileRepository)
        {
            var items = new IndicatorMetadataTextParser().Parse(userMTVChanges);

            foreach (var item in items)
            {
                IndicatorMetadataTextProperty property = properties.First(x => x.PropertyId == item.PropertyId);

                // Get properties + existing values
                IndicatorText indicatorText = Reader.GetIndicatorTextValues(indicatorId,
                        new List<IndicatorMetadataTextProperty> { property },
                        profileId).First();

                // Figure out whether to update generic or specific
                int? profileIdForProperty = null;
                if ((item.IsOverridden || indicatorText.HasSpecificValue()) && groupId.HasValue)
                {
                    profileIdForProperty = profileId;
                }

                // Text that is being replaced
                string oldText;
                if (item.IsOverridden)
                {
                    // The user is overriding the generic value for the first time
                    oldText = null;
                }
                else if (indicatorText.HasSpecificValue())
                {
                    // The user is modifying an existing specific value
                    oldText = indicatorText.ValueSpecific;
                }
                else
                {
                    // User has changed the generic value
                    oldText = indicatorText.ValueGeneric;
                }

                // Save to change log 
                profileRepository.LogPropertyChange(property.PropertyId, oldText, indicatorId, profileIdForProperty, userName,
                                              timeOfChange);

                var indicatorAlreadyOverridden = profileRepository.DoesOverriddenIndicatorMetaDataRecordAlreadyExist(indicatorId, profileId);

                var text = item.Text;
                if (indicatorAlreadyOverridden && indicatorText.HasSpecificValue())
                {
                    if (text == indicatorText.ValueSpecific || text == indicatorText.ValueGeneric)
                    {
                        text = null;
                    }
                    profileRepository.UpdateProperty(property, text, indicatorId, profileIdForProperty);
                }
                else
                {
                    if (!indicatorAlreadyOverridden && item.IsOverridden)
                    {
                        profileRepository.CreateNewOverriddenIndicator(property, text, indicatorId, profileId);
                    }
                    else
                    {
                        if (text == indicatorText.ValueSpecific || text == indicatorText.ValueGeneric)
                        {
                            text = null;
                        }
                        profileRepository.UpdateProperty(property, text, indicatorId, profileIdForProperty);
                    }
                }
            }

            RemoveEmptyOverrides(indicatorId, groupId, properties, profileRepository);
        }
        public static void CreateNewIndicatorTextValues(string selectedDomain, string userMTVChanges,
            IList<IndicatorMetadataTextProperty> properties, int nextIndicatorId, string userName, ProfileRepository profileRepository)
        {
            var items = new IndicatorMetadataTextParser().Parse(userMTVChanges);

            var allPropertiesToAdd = new List<IndicatorMetadataTextProperty>();

            foreach (var item in items)
            {
                IndicatorMetadataTextProperty property = properties.First(x => x.PropertyId == item.PropertyId);
                property.Text = item.Text;
                allPropertiesToAdd.Add(property);

                // Save to change log 
                profileRepository.LogPropertyChange(property.PropertyId, null, nextIndicatorId, Convert.ToInt32(selectedDomain), userName, DateTime.Now);
            }

            profileRepository.CreateIndicator(allPropertiesToAdd, nextIndicatorId);
        }