protected void convertVariable(DbVariable p, VariableDesc pp)
 {
     pp.Id = p.Id;
     pp.DataSetId = p.DataSetId;
     pp.DataType = p.VarType;
     pp.Name = p.Name;
     pp.IsCategVar = p.IsCateg;
     pp.IsIdVar = p.IsId;
     pp.IsNameVar = p.IsName;
     pp.IsImageVar = p.IsImageVar;
     pp.IsInfoVar = p.IsInfoVar;
     pp.Description = p.Description;
     pp.IsModified = false;
 }
 protected DbVariable maintainsVariable(statdataEntities ctx, VariableDesc oVar)
 {
     DbVariable pRet = null;
     if (oVar != null)
     {
         String sname = oVar.Name.Trim();
         int nId = oVar.DataSetId;
         String stype = oVar.DataType.Trim();
         if (String.IsNullOrEmpty(sname) || (nId == 0) || String.IsNullOrEmpty(stype))
         {
             return null;
         }
         pRet = findVariable(ctx, oVar);
         if (pRet != null)
         {
             pRet.Name = sname;
             pRet.VarType = stype;
             pRet.IsCateg = oVar.IsCategVar;
             pRet.IsId = oVar.IsIdVar;
             pRet.IsName = oVar.IsNameVar;
             pRet.IsImageVar = oVar.IsImageVar;
             pRet.IsInfoVar = oVar.IsInfoVar;
             pRet.Description = oVar.Description;
         }
         else
         {
             DbDataSet pSet = null;
             {
                 var qq = from x in ctx.DbDataSets where x.Id == nId select x;
                 if (qq.Count() < 1)
                 {
                     return null;
                 }
                 pSet = qq.First();
             }
             pRet = new DbVariable();
             pRet.Id = nextId(ctx, TAB_VARIABLE);
             pRet.DataSet = pSet;
             pRet.Name = sname;
             pRet.VarType = stype;
             pRet.IsCateg = oVar.IsCategVar;
             pRet.IsId = oVar.IsIdVar;
             pRet.IsName = oVar.IsNameVar;
             pRet.IsImageVar = oVar.IsImageVar;
             pRet.IsInfoVar = oVar.IsInfoVar;
             pRet.Description = oVar.Description;
             ctx.DbVariables.Add(pRet);
         }
     }
     return pRet;
 }
 public Tuple<bool, Exception> ReplaceDataSet(StatDataSet oSet, CancellationToken cancelletionToken, IProgress<int> progress)
 {
     if (cancelletionToken.IsCancellationRequested)
     {
         return new Tuple<bool, Exception>(false, null);
     }
     if (oSet == null)
     {
         return new Tuple<bool, Exception>(false, new ArgumentNullException());
     }
     String sRealDataSetName = oSet.Name;
     if (String.IsNullOrEmpty(sRealDataSetName))
     {
         return new Tuple<bool, Exception>(false, new ArgumentException());
     }
     String tempName = String.Format("{0}_tmpwork", sRealDataSetName);
     if (progress != null)
     {
         progress.Report(1);
     }
     int nTotalValues = 0;
     int nTotalVars = 0;
     foreach (var v in oSet.Variables)
     {
         ++nTotalVars;
         nTotalValues += v.Values.Count();
     }// v
     if ((nTotalVars < 1) || (nTotalValues < 1))
     {
         return new Tuple<bool, Exception>(false, new ArgumentNullException());
     }
     if (cancelletionToken.IsCancellationRequested)
     {
         return new Tuple<bool, Exception>(false, null);
     }
     double dTotal = (double)(2 * nTotalVars + 5);
     double dCur = 1;
     bool bRet = false;
     Exception err = null;
     bool bCont = true;
     try
     {
         using (var ctx = getContext())
         {
             DbDataSet pSet = null;
             var qq = from x in ctx.DbDataSets where x.Name.Trim().ToLower() == tempName.Trim().ToLower() select x;
             if (qq.Count() > 0)
             {
                 pSet = qq.First();
             }
             if (pSet != null)
             {
                 ctx.DbDataSets.Remove(pSet);
                 ctx.SaveChanges();
                 pSet = null;
             }// pSet
             if (progress != null)
             {
                 dCur = dCur + 1.0;
                 int nx = (int)((dCur / dTotal) * 100.0 + 0.5);
                 progress.Report(nx);
             }
             int nDataSetId = nextId(ctx, TAB_DATASET);
             int[] varIndexes = nextIds(ctx, TAB_VARIABLE, nTotalVars);
             int[] valIndexes = nextIds(ctx, TAB_VALEUR, nTotalValues);
             pSet = new DbDataSet();
             pSet.Id = nDataSetId;
             pSet.Name = tempName;
             pSet.Description = oSet.Description;
             ctx.DbDataSets.Add(pSet);
             nDataSetId = pSet.Id;
             int iCurrentVar = 0;
             int iCurrentVal = 0;
             HashSet<String> oSetNames = new HashSet<string>();
             foreach (var v in oSet.Variables)
             {
                 if (!bCont)
                 {
                     break;
                 }
                 if (v == null)
                 {
                     continue;
                 }
                 if (cancelletionToken.IsCancellationRequested)
                 {
                     bCont = false;
                     break;
                 }
                 String name = v.Name;
                 String stype = v.DataType;
                 if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(stype))
                 {
                     continue;
                 }
                 if (oSetNames.Contains(name))
                 {
                     continue;
                 }
                 DbVariable vv = new DbVariable();
                 int nVarId = varIndexes[iCurrentVar++];
                 vv.Id = nVarId;
                 vv.DataSetId = pSet.Id;
                 vv.Name = name;
                 vv.VarType = stype;
                 vv.Description = v.Description;
                 vv.IsCateg = v.IsCategVar;
                 vv.IsId = v.IsIdVar;
                 vv.IsImageVar = v.IsImageVar;
                 vv.IsInfoVar = v.IsInfoVar;
                 vv.IsName = v.IsNameVar;
                 ctx.DbVariables.Add(vv);
                 HashSet<int> oSetIndexes = new HashSet<int>();
                 foreach (var vx in v.Values)
                 {
                     if (!bCont)
                     {
                         break;
                     }
                     if (vx == null)
                     {
                         continue;
                     }
                     int ind = vx.Index;
                     if ((ind < 0) || oSetIndexes.Contains(ind))
                     {
                         continue;
                     }
                     oSetIndexes.Add(ind);
                     DbValue vz = new DbValue();
                     vz.Id = valIndexes[iCurrentVal++];
                     vz.VariableId = nVarId;
                     vz.Index = ind;
                     vz.Value = StatHelpers.ConvertValue(vx.DataStringValue);
                     ctx.DbValues.Add(vz);
                 }// vx
                 if (progress != null)
                 {
                     dCur = dCur + 1.0;
                     int nx = (int)((dCur / dTotal) * 100.0 + 0.5);
                     progress.Report(nx);
                 }
             }// v
             if (bCont)
             {
                 ctx.SaveChanges();
                 var qqq = from x in ctx.DbDataSets where x.Name.Trim().ToLower() == tempName.Trim().ToLower() select x;
                 if (qqq.Count() > 0)
                 {
                     pSet = qqq.First();
                     pSet.Name = sRealDataSetName;
                     ctx.SaveChanges();
                     bRet = true;
                 }
             }
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<bool, Exception>(bRet, err);
 }