Exemplo n.º 1
0
        private SyncItem AddSyncItemAndPerformInitialPropagation(IValueAccessor iva, IConfigKeyAccess cka, string ivaLookupName, string ivaFromCkaMappedName, string ckaFromIvaMappedName, string ckaLookupKeyName)
        {
            SyncItem syncItem = new SyncItem()
            {
                iva = iva, icka = cka, ivaLookupName = ivaLookupName, ivaFromCkaMappedName = ivaFromCkaMappedName, ckaFromIvaMappedName = ckaFromIvaMappedName, ckaLookupKeyName = ckaLookupKeyName
            };

            syncItemList.Add(syncItem);
            syncItemArray = null;
            ivaArray      = null;

            // add syncItem to both maps using both original names and found names, in case target is using name mapping and has applied it to this name.
            ivaNameToSyncItemDictionary[ivaLookupName]          = syncItem;
            ivaNameToSyncItemDictionary[iva.Name]               = syncItem;
            configKeyNameToSyncItemDictionary[ckaLookupKeyName] = syncItem;
            configKeyNameToSyncItemDictionary[cka.Key]          = syncItem;

            ivaArray = null;

            if (cka.HasValue)
            {
                ValueContainer vc = cka.VC;
                if (!iva.VC.IsEqualTo(vc))
                {
                    ValueTraceEmitter.Emit("Propagating initial cka '{0}' to iva '{1}'", cka, iva);
                    iva.Set(vc);
                }
                else
                {
                    ValueTraceEmitter.Emit("Initial cka '{0}' matches initial iva '{1}'", cka, iva);
                }
            }
            else if (iva.HasValueBeenSet)
            {
                ValueContainer vc = iva.VC;
                ValueTraceEmitter.Emit("Propagating initial iva '{0}' to cka '{1}'", iva, cka);
                cka.SetValue(vc, "{0}: Propagating initial value from iva '{1}'".CheckedFormat(PartID, iva));
            }

            syncItem.UpdateCopyInSet(ReferenceSet);

            return(syncItem);
        }
Exemplo n.º 2
0
        protected override string PerformServiceActionEx(IProviderFacet ipf, string serviceName, INamedValueSet npv)
        {
            switch (serviceName)
            {
            case "Sync":
                return(PerformSync());

            case "SetKey":
            {
                string         key          = npv["key"].VC.GetValue <string>(rethrow: true);
                ValueContainer value        = npv["value"].VC;
                string         comment      = npv["comment"].VC.GetValue <string>(rethrow: false, defaultValue: null).MapNullTo("{0} operation has been performed using the {1} part".CheckedFormat(serviceName, PartID));
                bool?          ensureExists = npv["ensureExists"].VC.GetValue <bool?>(rethrow: false);

                IConfigKeyAccess icka = Config.GetConfigKeyAccess(key, ensureExists: ensureExists, defaultValue: value);

                if (icka == null)
                {
                    return("Internal: GetConfigKeyAccess generated null ICKA for key '{0}'".CheckedFormat(key));
                }

                if (!icka.IsUsable)
                {
                    return("Key lookup for '{0}' gave error: {1}".CheckedFormat(key, icka.ResultCode));
                }

                string ec = icka.SetValue(value, commentStr: comment);

                ServiceBridge();

                return(ec.MapNullTo("[Internal: final ec was null]"));
            }

            case "SetKeys":
            {
                string []         keys    = npv["keys"].VC.GetValue <string []>(rethrow: true);
                ValueContainer [] values  = npv["values"].VC.GetValue <ValueContainer []>(rethrow: true);
                string            comment = npv["comment"].VC.GetValue <string>(rethrow: false, defaultValue: null).MapNullTo("{0} operation has been performed using the {1} part".CheckedFormat(serviceName, PartID));
                bool?ensureExists         = npv["ensureExists"].VC.GetValue <bool?>(rethrow: false);

                if (keys.SafeLength() != values.SafeLength())
                {
                    return("Given keys and values were not the same length");
                }

                if (keys.SafeLength() == 0)
                {
                    return("No keys were given to change");
                }

                IConfigKeyAccess[] ickaArray = keys.Select((key, idx) => Config.GetConfigKeyAccess(key, ensureExists: ensureExists, defaultValue: values[idx])).ToArray();

                if (ickaArray.Any(icka => icka == null))
                {
                    return("Internal: GetConfigKeyAccess generated null ICKA for one or more keys in [{0}]".CheckedFormat(string.Join(",", keys)));
                }

                var notFoundKeys = ickaArray.Where(icka => !icka.IsUsable).Select(icka => icka.Key).ToArray();
                if (!notFoundKeys.IsNullOrEmpty())
                {
                    return("Key lookup for keys '{0}' failed".CheckedFormat(string.Join(",", notFoundKeys)));
                }

                string ec = Config.SetValues(ickaArray.Select((icka, idx) => KVP.Create(icka, values[idx])).ToArray(), comment);

                ServiceBridge();

                return(ec.MapNullTo("[Internal: final ec was null]"));
            }

            default:
                return(base.PerformServiceAction(serviceName));
            }
        }