public void SetUp()
        {
            tipCalculatorService = Resolve <ITipCalculatorService>();
            rateList             = new List <RateBE>
            {
                new RateBE("EUR", "USD", 0.87m),
                new RateBE("USD", "EUR", 1.15m),
                new RateBE("EUR", "CAD", 0.73m),
                new RateBE("CAD", "EUR", 1.37m),
                new RateBE("USD", "AUD", 1.37m),
                new RateBE("AUD", "USD", 0.73m)
            };
            tipCalculatorService.GenerateGraph(rateList);
            transactionBE = new TransactionBE("T001", 10, "EUR");

            transactionList = new List <TransactionBE>
            {
                transactionBE
            };
            tipBE = new TipBE("T001", 10, 0.50m, "EUR");
            List <TipBE> tipList = new List <TipBE>
            {
                tipBE
            };

            billBE = new BillBE(0.44m, "USD", tipList);
        }
        public decimal GetTransactionAmount(TransactionBE transaction, string currency)
        {
            decimal rateValue = 0;

            try
            {
                rateValue = RecursiveDFS(transaction.Currency, currency);
            }
            #region Exceptions
            catch (ArgumentOutOfRangeException e)
            {
                Log.Error(e.Message);
                Log.Warning(e.StackTrace);
                throw new VuelingExamDomainException(e.Message, e.InnerException);
            }
            catch (OverflowException e)
            {
                Log.Error(e.Message);
                Log.Warning(e.StackTrace);
                throw new VuelingExamDomainException(e.Message, e.InnerException);
            }
            catch (ArgumentNullException e)
            {
                Log.Error(e.Message);
                Log.Warning(e.StackTrace);
                throw new VuelingExamDomainException(e.Message, e.InnerException);
            }
            #endregion
            return(rateValue);
        }
Пример #3
0
        public void Transactions_Update(TransactionBE trans)
        {
            string revertuserid = null;

            if (trans.RevertUserId != null)
            {
                revertuserid = trans.RevertUserId.Value.ToString();
            }

            DataCommand cmd = Catalog.NewQuery(@" /* Transactions_Update */
update `transactions` set 
`t_user_id`= ?USERID,
`t_page_id`= ?PAGEID,
`t_title`= ?TITLE,
`t_namespace`= ?NS,
`t_type`= ?TYPE,
`t_reverted`= ?REVERTED,
`t_revert_user_id` = ?REVERT_USERID,
`t_revert_timestamp` = ?REVERT_TS,
`t_revert_reason` = ?REVERT_REASON
where `t_id`= ?TID;"
                                               ).With("TID", trans.Id)
                              .With("USERID", trans.UserId)
                              .With("PAGEID", trans.PageId)
                              .With("TITLE", trans.Title.AsUnprefixedDbPath())
                              .With("NS", (int)trans.Title.Namespace)
                              .With("TYPE", (int)trans.Type)
                              .With("REVERTED", trans.Reverted)
                              .With("REVERT_REASON", trans.RevertReason);

            //TODO MaxM: This is a workaround for Datacommand.with not taking nullables.
            if (trans.RevertUserId.HasValue)
            {
                cmd = cmd.With("REVERT_USERID", trans.RevertUserId.Value);
            }
            else
            {
                cmd = cmd.With("REVERT_USERID", DBNull.Value);
            }

            if (trans.RevertTimeStamp.HasValue)
            {
                cmd = cmd.With("REVERT_TS", trans.RevertTimeStamp.Value);
            }
            else
            {
                cmd = cmd.With("REVERT_TS", DBNull.Value);
            }
            cmd.Execute();
        }
Пример #4
0
        public uint Transactions_Insert(TransactionBE trans)
        {
            return(Catalog.NewQuery(@" /* Transactions_Insert */
insert into `transactions` (`t_timestamp`,`t_user_id`,`t_page_id`,`t_title`,`t_namespace`,`t_type`) 
values ( ?TS, ?USERID, ?PAGEID, ?TITLE, ?NS, ?TYPE);
select last_insert_id();"
                                    ).With("TS", trans.TimeStamp)
                   .With("USERID", trans.UserId)
                   .With("PAGEID", trans.PageId)
                   .With("TITLE", trans.Title.AsUnprefixedDbPath())
                   .With("NS", (int)trans.Title.Namespace)
                   .With("TYPE", (int)trans.Type)
                   .ReadAsUInt() ?? 0);
        }
Пример #5
0
        public TransactionBE Transactions_GetById(uint transid)
        {
            TransactionBE t = null;

            Catalog.NewQuery(@" /* Transactions_GetById */
select *
from transactions
where t_id = ?TID")
            .With("TID", transid)
            .Execute(delegate(IDataReader dr) {
                if (dr.Read())
                {
                    t = Transactions_Populate(dr);
                }
            });
            return(t);
        }
Пример #6
0
        private TransactionBE Transactions_Populate(IDataReader dr)
        {
            TransactionBE transaction = new TransactionBE();

            transaction._Namespace      = dr.Read <ushort>("t_namespace");
            transaction._Title          = dr.Read <string>("t_title");
            transaction._Type           = dr.Read <uint>("t_type");
            transaction.Id              = dr.Read <uint>("t_id");
            transaction.PageId          = dr.Read <uint>("t_page_id");
            transaction.Reverted        = dr.Read <bool>("t_reverted");
            transaction.RevertReason    = dr.Read <string>("t_revert_reason");
            transaction.RevertTimeStamp = dr.Read <DateTime?>("t_revert_timestamp");
            transaction.RevertUserId    = dr.Read <uint?>("t_revert_user_id");
            transaction.TimeStamp       = dr.Read <DateTime>("t_timestamp");
            transaction.UserId          = dr.Read <uint>("t_user_id");
            return(transaction);
        }
Пример #7
0
        public static XDoc GetArchivedPagesXml(uint trans_limit, uint trans_offset, Title filterTitle)
        {
            XDoc ret = new XDoc("pages.archive");
            Dictionary <uint, TransactionBE> transactionsById = null;
            Dictionary <uint, UserBE>        usersById        = new Dictionary <uint, UserBE>();
            uint?queryTotalTransactionCount;

            //Lookup archived pages grouped by transactions
            IList <KeyValuePair <uint, IList <ArchiveBE> > > pagesByTransactions = DbUtils.CurrentSession.Archive_GetPagesByTitleTransactions(filterTitle, trans_offset, trans_limit, out transactionsById, out queryTotalTransactionCount);

            if (queryTotalTransactionCount != null)
            {
                ret.Attr("querycount", queryTotalTransactionCount.Value);
            }

            //Retrieve users that performed the delete
            foreach (TransactionBE t in transactionsById.Values)
            {
                usersById[t.UserId] = null;
            }
            usersById = DbUtils.CurrentSession.Users_GetByIds(usersById.Keys.ToArray()).AsHash(e => e.ID);

            //Build xml with the info
            foreach (KeyValuePair <uint, IList <ArchiveBE> > trans in pagesByTransactions)
            {
                IList <ArchiveBE> pages = trans.Value;
                TransactionBE     tran  = null;
                UserBE            user  = null;
                DateTime          ts    = DateTime.MinValue;

                //Retrieve user and timestamp based on transaction
                if (transactionsById.TryGetValue(trans.Key, out tran))
                {
                    ts = tran.TimeStamp;
                    usersById.TryGetValue(tran.UserId, out user);
                }

                //Display the page where the delete started
                GetArchivePageXml(ret, pages[0], user, ts);

                //Display href to see the subpages
                ret[ret.AsXmlNode.LastChild].Start("subpages").Attr("count", pages.Count - 1).Attr("href", DekiContext.Current.ApiUri.At("archive", "pages", pages[0].LastPageId.ToString(), "subpages")).End();
            }

            return(ret);
        }
Пример #8
0
        private IList <KeyValuePair <uint, IList <ArchiveBE> > > Archive_PopulateByTransactionQuery(DataCommand cmd, out Dictionary <uint, TransactionBE> transactionsById, out uint?queryTotalTransactionCount)
        {
            Dictionary <uint, TransactionBE> transactionsByIdtemp = new Dictionary <uint, TransactionBE>();
            List <KeyValuePair <uint, IList <ArchiveBE> > > archivedPagesByTransaction = new List <KeyValuePair <uint, IList <ArchiveBE> > >();
            uint?queryTotalTransactionCountTemp = null;

            cmd.Execute(delegate(IDataReader dr) {
                KeyValuePair <uint, IList <ArchiveBE> > currentTrans = new KeyValuePair <uint, IList <ArchiveBE> >();
                while (dr.Read())
                {
                    //Populate list of archived pages per trans. (assumes sort by transaction with first archived page being the transaction 'root')
                    uint tranId            = DbUtils.Convert.To <uint>(dr["t_id"]) ?? 0;
                    ArchiveBE archivedPage = Archive_Populate(dr);
                    TransactionBE tran     = null;
                    if (!transactionsByIdtemp.TryGetValue(tranId, out tran))
                    {
                        tran = Transactions_Populate(dr);
                        transactionsByIdtemp[tranId] = tran;
                    }

                    if (currentTrans.Key != tranId)
                    {
                        currentTrans = new KeyValuePair <uint, IList <ArchiveBE> >(tranId, new List <ArchiveBE>());
                        archivedPagesByTransaction.Add(currentTrans);
                    }

                    currentTrans.Value.Add(archivedPage);
                }

                if (dr.NextResult() && dr.Read())
                {
                    queryTotalTransactionCountTemp = DbUtils.Convert.To <uint>(dr["queryTotalTransactionCount"]);
                }
            });
            transactionsById           = transactionsByIdtemp;
            queryTotalTransactionCount = queryTotalTransactionCountTemp;
            return(archivedPagesByTransaction);
        }
Пример #9
0
        public static XDoc GetArchivePageXml(uint pageid)
        {
            XDoc      ret  = XDoc.Empty;
            ArchiveBE page = DbUtils.CurrentSession.Archive_GetPageHeadById(pageid);

            if (page == null)
            {
                throw new PageArchiveLogicNotFoundException(pageid);
            }

            //Retrieve metadata about the deletion to populate page info
            TransactionBE t         = DbUtils.CurrentSession.Transactions_GetById(page.TransactionId);
            DateTime      deleteTs  = DateTime.MinValue;
            UserBE        deletedBy = null;

            if (t != null)
            {
                deletedBy = UserBL.GetUserById(t.UserId);
                deleteTs  = t.TimeStamp;
            }

            return(GetArchivePageXml(ret, page, deletedBy, deleteTs));
        }
Пример #10
0
        public static XDoc RestoreDeletedPage(uint pageid, Title newRootPath, string revertReason)
        {
            //Retrieve initial revisions of pages to restore
            //First item in the list is the page that initiated the transaction.
            //Talk pages are included.
            uint initialDeleteTranId             = 0;
            IList <ArchiveBE> pagesToRestore     = DbUtils.CurrentSession.Archive_GetPagesInTransaction(pageid, out initialDeleteTranId);
            TransactionBE     initialDeleteTrans = DbUtils.CurrentSession.Transactions_GetById(initialDeleteTranId);

            //Validate deleted page + transaction
            if (pagesToRestore.Count == 0)
            {
                throw new PageArchiveLogicNotFoundException(pageid);
            }

            if (initialDeleteTrans == null)
            {
                throw new PageArchiveBadTransactionFatalException(initialDeleteTranId, pageid);
            }

            //TODO MaxM: move the above gathering of what pages to restore to another method. Make this private.

            //Look for title conflicts
            List <Title> titles           = new List <Title>();
            List <ulong> pageidsToRestore = new List <ulong>();
            Dictionary <ulong, PageBE> restoredPagesById = null;
            DateTime utcTimestamp = DateTime.UtcNow;

            foreach (ArchiveBE p in pagesToRestore)
            {
                Title t = p.Title;
                if (newRootPath != null)
                {
                    t = BuildNewTitlesForMovedPage(pagesToRestore[0].Title, p.Title, newRootPath);
                }
                titles.Add(t);

                pageidsToRestore.Add(p.LastPageId);
            }

            IList <PageBE> currentPages = DbUtils.CurrentSession.Pages_GetByTitles(titles.ToArray());

            if (currentPages.Count > 0)
            {
                //Remove all conflicting redirect pages from target of restore if all conflicting pages are redirects
                List <PageBE> conflictingRedirects = new List <PageBE>();
                foreach (PageBE p in currentPages)
                {
                    if (p.IsRedirect)
                    {
                        conflictingRedirects.Add(p);
                    }
                }

                if (currentPages.Count == conflictingRedirects.Count && conflictingRedirects.Count > 0)
                {
                    //Remove existing redirects and refresh the conflicting pages list
                    PageBL.DeletePages(conflictingRedirects.ToArray(), utcTimestamp, 0, false);
                    currentPages = DbUtils.CurrentSession.Pages_GetByTitles(titles.ToArray());
                }
            }

            if (currentPages.Count > 0)
            {
                //return the name(s) of the conflicting page title(s)
                StringBuilder conflictTitles = new StringBuilder();
                foreach (PageBE p in currentPages)
                {
                    if (conflictTitles.Length > 0)
                    {
                        conflictTitles.Append(", ");
                    }

                    conflictTitles.Append(p.Title.AsPrefixedUserFriendlyPath());
                }

                throw new PageArchiveRestoreNamedPageConflictException(conflictTitles.ToString());
            }

            //Gather revisions for all pages to be restored.
            //Revisions are sorted by timestamp: oldest first.
            Dictionary <ulong, IList <ArchiveBE> > revisionsByPageId = DbUtils.CurrentSession.Archive_GetRevisionsByPageIds(pageidsToRestore);

            uint restoredPageTranId = 0;

            try {
                TransactionBE newTrans = new TransactionBE();
                newTrans.UserId    = DekiContext.Current.User.ID;
                newTrans.PageId    = pageid;
                newTrans.Title     = pagesToRestore[0].Title;
                newTrans.Type      = RC.PAGERESTORED;
                newTrans.TimeStamp = DateTime.UtcNow;
                restoredPageTranId = DbUtils.CurrentSession.Transactions_Insert(newTrans);

                //Pages must be restored in correct order (alphabetical ensures parent pages are restored before children).
                //pagesToRestore must be in alphabetical title order

                bool minorChange = false;
                foreach (ArchiveBE pageToRestore in pagesToRestore)
                {
                    IList <ArchiveBE> revisions = null;
                    if (revisionsByPageId.TryGetValue(pageToRestore.LastPageId, out revisions))
                    {
                        //Optionally restore page to different title
                        Title restoreToTitle = pageToRestore.Title;
                        if (newRootPath != null)
                        {
                            restoreToTitle = BuildNewTitlesForMovedPage(pagesToRestore[0].Title, pageToRestore.Title, newRootPath);
                        }

                        RestorePageRevisionsForPage(revisions.ToArray(), restoreToTitle, restoredPageTranId, minorChange, utcTimestamp);
                        DbUtils.CurrentSession.Archive_Delete(revisions.Select(e => e.Id).ToList());
                    }
                    minorChange = true;
                }

                //Retrieve the restored pages
                restoredPagesById = PageBL.GetPagesByIdsPreserveOrder(pageidsToRestore).AsHash(e => e.ID);

                // Restore attachments
                IList <ResourceBE> attachmentsToRestore = ResourceBL.Instance.GetResourcesByChangeSet(initialDeleteTrans.Id, ResourceBE.Type.FILE);
                foreach (ResourceBE at in attachmentsToRestore)
                {
                    PageBE restoredPage;
                    if (restoredPagesById.TryGetValue(at.ParentPageId.Value, out restoredPage))
                    {
                        AttachmentBL.Instance.RestoreAttachment(at, restoredPage, utcTimestamp, restoredPageTranId);
                    }
                }

                //Update the old transaction as reverted
                initialDeleteTrans.Reverted        = true;
                initialDeleteTrans.RevertTimeStamp = utcTimestamp;
                initialDeleteTrans.RevertUserId    = DekiContext.Current.User.ID;
                initialDeleteTrans.RevertReason    = revertReason;
                DbUtils.CurrentSession.Transactions_Update(initialDeleteTrans);
            } catch (Exception) {
                DbUtils.CurrentSession.Transactions_Delete(restoredPageTranId);
                throw;
            }

            //Build restore summary
            XDoc ret = new XDoc("pages.restored");

            foreach (ulong restoredPageId in pageidsToRestore)
            {
                PageBE restoredPage = null;
                if (restoredPagesById.TryGetValue((uint)restoredPageId, out restoredPage))
                {
                    ret.Add(PageBL.GetPageXml(restoredPage, string.Empty));
                }
            }

            return(ret);
        }