Beispiel #1
0
        public void Save(KindOfGoods kindOfGoods)
        {
            this.ValidateFields(kindOfGoods);

            using (var conn = new OleDbConnection(this.ConnectionString))
            {
                conn.Open();
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        if (this.ExistsKindOfGoods(tran, kindOfGoods.UId))
                        {
                            this.Update(tran, kindOfGoods);
                        }
                        else
                        {
                            this.Add(tran, kindOfGoods);
                        }

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw ex;
                    }
                }
            }
        }
Beispiel #2
0
        public void VerifyThatShouldThrowWhileKindOfGoodsIsEmpty()
        {
            // empty.
            var kindOfGoods = new KindOfGoods {
                UId = "kind 01", Name = "				", Customized = true
            };

            this.AssertHasException <ArgumentException>("Kind name/id cannot be empty", () => new ReceiptDb().Save(kindOfGoods));

            // empty.
            kindOfGoods = new KindOfGoods {
                UId = "				", Name = "kind 01", Customized = true
            };
            this.AssertHasException <ArgumentException>("Kind name/id cannot be empty", () => new ReceiptDb().Save(kindOfGoods));

            // null
            kindOfGoods = new KindOfGoods {
                UId = "kind 01", Name = null, Customized = true
            };
            this.AssertHasException <ArgumentException>("Kind name/id cannot be empty", () => new ReceiptDb().Save(kindOfGoods));

            // null
            kindOfGoods = new KindOfGoods {
                UId = null, Name = "kind 01", Customized = true
            };
            this.AssertHasException <ArgumentException>("Kind name/id cannot be empty", () => new ReceiptDb().Save(kindOfGoods));
        }
Beispiel #3
0
        private void Save()
        {
            try
            {
                this.txtName.Text = this.txtName.Text.Trim();
                var currentKind = this.txtName.Tag as KindOfGoods;
                var isNew       = currentKind == null;
                if (!isNew)
                {
                    currentKind.Name = this.txtName.Text;
                }
                else
                {
                    currentKind = new KindOfGoods {
                        UId = Guid.NewGuid().ToString(), Name = this.txtName.Text, Customized = true
                    };
                    txtName.Tag = currentKind;
                }

                this.ReceiptDb.Save(currentKind);

                this.list.Items.Clear();
                this.list.Items.AddRange(this.ReceiptDb.GetAllKindsOfGoods().OrderBy(k => k.Name).ToArray());
                this.list.SelectedItem = currentKind;
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex);
                MessageBox.Show(this, String.Format("{0}\r\n\r\n{1}", Strings.MsgSaveKindOfGoodsFailed, ex.Message), Strings.MsgError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #4
0
        public void VerifyThatShouldThrowWhileKindOfGoodsIsNotCustomized()
        {
            var kindOfGoods = new KindOfGoods {
                UId = "kind 01", Name = "kind 01", BuiltIn = false, Customized = false
            };

            this.AssertHasException <ArgumentException>("Only customized kinds can be saved", () => new ReceiptDb().Save(kindOfGoods));
        }
Beispiel #5
0
        public void VerifyThatShouldThrowWhileKindOfGoodsIsBuiltIn()
        {
            var kindOfGoods = new KindOfGoods {
                UId = "kind 01", Name = "kind 01", BuiltIn = true, Customized = true
            };

            this.AssertHasException <ArgumentException>("Built-in kind cannot be saved", () => new ReceiptDb().Save(kindOfGoods));
        }
Beispiel #6
0
        private void SelectKind()
        {
            if (this.list.SelectedIndices.Count < 1)
            {
                return;
            }

            this.SelectedKind = this.ReceiptDb.GetAllKindsOfGoods().First(k => k.Name.Equals(this.list.SelectedItem.ToString()));
            this.DialogResult = DialogResult.OK;
        }
Beispiel #7
0
        private void AddNode(ComboTreeNodeCollection nodes, KindOfGoods kind)
        {
            var node = new ComboTreeNode(kind.Name)
            {
                Tag = kind, Expanded = true
            };

            nodes.Add(node);
            kind.SubKinds.ForEach(k => this.AddNode(node.Nodes, k));
        }
Beispiel #8
0
        private void Add(OleDbTransaction tran, KindOfGoods kindOfGoods)
        {
            OleDbConnection conn;
            var             newTran = (tran == null);

            if (newTran)
            {
                conn = new OleDbConnection(this.ConnectionString);
                conn.Open();
                tran = conn.BeginTransaction();
            }
            else
            {
                conn = tran.Connection;
            }

            try
            {
                var query = new QueryContext(null, @"INSERT INTO [KindsOfGoods] ([UId], [Name]) VALUES (?, ?)")
                {
                    Connection = conn, Transaction = tran
                };
                query.Parameters.AddWithValue(kindOfGoods.UId);
                query.Parameters.AddWithValue(kindOfGoods.Name);
                query.ExecuteNonQuery();

                if (newTran)
                {
                    tran.Commit();
                }

                if (this.allKindsOfGoods != null)
                {
                    this.allKindsOfGoods.RemoveAll(k => k.UId.Equals(kindOfGoods.UId));
                    this.allKindsOfGoods.Add(kindOfGoods);
                }
            }
            catch (Exception ex)
            {
                if (newTran)
                {
                    tran.Rollback();
                }
                throw ex;
            }
            finally
            {
                if (newTran)
                {
                    tran.Dispose();
                    conn.Dispose();
                }
            }
        }
Beispiel #9
0
        public void RemoveKindOfGoods()
        {
            var kindOfGoods = new KindOfGoods {
                UId = Guid.NewGuid().ToString(), Name = "kind 01", Customized = true
            };
            var receiptDb = new ReceiptDb();

            receiptDb.Save(kindOfGoods);
            receiptDb.RemoveKindOfGoods(kindOfGoods.UId);

            Assert.IsFalse(receiptDb.GetAllKindsOfGoods().Any(k => k.UId.Equals(kindOfGoods.UId)));
            Assert.IsFalse(new ReceiptDb().GetAllKindsOfGoods().Any(k => k.UId.Equals(kindOfGoods.UId)));
        }
Beispiel #10
0
        public void SaveKindOfGoods()
        {
            var kindOfGoods = new KindOfGoods {
                UId = "kind 01", Name = "kind 01", Customized = true
            };
            var receiptDb = new ReceiptDb();

            receiptDb.Save(kindOfGoods);

            var actual = receiptDb.GetAllKindsOfGoods().First(k => k.UId.Equals(kindOfGoods.UId));

            this.AssertAreEqual(kindOfGoods, actual);
        }
Beispiel #11
0
        private Receipt ReadReceipt(OleDbDataReader reader)
        {
            var readKindOfGoods = new Func <KindOfGoods>(() =>
            {
                var uid = reader["KindOfGoodsUId"].ToStringOrNull();
                if (uid == null)
                {
                    return(null);
                }

                var kind = new KindOfGoods {
                    UId = uid, Name = reader["KindOfGoodsName"].ToStringOrNull(), Customized = true
                };
                if (String.IsNullOrEmpty(kind.Name))
                {
                    var builtInKind = Const.BuiltInKindsOfGoods.FirstOrDefaultRecursively(k => k.UId.Equals(kind.UId));
                    if (builtInKind != null)
                    {
                        kind = builtInKind;
                    }
                }

                return(kind);
            });

            return(new Receipt
            {
                No = reader["No"].ToStringOrNull(),
                VesselName = reader["VesselName"].ToStringOrNull(),
                Time = reader["Time"].TryToNullableDateTime(),
                ReceiptFor = reader["ReceiptFor"].ToStringOrNull(),
                PortOfShipment = reader["PortOfShipment"].ToStringOrNull(),
                PortOfDestination = reader["PortOfDestination"].ToStringOrNull(),
                KindOfGoods = readKindOfGoods(),
                VesselStatus = VesselStatus.Parse(reader["VesselStatus"].ToStringOrNull()),
                TotalOfVolumeOfStandard = reader["TotalOfVolumeOfStandard"].TryToNullableDecimal(),
                TotalOfVolume = reader["TotalOfVolume"].TryToNullableDecimal(),
                TotalOfVolumeOfWater = reader["TotalOfVolumeOfWater"].TryToNullableDecimal(),
                TotalOfMass = reader["TotalOfMass"].TryToNullableDecimal(),
                TotalOfVolumeOfPipes = reader["TotalOfVolumeOfPipes"].TryToNullableDecimal(),
                OperaterName = reader["OperaterName"].ToStringOrNull(),
                AgentName = reader["AgentName"].ToStringOrNull(),
                ShipperName = reader["ShipperName"].ToStringOrNull(),
                ConsignerName = reader["ConsignerName"].ToStringOrNull(),
                ConsigneeName = reader["ConsigneeName"].ToStringOrNull(),
                TimeSaved = reader["TimeSaved"].TryToDateTime(),
                ReceiptType = reader["ReceiptType"].ToReceiptType()
            });
        }
Beispiel #12
0
        public void VerifyThatShouldThrowWhileRemovingAnInUsedKind()
        {
            var kindOfGoods = new KindOfGoods {
                UId = Guid.NewGuid().ToString(), Name = "kind 01", Customized = true
            };
            var receiptDb = new ReceiptDb();

            receiptDb.Save(kindOfGoods);
            var receipt = this.GetTestReceipts()[0];

            receipt.KindOfGoods = kindOfGoods;
            receiptDb.Save(receipt);

            this.AssertHasException <InvalidOperationException>(String.Format("The kind [{0}] is currently in used, cannot be removed", kindOfGoods.UId), () => receiptDb.RemoveKindOfGoods(kindOfGoods.UId));
        }
Beispiel #13
0
        public void UpdateKindOfGoods()
        {
            var kindOfGoods = new KindOfGoods {
                UId = "kind 01", Name = "kind 01", Customized = true
            };
            var receiptDb = new ReceiptDb();

            receiptDb.Save(kindOfGoods);
            kindOfGoods.Name = "kind 01 changed";
            receiptDb.Save(kindOfGoods);

            var actual = receiptDb.GetAllKindsOfGoods().First(k => k.UId.Equals(kindOfGoods.UId));

            Assert.AreEqual(kindOfGoods.Name, actual.Name);
            actual = new ReceiptDb().GetAllKindsOfGoods().First(k => k.UId.Equals(kindOfGoods.UId));
            Assert.AreEqual(kindOfGoods.Name, actual.Name);
        }
Beispiel #14
0
 private void ValidateFields(KindOfGoods kindOfGoods)
 {
     if (String.IsNullOrEmpty(kindOfGoods.UId) || kindOfGoods.UId.Trim().Length < 1 || String.IsNullOrEmpty(kindOfGoods.Name) || kindOfGoods.Name.Trim().Length < 1)
     {
         throw new ArgumentException("Kind name/id cannot be empty");
     }
     if (Const.BuiltInKindsOfGoods.Any(k => kindOfGoods.UId.Equals(k.UId)))
     {
         throw new ArgumentException(String.Format("Built-in kind \"{0}\" cannot be saved", Const.BuiltInKindsOfGoods.First(k => k.UId.Equals(kindOfGoods.UId)).Name));
     }
     if (kindOfGoods.BuiltIn)
     {
         throw new ArgumentException("Built-in kind cannot be saved");
     }
     if (!kindOfGoods.Customized)
     {
         throw new ArgumentException("Only customized kinds can be saved");
     }
 }
Beispiel #15
0
 private void AssertAreEqual(KindOfGoods expected, KindOfGoods actual)
 {
     Assert.AreEqual(expected.UId, actual.UId);
     Assert.AreEqual(expected.Name, actual.Name);
     Assert.AreEqual(expected, actual);
 }