Ejemplo n.º 1
0
        /// <summary>
        /// Update the database transactions table from the specified XML.
        /// </summary>
        /// <param name="corc"></param>
        /// <param name="fileXML"></param>
        /// <returns></returns>
        private int UpdateTransactionsFromXML(CharOrCorp corc, XmlDocument fileXML, short walletID)
        {
            int retVal = 0;
            EMMADataSet.TransactionsDataTable transData = new EMMADataSet.TransactionsDataTable();
            long highestIDSoFar = _apiSettings.GetHighestID(corc, APIDataType.Transactions);
            long highestID = 0;
            DateTime ticker = DateTime.UtcNow.AddSeconds(-10);

            try
            {
                int updated = 0;

                XmlNodeList transEntries = null;
                XmlDocument xml = new XmlDocument();

                UpdateStatus(0, 1, "Getting transactions from file", "", false);
                transEntries = EveAPI.GetResults(fileXML);
                UpdateStatus(1, 1, "", transEntries.Count + " entries found in file.", false);

                if (transEntries != null && transEntries.Count > 0)
                {
                    int batchPrg = 0;
                    UpdateStatus(0, transEntries.Count, "Processing transactions", "", false);

                    XmlNode entryIDNode = transEntries[0].SelectSingleNode("@transactionID");
                    //long fileMaxID = long.Parse(entryIDNode.Value,
                    //    System.Globalization.CultureInfo.InvariantCulture.NumberFormat);

                    // Loop through the results returned from this call to the API and add the line to
                    // the data table if the transactionID is not already in the database.
                    foreach (XmlNode transEntry in transEntries)
                    {
                        XmlNode transIDNode = transEntry.SelectSingleNode("@transactionID");
                        long transID = long.Parse(transIDNode.Value,
                            System.Globalization.CultureInfo.InvariantCulture.NumberFormat);

                        if (transID > highestID) { highestID = transID; }

                        //if (transID > highestIDSoFar)
                        //{
                        if (!Transactions.TransactionExists(transData, transID) &&
                            transData.FindByID(transID) == null)
                        {
                            // Actually create the line and add it to the data table
                            SortedList<long, string> nameIDs = new SortedList<long, string>();
                            EMMADataSet.TransactionsRow newRow = BuildTransRow(transID, transData,
                                transEntry, walletID, nameIDs, false);

                            transData.AddTransactionsRow(newRow);
                            retVal++;

                            // This section searches the character, item and station ref type tables
                            // for the values used in this new transaction entry.
                            // If they are not present in the table then they are added.
                            #region Check other tables and add values if needed.
                            foreach (KeyValuePair<long, string> checkName in nameIDs)
                            {
                                Names.AddName(checkName.Key, checkName.Value);
                            }
                            Items.AddItem(newRow.ItemID, transEntry.SelectSingleNode("@typeName").Value);
                            #endregion
                        }
                        else
                        {
                            SortedList<long, string> nameIDs = new SortedList<long, string>();
                            // We've got a transaction that already exists in the database,
                            // update the row with additional data if available.
                            EMMADataSet.TransactionsRow newRow =
                                BuildTransRow(transID, transData, transEntry, walletID, nameIDs, true);
                            EMMADataSet.TransactionsRow oldRow = transData.FindByID(transID);
                            bool updateDone = false;

                            if (newRow.BuyerWalletID != oldRow.BuyerWalletID && newRow.BuyerWalletID != 0)
                            {
                                oldRow.BuyerWalletID = newRow.BuyerWalletID;
                                updateDone = true;
                            }
                            if (newRow.SellerWalletID != oldRow.SellerWalletID && newRow.SellerWalletID != 0)
                            {
                                oldRow.SellerWalletID = newRow.SellerWalletID;
                                updateDone = true;
                            }
                            // If a corp sells somthing to another corp (or itself) then we will get into
                            // the position of having the other party set as a character when in fact
                            // it is that character's corp.
                            // We check for this here and correct it if required.
                            if (oldRow.BuyerID == _charID && newRow.BuyerID == _corpID)
                            {
                                oldRow.BuyerID = newRow.BuyerID;
                                oldRow.BuyerCharacterID = newRow.BuyerCharacterID;
                                oldRow.BuyerWalletID = newRow.BuyerWalletID;
                                oldRow.BuyerForCorp = newRow.BuyerForCorp;
                                updateDone = true;
                            }
                            if (oldRow.SellerID == _charID && newRow.SellerID == _corpID)
                            {
                                oldRow.SellerID = newRow.SellerID;
                                oldRow.SellerCharacterID = newRow.SellerCharacterID;
                                oldRow.SellerWalletID = newRow.SellerWalletID;
                                oldRow.SellerForCorp = newRow.SellerForCorp;
                                updateDone = true;
                            }

                            if (updateDone)
                            {
                                updated++;
                            }
                        }
                        //}

                        batchPrg++;
                        UpdateStatus(batchPrg, transEntries.Count, "", "", false);
                    }
                }

                if (highestID > highestIDSoFar)
                {
                    SetHighestID(corc, APIDataType.Transactions, highestID);
                }

                UpdateStatus(0, 0, retVal + " transactions added to database.", "", false);
                UpdateStatus(0, 0, updated + " transactions updated.", "", false);

                if (transData.Count > 0)
                {
                    Transactions.Store(transData);

                    UpdateStatus(1, 1, "", "Complete", true);
                }

            }
            catch (Exception ex)
            {
                EMMAException emmaEx = ex as EMMAException;
                if (emmaEx == null)
                {
                    // If we've caught a standard exception rather than an EMMA one then log it be creating a
                    // new exception.
                    // Note that we don't need to actually throw it..
                    emmaEx = new EMMAException(ExceptionSeverity.Error, "Error when adding transactions", ex);
                }

                SetLastAPIUpdateError(corc, APIDataType.Transactions, ex.Message);
                UpdateStatus(-1, 0, "Error", ex.Message, true);
            }

            if (UpdateEvent != null)
            {
                UpdateEvent(this, new APIUpdateEventArgs(APIDataType.Transactions,
                    corc == CharOrCorp.Char ? _charID : _corpID,
                    APIUpdateEventType.UpdateCompleted));
            }

            return retVal;
        }
Ejemplo n.º 2
0
        public void LoadOldEmmaXML(string filename, long charID, long corpID)
        {
            EMMADataSet.TransactionsDataTable table = new EMMADataSet.TransactionsDataTable();
            XmlDocument xml = new XmlDocument();
            //UpdateStatus(0, 0, "", "Loading file", false);
            xml.Load(filename);

            XmlNodeList nodes = xml.SelectNodes("/DocumentElement/Transactions");

            int counter = 0;
            UpdateStatus(0, 0, "", "Extracting data from XML", false);
            foreach (XmlNode node in nodes)
            {
                long transID = long.Parse(node.SelectSingleNode("ID").FirstChild.Value);

                if (!Transactions.TransactionExists(table, transID) &&
                    table.FindByID(transID) == null)
                {
                    // Actually create the line and add it to the data table
                    EMMADataSet.TransactionsRow newRow = BuildTransRow(transID, table, node, corpID, charID);

                    table.AddTransactionsRow(newRow);
                }
                else
                {
                    // We've got a transaction that already exists in the database,
                    // update the row with additional data if available.
                    EMMADataSet.TransactionsRow newRow = BuildTransRow(transID, table, node, corpID, charID);
                    EMMADataSet.TransactionsRow oldRow = table.FindByID(transID);

                    //if (newRow.BuyerWalletID != oldRow.BuyerWalletID && newRow.BuyerWalletID != 0)
                    //{
                    //    oldRow.BuyerWalletID = newRow.BuyerWalletID;
                    //}
                    //if (newRow.SellerWalletID != oldRow.SellerWalletID && newRow.SellerWalletID != 0)
                    //{
                    //    oldRow.SellerWalletID = newRow.SellerWalletID;
                    //}
                    // If a corp sells somthing to another corp (or itself) then we will get into
                    // the position of having the other party set as a character when in fact
                    // it is that character's corp.
                    // We check for this here and correct it if required.

                    // Change to just always update the database with the data from the import.
                    //if (oldRow.BuyerID == charID && newRow.BuyerID == corpID)
                    //{
                        oldRow.BuyerID = newRow.BuyerID;
                        oldRow.BuyerCharacterID = newRow.BuyerCharacterID;
                        oldRow.BuyerWalletID = newRow.BuyerWalletID;
                        oldRow.BuyerForCorp = newRow.BuyerForCorp;
                    //}
                    //if (oldRow.SellerID == charID && newRow.SellerID == corpID)
                    //{
                        oldRow.SellerID = newRow.SellerID;
                        oldRow.SellerCharacterID = newRow.SellerCharacterID;
                        oldRow.SellerWalletID = newRow.SellerWalletID;
                        oldRow.SellerForCorp = newRow.SellerForCorp;
                    //}

                        oldRow.DateTime = newRow.DateTime;
                        oldRow.Price = newRow.Price;
                        oldRow.Quantity = newRow.Quantity;
                        oldRow.ItemID = newRow.ItemID;
                        oldRow.StationID = newRow.StationID;
                }
                counter++;
                UpdateStatus(counter, nodes.Count, "", "", false);

                // If we've got 1000 rows then update the database and move on to the next batch.
                if (table.Count >= 1000)
                {
                    UpdateStatus(0, 0, "", "Updating database", false);
                    lock (tableAdapter)
                    {
                        tableAdapter.Update(table);
                        table.Clear();
                    }
                }
            }

            UpdateStatus(0, 0, "", "Updating database", false);
            lock (tableAdapter)
            {
                tableAdapter.Update(table);
            }
        }