void PopulateDatabase()
    {
        DateTime             start = DateTime.Now;
        ReadWriteTransaction t     = db.StartReadWriteTransaction();

        if (t.Verbs.Count == 0)
        {
            int    nBooks = nAuthors * nBooksPerAutor / nCoauthors;
            Item[] author = new Item[nAuthors];
            for (int i = 0; i < nAuthors; i++)
            {
                author[i] = t.CreateItem();
                t.Link(author[i], "name", GenerateName(i));
                t.IncludeInFullTextIndex(author[i]);
            }
            for (int i = 0, j = 0; i < nBooks; i++)
            {
                Item book = t.CreateItem();
                t.Link(book, "title", GenerateTitle(i));
                t.Link(book, "ISBN", GenerateISBN(i));
                t.Link(book, "publish-date", DateTime.Now.ToString());
                t.IncludeInFullTextIndex(book, new String[] { "title", "ISBN" });
                for (int k = 0; k < nCoauthors; k++)
                {
                    t.Link(book, "author", author[j++ % nAuthors]);
                }
            }
            Console.WriteLine("Elapsed time for populating database: " + (DateTime.Now - start));
        }
        t.Commit();
    }
    void UpdateDatabase()
    {
        ReadWriteTransaction t = db.StartReadWriteTransaction();
        Item patient           = Enumerable.First(t.Find(Predicate.Value("class") == "patient"
                                                         & Predicate.Value("name") == "John Smith"));

        t.Update(patient, "age", 56);
        t.Commit();
    }
    void PopulateDatabase()
    {
        ReadWriteTransaction t = db.StartReadWriteTransaction();

        Item patient = t.CreateItem();

        t.Link(patient, "class", "patient");
        t.Link(patient, "name", "John Smith");
        t.Link(patient, "age", 55);
        t.Link(patient, "wight", 65.7);
        t.Link(patient, "sex", "male");
        t.Link(patient, "phone", "1234567");
        t.Link(patient, "address", "123456, CA, Dummyngton, Outlook drive, 17");

        Item doctor = t.CreateItem();

        t.Link(doctor, "class", "doctor");
        t.Link(doctor, "name", "Robby Wood");
        t.Link(doctor, "speciality", "therapeutist");

        t.Link(doctor, "patient", patient);

        Item angina = t.CreateItem();

        t.Link(angina, "class", "disease");
        t.Link(angina, "name", "angina");
        t.Link(angina, "symptoms", "throat ache");
        t.Link(angina, "symptoms", "high temperature");
        t.Link(angina, "treatment", "milk&honey");

        Item flu = t.CreateItem();

        t.Link(flu, "class", "disease");
        t.Link(flu, "name", "flu");
        t.Link(flu, "symptoms", "stomachache");
        t.Link(flu, "symptoms", "high temperature");
        t.Link(flu, "treatment", "theraflu");

        Item diagnosis = t.CreateItem();

        t.Link(diagnosis, "class", "diagnosis");
        t.Link(diagnosis, "disease", flu);
        t.Link(diagnosis, "symptoms", "high temperature");
        t.Link(diagnosis, "diagnosed-by", doctor);
        t.Link(diagnosis, "date", "2010-09-23");
        t.Link(patient, "diagnosis", diagnosis);

        t.Commit();
    }
Exemple #4
0
        /// <summary>
        /// Gets called in regular intervals from a Timer in Class TTimedProcessing.
        /// </summary>
        /// <param name="ADataBaseObj">Already instatiated DB Access object with opened DB connection.</param>
        /// <param name="ARunManually">this is true if the process was called manually from the server admin console</param>
        public static void Process(TDataBase ADataBaseObj, bool ARunManually)
        {
            TDBTransaction        ReadWriteTransaction;
            bool                  NewTransaction;
            bool                  LastReminderDateAcquired;
            DateTime              LastReminderDate;
            DataSet               ReminderResultsDS;
            SSystemDefaultsRow    SystemDefaultsDR;
            PPartnerReminderTable PartnerReminderDT;
            int         ReminderFreqency;
            TSmtpSender Sender = null;

            if (TLogging.DebugLevel >= 6)
            {
                TLogging.Log("Entering TProcessPartnerReminders.Process...");
            }

            ReadWriteTransaction = ADataBaseObj.GetNewOrExistingTransaction(IsolationLevel.ReadCommitted,
                                                                            out NewTransaction);

            /*
             * This whole process must either succeed or fail, therefore the whole thing is in a try-catch.
             */
            try
            {
                /*
                 * Obtain date when PartnerReminders last ran. This is stored in a SystemDefault. If it doesn't exist already,
                 * a new SystemDefault with an ancient date is created for us.
                 */
                LastReminderDateAcquired = GetLastReminderDate(out LastReminderDate, out SystemDefaultsDR, ReadWriteTransaction);

                if (!LastReminderDateAcquired)
                {
                    TLogging.Log(
                        TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                        ": Could not send Partner Reminders because Petra couldn't create the required SystemDefault setting for the Last Reminder Date!");

                    ReadWriteTransaction.Rollback();

                    return;
                }

                try
                {
                    Sender = new TSmtpSender();
                }
                catch (ESmtpSenderInitializeException e)
                {
                    TLogging.Log(
                        TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                        ": " + e.Message);

                    if (e.InnerException != null)
                    {
                        TLogging.Log(e.InnerException.ToString(), TLoggingType.ToLogfile);
                    }

                    return;
                }

                // Retrieve all PartnerReminders we need to process.
                ReminderResultsDS = GetRemindersToProcess(LastReminderDate, out PartnerReminderDT,
                                                          ADataBaseObj, ReadWriteTransaction);

                /*
                 * We now have a Typed DataTable with the PartnerReminders that we need to process.
                 * Iterate through the PartnerReminders, update data, and send an email for each PartnerReminder.
                 */
                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                    TLogging.Log("PartnerReminders data before we start processing all PartnerReminders....");
                    TLogging.Log(ReminderResultsDS.GetXml().ToString());
                }

                foreach (PPartnerReminderRow PartnerReminderDR in PartnerReminderDT.Rows)
                {
                    if (TLogging.DebugLevel >= 4)
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Processing Reminder ID {0} for Partner {1}.", PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                    }

                    ReminderFreqency = (PartnerReminderDR.IsReminderFrequencyNull()) ? 0 : PartnerReminderDR.ReminderFrequency;

                    PartnerReminderDR.BeginEdit();
                    PartnerReminderDR.LastReminderSent = DateTime.Now.Date;
                    if (ReminderFreqency == 0)
                    {
                        PartnerReminderDR.SetNextReminderDateNull();
                    }
                    else
                    {
                        PartnerReminderDR.NextReminderDate = DateTime.Now.Date.AddDays(ReminderFreqency);
                    }

                    if (!PartnerReminderDR.IsEventDateNull())   // Reminder has an Event Date
                    {
                        if (PartnerReminderDR.NextReminderDate > PartnerReminderDR.EventDate)
                        {
                            if (TLogging.DebugLevel >= 5)
                            {
                                TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                           ": Deactivating Reminder ID {0} for Partner {1} as its Event Date is in the past.",
                                                           PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                            }

                            PartnerReminderDR.ReminderActive = false;
                        }
                    }

                    if (TLogging.DebugLevel >= 4)
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Sending email for Reminder ID {0} for Partner {1}.", PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                    }

                    Boolean emailSentOk = false;
                    try
                    {
                        emailSentOk = SendReminderEmail(PartnerReminderDR, ReadWriteTransaction, Sender);
                    }
                    catch (ESmtpSenderInitializeException e) // if an exception was thrown, assume the email didn't go.
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Reminder ID {0} for Partner {1}: {2}", PartnerReminderDR.ReminderId,
                                                   PartnerReminderDR.PartnerKey,
                                                   e.Message));

                        if (e.InnerException != null)
                        {
                            TLogging.Log(e.InnerException.Message);
                        }
                    }
                    catch (ESmtpSenderSendException e)
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Reminder ID {0} for Partner {1}: {2}", PartnerReminderDR.ReminderId,
                                                   PartnerReminderDR.PartnerKey,
                                                   e.Message));

                        if (e.InnerException != null)
                        {
                            TLogging.Log(e.InnerException.Message);
                        }
                    }
                    catch (Exception e)
                    {
                        TLogging.Log(e.Message);
                    }

                    if (emailSentOk)
                    {
                        // Accept the edit
                        if (TLogging.DebugLevel >= 4)
                        {
                            TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                       ": Reminder ID {0} for Partner {1} accepted by SMTP server.", PartnerReminderDR.ReminderId,
                                                       PartnerReminderDR.PartnerKey));
                        }

                        PartnerReminderDR.EndEdit();
                    }
                    else
                    {
                        // Cancel the edit
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Reminder ID {0} for Partner {1} REJECTED by SMTP server.", PartnerReminderDR.ReminderId,
                                                   PartnerReminderDR.PartnerKey));

                        PartnerReminderDR.CancelEdit();
                    }
                }

                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                    TLogging.Log("PartnerReminders data after processing all PartnerReminders, before writing it to DB....");
                    TLogging.Log(PartnerReminderDT.DataSet.GetXml().ToString());
                }

                // Update all the changed PartnerReminder Rows
                PPartnerReminderAccess.SubmitChanges(PartnerReminderDT, ReadWriteTransaction);

                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                }

                /*
                 * Update the SystemDefault that keeps track of when Partner Reminders last ran.
                 * (SystemDefaultsDR will point to the row we loaded earlier on, OR the row we added earlier on
                 * if there wasn't already a SystemDefault row.)
                 */
                UpdateLastReminderDate(SystemDefaultsDR, ReadWriteTransaction);

                if (NewTransaction)
                {
                    ReadWriteTransaction.Commit();
                }

                TLogging.LogAtLevel(1, TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing + " ran succesfully.");
            }
            catch (Exception Exc)
            {
                TLogging.Log(
                    TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing + " encountered an Exception:" + Environment.NewLine +
                    Exc.ToString());

                if (NewTransaction)
                {
                    ReadWriteTransaction.Rollback();
                }

                throw;
            }
            finally
            {
                if (Sender != null)
                {
                    Sender.Dispose();
                }
            }
        }