public void testStoreManager()
        {
            try {
                //week 3
                //IManagerSvc ics = factory.getManagerSvc();

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

                // First let's store the Inventory
                Assert.True(ics.storeManager(m));

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

                // Update Manager
                m.lastName = "Smith";
                Assert.True(ics.storeManager(m));

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

                Assert.False(m.validate());
            }
            catch(Exception e) {
                Console.WriteLine("Exception in testInvalidManager: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testNotEqualsManager()
        {
            try {
                Manager m = new Manager("Jim","Bloom",1);
                Manager n = new Manager();

                Assert.False(m.Equals(n));
            }
            catch(Exception e) {
                Console.WriteLine("Exception in testNotEqualsManager: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testValidateManager()
        {
            try {
                Manager m = new Manager();
                m.id = 1;
                m.firstName = "Jim";
                m.lastName = "Bloom";

                Assert.True(m.validate());
            }
            catch(Exception e) {
                Console.WriteLine("Exception in testValidateManager: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        /// <summary>
        /// This method retrieves a manager.
        /// </summary>
        /// <param name="id">Unique ID of manager to retrieve</param>
        /// <returns>customer object</returns>
        public Manager getManager(Int32 id)
        {
            dLog.Info("Entering method getManager | ID: " + id);
            Manager m = new Manager();
            SqlDataReader rs = null;
            SqlConnection conn = null;
            SqlCommand stmt = null;

            try
            {
                String sqlStr = "SELECT FirstName, isManager, LastName FROM Employee 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");
                    m.id = id;

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

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

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

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

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

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

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

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

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

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

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

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

            return result;
        }
 protected void SetUp()
 {
     factory = Factory.getInstance();
     m = new Manager("Jim", "Bloom", 1);
 }
        /// <summary>
        /// This method stores a manager.
        /// </summary>
        /// <param name="m">The manager object to store</param>
        /// <returns>Success/Failure</returns>
        public Boolean storeManager(Manager m)
        {
            dLog.Info("Entering method storeManager | ID: " + m.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("employee");
                    var query = new QueryDocument("id", m.id);

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

                    query.Add("firstName", m.firstName);
                    query.Add("isManager", m.isManager);
                    query.Add("lastName", m.lastName);

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

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

                        SafeModeResult wr = coll.Update(new QueryDocument("id", m.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 storeManager: " + e2.Message);
            }
            finally
            {
                //using statement above already calls RequestDone()
            }

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

            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("employee");
                    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
                        m.id = id;
                        m.firstName = myDoc["firstName"].AsString;
                        m.isManager = myDoc["isManager"].AsBoolean;
                        m.lastName = myDoc["lastName"].AsString;
                        #endregion
                    }
                    dLog.Debug("Finishing setting manager");
                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in getManager: " + e2.Message + "\n" + e2.StackTrace);
                m = new Manager();
            }
            finally
            {
                //using statement above already calls RequestDone()
            }

            return m;
        }
        public Boolean login(Manager m, String password)
        {
            Boolean result = false;

            try
            {
                IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPEndPoint localEP = new IPEndPoint(ipHostInfo.AddressList[0], 8000);
                foreach (IPAddress ip in ipHostInfo.AddressList)
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                        localEP = new IPEndPoint(ip, 8000);

                dLog.Debug("Local address and port: " + localEP.Address.ToString() + " | " + localEP.Port.ToString());

                client = new Socket(localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(localEP.Address, 8000);

                String inputStr = readObject();
                writeObject(m.firstName);
                inputStr = readObject();
                writeObject(password);
                inputStr = readObject();

                //check out input
                if (inputStr.Split(' ')[0].Equals("OK"))
                    result = true;

                writeObject("exit");
            }
            catch (Exception e1)
            {
                dLog.Error("Exception in login: " + e1.Message);
            }
            finally
            {
                if (client.Connected)
                    client.Shutdown(SocketShutdown.Both);

                if (client.IsBound)
                {
                    client.Close();
                    client = null;
                }
            }

            return result;
        }
        /// <summary>
        /// This method retrieves a manager.
        /// </summary>
        /// <param name="id">Unique ID of manager to retrieve</param>
        /// <returns>burrito object</returns>
        public Manager getManager(Int32 id)
        {
            dLog.Info("Entering method getManager | ID: " + id);
            Manager m = new Manager();
            ISession session = null;

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

                        m = query.List<Manager>()[0];
                    }

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

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

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

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

            return result;
        }