/* I M P O R T C S V */ /*---------------------------------------------------------------------------- * %%Function: ImportCsv * %%Qualified: RwpSvc.Practice:Teams.ImportCsv * %%Contact: rlittle * * ----------------------------------------------------------------------------*/ public static RSR ImportCsv(Stream stm) { RSR sr; Sql sql; CsvTeams csv = new CsvTeams(); Random rnd = new Random(System.Environment.TickCount); TextReader tr = new StreamReader(stm); // process each line sr = RSR.FromSR(Sql.OpenConnection(out sql, Startup._sResourceConnString)); if (!sr.Result) { return(sr); } sr = RSR.FromSR(sql.BeginTransaction()); if (!sr.Result) { return(sr); } bool fHeadingRead = false; string sLine; int iLine = 0; RwpTeam rwpt; bool fAdd; List <string> plsDiff; try { while ((sLine = tr.ReadLine()) != null) { iLine++; if (sLine.Length < 2) { continue; } if (!fHeadingRead) { sr = csv.ReadHeaderFromString(sLine); if (!sr.Result) { throw new Exception(String.Format("Failed to make heading at line {0}: {1}", iLine - 1, sr.Reason)); } fHeadingRead = true; continue; } sr = csv.LoadRwptFromCsv(sLine, sql, out rwpt, out fAdd, out plsDiff); if (!sr.Result) { throw new Exception(String.Format("Failed to process line {0}: {1}", iLine - 1, sr.Reason)); } if (rwpt.Name == "") { continue; } // at this point, rwpt is a fully loaded team; check for errors and generate a passowrd if necessary bool fTeamExists = false; bool fAuthExists = false; sr = rwpt.Preflight(sql, out fTeamExists, out fAuthExists); if (!sr.Result) { throw new Exception(String.Format("Failed to preflight line {0}: {1}", iLine - 1, sr.Reason)); } if (!fTeamExists) { if (rwpt.Created == null) { rwpt.Created = DateTime.Now; } if (rwpt.Updated == null) { rwpt.Updated = rwpt.Created; } if (rwpt.ReleasedCagesDate == null) { rwpt.ReleasedCagesDate = DateTime.Parse("1/1/2013"); } if (rwpt.ReleasedFieldsDate == null) { rwpt.ReleasedFieldsDate = DateTime.Parse("1/1/2013"); } // at this point, we would insert... string sInsert = rwpt.SGenerateUpdateQuery(sql, fAdd); Sql.ExecuteNonQuery(sql, sInsert, null); } // now, add to the auth table (again checking to see if this already exists) if (!fAuthExists) { InsertAuthUser(rwpt.Identity, rwpt.Name, Guid.Parse(rwpt.Tenant), sql); } } } catch (Exception e) { sql.Rollback(); sql.Close(); return(RSR.Failed(e)); } sql.Commit(); sql.Close(); return(RSR.Success()); }
/* I M P O R T C S V */ /*---------------------------------------------------------------------------- %%Function: ImportCsv %%Qualified: RwpSvc.Practice:Teams.ImportCsv %%Contact: rlittle ----------------------------------------------------------------------------*/ public static RSR ImportCsv(Stream stm) { RSR sr; Sql sql; CsvTeams csv = new CsvTeams(); Random rnd = new Random(System.Environment.TickCount); TextReader tr = new StreamReader(stm); // process each line sr = RSR.FromSR(Sql.OpenConnection(out sql, _sResourceConnString)); if (!sr.Result) return sr; sr = RSR.FromSR(sql.BeginTransaction()); if (!sr.Result) return sr; bool fHeadingRead = false; string sLine; int iLine = 0; RwpTeam rwpt; bool fAdd; List<string> plsDiff; try { while ((sLine = tr.ReadLine()) != null) { iLine++; if (sLine.Length < 2) continue; if (!fHeadingRead) { sr = csv.ReadHeaderFromString(sLine); if (!sr.Result) throw new Exception(String.Format("Failed to make heading at line {0}: {1}", iLine - 1, sr.Reason)); fHeadingRead = true; continue; } sr = csv.LoadRwptFromCsv(sLine, sql, out rwpt, out fAdd, out plsDiff); if (!sr.Result) throw new Exception(String.Format("Failed to process line {0}: {1}", iLine - 1, sr.Reason)); if (rwpt.Name == "") continue; // at this point, rwpt is a fully loaded team; check for errors and generate a passowrd if necessary sr = rwpt.Preflight(sql); if (!sr.Result) throw new Exception(String.Format("Failed to preflight line {0}: {1}", iLine - 1, sr.Reason)); if (string.IsNullOrEmpty(rwpt.Password)) rwpt.Password = RwpTeam.SGenRandomPassword(rnd, rwpt.Name); if (rwpt.Created == null) rwpt.Created = DateTime.Now; if (rwpt.Updated == null) rwpt.Updated =rwpt.Created; if (rwpt.ReleasedCagesDate == null) rwpt.ReleasedCagesDate = DateTime.Parse("1/1/2013"); if (rwpt.ReleasedFieldsDate == null) rwpt.ReleasedFieldsDate = DateTime.Parse("1/1/2013"); // at this point, we would insert... string sInsert = rwpt.SGenerateUpdateQuery(sql, fAdd); SqlCommand sqlcmd = sql.CreateCommand(); sqlcmd.CommandText = sInsert; sqlcmd.Transaction = sql.Transaction; sqlcmd.ExecuteNonQuery(); } } catch (Exception e) { sql.Rollback(); sql.Close(); return RSR.Failed(e); } sql.Commit(); sql.Close(); return RSR.Success(); }
/* I M P O R T C S V */ /*---------------------------------------------------------------------------- * %%Function: ImportCsv * %%Qualified: RwpSvc.Practice:Teams.ImportCsv * %%Contact: rlittle * * ----------------------------------------------------------------------------*/ public static RSR ImportCsv(Stream stm) { RSR sr; Sql sql; CsvTeams csv = new CsvTeams(); Random rnd = new Random(System.Environment.TickCount); TextReader tr = new StreamReader(stm); // process each line sr = RSR.FromSR(Sql.OpenConnection(out sql, _sResourceConnString)); if (!sr.Result) { return(sr); } sr = RSR.FromSR(sql.BeginTransaction()); if (!sr.Result) { return(sr); } bool fHeadingRead = false; string sLine; int iLine = 0; RwpTeam rwpt; bool fAdd; List <string> plsDiff; try { while ((sLine = tr.ReadLine()) != null) { iLine++; if (sLine.Length < 2) { continue; } if (!fHeadingRead) { sr = csv.ReadHeaderFromString(sLine); if (!sr.Result) { throw new Exception(String.Format("Failed to make heading at line {0}: {1}", iLine - 1, sr.Reason)); } fHeadingRead = true; continue; } sr = csv.LoadRwptFromCsv(sLine, sql, out rwpt, out fAdd, out plsDiff); if (!sr.Result) { throw new Exception(String.Format("Failed to process line {0}: {1}", iLine - 1, sr.Reason)); } if (rwpt.Name == "") { continue; } // at this point, rwpt is a fully loaded team; check for errors and generate a passowrd if necessary sr = rwpt.Preflight(sql); if (!sr.Result) { throw new Exception(String.Format("Failed to preflight line {0}: {1}", iLine - 1, sr.Reason)); } if (string.IsNullOrEmpty(rwpt.Password)) { rwpt.Password = RwpTeam.SGenRandomPassword(rnd, rwpt.Name); } if (rwpt.Created == null) { rwpt.Created = DateTime.Now; } if (rwpt.Updated == null) { rwpt.Updated = rwpt.Created; } if (rwpt.ReleasedCagesDate == null) { rwpt.ReleasedCagesDate = DateTime.Parse("1/1/2013"); } if (rwpt.ReleasedFieldsDate == null) { rwpt.ReleasedFieldsDate = DateTime.Parse("1/1/2013"); } // at this point, we would insert... string sInsert = rwpt.SGenerateUpdateQuery(sql, fAdd); SqlCommand sqlcmd = sql.CreateCommand(); sqlcmd.CommandText = sInsert; sqlcmd.Transaction = sql.Transaction; sqlcmd.ExecuteNonQuery(); } } catch (Exception e) { sql.Rollback(); sql.Close(); return(RSR.Failed(e)); } sql.Commit(); sql.Close(); return(RSR.Success()); }