public Tuple<IEnumerable<VariableDesc>, Exception> GetDataSetVariablesAndData(StatDataSet oSet) { List<VariableDesc> oRet = null; Exception err = null; if (oSet == null) { return new Tuple<IEnumerable<VariableDesc>, Exception>(null, new ArgumentNullException()); } try { using (var ctx = getContext()) { DbDataSet pSet = findDataSet(ctx, oSet); if (pSet != null) { var q = from x in pSet.Variables orderby x.Name ascending select x; oRet = new List<VariableDesc>(); foreach (var p in q) { VariableDesc pp = new VariableDesc(); convertVariable(p, pp); List<ValueDesc> oList = new List<ValueDesc>(); var col = p.Values; foreach (var v in col) { if (v.Index >= 0) { ValueDesc vv = new ValueDesc(); convertValue(v, vv); oList.Add(vv); }// v }// v pp.Values = new ValueDescs(oList); var xx = getVariableInfo(ctx, pp); if (xx.Item2 != null) { return new Tuple<IEnumerable<VariableDesc>, Exception>(oRet, err); } pp.Info = xx.Item1; oRet.Add(pp); }// p }// pSet }// ctx if ((oRet != null) && (oRet.Count > 1)) { oRet.Sort(); } } catch (Exception ex) { err = ex; } return new Tuple<IEnumerable<VariableDesc>, Exception>(oRet, err); }
public Tuple<IEnumerable<ValueDesc>, Exception> GetDataSetIndivValues(StatDataSet oSet, int iIndex) { if (oSet == null) { return new Tuple<IEnumerable<ValueDesc>, Exception>(null, new ArgumentNullException()); } if (iIndex < 0) { return new Tuple<IEnumerable<ValueDesc>, Exception>(null, new ArgumentOutOfRangeException()); } List<ValueDesc> oRet = null; Exception err = null; try { using (var ctx = getContext()) { oRet = new List<ValueDesc>(); var pSet = findDataSet(ctx, oSet); if (pSet != null) { var q = from x in ctx.DbValues where (x.Variable.DataSetId == pSet.Id) && (x.Index == iIndex) select x; foreach (var p in q) { ValueDesc pp = new ValueDesc(); convertValue(p, pp); oRet.Add(pp); }// p } }// ctx } catch (Exception ex) { err = ex; } return new Tuple<IEnumerable<ValueDesc>, Exception>(oRet, err); }
public Tuple<IEnumerable<VariableDesc>, Exception> GetDataSetVariables(StatDataSet oSet) { List<VariableDesc> oRet = null; Exception err = null; if (oSet == null) { return new Tuple<IEnumerable<VariableDesc>, Exception>(null, new ArgumentNullException()); } try { using (var ctx = getContext()) { DbDataSet pSet = findDataSet(ctx, oSet); if (pSet != null) { var q = from x in pSet.Variables orderby x.Name ascending select x; oRet = new List<VariableDesc>(); foreach (var p in q) { VariableDesc pp = new VariableDesc(); convertVariable(p, pp); oRet.Add(pp); }// p }// pSet }// ctx if ((oRet != null) && (oRet.Count > 1)) { oRet.Sort(); } } catch (Exception ex) { err = ex; } return new Tuple<IEnumerable<VariableDesc>, Exception>(oRet, err); }
public Tuple<IndivDescs, VariableDescs, Exception> FetchAllDataSetData(StatDataSet oSet) { IndivDescs indivs = null; VariableDescs vars = null; Exception err = null; try { using (var ctx = getContext()) { DbDataSet pSet = findDataSet(ctx, oSet); if (pSet == null) { return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err); } DbVariable pIdVar = null; DbVariable pNameVar = null; DbVariable pImageVar = null; var q = from x in pSet.Variables orderby x.Name select x; foreach (var v in q) { if (v.IsId) { pIdVar = v; } if (v.IsName) { pNameVar = v; } if (v.IsImageVar) { pImageVar = v; } }// v bool bImageInt = false; if (pImageVar != null) { String s = pImageVar.VarType.Trim().ToLower(); if ((s != "string") && (s != "bool")) { bImageInt = true; } } List<VariableDesc> oRet = new List<VariableDesc>(); HashSet<int> oSetIndex = new HashSet<int>(); List<IndivDesc> listIndivs = new List<IndivDesc>(); Dictionary<int, ValueDescs> vals = new Dictionary<int, ValueDescs>(); foreach (var p in q) { VariableDesc pp = new VariableDesc(); convertVariable(p, pp); var col = p.Values; List<ValueDesc> pList = new List<ValueDesc>(); foreach (var v in col) { int index = v.Index; if (index >= 0) { ValueDesc vv = new ValueDesc(); convertValue(v, vv); pList.Add(vv); if (!vals.ContainsKey(index)) { vals[index] = new ValueDescs(); } (vals[index]).Add(vv); if (!oSetIndex.Contains(index)) { oSetIndex.Add(index); var ind = new IndivDesc(); listIndivs.Add(ind); ind.IndivIndex = index; if (pIdVar != null) { var qx = from x in pIdVar.Values where x.Index == index select x; if (qx.Count() > 0) { ind.IdString = qx.First().Value; } }// Id if (pNameVar != null) { var qx = from x in pNameVar.Values where x.Index == index select x; if (qx.Count() > 0) { ind.Name = qx.First().Value; } }// name if (pImageVar != null) { var qx = from x in pImageVar.Values where x.Index == index select x; if (qx.Count() > 0) { var px = qx.First(); String sx = StatHelpers.ConvertValue(px.Value); if (!String.IsNullOrEmpty(sx)) { if (bImageInt) { double dval = 0.0; if (double.TryParse(sx, out dval)) { int ival = (int)dval; if (ival != 0) { var qz = from x in ctx.DbPhotoes where x.Id == ival select x; if (qz.Count() > 0) { var pz = qz.First(); ind.PhotoId = pz.Id; ind.PhotoData = pz.DataBytes; } }// ival } } else { var qz = from x in ctx.DbPhotoes where x.Name.Trim().ToLower() == sx.ToLower() select x; if (qz.Count() > 0) { var pz = qz.First(); ind.PhotoId = pz.Id; ind.PhotoData = pz.DataBytes; } }// ival }// sx } }// photo }// add value }// v }// v var xx = getVariableInfo(ctx, pp); if (xx.Item2 != null) { return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err); } pp.Info = xx.Item1; pp.Values = new ValueDescs(pList); oRet.Add(pp); }// p foreach (var ind in listIndivs) { int index = ind.IndivIndex; if (vals.ContainsKey(index)) { ind.Values = vals[index]; } }// ind if (listIndivs.Count > 1) { listIndivs.Sort(); } if (oRet.Count > 1) { oRet.Sort(); } indivs = new IndivDescs(listIndivs); vars = new VariableDescs(oRet); }// ctx }// try catch (Exception ex) { err = ex; } return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err); }
public Tuple<IndivDescs, Exception> GetDataSetIndivs(StatDataSet oSet) { if (oSet == null) { return new Tuple<IndivDescs, Exception>(null, new ArgumentNullException()); } IndivDescs oRet = null; Exception err = null; try { using (var ctx = getContext()) { DbDataSet pSet = findDataSet(ctx, oSet); if (pSet == null) { return new Tuple<IndivDescs, Exception>(oRet, err); } DbVariable pIdVar = null; DbVariable pNameVar = null; DbVariable pImageVar = null; var q = from x in pSet.Variables select x; foreach (var v in q) { if (v.IsId) { pIdVar = v; } if (v.IsName) { pNameVar = v; } if (v.IsImageVar) { pImageVar = v; } }// v bool bImageInt = false; if (pImageVar != null) { String s = pImageVar.VarType.Trim().ToLower(); if ((s != "string") && (s != "bool")) { bImageInt = true; } } List<IndivDesc> oList = new List<IndivDesc>(); var indexes = (from x in ctx.DbValues where x.Variable.DataSetId == pSet.Id select x.Index).Distinct(); foreach (var ind in indexes) { IndivDesc pp = new IndivDesc(); pp.IndivIndex = ind; if (pIdVar != null) { var qx = from x in ctx.DbValues where (x.VariableId == pIdVar.Id) && (x.Index == ind) select x; if (qx.Count() > 0) { pp.IdString = qx.First().Value; } }// Id if (pNameVar != null) { var qx = from x in ctx.DbValues where (x.VariableId == pNameVar.Id) && (x.Index == ind) select x; if (qx.Count() > 0) { pp.Name = qx.First().Value; } }// name if (pImageVar != null) { var qx = from x in ctx.DbValues where (x.VariableId == pImageVar.Id) && (x.Index == ind) select x; if (qx.Count() > 0) { var px = qx.First(); String sx = StatHelpers.ConvertValue(px.Value); if (!String.IsNullOrEmpty(sx)) { if (bImageInt) { double dval = 0.0; if (double.TryParse(sx, out dval)) { int ival = (int)dval; if (ival != 0) { var qz = from x in ctx.DbPhotoes where x.Id == ival select x; if (qz.Count() > 0) { var pz = qz.First(); pp.PhotoId = pz.Id; pp.PhotoData = pz.DataBytes; } }// ival } } else { var qz = from x in ctx.DbPhotoes where x.Name.Trim().ToLower() == sx.ToLower() select x; if (qz.Count() > 0) { var pz = qz.First(); pp.PhotoId = pz.Id; pp.PhotoData = pz.DataBytes; } }// ival }// sx } }// photo ValueDescs vv = new ValueDescs(); var qq = from x in ctx.DbValues where (x.Variable.DataSetId == pSet.Id) && (x.Index == ind) select x; foreach (var v in qq) { ValueDesc fx = new ValueDesc(); convertValue(v, fx); vv.Add(fx); }// v pp.Values = vv; oList.Add(pp); }// ind if (oList.Count > 1) { oList.Sort(); } oRet = new IndivDescs(oList); }// ctx }// try catch (Exception ex) { err = ex; } return new Tuple<IndivDescs, Exception>(oRet, err); }
public Tuple<StatDataSet, Exception> GetDataSet(StatDataSet oSet) { StatDataSet oRet = null; Exception err = null; if (oSet == null) { return new Tuple<StatDataSet, Exception>(null, new ArgumentException()); } try { using (var ctx = getContext()) { DbDataSet pSet = this.findDataSet(ctx, oSet); if (pSet != null) { oRet = new StatDataSet(); convertDataSet(pSet, oRet); }// pSet }// ctx } catch (Exception ex) { err = ex; } return new Tuple<StatDataSet, Exception>(oRet, err); }
public Tuple<IEnumerable<int>, Exception> GetDataSetIndexes(StatDataSet oSet) { if (oSet == null) { return new Tuple<IEnumerable<int>, Exception>(null, new ArgumentNullException()); } List<int> oList = null; Exception err = null; try { using (var ctx = getContext()) { var pSet = findDataSet(ctx, oSet); if (pSet != null) { var q = (from x in ctx.DbValues where x.Variable.DataSetId == pSet.Id orderby x.Index ascending select x.Index).Distinct(); oList = q.ToList(); } }// ctx } catch (Exception ex) { err = ex; } return new Tuple<IEnumerable<int>, Exception>(oList, err); }
protected DbDataSet maintainsDataSet(statdataEntities ctx, StatDataSet oSet) { if (oSet == null) { return null; } String sname = oSet.Name.Trim(); if (String.IsNullOrEmpty(sname)) { return null; } DbDataSet pRet = findDataSet(ctx, oSet); if (pRet != null) { pRet.Name = oSet.Name; pRet.Description = oSet.Description; } else { pRet = new DbDataSet(); pRet.Id = nextId(ctx, TAB_DATASET); pRet.LastIndex = 0; pRet.Name = sname; pRet.Description = oSet.Description; ctx.DbDataSets.Add(pRet); ctx.SaveChanges(); } return pRet; }
public Tuple<IEnumerable<StatDataSet>, Exception> GetAllStatDataSets() { List<StatDataSet> oRet = null; Exception err = null; try { using (var ctx = getContext()) { var q = from x in ctx.DbDataSets orderby x.Name ascending select x; oRet = new List<StatDataSet>(); foreach (var p in q) { StatDataSet pp = new StatDataSet(); convertDataSet(p, pp); oRet.Add(pp); }// p }// ctx } catch (Exception ex) { err = ex; } return new Tuple<IEnumerable<StatDataSet>, Exception>(oRet, err); }
protected void convertDataSet(DbDataSet p, StatDataSet pp) { pp.Id = p.Id; pp.LastIndex = p.LastIndex; pp.Name = p.Name; pp.Description = p.Description; pp.IsModified = false; }
protected DbDataSet findDataSet(statdataEntities ctx, StatDataSet oSet) { DbDataSet pRet = null; if (oSet != null) { if (oSet.Id != 0) { var q = from x in ctx.DbDataSets where x.Id == oSet.Id select x; if (q.Count() > 0) { return q.First(); } } String sname = oSet.Name.Trim().ToLower(); if (!String.IsNullOrEmpty(sname)) { var q = from x in ctx.DbDataSets where x.Name.Trim().ToLower() == sname select x; if (q.Count() > 0) { return q.First(); } } }// oSet 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); }
public Tuple<bool, Exception> RemoveDataSet(StatDataSet oSet) { bool bRet = false; Exception err = null; if (oSet == null) { return new Tuple<bool, Exception>(false, new ArgumentNullException()); } try { using (var ctx = getContext()) { DbDataSet pSet = findDataSet(ctx, oSet); if (pSet != null) { ctx.DbDataSets.Remove(pSet); ctx.SaveChanges(); bRet = true; } } } catch (Exception ex) { err = ex; } return new Tuple<bool, Exception>(bRet, err); }
public Tuple<StatDataSet, Exception> MaintainsDataSet(StatDataSet oSet) { StatDataSet oRet = null; Exception err = null; if (oSet == null) { return new Tuple<StatDataSet, Exception>(null, new ArgumentNullException()); } try { using (var ctx = getContext()) { DbDataSet pSet = maintainsDataSet(ctx, oSet); if (pSet != null) { ctx.SaveChanges(); oRet = new StatDataSet(); convertDataSet(pSet, oRet); } }// ctx } catch (Exception ex) { err = ex; } return new Tuple<StatDataSet, Exception>(oRet, err); }
private Task<Tuple<bool, Exception>> exportTSV(IStoreDataManager pMan, StatDataSet oSet, String filename, CancellationToken cancellationToken, IProgress<int> progress) { return Task.Run<Tuple<bool, Exception>>(() => { bool bRet = false; Exception err = null; try { var xx = pMan.GetDataSetIndexes(oSet); if (xx == null) { return new Tuple<bool, Exception>(bRet, err); } var indexes = xx.Item1; if (indexes == null) { return new Tuple<bool, Exception>(bRet, err); } int nMaxIndex = indexes.Max() + 1; int nm1 = nMaxIndex - 1; double dTotal = (double)nMaxIndex; StringBuilder sb = new StringBuilder(); int nCur = 0; var varsCol = oSet.Variables.ToArray(); int nVars = varsCol.Length; for (int i = 0; i < nVars; ++i) { if (i > 0) { sb.Append("\t"); } sb.Append(varsCol[i].Name); }// i sb.Append("\n"); if (progress != null) { int f = (int)((nCur++ / dTotal) * 100.0 + 0.5); progress.Report(f); } for (int irow = 0; irow < nMaxIndex; ++irow) { if (cancellationToken.IsCancellationRequested) { break; } String[] vals = new String[nVars]; for (int i = 0; i < nVars; ++i) { vals[i] = "N/A"; }// i if (indexes.Contains(irow)) { var yy = pMan.GetDataSetIndivValues(oSet, irow); if ((yy != null) && (yy.Item1 != null) && (yy.Item2 == null)) { var xvals = yy.Item1; for (int j = 0; j < nVars; ++j) { int ind = varsCol[j].Id; var qq = from x in xvals where x.VariableId == ind select x; if (qq.Count() > 0) { var z = StatHelpers.ConvertValue(qq.First().DataStringValue); if (!String.IsNullOrEmpty(z)) { vals[j] = z; } }// ok }// j }// ok } for (int i = 0; i < nVars; ++i) { if (i > 0) { sb.Append("\t"); } sb.Append(vals[i]); }// i if (irow < nm1) { sb.Append("\n"); } if (progress != null) { int f = (int)((nCur++ / dTotal) * 100.0 + 0.5); progress.Report(f); } }// irow String s = sb.ToString(); String ss = s.Replace(",", "."); FileStream fs = new FileStream(filename, FileMode.OpenOrCreate); StreamWriter writer = new StreamWriter(fs); writer.Write(ss); writer.Close(); bRet = true; }// try catch (Exception ex) { err = ex; } return new Tuple<bool, Exception>(bRet, err); }, cancellationToken); }
private Task<Tuple<StatDataSet, Exception>> readDataSet(String filename, sds.DataSet input, CancellationToken cancellationToken, IProgress<int> progress) { return Task.Run<Tuple<StatDataSet, Exception>>(() => { StatDataSet oSet = null; Exception err = null; try { FileInfo info = new FileInfo(filename); oSet = new StatDataSet(); oSet.Name = info.Name; var col = input.Variables; int nTotal = col.Count; int nCur = 0; foreach (var v in col) { if (progress != null) { progress.Report(nCur++); } if (v.Dimensions.Count != 1) { continue; } VariableDesc vv = new VariableDesc(); ValueDescs vals = new ValueDescs(); vv.Name = v.Name; Type t = v.TypeOfData; if (t == typeof(String)) { vv.DataType = "string"; vv.IsCategVar = true; String[] data = input.GetData<String[]>(v.ID); if ((data != null) && (data.Length > 0)) { for (int i = 0; i < data.Length; ++i) { ValueDesc vx = new ValueDesc(); vx.Index = i; vx.DataStringValue = data[i]; vals.Add(vx); }// i }// data } else if (t == typeof(double)) { vv.DataType = "double"; vv.IsCategVar = false; double[] data = input.GetData<double[]>(v.ID); if ((data != null) && (data.Length > 0)) { for (int i = 0; i < data.Length; ++i) { ValueDesc vx = new ValueDesc(); vx.Index = i; String s = Convert.ToString(myconvert(data[i])); vx.DataStringValue = s; vals.Add(vx); }// i }// data } else if (t == typeof(float)) { vv.DataType = "float"; vv.IsCategVar = false; float[] data = input.GetData<float[]>(v.ID); if ((data != null) && (data.Length > 0)) { for (int i = 0; i < data.Length; ++i) { ValueDesc vx = new ValueDesc(); vx.Index = i; String s = Convert.ToString(myconvert(data[i])); vx.DataStringValue = s; vals.Add(vx); }// i }// data } else if (t == typeof(int)) { vv.DataType = "int"; vv.IsCategVar = false; int[] data = input.GetData<int[]>(v.ID); if ((data != null) && (data.Length > 0)) { for (int i = 0; i < data.Length; ++i) { ValueDesc vx = new ValueDesc(); vx.Index = i; String s = Convert.ToString(data[i]); vx.DataStringValue = s; vals.Add(vx); }// i }// data } else if (t == typeof(short)) { vv.DataType = "short"; vv.IsCategVar = false; short[] data = input.GetData<short[]>(v.ID); if ((data != null) && (data.Length > 0)) { for (int i = 0; i < data.Length; ++i) { ValueDesc vx = new ValueDesc(); vx.Index = i; String s = Convert.ToString(data[i]); vx.DataStringValue = s; vals.Add(vx); }// i }// data } else if (t == typeof(bool)) { vv.DataType = "bool"; vv.IsCategVar = true; bool[] data = input.GetData<bool[]>(v.ID); if ((data != null) && (data.Length > 0)) { for (int i = 0; i < data.Length; ++i) { ValueDesc vx = new ValueDesc(); vx.Index = i; String s = Convert.ToString(data[i]); vx.DataStringValue = s; vals.Add(vx); }// i }// data } vv.Values = vals; oSet.Variables.Add(vv); }// v } catch (Exception ex) { err = ex; } return new Tuple<StatDataSet, Exception>(oSet, err); }, cancellationToken); }// readDataSet