Exemplo n.º 1
0
Arquivo: Schema.cs Projeto: rioka/nfx
            private void ctor(string name, int order, Type type, IEnumerable <FieldAttribute> attrs, PropertyInfo memberInfo = null)
            {
                if (name.IsNullOrWhiteSpace() || type == null || attrs == null)
                {
                    throw new CRUDException(StringConsts.ARGUMENT_ERROR + "FieldDef.ctor(..null..)");
                }

                m_Name  = name;
                m_Order = order;
                m_Type  = type;
                m_Attrs = new List <FieldAttribute>(attrs);

                if (m_Attrs.Count < 1)
                {
                    throw new CRUDException(StringConsts.CRUD_FIELDDEF_ATTR_MISSING_ERROR.Args(name));
                }

                //add ANY_TARGET attribute
                if (!m_Attrs.Any(a => a.TargetName == TargetedAttribute.ANY_TARGET))
                {
                    var isAnyKey = m_Attrs.Any(a => a.Key);
                    var ata      = new FieldAttribute(FieldAttribute.ANY_TARGET, key: isAnyKey);
                    m_Attrs.Add(ata);
                }

                m_MemberInfo = memberInfo;

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    m_NonNullableType = type.GetGenericArguments()[0];
                }
                else
                {
                    m_NonNullableType = type;
                }

                m_AnyTargetKey = this[null].Key;
            }
Exemplo n.º 2
0
        protected Exception CheckMinMax(FieldAttribute atr, string fName, IComparable val)
        {
            if (atr.Min != null)
            {
                var bound = atr.Min as IComparable;
                if (bound != null)
                {
                    var tval = val.GetType();

                    bound = Convert.ChangeType(bound, tval) as IComparable;

                    if (val.CompareTo(bound) < 0)
                    {
                        return(new CRUDFieldValidationException(Schema.Name, fName, StringConsts.CRUD_FIELD_VALUE_MIN_BOUND_ERROR));
                    }
                }
            }

            if (atr.Max != null)
            {
                var bound = atr.Max as IComparable;
                if (bound != null)
                {
                    var tval = val.GetType();

                    bound = Convert.ChangeType(bound, tval) as IComparable;

                    if (val.CompareTo(bound) > 0)
                    {
                        return(new CRUDFieldValidationException(Schema.Name, fName, StringConsts.CRUD_FIELD_VALUE_MAX_BOUND_ERROR));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 3
0
 public FieldDef(string name, Type type, FieldAttribute attr)
 {
     ctor(name, 0, type, new[] { attr }, null);
 }
Exemplo n.º 4
0
      private Schema erlSchemaToCRUDSchema(string name)
      {
        var erlSect = GetErlSchemaSection(name);
        if (erlSect==null) 
         throw new ErlDataAccessException(StringConsts.ERL_DS_SCHEMA_MAP_NOT_KNOWN_ERROR.Args(name));

        var defs = new List<Schema.FieldDef>();

        var keyCount = 0;
        var nodeFields = erlSect.Children.Where(c => c.IsSameName(CONFIG_FIELD_SECTION));
        foreach(var nodeField in nodeFields)
        {
          var fname   = nodeField.AttrByName(Configuration.CONFIG_NAME_ATTR).Value;
          var ftitle  = nodeField.AttrByName(CONFIG_TITLE_ATTR).Value;
          var isKey   = nodeField.AttrByName(CONFIG_KEY_ATTR).ValueAsBool();
          var required= nodeField.AttrByName(CONFIG_REQUIRED_ATTR).ValueAsBool(false);
          var type = nodeField.AttrByName(CONFIG_TYPE_ATTR).Value;
          var clrType = mapErlTypeToCLR(type);

          var strDfltValue = nodeField.AttrByName(CONFIG_DEFAULT_ATTR).ValueAsString(string.Empty);
          object dfltValue = null;
          
          if (clrType!=typeof(string))
          {
            if (strDfltValue.IsNotNullOrWhiteSpace())
            {
               if (clrType==typeof(DateTime?))
                dfltValue = ((long)strDfltValue.AsType(typeof(long), false)).FromMicrosecondsSinceUnixEpochStart();
               else
                dfltValue = strDfltValue.AsType(clrType, false);
            }
          }
          else
          {
            dfltValue = strDfltValue;
          }

          if (isKey) keyCount++;

          List<string> vList = null;
                   
          var values = nodeField.Children.Where( c => c.IsSameName(CONFIG_VALUE_SECTION));
          foreach(var vnode in values)
          {
            var code = vnode.AttrByName(CONFIG_CODE_ATTR).Value;
            var disp = vnode.AttrByName(CONFIG_DISPLAY_ATTR).Value;
            if (code.IsNullOrWhiteSpace()) continue;

            if (vList==null) vList = new List<string>();
            if (disp.IsNullOrWhiteSpace())
              vList.Add(code);
            else
              vList.Add("{0}: {1}".Args(code, disp));
          }


          var atr = new FieldAttribute(
                         targetName: m_Store.TargetName, 
                         backendName: fname,
                         backendType: type,
                         storeFlag: StoreFlag.LoadAndStore,
                         key: isKey,
                         required: required,
                         dflt: dfltValue,

                         visible:   nodeField.AttrByName(CONFIG_VISIBLE_ATTR).ValueAsBool(true),
                         maxLength: nodeField.AttrByName(CONFIG_LEN_ATTR).ValueAsInt(0),
                         description: nodeField.AttrByName(CONFIG_DESCR_ATTR).Value,
                         displayFormat: nodeField.AttrByName(CONFIG_DISPLAY_FORMAT_ATTR).Value,
                         valueList: vList==null ? null : string.Join(",", vList),//"CAR: Car Driver,SMK: Smoker, REL: Religious, CNT: Country music lover, GLD: Gold collector"
                         metadata: nodeField.ToLaconicString());

          var def = new Schema.FieldDef(fname ?? ftitle, clrType, new FieldAttribute[]{atr});
          defs.Add( def );
        }//for fields 

        var result = new Schema(name, false, defs.ToArray());

        result.ExtraData[SCHEMA_KEY_COUNT] = keyCount; 

        return result;
      }
Exemplo n.º 5
0
    public static Schema ErlSchemaToCRUDSchema(string name, IConfigSectionNode erlSect)
    {
      var defs       = new List<Schema.FieldDef>();

      var isInsert   = erlSect.AttrByName(CONFIG_INSERT_ATTR).ValueAsBool(false);
      var isUpdate   = erlSect.AttrByName(CONFIG_UPDATE_ATTR).ValueAsBool(false);
      var isDelete   = erlSect.AttrByName(CONFIG_DELETE_ATTR).ValueAsBool(false);
      var schDescr   = erlSect.AttrByName(CONFIG_DESCR_ATTR).ValueAsString();

      var isReadonly = !(isInsert || isUpdate || isDelete);

      var keyCount = 0;
      var nodeFields = erlSect.Children.Where(c => c.IsSameName(CONFIG_FIELD_SECTION));
      foreach(var nodeField in nodeFields)
      {
        var cfg      = new LaconicConfiguration();
        cfg.CreateFromNode(nodeField);
        var node     = cfg.Root;

        var fname    = node.AttrByName(Configuration.CONFIG_NAME_ATTR).Value;
        var ftitle   = node.AttrByName(CONFIG_TITLE_ATTR).Value;
        var fhint    = node.AttrByName(CONFIG_DESCR_ATTR).ValueAsString("");
        var isKey    = node.AttrByName(CONFIG_KEY_ATTR).ValueAsBool();
        var required = node.AttrByName(CONFIG_REQUIRED_ATTR).ValueAsBool(false);
        var type     = node.AttrByName(CONFIG_TYPE_ATTR).Value;
        var clrType  = mapErlTypeToCLR(type);
        var fmtdesc  = node.AttrByName(CONFIG_FORMAT_DESCR_ATTR).Value;
        var dispFmt  = node.AttrByName(CONFIG_DISPLAY_FORMAT_ATTR).Value;

        if (dispFmt != null && (dispFmt.Length < 3 || dispFmt.Substring(0, 3) != "{0:"))
          dispFmt = "{{0:{0}}}".Args(dispFmt);

        node.AddAttributeNode(CONFIG_TITLE_ATTR, ftitle);

        object minV  = null;
        object maxV  = null;

        var sv = node.AttrByName(CONFIG_MIN_ATTR).Value;
        if (sv.IsNotNullOrWhiteSpace()) minV = sv.AsType(clrType, true);

        sv = node.AttrByName(CONFIG_MAX_ATTR).Value;
        if (sv.IsNotNullOrWhiteSpace()) maxV = sv.AsType(clrType, true);


        var strDfltValue = node.AttrByName(CONFIG_DEFAULT_ATTR).ValueAsString(string.Empty);
        object dfltValue = null;
          
        if (clrType==typeof(string))
          dfltValue = strDfltValue;
        else if (strDfltValue.IsNotNullOrWhiteSpace())
          dfltValue = clrType==typeof(DateTime?) 
                    ? ((long)strDfltValue.AsType(typeof(long), false)).FromMicrosecondsSinceUnixEpochStart() 
                    : strDfltValue.AsType(clrType, false);

        if (isKey) keyCount++;

        List<string> vList = null;
                   
        var values = node.Children.Where( c => c.IsSameName(CONFIG_VALUE_SECTION));
        foreach(var vnode in values)
        {
          var code = vnode.AttrByName(CONFIG_CODE_ATTR).Value;
          var disp = vnode.AttrByName(CONFIG_DISPLAY_ATTR).Value;
          if (code.IsNullOrWhiteSpace()) continue;

          if (vList==null) vList = new List<string>();
          if (disp.IsNullOrWhiteSpace())
            vList.Add(code);
          else
            vList.Add("{0}: {1}".Args(code, disp));
        }

        var caze = CharCase.AsIs;

        var ca = node.AttrByName(CONFIG_CASE_ATTR).Value;
        if (ca.EqualsOrdIgnoreCase("upper")) caze = CharCase.Upper;
        else
        if (ca.EqualsOrdIgnoreCase("lower")) caze = CharCase.Lower;

        var atr = new FieldAttribute(
            targetName:    TargetedAttribute.ANY_TARGET, 
            backendName:   fname,
            backendType:   type,
            storeFlag:     StoreFlag.LoadAndStore,
            key:           isKey,
            required:      required,
            dflt:          dfltValue,
                             
            min:           minV,
            max:           maxV,
                             
            charCase:      caze,

            visible:       node.AttrByName(CONFIG_VISIBLE_ATTR).ValueAsBool(true),
            maxLength:     node.AttrByName(CONFIG_LEN_ATTR).ValueAsInt(0),
            description:   fmtdesc.IsNullOrEmpty() ? fhint : "{0}\nFormat: {1}".Args(fhint,fmtdesc),
                            //Parsing.Utils.ParseFieldNameToDescription(ftitle, true),
            formatRegExp:  node.AttrByName(CONFIG_FORMAT_ATTR).Value,
            formatDescr:   fmtdesc,
            displayFormat: dispFmt,
            valueList:     vList==null ? null : string.Join(",", vList),//"CAR: Car Driver,SMK: Smoker, REL: Religious, CNT: Country music lover, GLD: Gold collector"
            metadata:      node.ToLaconicString());

        var def = new Schema.FieldDef(fname, clrType, new []{atr});
        defs.Add( def );
      }//for fields 

      var result = new Schema(name, isReadonly, defs.ToArray());

      if (schDescr.IsNotNullOrWhiteSpace())
        result.ExtraData[CONFIG_DESCR_ATTR]               = schDescr;

      result.ExtraData[SCHEMA_KEY_COUNT]                  = keyCount; 
      result.ExtraData[Schema.EXTRA_SUPPORTS_INSERT_ATTR] = isInsert;
      result.ExtraData[Schema.EXTRA_SUPPORTS_UPDATE_ATTR] = isUpdate;
      result.ExtraData[Schema.EXTRA_SUPPORTS_DELETE_ATTR] = isDelete;

      return result;
    }