public List<FirmInstitutionOptionAttributeValue> GetSetAttributeValuesByOptionAttr(FIDetailModel model, OptionAttribute optAttribute)
        {
            List<FirmInstitutionOptionAttributeValue> retVal = new List<FirmInstitutionOptionAttributeValue>();

            string idPrefix = string.Empty;
            if (optAttribute.Datatype.ToLower().Equals("string") || optAttribute.Datatype.ToLower().Equals("int")) idPrefix = "txtAttr_{0}_{1}";
            else if (optAttribute.Datatype.ToLower().Equals("datetime")) idPrefix = "dtAttr_{0}_{1}";
            else if (optAttribute.Datatype.ToLower().Equals("multiline")) idPrefix = "txtAttr_{0}_{1}";
            else if
                (optAttribute.Datatype.ToLower().Equals("institution")) idPrefix = "txtAttr_{0}_{1}";

            string postDataName = string.Format(idPrefix, optAttribute.idOption, optAttribute.idOptionAttribute);
            if (!string.IsNullOrEmpty(Request.Params[postDataName]))
            {
                //string dataValue = Request.Params[postDataName];

                if (optAttribute.Datatype.ToLower().Equals("institution"))
                {
                    if (!string.IsNullOrEmpty(Request.Params[postDataName]))
                    {
                        string dataValue = string.Empty;
                        string[] vals = Request.Params[postDataName].Split(',');
                        string finals = string.Empty;

                        foreach (var v in vals)
                        {
                            if (!finals.Contains(v))
                                finals = finals + "," + v;
                        }

                        FirmInstitutionOptionAttributeValue optAttrVal = new FirmInstitutionOptionAttributeValue();
                        optAttrVal.idFirmInstitution = model.FirmInstitution.idFirmInstitution;
                        optAttrVal.idOptionAttrbiute = optAttribute.idOptionAttribute;
                        optAttrVal.Value = finals.Trim().Trim(',');
                        retVal.Add(optAttrVal);
                    }
                }
                else
                {
                    FirmInstitutionOptionAttributeValue optAttrVal = new FirmInstitutionOptionAttributeValue();
                    optAttrVal.idFirmInstitution = model.FirmInstitution.idFirmInstitution;
                    optAttrVal.idOptionAttrbiute = optAttribute.idOptionAttribute;
                    optAttrVal.Value = Request.Params[postDataName];
                    retVal.Add(optAttrVal);
                }
            }

            return retVal;
        }
        private static void SetOptionToSave(Guid idReport, Upsilab.Data.Model.Option option, OptionAttribute optionAttribute, string value, string fieldType, List<ReportOptionValue> lstOptionValues, UpsilabEntities db)
        {
            if (option == null && optionAttribute == null)
            {
                return; //ce cas ne doit pas se présenter
            }

            string action = string.Empty;

            ReportOptionValue optionValue = null;
            ReportOptionAttributeValue optionAttributeValue = null;

            //1- Champ
            #region TXT
            if (fieldType == "txt")
            {
                //Do nothing if value is null. Don't check empty because it's a value :) ?
                if (value == null)
                {
                    return;
                }

                optionValue = lstOptionValues.Where(rov => rov.idOption == optionAttribute.idOption).FirstOrDefault();

                if (optionValue == null)
                {
                    action = "I";
                }
                else
                {
                    optionAttributeValue = optionValue.ReportOptionAttributeValue.Where(roav => roav.idOptionAttribute == optionAttribute.idOptionAttribute).FirstOrDefault();
                    if (optionAttributeValue == null)
                    {
                        action = "I";
                    }
                    else
                    {
                        //No need to add if the same value
                        if (optionAttributeValue.Value != value)
                        {
                            optionAttributeValue.Value = value;
                            //optionAttributeValue.DateUpdated = DateTime.Now; TODO

                            //TODO
                            //db.ReportOptionAttributeValue.Attach(optionAttributeValue);
                            //db.ObjectStateManager.ChangeObjectState(optionAttributeValue, System.Data.EntityState.Modified);
                            //db.SaveChanges();
                        }
                    }
                }

                if (action == "I")
                {
                    //Check if reportOptionValue exists
                    if (optionValue == null) //insert option value
                    {
                        optionValue = new ReportOptionValue()
                        {
                            idReport = idReport,
                            idOption = option.idOption,
                            Value = string.Empty,
                            DateCreated = DateTime.Now,
                        };

                        //Add ReportOptionValue to DB : don't save yet
                        lstOptionValues.Add(optionValue);

                        db.ReportOptionValue.Attach(optionValue);
                        db.ObjectStateManager.ChangeObjectState(optionValue, System.Data.EntityState.Added);

                        //Insert parent ??
                        List<ReportOptionValue> lstParents = GetParentOptionToAdd(idReport, lstOptionValues, option);
                        foreach (var optionParentValue in lstParents)
                        {
                            lstOptionValues.Add(optionParentValue);

                            //db.ReportOptionValue.Attach(optionParentValue);
                            //db.ObjectStateManager.ChangeObjectState(optionValue, System.Data.EntityState.Added);

                            db.ReportOptionValue.AddObject(optionParentValue);
                        }
                    }

                    //Insert optionattrubutevalue
                    ReportOptionAttributeValue newReportOptionAttributeValue = new ReportOptionAttributeValue()
                    {
                        idReport = idReport,
                        idOptionAttribute = optionAttribute.idOptionAttribute,
                        idReportOptionValue = optionValue.idReportOptionValue,
                        Value = value,
                        DateCreated = DateTime.Now,
                    };

                    //Add ReportOptionAttributeValue to DB : don't save yet
                    optionValue.ReportOptionAttributeValue.Add(newReportOptionAttributeValue);
                }
            }
            #endregion

            //2- Radio
            #region RB
            //2- Save option attribute value (Radio button)
            else if (fieldType == "rb")
            {
                //No need to delete if it's the option
                optionValue = lstOptionValues.Where(rov => rov.idOption == option.idOption).FirstOrDefault();
                if (optionValue == null)
                {
                    //Delete old rb
                    IList<ReportOptionValue> optionValues = (from rov in lstOptionValues
                                                             where rov.Option.idParent == option.idParent
                                                             select rov).ToList();

                    //Delete option attribute values attached to option value
                    if (optionValues != null)
                    {
                        foreach (var itemOptionValue in optionValues)
                        {
                            IList<ReportOptionAttributeValue> lstOptionAttributeValues = new List<ReportOptionAttributeValue>(itemOptionValue.ReportOptionAttributeValue);
                            foreach (var item in lstOptionAttributeValues)
                            {
                                var optionAttrValueToBeDel = itemOptionValue.ReportOptionAttributeValue.Where(oa => oa.idReportOptionAttributeValue == item.idReportOptionAttributeValue).FirstOrDefault();

                                db.ReportOptionAttributeValue.DeleteObject(optionAttrValueToBeDel);
                            }

                            lstOptionValues.Remove(itemOptionValue);

                            db.ReportOptionValue.DeleteObject(itemOptionValue);

                            //Delete child option value
                            IList<ReportOptionValue> lstOptionValuesChild = (from rov in lstOptionValues
                                                                             where rov.Option.idParent == itemOptionValue.idOption
                                                                             select rov).ToList();

                            if (lstOptionValuesChild != null)
                            {
                                foreach (var optionValueChild in lstOptionValuesChild)
                                {
                                    lstOptionAttributeValues = new List<ReportOptionAttributeValue>(optionValueChild.ReportOptionAttributeValue);
                                    foreach (var item in lstOptionAttributeValues)
                                    {
                                        var optionAttrValueToBeDel = optionValueChild.ReportOptionAttributeValue.Where(oa => oa.idReportOptionAttributeValue == item.idReportOptionAttributeValue).FirstOrDefault();

                                        db.ReportOptionAttributeValue.DeleteObject(optionAttrValueToBeDel);
                                    }

                                    lstOptionValues.Remove(optionValueChild);

                                    db.ReportOptionValue.DeleteObject(optionValueChild);
                                }
                            }
                        }

                    }

                    //Insert new rb
                    ReportOptionValue newOptionValue = new ReportOptionValue()
                    {
                        idReport = idReport,
                        idOption = option.idOption,
                        Value = string.Empty,
                        DateCreated = DateTime.Now,
                    };

                    //Add ReportOptionValue to DB : don't save yet
                    lstOptionValues.Add(newOptionValue);

                    db.ReportOptionValue.AddObject(newOptionValue);

                    //Insert parent
                    List<ReportOptionValue> lstParents = GetParentOptionToAdd(idReport, lstOptionValues, option);
                    foreach (var optionParentValue in lstParents)
                    {
                        lstOptionValues.Add(optionParentValue);

                        db.ReportOptionValue.AddObject(optionParentValue);
                    }
                }
            }
            #endregion

        }
        public List<ReportOptionAttributeValue> RetrieveCustomDataTypeValues(OptionAttribute _optionAttribute, Guid _idReport)
        {
            List<ReportOptionAttributeValue> retVal = new List<ReportOptionAttributeValue>();

            //List<string> customAttribute = new List<string>();
            List<string> customAttribute = OptionBL.GetCustomAttributeNames(_optionAttribute.Datatype);

            if (customAttribute.Count > 0)
            {
                string param_Prefix = string.Format("{0}_{1}_{2}", _optionAttribute.Datatype.ToLower(), _optionAttribute.idOption, _optionAttribute.idOptionAttribute);
                var paramList = Request.Params.AllKeys.Where(s => s.StartsWith(param_Prefix));

                //int entryCount = paramList.Count() / customAttribute.Count; TODO : For knowledgelevet, this condition doesn't work                 
                int entryCount = paramList.Count();

                int entryCountFinal = paramList.Where(x => x.Contains(customAttribute[0])).Count();
                if (entryCountFinal > 0)
                {
                    entryCount = entryCountFinal;
                }

                for (int i = 0; i < entryCount; i++)
                {
                    dynamic _customData = new Upsilab.Data.Model.CustomObject();
                    foreach (string _attr in customAttribute)
                    {
                        string _strVal = string.Empty;
                        string param = string.Format("{0}_{1}", param_Prefix, _attr);
                        if (_optionAttribute.SingleOption.HasValue && !_optionAttribute.SingleOption.Value)
                        {
                            param = string.Format("{0}_{1}_{2}", param_Prefix, i, _attr);
                        }
                        if (!string.IsNullOrEmpty(Request.Params[param]))
                        {
                            _strVal = Request.Params[param];
                        }
                        CustomObject.SetValueByPropertyName(_customData, _attr, _strVal);
                    }
                    ReportOptionAttributeValue newVal = new ReportOptionAttributeValue();
                    newVal.idOptionAttribute = _optionAttribute.idOptionAttribute;
                    newVal.idReport = _idReport;
                    newVal.CustomObjectValue = _customData;
                    retVal.Add(newVal);
                }
            }

            return retVal;
        }