public void testCustomerSvc()
        {
            try {
                //week 3
                //ICustomerSvc ics = factory.getCustomerSvc();

                //week 4
                ICustomerSvc ics = (ICustomerSvc) factory.getService("ICustomerSvc");

                // First let's store the Customer
                Assert.True(ics.storeCustomer(c));

                // Then let's read it back in
                c = ics.getCustomer(c.id);
                Assert.True(c.validate());

                // Update customer
                c.emailAddress = "*****@*****.**";
                Assert.True(ics.storeCustomer(c));

                // Finally, let's cleanup the file that was created
                Assert.True(ics.deleteCustomer(c.id));
            }
            catch(Exception e) {
                Console.WriteLine("Exception in testStoreCustomer: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testInvalidCustomer()
        {
            try {
                Customer c = new Customer();

                Assert.False(c.validate());
            }
            catch(Exception e) {
                Console.WriteLine("Exception in testInvalidCustomer: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testNotEqualsCustomer()
        {
            try {
                Customer c = new Customer(1, "Jim", "Bloom", "*****@*****.**");
                Customer d = new Customer();

                Assert.False(c.Equals(d));
            }
            catch(Exception e) {
                Console.WriteLine("Exception in testNotEqualsCustomer: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testValidate()
        {
            try {
                Customer c = new Customer();
                c.id = 1;
                c.emailAddress = "*****@*****.**";
                c.firstName = "Jim";
                c.lastName = "Bloom";

                Assert.True(c.validate());
            }
            catch(Exception e) {
                Console.WriteLine("Exception in testValidate: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testInvalidGetCustomer()
        {
            try {
                //week 3
                //ICustomerSvc ics = factory.getCustomerSvc();

                //week 4
                ICustomerSvc ics = (ICustomerSvc) factory.getService("ICustomerSvc");
                c = ics.getCustomer(1234);

                if(c != null)
                    Assert.False(c.validate());
            }
            catch(Exception e) {
                Console.WriteLine("Exception in testInvalidGetCustomer: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        /// <summary>
        /// This method retrieves a customer.
        /// </summary>
        /// <param name="id">Unique ID of customer to retrieve</param>
        /// <returns>customer object</returns>
        public Customer getCustomer(Int32 id)
        {
            dLog.Info("Entering method getCustomer | ID: " + id);
            Customer c = new Customer();
            ISession session = null;

            try {
                using (session = getSession()) {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        IQuery query = session.CreateQuery(@"FROM Customer WHERE id = :id");
                        query.SetParameter("id", id);

                        c = query.List<Customer>()[0];
                    }

                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in getCustomer: " + e2.Message + "\n" + e2.StackTrace);
                c = new Customer();
            }
            finally
            {
                //ensure that session is close regardless of the errors in try/catch
                if (session != null && session.IsOpen)
                    session.Close();
            }

            return c;
        }
        /// <summary>
        /// This method stores a customer.
        /// </summary>
        /// <param name="c">The customer object to store</param>
        /// <returns>Success/Failure</returns>
        public Boolean storeCustomer(Customer c)
        {
            dLog.Info("Entering method storeCustomer | ID: " + c.id);
            Boolean result = false;
            ISession session = null;

            try
            {
                using (session = getSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        session.Save(c);
                        transaction.Commit();

                        if (transaction.WasCommitted)
                            result = true;
                    }
                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in storeCustomer: " + e2.Message);
            }
            finally
            {
                //ensure that session is close regardless of the errors in try/catch
                if (session != null && session.IsOpen)
                    session.Close();
            }

            return result;
        }
        /// <summary>
        /// This method stores a customer.
        /// </summary>
        /// <param name="c">The customer object to store</param>
        /// <returns>Success/Failure</returns>
        public Boolean storeCustomer(Customer c)
        {
            dLog.Info("Entering method storeCustomer | ID: " + c.id);
            Stream output = null;
            Boolean result = false;

            try
            {
                //ensure we were passed a valid object before attempting to write
                if (c.validate())
                {
                    output = File.Open("Customer_" + c.id + ".txt", FileMode.Create);
                    BinaryFormatter bFormatter = new BinaryFormatter();
                    bFormatter.Serialize(output, c);
                    result = true;
                }
            }
            catch (IOException e1)
            {
                dLog.Error("IOException in storeCustomer: " + e1.Message);
                result = false;
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in storeCustomer: " + e2.Message);
                result = false;
            }
            finally
            {
                //ensure that output is close regardless of the errors in try/catch
                if (output != null)
                {
                    output.Close();
                }
            }

            return result;
        }
        /// <summary>
        /// This method retrieves a customer.
        /// </summary>
        /// <param name="id">Unique ID of customer to retrieve</param>
        /// <returns>customer object</returns>
        public Customer getCustomer(Int32 id)
        {
            dLog.Info("Entering method getCustomer | ID: " + id);
            Customer c = new Customer();
            SqlDataReader rs = null;
            SqlConnection conn = null;
            SqlCommand stmt = null;

            try
            {
                String sqlStr = "SELECT EmailAddress, FirstName, LastName FROM Customer WHERE id = @id";

                conn = new SqlConnection(connString);
                conn.Open();
                stmt = new SqlCommand(sqlStr, conn);
                stmt.Parameters.Add(new SqlParameter("@id", id));
                rs = stmt.ExecuteReader();

                while (rs.Read())
                {
                    dLog.Info("Got the " + rs.FieldCount + " fields of the record");
                    c.id = id;

                    #region Read Fields
                    if (!rs.IsDBNull(0))
                        c.emailAddress = rs.GetString(0);
                    if(!rs.IsDBNull(1))
                        c.firstName = rs.GetString(1);
                    if(!rs.IsDBNull(2))
                        c.lastName = rs.GetString(2);
                    #endregion
                }
            }
            catch (SqlException e1)
            {
                dLog.Error("SqlException in getCustomer: " + e1.Message + "\n" + e1.StackTrace);
                c = new Customer();
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in getCustomer: " + e2.Message + "\n" + e2.StackTrace);
                c = new Customer();
            }
            finally
            {
                if (rs != null && !rs.IsClosed)
                    rs.Close();
                if (conn != null && conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
            }

            return c;
        }
        /// <summary>
        /// This method stores a customer.
        /// </summary>
        /// <param name="c">The customer object to store</param>
        /// <returns>Success/Failure</returns>
        public Boolean storeCustomer(Customer c)
        {
            dLog.Info("Entering method storeCustomer | ID: " + c.id);
            Boolean result = false;
            SqlConnection conn = null;
            SqlCommand stmt = null;

            try
            {
                String sqlStr = "SELECT COUNT(1) FROM Customer WHERE id = @id";

                conn = new SqlConnection(connString);
                conn.Open();
                stmt = new SqlCommand(sqlStr, conn);
                stmt.Parameters.Add(new SqlParameter("@id", c.id));

                if (Int32.Parse(stmt.ExecuteScalar().ToString()) > 0)
                {
                    //if first is a valid row, then we need to do an update
                    dLog.Info("Updating customer in database");

                    sqlStr = "UPDATE Customer SET EmailAddress=@EmailAddress, FirstName=@FirstName, LastName=@LastName WHERE id=@id";
                }
                else
                {
                    //if first is null, then we need to do an insert
                    dLog.Info("Inserting customer into database");

                    sqlStr = "INSERT INTO Customer (EmailAddress, FirstName, LastName, id) ";
                    sqlStr += "VALUES (@EmailAddress, @FirstName, @LastName, @id)";
                }

                dLog.Info("SQL Statement: " + sqlStr);
                stmt = new SqlCommand(sqlStr, conn);

                #region Add SQL Parameters
                stmt.Parameters.Add(new SqlParameter("@id", c.id));
                stmt.Parameters.Add(new SqlParameter("@EmailAddress", c.emailAddress));
                stmt.Parameters.Add(new SqlParameter("@FirstName", c.firstName));
                stmt.Parameters.Add(new SqlParameter("@LastName", c.lastName));
                #endregion

                if (stmt.ExecuteNonQuery() > 0)
                    result = true;
            }
            catch (SqlException e1)
            {
                dLog.Error("SqlException in storeCustomer: " + e1.Message);
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in storeCustomer: " + e2.Message);
            }
            finally
            {
                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
            }

            return result;
        }
        /// <summary>
        /// This method stores a customer.
        /// </summary>
        /// <param name="c">The customer object to store</param>
        /// <returns>Success/Failure</returns>
        public Boolean storeCustomer(Customer c)
        {
            dLog.Info("Entering method storeCustomer | ID: " + c.id);
            Boolean result = false;

            try
            {
                MongoServer server = MongoServer.Create();
                MongoDatabase db = server.GetDatabase("neatoBurrito");
                //MongoCredentials credentials = new MongoCredentials("username", "password");
                //MongoDatabase salaries = server.GetDatabase("salaries", credentials);

                using (server.RequestStart(db))
                {
                    MongoCollection<BsonDocument> coll = db.GetCollection("customer");
                    var query = new QueryDocument("id", c.id);

                    dLog.Debug("Finding if customer exists");
                    BsonDocument myDoc = coll.FindOne(query);

                    query.Add("emailAddress", c.emailAddress);
                    query.Add("lastName", c.lastName);
                    query.Add("firstName", c.firstName);

                    //ensure we were passed a valid object before attempting to write
                    if (myDoc == null)
                    {
                        dLog.Debug("Inserting customer");
                        coll.Insert(query);

                        result = true;
                    }
                    else
                    {
                        var update = new UpdateDocument();
                        update.Add(query.ToBsonDocument());
                        dLog.Debug("Updating customer");
                        dLog.Debug("myDoc: " + myDoc.ToString());
                        dLog.Debug("update Query: " + update.ToString());

                        SafeModeResult wr = coll.Update(new QueryDocument("id", c.id), update, SafeMode.True);

                        dLog.Debug("SafeModeResult: " + wr.Ok);
                        if (wr.LastErrorMessage == null && wr.Ok)
                        {
                            result = true;
                        }
                        else
                        {
                            dLog.Debug("SafeModeResult: " + wr.LastErrorMessage);
                        }
                    }
                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in storeCustomer: " + e2.Message);
            }
            finally
            {
                //using statement above already calls RequestDone()
            }

            return result;
        }
        /// <summary>
        /// This method retrieves a customer.
        /// </summary>
        /// <param name="id">Unique ID of customer to retrieve</param>
        /// <returns>customer object</returns>
        public Customer getCustomer(Int32 id)
        {
            dLog.Info("Entering method getCustomer | ID: " + id);
            Customer c = new Customer();

            try
            {
                MongoServer server = MongoServer.Create();
                MongoDatabase db = server.GetDatabase("neatoBurrito");
                //MongoCredentials credentials = new MongoCredentials("username", "password");
                //MongoDatabase salaries = server.GetDatabase("salaries", credentials);

                using (server.RequestStart(db))
                {
                    MongoCollection<BsonDocument> coll = db.GetCollection("customer");
                    var query = new QueryDocument("id", id);

                    BsonDocument myDoc = coll.FindOne(query);

                    //ensure we were passed a valid object before attempting to read
                    if (myDoc != null)
                    {
                        dLog.Debug("myDoc: " + myDoc.ToString());

                        #region Read Fields
                        c.id = id;
                        c.emailAddress = myDoc["emailAddress"].AsString;
                        c.firstName = myDoc["firstName"].AsString;
                        c.lastName = myDoc["lastName"].AsString;
                        #endregion
                    }
                    dLog.Debug("Finishing setting customer");
                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in getCustomer: " + e2.Message + "\n" + e2.StackTrace);
                c = new Customer();
            }
            finally
            {
                //using statement above already calls RequestDone()
            }

            return c;
        }
 protected void SetUp()
 {
     factory = Factory.getInstance();
     c = new Customer(1, "Jim", "Bloom", "*****@*****.**");
 }