Exemple #1
0
        internal static string GenerateRegexForURL(ModelListMethod mlm, MethodInfo mi)
        {
            Logger.Debug("Generating regular expression for model list method at path " + mlm.Path);
            string ret = "^(GET\t";

            if (mlm.Host == "*")
            {
                ret += ".+";
            }
            else
            {
                ret += mlm.Host;
            }
            if (mi.GetParameters().Length > 0)
            {
                ParameterInfo[] pars   = mi.GetParameters();
                string[]        regexs = new string[pars.Length];
                for (int x = 0; x < pars.Length; x++)
                {
                    Logger.Trace("Adding parameter " + pars[x].Name + "[" + pars[x].ParameterType.FullName + "]");
                    regexs[x] = _GetRegexStringForParameter(pars[x]);
                }
                string path = string.Format((mlm.Path + (mlm.Paged ? (mlm.Path.Contains("?") ? "&" : "?") + "PageStartIndex={" + (regexs.Length - 3).ToString() + "}&PageSize={" + (regexs.Length - 2).ToString() + "}" : "")).Replace("?", "\\?"), regexs);
                ret += (path.StartsWith("/") ? path : "/" + path).TrimEnd('/');
            }
            else
            {
                ret += (mlm.Path.StartsWith("/") ? mlm.Path : "/" + mlm.Path).Replace("?", "\\?").TrimEnd('/');
            }
            Logger.Trace("Regular expression constructed: " + ret + ")$");
            return(ret + ")$");
        }
Exemple #2
0
 internal static string CreateJavacriptUrlCode(ModelListMethod mlm, MethodInfo mi, Type modelType)
 {
     Logger.Debug("Creating the javascript url call for the model list method at path " + mlm.Path);
     ParameterInfo[] pars = mi.GetParameters();
     if (pars.Length > 0)
     {
         string[] pNames = new string[pars.Length];
         for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
         {
             StringBuilder sb = new StringBuilder();
             sb.Append("'+(");
             if (pars[x].ParameterType == typeof(bool))
             {
                 sb.AppendFormat("{0}==undefined ? 'false' : ({0}==null ? 'false' : ({0} ? 'true' : 'false')))+'", pars[x].Name);
             }
             else if (pars[x].ParameterType == typeof(DateTime))
             {
                 sb.AppendFormat("{0}==undefined ? 'NULL' : ({0}==null ? 'NULL' : _.extractUTCDate({0})))+'", pars[x].Name);
             }
             else
             {
                 sb.AppendFormat("{0}==undefined ? 'NULL' : ({0} == null ? 'NULL' : {0}))+'", pars[x].Name);
             }
             pNames[x] = sb.ToString();
         }
         return("var url='" + string.Format((mlm.Path.StartsWith("/") ? mlm.Path : "/" + mlm.Path).TrimEnd('/'), pNames) + "';");
     }
     else
     {
         return("var url='" + (mlm.Path.StartsWith("/") ? mlm.Path : "/" + mlm.Path).TrimEnd('/') + "';");
     }
 }
Exemple #3
0
            public sModelListCall(ModelListMethod mlm, MethodInfo mi)
            {
                _isPaged = mlm.Paged;
                string reg = "";

                _groupIndexes = null;
                _usesSession  = Utility.UsesSecureSession(mi, out _sessionIndex);
                ParameterInfo[] pars = Utility.ExtractStrippedParameters(mi);
                if (pars.Length > 0)
                {
                    string[] regexs = new string[pars.Length];
                    reg = (mlm.Path + (mlm.Paged ? (mlm.Path.Contains("?") ? "&" : "?") + "PageStartIndex={" + (regexs.Length - 3).ToString() + "}&PageSize={" + (regexs.Length - 2).ToString() + "}" : "")).Replace("?", "\\?");
                    for (int x = 0; x < pars.Length; x++)
                    {
                        Type ptype    = pars[x].ParameterType;
                        bool nullable = false;
                        regexs[x] = "(.+)";
                        if (ptype.FullName.StartsWith("System.Nullable"))
                        {
                            nullable = true;
                            if (ptype.IsGenericType)
                            {
                                ptype = ptype.GetGenericArguments()[0];
                            }
                            else
                            {
                                ptype = ptype.GetElementType();
                            }
                        }
                        if (ptype == typeof(DateTime))
                        {
                            regexs[x] = "(\\d+" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(int) ||
                                 ptype == typeof(long) ||
                                 ptype == typeof(short) ||
                                 ptype == typeof(byte))
                        {
                            regexs[x] = "(-?\\d+" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(uint) ||
                                 ptype == typeof(ulong) ||
                                 ptype == typeof(ushort))
                        {
                            regexs[x] = "(-?\\d+" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(double) ||
                                 ptype == typeof(decimal) ||
                                 ptype == typeof(float))
                        {
                            regexs[x] = "(-?\\d+(.\\d+)?" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(bool))
                        {
                            regexs[x] = "(true|false" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype.IsEnum)
                        {
                            regexs[x] = "(";
                            foreach (string str in Enum.GetNames(ptype))
                            {
                                regexs[x] += str + "|";
                            }
                            regexs[x] = regexs[x].Substring(0, regexs[x].Length - 1) + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(Guid))
                        {
                            regexs[x] = "([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}" + (nullable ? "|NULL" : "") + ")";
                        }
                    }
                    _groupIndexes = new Dictionary <int, int>();
                    MatchCollection matches = _regParameter.Matches(reg);
                    for (int x = 0; x < matches.Count; x++)
                    {
                        int idx = int.Parse(matches[x].Groups[1].Value);
                        if (_usesSession)
                        {
                            if (idx >= _sessionIndex)
                            {
                                idx++;
                            }
                        }
                        _groupIndexes.Add(idx, x);
                    }
                    reg = string.Format(reg, regexs);
                    reg = (reg.StartsWith("/") ? reg : "/" + reg).TrimEnd('/');
                }
                else
                {
                    reg = mlm.Path.Replace("?", "\\?");
                }
                _reg    = new Regex(reg, RegexOptions.Compiled | RegexOptions.ECMAScript);
                _method = mi;
            }
Exemple #4
0
        public void GeneratorJS(ref WrappedStringBuilder builder, Type modelType)
        {
            foreach (MethodInfo mi in modelType.GetMethods(Constants.LOAD_METHOD_FLAGS))
            {
                if (mi.GetCustomAttributes(typeof(ModelListMethod), false).Length > 0)
                {
                    ModelListMethod mlm = (ModelListMethod)mi.GetCustomAttributes(typeof(ModelListMethod), false)[0];
                    builder.AppendFormat(@"App.Models.{0}=extend(App.Models.{0},{{
    {1}:function(", new object[] { modelType.Name, mi.Name });
                    ParameterInfo[] pars = Utility.ExtractStrippedParameters(mi);
                    string          url  = _CreateJavacriptUrlCode(mlm, pars, modelType);
                    if (mlm.Paged)
                    {
                        url += string.Format("+'{0}PageStartIndex='+this.currentIndex()+'&PageSize='+this.currentPageSize()", (mlm.Path.Contains("?") ? "&" : "?"));
                    }
                    for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                    {
                        builder.Append((x > 0 ? "," : "") + pars[x].Name);
                    }
                    if (mlm.Paged)
                    {
                        builder.Append((pars.Length > 3 ? "," : "") + "pageStartIndex,pageSize");
                        builder.AppendLine(@"){
        pageStartIndex = (pageStartIndex == undefined ? 0 : (pageStartIndex == null ? 0 : pageStartIndex));
        pageSize = (pageSize == undefined ? 10 : (pageSize == null ? 10 : pageSize));
        var ret = extend([],{
            currentIndex:function(){return pageStartIndex;},
            currentPageSize:function(){return pageSize;},
            currentPage:function(){return Math.floor(this.currentIndex()/this.currentPageSize());},
            totalPages:function(){return 0;},
            moveToPage:function(pageNumber){
                if (pageNumber>=this.totalPages()){
                    throw 'Unable to move to Page that exceeds current total pages.';
                }else{
                    this.currentIndex=function(){return pageNumber*this.currentPageSize();};
                    return this.reload();
                }
            },
            moveToNextPage:function(){
                if(Math.floor(this.currentIndex()/this.currentPageSize())+1<this.totalPages()){
                    return this.moveToPage(Math.floor(this.currentIndex()/this.currentPageSize())+1);
                }else{
                    throw 'Unable to move to next Page as that will excess current total pages.';
                }
            },
            moveToPreviousPage:function(){
                if(Math.floor(this.currentIndex()/this.currentPageSize())-1>=0){
                    return this.moveToPage(Math.floor(this.currentIndex()/this.currentPageSize())-1);
                }else{
                    throw 'Unable to move to previous Page as that will be before the first page.';
                }
            },
            changePageSize:function(size){
                this.currentPageSize = function(){ return size;};
                return this.reload();
            },");
                    }
                    else
                    {
                        builder.AppendLine(@"){
        var ret = extend([],{");
                    }
                    builder.Append(string.Format("url:function(){{ return {0};}},", url));
                    builder.Append(Constants._LIST_EVENTS_CODE);
                    builder.Append(Constants.ARRAY_TO_VUE_METHOD);
                    builder.Append(Constants._LIST_RELOAD_CODE.Replace("$url$", "this.url()").Replace("$type$", modelType.Name));
                    if ((mlm.Paged && pars.Length > 3) || (!mlm.Paged && pars.Length > 0))
                    {
                        builder.AppendLine(@",
            currentParameters:function(){
                return {");
                        for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                        {
                            builder.AppendLine(string.Format("              {0}:{0}{1}", new object[] { pars[x].Name, (x + 1 == (mlm.Paged ? pars.Length - 3 : pars.Length) ? "" : ",") }));
                        }
                        builder.AppendLine(@"                };
            },");
                        builder.Append("            changeParameters:function(");
                        for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                        {
                            builder.Append((x > 0 ? "," : "") + pars[x].Name);
                        }
                        builder.AppendLine(@"){
                if (arguments.length!=0){
                    if (Array.isArray(arguments[0]) && arguments.length==1){
                        var args = arguments[0];");
                        for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                        {
                            builder.AppendLine(string.Format("                      {0} = (args.length>{1} ? args[{1}] : undefined);", pars[x].Name, x));
                        }
                        builder.AppendLine(string.Format(@"                    }}
                }}
                this.url=function(){{ return {0};}};
                this.currentParameters=function(){{
                    return {{", url));
                        for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                        {
                            builder.AppendLine(string.Format("              {0}:{0}{1}", new object[] { pars[x].Name, (x + 1 == (mlm.Paged ? pars.Length - 3 : pars.Length) ? "" : ",") }));
                        }
                        builder.AppendLine(@"                   };
                };
                this.reload();
            }");
                    }
                    builder.AppendLine(@"        });
        ret.reload();
        return ret;
    }
});");
                }
            }
        }
Exemple #5
0
        public void GeneratorJS(ref WrappedStringBuilder builder, bool minimize, Type modelType)
        {
            foreach (MethodInfo mi in modelType.GetMethods(Constants.LOAD_METHOD_FLAGS))
            {
                if (mi.GetCustomAttributes(typeof(ModelListMethod), false).Length > 0)
                {
                    ModelListMethod mlm = (ModelListMethod)mi.GetCustomAttributes(typeof(ModelListMethod), false)[0];
                    builder.AppendFormat(@"{0}=$.extend({0},{{
    {1}:function(", new object[] { Constants.STATICS_VARAIBLE, mi.Name });
                    ParameterInfo[] pars = mi.GetParameters();
                    string          url  = _CreateJavacriptUrlCode(mlm, mi, modelType);
                    if (mlm.Paged)
                    {
                        url += string.Format("+'{0}PageStartIndex='+this.currentIndex()+'&PageSize='+this.currentPageSize()", (mlm.Path.Contains("?") ? "&" : "?"));
                    }
                    for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                    {
                        builder.Append((x > 0 ? "," : "") + pars[x].Name);
                    }
                    if (mlm.Paged)
                    {
                        builder.Append((pars.Length > 3 ? "," : "") + "pageStartIndex,pageSize");
                        builder.AppendLine(@"){
        pageStartIndex = (pageStartIndex == undefined ? 0 : (pageStartIndex == null ? 0 : pageStartIndex));
        pageSize = (pageSize == undefined ? 10 : (pageSize == null ? 10 : pageSize));
        var ret = $.extend([],{
            currentIndex:function(){return pageStartIndex;},
            currentPageSize:function(){return pageSize;},
            currentPage:function(){return Math.floor(this.currentIndex()/this.currentPageSize());},
            totalPages:function(){return 0;},
            moveToPage:function(pageNumber){
                if (pageNumber>=this.totalPages()){
                    throw 'Unable to move to Page that exceeds current total pages.';
                }else{
                    this.currentIndex=function(){return pageNumber*this.currentPageSize();};
                    this.reload();
                }
            },
            moveToNextPage:function(){
                if(Math.floor(this.currentIndex()/this.currentPageSize())+1<this.totalPages()){
                    this.moveToPage(Math.floor(this.currentIndex()/this.currentPageSize())+1);
                }else{
                    throw 'Unable to move to next Page as that will excess current total pages.';
                }
            },
            moveToPreviousPage:function(){
                if(Math.floor(this.currentIndex()/this.currentPageSize())-1>=0){
                    this.moveToPage(Math.floor(this.currentIndex()/this.currentPageSize())-1);
                }else{
                    throw 'Unable to move to previous Page as that will be before the first page.';
                }
            },
            changePageSize:function(size){
                this.currentPageSize = function(){ return size;};
                this.reload();
            },");
                    }
                    else
                    {
                        builder.AppendLine(@"){
        var ret = $.extend([],{");
                    }
                    builder.Append(string.Format("url:function(){{ return {0};}},", url));
                    builder.Append(Constants._LIST_EVENTS_CODE);
                    builder.Append(Constants._LIST_RELOAD_CODE.Replace("$url$", "this.url()").Replace("$type$", modelType.Name));

                    /*builder.AppendLine(@"reload:function(){
                     * var tmp = this;
                     * var response = $.ajax({
                     * type:'GET',
                     * url:this.url(),
                     * dataType:'text',
                     * async:false,
                     * cache:false
                     * }).fail(function(jqXHR,testStatus,errorThrown){
                     * throw errorThrown;
                     * }).done(function(data,textStatus,jqXHR){
                     * if (jqXHR.status==200){
                     *  data = JSON.parse(data);
                     *  while(tmp.length>0){ret.pop();}");
                     * if (mlm.Paged)
                     *  builder.AppendLine("tmp.totalPages=function(){return data.TotalPages;};");
                     * builder.AppendLine(string.Format(@"                 if (data{2}!=null){{
                     *      for(var x=0;x<data{2}.length;x++){{
                     *          tmp.push({1}['{0}'](data{2}[x],new App.Models.{0}()));
                     *      }}
                     *  }}
                     *  for(var x=0;x<tmp.length;x++){{
                     *      tmp[x].$on('{4}',function(model){{
                     *          tmp.reload();
                     *      }});
                     *      tmp[x].$on('{5}',function(model){{
                     *          for(var x=0;x<tmp.length;x++){{
                     *              if (tmp[x].id()==model.id()){{
                     *                  Vue.set(tmp,x,model);
                     *                  break;
                     *              }}
                     *          }}
                     *      }});
                     *      tmp[x].$on('{6}',function(model){{
                     *          for(var x=0;x<tmp.length;x++){{
                     *              if (tmp[x].id()==model.id()){{
                     *                  Vue.set(tmp,x,model);
                     *                  break;
                     *              }}
                     *          }}
                     *      }});
                     *  }}
                     * }}else{{
                     *  throw data;
                     * }}
                     * }});
                     * }},
                     * url:function(){{ return {3};}}", new object[] {
                     *  modelType.Name,
                     *  Constants.PARSERS_VARIABLE,
                     *  (mlm.Paged ? ".response" : ""),
                     *  url,
                     *  Constants.Events.MODEL_DESTROYED,
                     *  Constants.Events.MODEL_UPDATED,
                     *  Constants.Events.MODEL_LOADED
                     * }));*/
                    if ((mlm.Paged && pars.Length > 3) || (!mlm.Paged && pars.Length > 0))
                    {
                        builder.AppendLine(@",
            currentParameters:function(){
                return {");
                        for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                        {
                            builder.AppendLine(string.Format("              {0}:{0}{1}", new object[] { pars[x].Name, (x + 1 == (mlm.Paged ? pars.Length - 3 : pars.Length) ? "" : ",") }));
                        }
                        builder.AppendLine(@"                };
            },");
                        builder.Append("            changeParameters:function(");
                        for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                        {
                            builder.Append((x > 0 ? "," : "") + pars[x].Name);
                        }
                        builder.AppendLine(string.Format(@"){{
                this.url=function(){{ return {0};}};
                this.currentParameters=function(){{
                    return {{", url));
                        for (int x = 0; x < (mlm.Paged ? pars.Length - 3 : pars.Length); x++)
                        {
                            builder.AppendLine(string.Format("              {0}:{0}{1}", new object[] { pars[x].Name, (x + 1 == (mlm.Paged ? pars.Length - 3 : pars.Length) ? "" : ",") }));
                        }
                        builder.AppendLine(@"                   };
                };
                this.reload();
            }");
                    }
                    builder.AppendLine(@"        });
        ret.reload(false);
        return ret;
    }
});");
                }
            }
        }
Exemple #6
0
            public sModelListCall(ModelListMethod mlm, MethodInfo mi)
            {
                _isPaged = mlm.Paged;
                string reg = "";

                _groupIndexes = null;
                if (mi.GetParameters().Length > 0)
                {
                    ParameterInfo[] pars   = mi.GetParameters();
                    string[]        regexs = new string[pars.Length];
                    reg = (mlm.Path + (mlm.Paged ? (mlm.Path.Contains("?") ? "&" : "?") + "PageStartIndex={" + (regexs.Length - 3).ToString() + "}&PageSize={" + (regexs.Length - 2).ToString() + "}" : "")).Replace("?", "\\?");
                    for (int x = 0; x < pars.Length; x++)
                    {
                        Type ptype    = pars[x].ParameterType;
                        bool nullable = false;
                        regexs[x] = "(.+)";
                        if (ptype.FullName.StartsWith("System.Nullable"))
                        {
                            nullable = true;
                            if (ptype.IsGenericType)
                            {
                                ptype = ptype.GetGenericArguments()[0];
                            }
                            else
                            {
                                ptype = ptype.GetElementType();
                            }
                        }
                        if (ptype == typeof(DateTime))
                        {
                            regexs[x] = "(\\d+" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(int) ||
                                 ptype == typeof(long) ||
                                 ptype == typeof(short) ||
                                 ptype == typeof(byte))
                        {
                            regexs[x] = "(-?\\d+" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(uint) ||
                                 ptype == typeof(ulong) ||
                                 ptype == typeof(ushort))
                        {
                            regexs[x] = "(-?\\d+" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(double) ||
                                 ptype == typeof(decimal) ||
                                 ptype == typeof(float))
                        {
                            regexs[x] = "(-?\\d+(.\\d+)?" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype == typeof(bool))
                        {
                            regexs[x] = "(true|false" + (nullable ? "|NULL" : "") + ")";
                        }
                        else if (ptype.IsEnum)
                        {
                            regexs[x] = "(";
                            foreach (string str in Enum.GetNames(ptype))
                            {
                                regexs[x] += str + "|";
                            }
                            regexs[x] = regexs[x].Substring(0, regexs[x].Length - 1) + (nullable ? "|NULL" : "") + ")";
                        }
                    }
                    _groupIndexes = new int[(mlm.Paged ? pars.Length - 1 : pars.Length)];
                    MatchCollection matches = _regParameter.Matches(reg);
                    for (int x = 0; x < matches.Count; x++)
                    {
                        _groupIndexes[int.Parse(matches[x].Groups[1].Value)] = x;
                    }
                    reg = string.Format(reg, regexs);
                    reg = (reg.StartsWith("/") ? reg : "/" + reg).TrimEnd('/');
                }
                else
                {
                    reg = mlm.Path.Replace("?", "\\?");
                }
                _reg    = new Regex(reg, RegexOptions.Compiled | RegexOptions.ECMAScript);
                _method = mi;
            }