예제 #1
0
            /* G E T  C S V */

            /*----------------------------------------------------------------------------
            *       %%Function: GetCsv
            *       %%Qualified: RwpSvc.Practice:RwpSlots.GetCsv
            *       %%Contact: rlittle
            *
            *  ----------------------------------------------------------------------------*/
            public SR GetCsv(Stream stm)
            {
                CsvSlots   csvs = new CsvSlots();
                SqlWhere   sw   = new SqlWhere();
                TextWriter tw   = new StreamWriter(stm);

                sw.AddAliases(RwpSlot.s_mpAliases);

                m_plrwps = new List <RwpSlot>();

                SR sr = Sql.ExecuteQuery(sw.GetWhere(RwpSlot.s_sSqlQueryString), this, _sResourceConnString);

                tw.WriteLine(csvs.Header());
                foreach (RwpSlot rwps in m_plrwps)
                {
                    tw.WriteLine(csvs.CsvMake(rwps));
                }

                tw.Flush();
                return(SR.Success());
            }
예제 #2
0
        /* I M P O R T  C S V */

        /*----------------------------------------------------------------------------
        *   %%Function: ImportCsv
        *   %%Qualified: RwpSvc.Practice:RwpSlots.ImportCsv
        *   %%Contact: rlittle
        *
        *  ----------------------------------------------------------------------------*/
        public static RSR ImportCsv(Stream stm, TimeZoneInfo tzi)
        {
            RSR      sr;
            Sql      sql;
            CsvSlots csv = new CsvSlots();

            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)
            {
                sql.Close();
                return(sr);
            }

            bool          fHeadingRead = false;
            string        sLine;
            int           iLine = 0;
            RwpSlot       rwps;
            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.LoadRwpsFromCsv(sLine, sql, out rwps, out fAdd, out plsDiff, tzi);
                    if (!sr.Result)
                    {
                        throw new Exception(String.Format("Failed to process line {0}: {1}", iLine - 1, sr.Reason));
                    }

                    if (rwps == null) // this means it was an empty csv line
                    {
                        continue;
                    }

                    // at this point, rwps is a fully loaded team; check for errors and generate a passowrd if necessary
                    sr = rwps.Preflight(sql);
                    if (!sr.Result)
                    {
                        throw new Exception(String.Format("Failed to preflight line {0}: {1}", iLine - 1, sr.Reason));
                    }

                    // at this point, we would insert...
                    string sInsert = rwps.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());
        }