Ejemplo n.º 1
0
        /*
         * //------------------------------------------------------------------------------
         * static bool TryToMigrateFromPrevFormat(ISettingsSerializer serializer,
         *                                     string curentSettingName, bool secure,
         *                                     Type settingType,
         *                                     out object value)
         * {
         *  var delimeter = SettingNameFormatInfo.GetSettingNameDelimeter(serializer as ISettingsSerializerWishes);
         *  SettingNameFormatInfo fi = new SettingNameFormatInfo(curentSettingName, delimeter, SettingNameMode.Unknown);
         *
         *  try
         *  {
         *      InternalConfiguration.PlatformHelper.Log(LogLevel.Info, "Format migration",
         *                                               String.Format("Try to migrate '{0}' from previous format version", curentSettingName));
         *      bool loaded = serializer.Load(fi.V1SettingName, secure, out value);
         *
         *      if (loaded)
         *      {
         *          value = InternalConfiguration.PlatformHelper.CorrectSettingType_V1(value, settingType);
         *
         *          InternalConfiguration.PlatformHelper.Log(LogLevel.Info, "Format migration",
         *                                                   String.Format("From '{0}' to '{1}' - success",
         *                                                                 fi.V1SettingName, curentSettingName));
         *
         *          return true;
         *      }
         *      else
         *      {
         *          InternalConfiguration.PlatformHelper.Log(LogLevel.Info, "Format migration",
         *                                                   String.Format("'{0}' skip migration, no previous value", curentSettingName));
         *      }
         *  }
         *  catch (Exception exc)
         *  {
         *      InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
         *                                               String.Format("Exception during format migration from '{0}' to '{1}'",
         *                                                             fi.V1SettingName, curentSettingName),
         *                                               exc);
         *  }
         *
         *  value = null;
         *
         *  return false;
         * }
         */

        //------------------------------------------------------------------------------
        static public bool Load(ISettingsSerializer serializer,
                                SettingBaseAttribute attr,
                                string settingName, bool secure,
                                Wrapper attributeDefaultValueWrapper,
                                Type destinationType,
                                ref SettingInfo outSettingInfo,
                                FunctionalityHook functionalityHook)
        {
            bool   valueWasLoaded = false;
            object loadedValue    = null;

            object defaultValue;

            if (attributeDefaultValueWrapper != null)
            {
                defaultValue = attributeDefaultValueWrapper.Value;
            }
            else
            {
                defaultValue = GetTypeDefaultValue(destinationType);
            }

            try
            {
                valueWasLoaded = serializer.Load(settingName, secure, attr, out loadedValue);

                bool valueWasSet = false;

                if (valueWasLoaded)
                {
                    // CorrectValueType in case of exception default value will be returned
                    var outTuple = CorrectValueType(loadedValue, destinationType, defaultValue, functionalityHook);

                    outSettingInfo.SettingValue        = outTuple.Item1;
                    outSettingInfo.SettingValueType    = outTuple.Item2;
                    outSettingInfo.SettingDefaultValue = outTuple.Item3;
                    outSettingInfo.SettingName         = settingName;

                    valueWasSet = true;
                }
                else
                {
                    bool   valueWasMigrated = false;
                    object migratedValue    = null;

                    /*
                     * if (SettingsBaseConfiguration.EnableFormatMigration)
                     * {
                     *  try
                     *  {
                     *      valueWasMigrated = TryToMigrateFromPrevFormat(serializer, settingName, secure, destinationType, out migratedValue);
                     *  }
                     *  catch(Exception exc)
                     *  {
                     *      InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                     *                                           String.Format("Format migration exception for setting '{0}'", settingName),
                     *                                           exc);
                     *
                     *      return false;
                     *  }
                     * }
                     */

                    if (valueWasMigrated)
                    {
                        // in success, object with non-curent type can be returned
                        // correct setting type
                        // CorrectValueType function in case of exception return default value
                        var outTuple = CorrectValueType(migratedValue, destinationType, defaultValue, functionalityHook);

                        outSettingInfo.SettingValue        = outTuple.Item1;
                        outSettingInfo.SettingValueType    = outTuple.Item2;
                        outSettingInfo.SettingDefaultValue = outTuple.Item3;
                        outSettingInfo.SettingName         = settingName;

                        valueWasSet = true;
                    }
                    else if (attributeDefaultValueWrapper != null)
                    {
                        // Use default value only if it was set
                        outSettingInfo.SettingValue = defaultValue;

                        outSettingInfo.SettingValueType    = destinationType;
                        outSettingInfo.SettingDefaultValue = defaultValue;
                        outSettingInfo.SettingName         = settingName;

                        valueWasSet = true;
                    }
                }

                return(valueWasSet);
            }
            catch (ExceptionForUser exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                                                         String.Format("Load exception for setting '{0}'", settingName),
                                                         exc.InnerException);

                throw exc.InnerException;
            }
            catch (Exception exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                                                         String.Format("Load exception for setting '{0}'", settingName),
                                                         exc);
            }

            return(false);
        }