Esempio n. 1
0
        public bool IsTemplateForReference(ConditionalTemplate template, Citation citation)
        {
            if (citation == null)
            {
                return(false);
            }
            if (citation.Reference == null)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(citation.Reference.YearResolved))
            {
                return(false);
            }

            DateTime result;

            if (!DateTimeInformation.TryParse(citation.Reference.YearResolved, out result))
            {
                return(false);
            }

            if (result.Year > 1800)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Generates the key with the temporal and computer specific attributes
        /// </summary>
        protected StringBuilder GenerateKey()
        {
            //-----a----- Start building the encrypted string
            StringBuilder tokenToEncrypt = new StringBuilder();

            tokenToEncrypt.Append(MGLEncryption.GetSalt(1));

            //-----b----- Double check that the anonID cookie has been set and set it again if not... (this should never not have happened as it is called first OnInit in the MGLBasePage)
            SetAnonIDCookie();

            //-----c----- The AnonID is a GUID - always 36 characters
            string tempValue = DefaultAnonID;

            if (Request.Cookies["AnonID"] != null)
            {
                tempValue = Request.Cookies["AnonID"].Value;
            }
            tokenToEncrypt.Append(tempValue);

            //-----d----- Add some padding
            tokenToEncrypt.Append(MGLEncryption.GetSalt(1));

            //-----e----- The date time will be of the form yyyy-mm-dd hh:mm:ss - and will always be dd and mm (not d and m for e.g. 01)
            tokenToEncrypt.Append(DateTimeInformation.FormatDatabaseDate(DateTime.Now, true, true));
            tokenToEncrypt.Append(MGLEncryption.GetSalt(1));

            //-----f----- Do the encryption itself
            StringBuilder authKey = MGLEncryption.Encrypt(tokenToEncrypt);

            //-----g----- And lastly, lets make this key HTML secure ...
            authKey = MGLEncryption.HTMLifyString(authKey);

            return(authKey);
        }
Esempio n. 3
0
        private List <Segment> GetSegments(string completeString)
        {
            List <Segment> segments = new List <Segment>();

            if (string.IsNullOrEmpty(completeString))
            {
                return(segments);
            }

            List <DateTimeMatch> dateTimeMatches = DateTimeInformation.Matches(completeString);

            if (dateTimeMatches.Count == 0)
            {
                segments.Add(new Segment()
                {
                    text = completeString, type = SegmentType.Text, dateTime = SwissAcademic.Environment.NullDate
                });
                return(segments);
            }

            int currentPosition = 0;

            foreach (DateTimeMatch dateTimeMatch in dateTimeMatches)
            {
                int matchPosition = dateTimeMatch.Match.Index;
                int matchLength   = dateTimeMatch.Match.Length;

                if (matchPosition > currentPosition)
                {
                    //we add a simple string segment
                    segments.Add(new Segment()
                    {
                        text = completeString.Substring(currentPosition, matchPosition - currentPosition), type = SegmentType.Text, dateTime = SwissAcademic.Environment.NullDate
                    });
                    currentPosition = matchPosition + matchLength;
                }

                //we add the found date string segment
                segments.Add(new Segment()
                {
                    text     = completeString.Substring(matchPosition, matchLength),
                    type     = SegmentType.DateTime,
                    dateTime = dateTimeMatch.DateTime,
                    ContainsDayInformation   = !dateTimeMatch.MissingDayWasAutoCompleted,
                    ContainsMonthInformation = !dateTimeMatch.MissingMonthWasAutoCompleted
                });
                currentPosition = matchPosition + matchLength;
            }

            if (currentPosition <= completeString.Length - 1)
            {
                //we add a remaining string segment
                segments.Add(new Segment()
                {
                    text = completeString.Substring(currentPosition, completeString.Length - currentPosition), type = SegmentType.Text, dateTime = SwissAcademic.Environment.NullDate
                });
            }

            return(segments);
        }
Esempio n. 4
0
        //ConfigurationInfo ci;

        ////-------------------------------------------------------------------------------------------------------------------------------------------------------------
        //public LocaliseTime(ConfigurationInfo ci) {
        //    this.ci = ci;
        //}


        //-------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     localisationOption can be 1=System default (normally UTC), 2=Frontend default, 3=User last login / access,
        ///     DEPRECATED - 4 - DEPRECATED=current login credentials
        ///     If 3 is not available, will default to 2; if 2 not available, will default to 1
        ///     4 is only really relevant in a couple of special cases, the PasswordResetRequest and the PasswordReset, as the user does
        ///     not actually login to complete these.  We now handle the collection of this information in these two pages, if it differs from
        ///     previous information.
        /// </summary>
        public static bool Localise(ConfigurationInfo ci, DateTime utcDT, int localisationOption, int userID, out DateTime localDT)
        {
            bool success = false;

            localDT = utcDT;
            int timezoneOffset = 0;

            try {
                if (DateTimeInformation.IsNullDate(utcDT) == false)
                {
                    if (localisationOption > 3 || localisationOption < 1 || (localisationOption == 3 && userID == 0))
                    {
                        Logger.LogError(6, "Unknown or invalid localisation option provided (" + localisationOption + " with userID " + userID + ") in LocaliseTime.Localise: " + Environment.StackTrace);
                    }
                    else
                    {
                        // try with 3
                        if (localisationOption == 3)
                        {
                            success = GetTimezoneOffset(ci, 0, userID, out timezoneOffset);
                        }

                        // try with 2 or if 3 failed
                        if (localisationOption == 2 || (localisationOption == 3 && success == false))
                        {
                            success = GetTimezoneOffset(ci, 2, 0, out timezoneOffset);
                        }

                        // try with 1 or if 3 failed
                        if (localisationOption == 1 || success == false)
                        {
                            success = GetTimezoneOffset(ci, 1, 0, out timezoneOffset);
                        }

                        if (success == true)
                        {
                            // Timezones ahead of GMT / UTC actually are negative (a bit counterintuitively) so these will need to be added
                            if (timezoneOffset == 0)
                            {
                                // Do nothing ...
                            }
                            else if (timezoneOffset < 0)
                            {
                                localDT = utcDT.Add(new TimeSpan(0, timezoneOffset * -1, 0));
                            }
                            else
                            {
                                localDT = utcDT.Subtract(new TimeSpan(0, timezoneOffset, 0));
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.LogError(5, "Problem localising the given date time (" + utcDT + ") using localisation option " + localisationOption + " and userID " + userID + ".  Check it out:" + ex.ToString());
            } finally {
            }

            return(success);
        }
Esempio n. 5
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Optimised localisation where the timezone offset is known and can be utilised for multiple entries
        ///     e.g. the search and export functions...
        ///     Note that this always returns true!
        ///     Returns the local date formulated in a pretty string ...
        /// </summary>
        public static string Localise(int timezoneOffset, DateTime utcDT)
        {
            DateTime localDT = Localise(utcDT, timezoneOffset);

            string prettyLocalDate = DateTimeInformation.PrettyDateTimeFormat(localDT, timezoneOffset);

            return(prettyLocalDate);
        }
Esempio n. 6
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Parses the User Information from the database
        /// </summary>
        private MGUser BuildUserInfo(string[] row)
        {
            MGUser user = null;

            try {
                if (row != null)
                {
                    user = new MGUser();

                    // These are the fields ....
                    //                protected static readonly string userFields = " ID, UserName, Password, FirstName, LastName, JobTitle, Organisation, Telephone, Email, NumberOfIncorrectLogins,
                    //                TotalLogins, Description, StartDate, LastLoginDate, LastIP, LastBrowser";

                    int i       = 0;
                    int tempInt = 0;

                    int.TryParse(row[i++], out tempInt);              // ID
                    user.ID = tempInt;

                    user.Username = SecureStringWrapper.Encrypt(row[i++]);                             // UserName
                    user.Password = SecureStringWrapper.Encrypt(row[i++]);                             // Password

                    // June 2013 - these are New new new NEW!
                    user.FirstName = SecureStringWrapper.Encrypt(row[i++]);    // FirstName
                    user.LastName  = SecureStringWrapper.Encrypt(row[i++]);    // LastName

                    user.JobTitle     = SecureStringWrapper.Encrypt(row[i++]); // JobTitle
                    user.Organisation = SecureStringWrapper.Encrypt(row[i++]); // Organisation
                    user.Telephone    = SecureStringWrapper.Encrypt(row[i++]); // Telephone
                    user.Email        = SecureStringWrapper.Encrypt(row[i++]); // Email

                    int.TryParse(row[i++], out tempInt);                       // NumberOfIncorrectLogins
                    user.NumIncorrectLogins = tempInt;

                    int.TryParse(row[i++], out tempInt);            // Total Logins ...
                    user.TotalLogins = tempInt;

                    user.Description = row[i++];                        // Description

                    // 13-Oct-2015 - bug!!  The start date was not being set - it is now ....
                    user.StartDate = DateTimeInformation.FormatDate(row[i++], true, true); // Start Date
                    user.LastLogin = DateTimeInformation.FormatDate(row[i++], true, true); // Last Login Date

                    user.LastIP      = row[i++];                                           // Last IP ...
                    user.LastBrowser = row[i++];                                           // Last Browser ...

                    int.TryParse(row[i++], out tempInt);                                   // Organisation ID ...
                    user.OrganisationID = tempInt;
                }
            } catch (Exception ex) {
                Logger.LogError(8, "Error Parsing the MGUser: " + ex.ToString());
            }

            return(user);
        }
Esempio n. 7
0
        //---------------------------------------------------------------------------------------------------------------------------------------------------------------
        public bool LogLogin(int userID, bool successful)
        {
            ////////////////////////////////////////
            // increment the total logins, clear the number of incorrect logins ....
            // add todays date, IP address etc ....

            if (successful)
            {
                dbInfo.ExecuteSQL("UPDATE " + tnUsers + " SET TotalLogins=TotalLogins+1, NumberOfIncorrectLogins=0 WHERE ID=" + userID + ";", ref successful);

                //Update other bits of info in the user table
                StringBuilder builder = new StringBuilder();

                builder.Append("UPDATE " + tnUsers + " SET LastLoginDate = '");
                builder.Append(DateTimeInformation.GetUniversalDateTime(DateTime.Now).ToString());
                builder.Append("' WHERE ID = ");
                builder.Append(userID);
                builder.Append(";");

                // Last IP
                builder.Append("UPDATE " + tnUsers + " SET LastIP = ");
                builder.Append("'");
                // 27-Nov-2015 - Converted to use this v4IPAddress method.
                builder.Append(IPAddressHelper.GetIP4OrAnyAddressFromHTTPRequest());
//                builder.Append(HttpContext.Current.Request.UserHostAddress);
                builder.Append("' ");
                builder.Append(" WHERE ID = ");
                builder.Append(userID);
                builder.Append(";");

                // Last browser
                builder.Append("UPDATE " + tnUsers + " SET LastBrowser = ");
                builder.Append("'");
                builder.Append(HttpContext.Current.Request.Browser.Browser);
                builder.Append(" ");
                builder.Append(HttpContext.Current.Request.Browser.Version);
                builder.Append("'");
                builder.Append(" WHERE ID = ");
                builder.Append(userID);
                builder.Append(";");

                dbInfo.ExecuteSQL(builder.ToString(), ref successful);
            }
            else
            {
                dbInfo.ExecuteSQL("UPDATE " + tnUsers + " SET NumberOfIncorrectLogins=NumberOfIncorrectLogins+1 WHERE ID=" + userID + ";", ref successful);
            }
            //            return ! Logger.LogList(dbInfo.GetErrors(), "UserOperations", "LogLogin");
            return(successful);
        }
Esempio n. 8
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     localisationOption can be 1=System default (normally UTC), 2=Frontend default, 3=User last login / access,
        ///     DEPRECATED - 4 - DEPRECATED=current login credentials
        ///     If 3 is not available, will default to 2; if 2 not available, will default to 1
        ///     4 is only really relevant in a couple of special cases, the PasswordResetRequest and the PasswordReset, as the user does
        ///     not actually login to complete these.  We now handle the collection of this information in these two pages, if it differs from
        ///     previous information.
        /// </summary>
        public static bool Localise(ConfigurationInfo ci, DateTime utcDT, int localisationOption, int userID, out string prettyLocalDate)
        {
            bool success = false;

            prettyLocalDate = "";
            DateTime localDT = utcDT;

            success = Localise(ci, utcDT, localisationOption, userID, out localDT);

            // have to get the difference between the utcDT and the localDT so that we can generate the pretty version ...
            if (success == true)
            {
                TimeSpan t1           = utcDT.Subtract(localDT);
                int      totalMinutes = (int)Math.Round(t1.TotalMinutes);
                prettyLocalDate = DateTimeInformation.PrettyDateTimeFormat(localDT, totalMinutes);
            }


            return(success);
        }
Esempio n. 9
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     The excel export code needs this version ...
        ///     Optimised localisation where the timezone offset is known and can be utilised for multiple entries
        ///     e.g. the search and export functions...
        ///     Note that this always returns true!
        ///     Returns the localised datetime object
        /// </summary>
        public static DateTime Localise(DateTime utcDT, int timezoneOffset)
        {
            DateTime localDate = utcDT;

            if (DateTimeInformation.IsNullDate(utcDT) == false)
            {
                // Timezones ahead of GMT / UTC actually are negative (a bit counterintuitively) so these will need to be added
                if (timezoneOffset == 0)
                {
                    // Do nothing ...
                }
                else if (timezoneOffset < 0)
                {
                    localDate = utcDT.Add(new TimeSpan(0, timezoneOffset * -1, 0));
                }
                else
                {
                    localDate = utcDT.Subtract(new TimeSpan(0, timezoneOffset, 0));
                }
            }

            return(localDate);
        }
Esempio n. 10
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     14-Oct-13 - New method to log the page request information in the database ...
        /// </summary>
        public static void LogPageRequestInDatabase(ConfigurationInfo ci, string applicationName, string aspSessionID, string uniqueSessionID,
                                                    string pageURL, DateTime pageRequestTime, double serverProcessingMS, int currentUserID, string ipAddress)
        {
            bool success = false;

            DatabaseWrapper dbInfo = null;

            try {
                //_____ Check for a null configurationInfo or dbConInfo object
                if (ci == null || ci.DbConInfo.NAME == null)
                {
                    Logger.LogError(200, "Error inserting the PageRequest log into the database - null configurationInfo found, probably because of an application reset or session timeout.");
                }
                else
                {
                    // 3-Mar-2016 - ignore specific IP addresses (e.g. robots like the UptimeRobot), as these are artificially bloating the logs with limited utility!
                    if (ipAddress == null || AddressesToIgnore.Contains(ipAddress) == false)
                    {
                        //_____ Check for s***e data and make it blank if so, so it doesn't kill the database ...
                        applicationName = (applicationName == null) ? "" : applicationName;
                        aspSessionID    = (aspSessionID == null) ? "" : aspSessionID;
                        uniqueSessionID = (uniqueSessionID == null) ? "" : uniqueSessionID;
                        pageURL         = (pageURL == null) ? "" : pageURL;

                        //_____ Connect to the database ...
                        dbInfo = new DatabaseWrapper(ci);
                        dbInfo.Connect();

                        //_____ Create the table if it does not already exist
                        string createTableSQL = @"
                    CREATE TABLE " + logTN + @" (
                        ID int NOT NULL Auto_Increment, PRIMARY KEY(ID),
                        Application_Name VARCHAR(255), INDEX Application_Name(Application_Name),
                        Session_ID_ASP VARCHAR(50), INDEX Session_ID_ASP(Session_ID_ASP),
                        Session_ID_Unique VARCHAR(50), INDEX Session_ID_Unique(Session_ID_Unique),
                        Page_Name VARCHAR(255), INDEX Page_Name(Page_Name),
                        Page_URL LONGTEXT,
                        Page_Request_Date DATETIME, INDEX Page_Request_Date(Page_Request_Date),
                        Server_Render_Speed DOUBLE, INDEX Server_Render_Speed(Server_Render_Speed),
                        Current_User_ID INT, INDEX Current_User_ID(Current_User_ID),
                        IP_Address	VARCHAR(20),    INDEX IP_Address(IP_Address)
                    ) ENGINE=MyISAM;
                ";

                        if (dbInfo.TableExists(logTN) == false)
                        {
                            dbInfo.ExecuteSQL(createTableSQL, ref success);
                        }

                        //_____ Parse the page name
                        string pageName = "";
                        {
                            // remove the ?...... page params
                            string[] bits1 = pageURL.Split(new string[] { "?", "&" }, StringSplitOptions.None);
                            string[] bits2 = bits1[0].Split(new string[] { "/", "\\" }, StringSplitOptions.None);
                            pageName = bits2[bits2.Length - 1];
                        }

                        //_____ Build the SQL and run the insert ...
                        StringBuilder sql = new StringBuilder();
                        sql.Append("INSERT INTO " + logTN
                                   + " (Application_Name, Session_ID_ASP, Session_ID_Unique, Page_Name, Page_URL, Page_Request_Date, Server_Render_Speed, Current_User_ID, IP_Address ) VALUES (");
                        sql.Append(DataUtilities.Quote(applicationName) + ", ");
                        sql.Append(DataUtilities.Quote(aspSessionID) + ", ");
                        sql.Append(DataUtilities.Quote(uniqueSessionID) + ", ");
                        sql.Append(DataUtilities.Quote(pageName) + ", ");
                        sql.Append(DataUtilities.Quote(pageURL) + ", ");
                        sql.Append(DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(pageRequestTime, true, true)) + ", ");
                        sql.Append(serverProcessingMS + ", ");
                        sql.Append(currentUserID + ", ");
                        sql.Append(DataUtilities.DatabaseifyString(ipAddress, false));

                        sql.Append(");");

                        int numInserts = dbInfo.ExecuteSQL(sql.ToString(), ref success);

                        if (success == false)
                        {
                            Logger.LogError(201, "Error inserting the PageRequest log into the database - check the detailed log for details.");
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.LogError(202, "Error inserting the PageRequest log into the database: " + ex.ToString());
            } finally {
                if (dbInfo != null)
                {
                    dbInfo.Disconnect();
                }
            }
        }
Esempio n. 11
0
        //--------------------------------------------------------------------------------------------------------------------------------------------------------------
        public static void PerformanceTestMySQLWrite()
        {
            Logger.LogSubHeading("Testing MySQL!");
            bool success = false;

            // Now lets connect to the MySQL db
            Logger.Log("Connecting to the database...");
            DatabaseWrapper dbInfo = new DatabaseWrapper(new DatabaseConnectionInfo("localhost", "mysql", "redis_comparison",
                                                                                    SecureStringWrapper.Encrypt("forum"), SecureStringWrapper.Encrypt(TestParameters.RedisAuth), 3306));

            dbInfo.Connect();

            if (TestParameters.DoWrite == true)
            {
                bool tnExists = dbInfo.TableExists(TestObj.ObjectName);

                // Drop the table if it exists already ...
                bool tnDeleted = dbInfo.DeleteTable(TestObj.ObjectName);

                // And then recreate it ...
                // 22-Aug-2016 - Remove the boolean index to mirror the Redis implementation - Index TestBool(TestBool),
                bool tnCreated = dbInfo.CreateTable(TestObj.ObjectName, @"
                ID bigint unsigned,	    Primary Key(ID),
                TestInt int,                    Index TestInt(TestInt),
                TestLong bigint,        Index TestLong(TestLong),
                TestDouble double,      Index TestDouble(TestDouble),
                TestBool bool,          
                TestStr Varchar(20),    Index TestStr(TestStr),
                TestDT DateTime,        Index TestDT(TestDT)
            ", false);
            }

            Logger.Log("Writing data");
            DateTime writeStart = DateTime.Now;

            if (TestParameters.DoWrite == true)
            {
                int writeCount = 0;
                foreach (object o in TestParameters.RandomObjects)
                {
                    TestObj rto = (TestObj)o;

                    StringBuilder sql = new StringBuilder();

                    sql.Append("INSERT INTO " + TestObj.ObjectName + " (ID, TestInt, TestLong, TestDouble, TestBool, TestStr, TestDT) VALUES (");
                    sql.Append(rto.ID + ",");
                    sql.Append(rto.TestInt + ",");
                    sql.Append(rto.TestLong + ",");
                    sql.Append(rto.TestDouble + ",");
                    sql.Append(rto.TestBool + ",");
                    sql.Append(DataUtilities.Quote(rto.TestStr) + ",");
                    sql.Append(DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(rto.TestDT, true, true)));
                    sql.Append(");");
                    dbInfo.ExecuteSQL(sql.ToString(), ref success);

                    Logger.Log(++writeCount, 100, TestParameters.RandomObjects.Count);
                }
            }
            Logger.Log("");
            Logger.Log("Writing completed.");
            TestParameters.WriteMySQL = DateTime.Now.Subtract(writeStart);

            Logger.Log("Disconnect from the database.");
            dbInfo.Disconnect();
        }
Esempio n. 12
0
        //--------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     So this loads the full object for each search match found ... but does not persist those as that would likely kill the memory allocation
        /// </summary>
        /// <param name="chunkSize"></param>
        /// <param name="startIndex"></param>
        public static void PerformanceTestMySQLThreadBlock(int chunkSize, int startIndex)
        {
            Logger.LogSubHeading("Testing MySQL!");
            //bool success = false;

            int i = startIndex;

            if ((startIndex + chunkSize) >= TestParameters.PatternsToSearchFor.Count)
            {
                chunkSize = TestParameters.PatternsToSearchFor.Count - startIndex;
            }

            Logger.Log("Starting chunk with index " + startIndex + " and size " + chunkSize + " ...");


//            HashSet<ulong> ids = new HashSet<ulong>();

            // Now lets connect to the MySQL db
            Logger.Log("Connecting to the database...");
            DatabaseWrapper dbInfo = new DatabaseWrapper(new DatabaseConnectionInfo("localhost", "mysql", "redis_comparison",
                                                                                    SecureStringWrapper.Encrypt("forum"), SecureStringWrapper.Encrypt(TestParameters.RedisAuth), 3306));

            dbInfo.Connect();


            Logger.Log("Reading data");
            StringBuilder log      = new StringBuilder();
            StringBuilder queryTxt = new StringBuilder();

            //long searchIndex = startIndex;

            for (i = startIndex; i < (startIndex + chunkSize); i++)
            {
                List <DatabaseSearchPattern> rsps = TestParameters.PatternsToSearchFor[i];

                /////////////
                //bool storeQuery = false;

                StringBuilder sql          = new StringBuilder();
                StringBuilder searchParams = new StringBuilder();


                // And build the query here ...
                sql.Append("SELECT ID, TestInt, TestLong, TestDouble, TestBool, TestStr, TestDT FROM " + TestObj.ObjectName + " WHERE ");
                foreach (DatabaseSearchPattern rsp in rsps)
                {
                    if (searchParams.Length > 0)
                    {
                        searchParams.Append(" AND ");
                    }

                    if (rsp.SearchType == SearchType.PrimaryKey)
                    {
                        searchParams.Append(" " + rsp.Parameter + "=" + rsp.Score);
                    }
                    else if (rsp.SearchType == SearchType.Text)
                    {
                        searchParams.Append(" " + rsp.Parameter + " LIKE " + DataUtilities.Quote(rsp.PatternStartsWith(RedisWrapper.TextIndexLength) + "%"));
                        //searchParams.Append(" " + rsp.Parameter + " LIKE " + DataUtilities.Quote(rsp.Pattern.Substring(0, 1) + "%"));
                    }
                    else if (rsp.SearchType == SearchType.Bool)
                    {
                        searchParams.Append(" " + rsp.Parameter + "=" + rsp.Score);
                    }
                    else if (rsp.SearchType == SearchType.DateTime)
                    {
                        // could also add switches on ==, >= and <=
                        if (rsp.SearchComparisonExpression == SearchComparisonExpression.Equivalent)
                        {
                            //if (rsp.ScoreMin == rsp.ScoreMax) {                                    // an exact date and time
                            searchParams.Append(rsp.Parameter + " = "
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime(rsp.Score), true, true)));
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeLessThanOrEqualTo)
                        {
                            //} else if (rsp.ScoreMin == DateTime.MinValue.Ticks) {
                            // less than or equal to ...
                            //                            searchParams.Append(rsp.Parameter + " <= "
                            //                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)Math.Round(rsp.ScoreMax)), true, true)));
                            // Note that we want to search inclusively.  Which means we need to search less than the specified max day PLUS A DAY
                            // equivalent to <= 2016-04-20 23:59:59 to ensure that the day itself is included ....

                            //DateTime dt = new DateTime((long)Math.Round(rsp.ScoreMax));
                            DateTime dt = new DateTime((long)rsp.ScoreMax);

                            searchParams.Append(rsp.Parameter + " <= "
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(dt, true, true)));

                            /////////////////////////////////////
                            //string checkTHIS = "";
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeGreaterThanOrEqualTo)
                        {
                            //} else if (rsp.ScoreMax == DateTime.MaxValue.Ticks) {           // greater than or equal to ...

                            //DateTime dt = new DateTime((long)Math.Round(rsp.ScoreMin));
                            DateTime dt = new DateTime((long)rsp.ScoreMin);

                            searchParams.Append(rsp.Parameter + " >= "
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(dt, true, true)));

                            /////////////////////////////////////
                            //string checkTHIS = rsp.AsText();
                            //checkTHIS = rsp.AsText();
                            //storeQuery = true;
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeBetween)
                        {
                            //                        } else {                                                                            // Must be a real between

                            // Note that we want to search inclusively.  Which means we need to search less than the specified max day PLUS A DAY
                            // equivalent to <= 2016-04-20 23:59:59 to ensure that the day itself is included ....
                            searchParams.Append(rsp.Parameter + " BETWEEN "
                                                //+ DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)Math.Round(rsp.ScoreMin)), true, true))
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)rsp.ScoreMin), true, true))
                                                + " AND "
                                                //+ DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)Math.Round(rsp.ScoreMax)), true, true)));
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)rsp.ScoreMax), true, true)));
                        }
                        else
                        {
                            string ohFuck = "";
                        }
                    }
                    else if (rsp.SearchType == SearchType.Score)
                    {
                        if (rsp.SearchComparisonExpression == SearchComparisonExpression.Equivalent)
                        {
                            //if (rsp.ScoreMin == rsp.ScoreMax) {                                    // an exact value
                            searchParams.Append(rsp.Parameter + " = " + rsp.ScoreMin.ToString("0." + new string('#', 339)));
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeLessThanOrEqualTo)
                        {
                            //} else if (rsp.ScoreMin == Double.MinValue) {           // less than or equal to ...
                            searchParams.Append(rsp.Parameter + " <= " + rsp.ScoreMax.ToString("0." + new string('#', 339)));
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeGreaterThanOrEqualTo)
                        {
                            //} else if (rsp.ScoreMax == Double.MaxValue) {           // greater than or equal to ...
                            searchParams.Append(rsp.Parameter + " >= " + rsp.ScoreMin.ToString("0." + new string('#', 339)));
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeBetween)
                        {
                            //} else {                                                                            // Must be a real between
                            searchParams.Append(rsp.Parameter + " BETWEEN "
                                                + rsp.ScoreMin.ToString("0." + new string('#', 339)) + " AND "
                                                + rsp.ScoreMax.ToString("0." + new string('#', 339)));
                        }
                        else
                        {
                            string ohFuck = "";
                        }
                    }
                }
                sql.Append(searchParams);
                sql.Append(";");

                List <string[]> sqlData = dbInfo.GetDataList(sql.ToString());

                int objCount = 0;
                if (sqlData != null)
                {
                    objCount = sqlData.Count;

                    foreach (string[] row in sqlData)
                    {
                        ulong id = 0;
                        ulong.TryParse(row[0], out id);

                        int testInt = 0;
                        int.TryParse(row[1], out testInt);

                        long testLong = 0;
                        long.TryParse(row[2], out testLong);

                        double testDouble = 0;
                        double.TryParse(row[3], out testDouble);

                        bool testBool = (row[4] != null && row[4].Equals("1") == true) ? true : false;

                        // string is obvs good to go!!

                        DateTime testDT = DateTimeInformation.FormatDateTime(row[6]);

                        TestObj rto = new TestObj(id, testInt, testLong, testDouble, testBool, row[5], testDT);

                        // Keep adding the objects, as long as they have not already been added and the MaxNumObjects is not exceeded - probably 1m
                        // Now append the ids to the global list ...
                        lock (IDs) {
                            if (IDs.Contains((uint)rto.ID) == false)
                            {
                                IDs.Add((uint)rto.ID);
                                TestObjects.Add(rto);
                            }
                        }
                    }
                }


                // Do the printing of all the specific queries only if DoDebug is set to true ....
                if (RedisWrapper.DoDebug == true)
                {
                    // 19-Aug-2016 - F**k a duck - DO NOT SORT THE QUERIES HERE
                    // We need to make sure not to sort the master list as it potentially confuses the calculations in the search objects method ...

                    queryTxt.Clear();
                    foreach (DatabaseSearchPattern rsp in rsps)
                    {
                        queryTxt.Append(" " + rsp.AsText());
                    }

                    log.Append("\n" + "Search " + i + " found " + objCount + " objects. Query Text: " + queryTxt);
                }



                //foreach (RedisSearchPattern rsp in rsps) {
                //    queryTxt.Append(" " + rsp.AsText());
                //}

                ////log.Append("\n" + "Search " + (++searchIndex) + " found " + objCount + " objects. Query Text: " + queryTxt);
                //log.Append("\n" + "Search " + (++searchIndex) + " found " + objCount + " objects. Query Text: " + queryTxt + "\t" + sql);

                ////////////////////////////
                //if (storeQuery == true && (sqlData == null || sqlData.Count == 0)) {
                //    log.Append("\t" + sql);
                //}


                if (TestParameters.UseThreading == true)
                {
                    DNThreadManager.IncrementNumIterationsCompleted();
                }
                else
                {
                    Logger.Log(i, 10, TestParameters.PatternsToSearchFor.Count);
                }
            }


            Logger.Log("");
            Logger.Log(log.ToString());

            // And lastly, lets disconnect from the database!
            dbInfo.Disconnect();
        }
Esempio n. 13
0
        public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled)
        {
            //return handled = true if this macro generates the output (as an IEnumerable<ITextUnit>); the standard output will be suppressed
            //return handled = false if you want Citavi to produce the standard output;

            handled = false;

            if (citation == null)
            {
                return(null);
            }
            if (citation.Reference == null)
            {
                return(null);
            }
            if (componentPart == null)
            {
                return(null);
            }
            if (componentPart.Elements == null || componentPart.Elements.Count == 0)
            {
                return(null);
            }

            Reference referenceInScope = null;

            if (componentPart.Scope == ComponentPartScope.Reference)
            {
                referenceInScope = citation.Reference;
            }
            else if (componentPart.Scope == ComponentPartScope.ParentReference)
            {
                referenceInScope = citation.Reference.ParentReference;
            }
            if (referenceInScope == null)
            {
                return(null);
            }

            DateTimeFieldElement dateTimeFieldElement = componentPart.Elements.OfType <DateTimeFieldElement>().FirstOrDefault();

            if (dateTimeFieldElement == null)
            {
                return(null);
            }

            var propertyId          = dateTimeFieldElement.PropertyId;
            var dateTimeStringValue = referenceInScope.GetValue(propertyId) as string;

            if (string.IsNullOrEmpty(dateTimeStringValue))
            {
                return(null);
            }

            DateTime dateValue;

            if (!DateTimeInformation.TryParse(dateTimeStringValue, out dateValue))
            {
                return(null);
            }

            var formattedDate = string.Format(new MyCustomDateProvider(), "{0}", dateValue);

            if (string.IsNullOrEmpty(formattedDate))
            {
                return(null);
            }

            var formattedDateTextUnits = TextUnitCollectionUtility.TaggedTextToTextUnits(null, formattedDate);

            if (formattedDateTextUnits == null || !formattedDateTextUnits.Any())
            {
                return(null);
            }

            if (dateTimeFieldElement.FontStyle != FontStyle.Neutral)
            {
                foreach (ITextUnit textUnit in formattedDateTextUnits)
                {
                    textUnit.FontStyle |= dateTimeFieldElement.FontStyle;
                }
            }

            List <LiteralElement> outputDateLiteralElements = formattedDateTextUnits.TextUnitsToLiteralElements(componentPart);

            componentPart.Elements.ReplaceItem(dateTimeFieldElement, outputDateLiteralElements);

            foreach (LiteralElement literalElement in componentPart.Elements.OfType <LiteralElement>())
            {
                literalElement.ApplyCondition = ElementApplyCondition.Always;
            }

            return(null);
        }
Esempio n. 14
0
        //Version 2.4 handle missing day or month correctly
        //Version 2.3 introduction of debug mode
        //Version 2.2 added parameter 'doNotAbbreviateIfLengthIsEqualToOrLess' to avoid abbreviation of June and July (=4)
        public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled)
        {
            bool debugMode = false;             //set to true to see, what date Citavi recognized

            handled = false;

            if (componentPart == null)
            {
                return(null);
            }
            if (componentPart.Elements == null || !componentPart.Elements.Any())
            {
                return(null);
            }
            if (citation == null || citation.Reference == null)
            {
                return(null);
            }

            Reference referenceInScope = null;

            if (componentPart.Scope == ComponentPartScope.Reference)
            {
                referenceInScope = citation.Reference;
            }
            else if (componentPart.Scope == ComponentPartScope.ParentReference)
            {
                referenceInScope = citation.Reference.ParentReference;
            }
            if (referenceInScope == null)
            {
                return(null);
            }


            IEnumerable <DateTimeFieldElement> dateFieldElements = componentPart.Elements.OfType <DateTimeFieldElement>().ToList();

            if (dateFieldElements == null || !dateFieldElements.Any())
            {
                return(null);
            }


            bool dateFound = false;

            CultureInfo targetCulture = CultureInfo.CreateSpecificCulture("en-US");

            int doNotAbbreviateIfLengthIsEqualToOrLess = 4;

            //put 4 for most English styles:
            //3: Jan[uary]=7, Feb[ruary]=8, Mar[ch]=5, Apr[il]=5, May=3, Jun[e]=4, Jul[y]=4, Aug[ust]=6, Sep[tember]=9, Oct[ober]=7, Nov[ember]=8, Dec[ember]=8
            //4: Jan[uary]=7, Feb[ruary]=8, Mar[ch]=5, Apr[il]=5, May=3, June=4, July=4, Aug[ust]=6, Sep[tember]=9, Oct[ober]=7, Nov[ember]=8, Dec[ember]=8

            foreach (DateTimeFieldElement dateFieldElement in dateFieldElements)
            {
                string dateString = referenceInScope.GetValue(dateFieldElement.PropertyId) as string;
                if (string.IsNullOrEmpty(dateString))
                {
                    continue;
                }

                List <DateTimeMatch> dateTimeMatches = DateTimeInformation.Matches(dateString);
                if (!dateTimeMatches.Any() || dateTimeMatches.Count > 1)
                {
                    continue;
                }
                DateTimeMatch dateTimeMatch = dateTimeMatches.ElementAt(0);

                bool containsDayInformation   = !dateTimeMatch.MissingDayWasAutoCompleted;
                bool containsMonthInformation = !dateTimeMatch.MissingMonthWasAutoCompleted;

                DateTime dateValue = dateTimeMatch.DateTime;


                int day   = dateValue.Day;
                int month = dateValue.Month;
                int year  = dateValue.Year;

                string monthStringShort = dateValue.ToString("MMM", targetCulture);
                string monthStringLong  = dateValue.ToString("MMMM", targetCulture);

                string monthString = monthStringShort == monthStringLong ? monthStringShort : monthStringLong.Length <= doNotAbbreviateIfLengthIsEqualToOrLess ? monthStringLong : monthStringShort + ".";
                //string dayString = day.ToString("D2");				//2-digit day, padded with leading 0 if necessary, so 08 instead of 8
                string dayString  = day.ToString();
                string yearString = dateValue.ToString("yyyy");                                 //4-digit year

                if (debugMode)
                {
                    string debugDatePattern = "Original: {0} - Year: {1} Month: {2} Day: {3}";
                    dateString = string.Format(debugDatePattern, dateString, dateValue.Year, dateValue.Month, dateValue.Day);
                }
                else
                {
                    if (dateTimeMatch.MissingMonthWasAutoCompleted && dateTimeMatch.MissingDayWasAutoCompleted)
                    {
                        string newDatePattern = "{0}";
                        dateString = string.Format(newDatePattern, yearString);
                    }
                    else if (dateTimeMatch.MissingDayWasAutoCompleted)
                    {
                        string newDatePattern = "{0} {1}";
                        dateString = string.Format(newDatePattern, monthString, yearString);
                    }

                    else
                    {
                        string newDatePattern = "{0} {1} {2}";
                        dateString = string.Format(newDatePattern, dayString, monthString, yearString);
                    }
                }

                LiteralElement outputDateElement = new LiteralElement(componentPart, dateString);
                outputDateElement.FontStyle = dateFieldElement.FontStyle;
                componentPart.Elements.ReplaceItem(dateFieldElement, outputDateElement);
                dateFound = true;
            }

            if (dateFound)
            {
                foreach (LiteralElement literalElement in componentPart.Elements.OfType <LiteralElement>())
                {
                    literalElement.ApplyCondition = ElementApplyCondition.Always;
                }
            }

            handled = false;
            return(null);
        }