Beispiel #1
0
        /// <summary>
        /// Bulig error message
        /// </summary>
        /// <param name="res"></param>
        /// <param name="validator"></param>
        /// <param name="obj"></param>
        /// <param name="param"></param>
        /// <param name="first_message"></param>
        /// <returns></returns>
        public static ObjectResultData BuildErrorMessage(ObjectResultData res, ValidatorUtil validator, object[] obj = null, MessageParameter param = null, bool first_message = true)
        {
            try
            {
                if (obj != null)
                {
                    foreach (object o in obj)
                    {
                        if (o != null)
                        {
                            validator.Validate(o);
                            validator.ValidateAtLeast1Field(o);
                        }
                    }
                }
                if (validator.IsValid == false)
                {
                    List <ValidateMessage> msgLst = validator.ConvertToMessage();
                    foreach (ValidateMessage msg in msgLst)
                    {
                        string[] pm = null;
                        if (param != null)
                        {
                            pm = param.GetParameter(msg.Code);
                        }
                        else if (msg.Params != null)
                        {
                            pm = new string[] { CommonUtil.TextList(msg.Params.ToArray()) }
                        }
                        ;

                        MessageUtil.MessageList msgCode;
                        if (Enum.TryParse <MessageUtil.MessageList>(msg.Code, out msgCode))
                        {
                            res.AddErrorMessage(msg.Controller, msg.Screen, msg.Module, msgCode, pm, msg.Controls == null ? null : msg.Controls.ToArray());
                        }

                        if (first_message == true)
                        {
                            break;
                        }
                    }
                }

                return(res);
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Mapping text from resource
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="screen"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        private string[] ConvertParameters(string controller, string screen, params string[] param)
        {
            List <string> nparam = new List <string>();

            if (param != null)
            {
                string lang = CommonUtil.GetCurrentLanguage();
                if (lang == ConstantValue.CommonValue.DEFAULT_LANGUAGE_EN)
                {
                    lang = string.Empty;
                }
                else
                {
                    lang = "." + lang;
                }

                string resourcePath = string.Format("{0}{1}\\{2}\\{3}{4}.resx",
                                                    CommonUtil.WebPath,
                                                    ConstantValue.CommonValue.APP_GLOBAL_RESOURCE_FOLDER,
                                                    controller,
                                                    screen,
                                                    lang);
                XmlDocument rDoc = new XmlDocument();
                rDoc.Load(resourcePath);

                foreach (string p in param)
                {
                    bool          isMappingResource = false;
                    List <string> slst = new List <string>();
                    string[]      lp   = p.Split(",".ToCharArray());
                    foreach (string l in lp)
                    {
                        XmlNode rNode = rDoc.SelectSingleNode(string.Format("root/data[@name=\"{0}\"]/value", l.Trim()));
                        if (rNode != null)
                        {
                            slst.Add(rNode.InnerText);
                            isMappingResource = true;
                        }
                        else
                        {
                            slst.Add(l);
                        }
                    }

                    string txt = CommonUtil.TextList(slst.ToArray());
                    if (isMappingResource == false && lp.Length > 1)
                    {
                        //Check is Number
                        decimal dec = 0;
                        if (decimal.TryParse(p, out dec))
                        {
                            txt = p;
                        }
                    }

                    nparam.Add(txt);
                }
            }

            return(nparam.ToArray());
        }
Beispiel #3
0
        private void ValidateAtLeast1Field(object obj)
        {
            try
            {
                List <ValidateMessage> msgLst = new List <ValidateMessage>();
                Dictionary <string, AtLeast1FieldNotNullOrEmptyAttribute> langAttr =
                    CommonUtil.CreateAttributeDictionary <AtLeast1FieldNotNullOrEmptyAttribute>(obj);
                foreach (KeyValuePair <string, AtLeast1FieldNotNullOrEmptyAttribute> attr in langAttr)
                {
                    ValidateMessage nm = null;
                    foreach (ValidateMessage m in msgLst)
                    {
                        if (m.Controller == attr.Value.Controller &&
                            m.Screen == attr.Value.Screen &&
                            m.Module == attr.Value.Module &&
                            m.Code == attr.Value.MessageCode.ToString())
                        {
                            nm = m;
                            break;
                        }
                    }
                    if (nm == null)
                    {
                        nm = new ValidateMessage()
                        {
                            Controller = attr.Value.Controller,
                            Screen     = attr.Value.Screen,
                            Module     = attr.Value.Module,
                            Code       = attr.Value.MessageCode.ToString(),

                            HasNullValue = true
                        };
                        msgLst.Add(nm);
                    }
                    if (nm.HasNullValue == false)
                    {
                        continue;
                    }

                    PropertyInfo prop = obj.GetType().GetProperty(attr.Key);
                    if (prop != null)
                    {
                        object val = prop.GetValue(obj, null);
                        if (CommonUtil.IsNullOrEmpty(val) == false)
                        {
                            nm.HasNullValue = false;
                            nm.ClearParameter();
                        }
                        else if (attr.Value.UseControl == true)
                        {
                            nm.SetParameter(0, CommonUtil.IsNullOrEmpty(attr.Value.ControlName) ? attr.Key : attr.Value.ControlName);
                        }
                    }
                }
                if (msgLst.Count > 0)
                {
                    foreach (ValidateMessage msg in msgLst)
                    {
                        if (msg.HasNullValue == true)
                        {
                            MessageUtil.MessageList msgCode;
                            if (Enum.TryParse <MessageUtil.MessageList>(msg.Code, out msgCode))
                            {
                                string template = CommonUtil.TextList(msg.Params == null? null : msg.Params.ToArray());
                                this.AddErrorMessage(msg.Controller, msg.Screen, msg.Module, msgCode, obj.GetHashCode().ToString(), "", template);
                                this.IsValid = false;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }