Пример #1
0
        public bool IsTemplateForReference(ConditionalTemplate template, Citation citation)
        {
            if (citation == null)
            {
                return(false);
            }

            var reference = citation.Reference as Reference;

            if (reference == null)
            {
                return(false);
            }

            IEnumerable <ReferencePropertyId> fieldsToCheck =
                (from kvp in FieldsToCheck
                 where kvp.Value == Ensure.IsEmpty || kvp.Value == Ensure.IsNotEmpty
                 select kvp.Key).ToList();

            if (!fieldsToCheck.Any())
            {
                return(false);
            }

            foreach (ReferencePropertyId propertyId in fieldsToCheck)
            {
                ReferencePropertyDescriptor property = ReferencePropertyDescriptor.GetPropertyDescriptor(propertyId);
                if (property == null)
                {
                    continue;
                }

                if (!IsConditionMet(reference, property, FieldsToCheck[propertyId]))
                {
                    return(false);
                }
            }

            return(true);
        }
    public static void Main()
    {
        if (Program.ProjectShells.Count == 0)
        {
            return;                                                     //no project open
        }
        if (IsBackupAvailable() == false)
        {
            return;                                                             //user wants to backup his/her project first
        }
        List <Reference> references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences();

        if (references == null || references.Count == 0)
        {
            return;
        }

        int counter = 0;

        //NOTE: Change the order or listing of cultures in the following array, to prioritize certain cultures
        CultureInfo[] cultures = new CultureInfo[] {
            CultureInfo.GetCultureInfo("en-US"),
            CultureInfo.GetCultureInfo("de-DE"),
            CultureInfo.GetCultureInfo("es-ES"),
            CultureInfo.GetCultureInfo("pt-BR"),
            CultureInfo.GetCultureInfo("fr-FR"),
            CultureInfo.GetCultureInfo("pl-PL")
        };

        //NOTE: Change the target culture and format to your needs
        CultureInfo targetCulture = CultureInfo.GetCultureInfo("de-DE");
        string      targetFormat  = "d";   //Standard date ... differs from culture to culture, e.g. 12/24/2015 in en-US and 24.12.2015 in de-DE

        //NOTE: Change the reference fields where this macros looks for valid dates to convert
        ReferencePropertyDescriptor[] properties = new ReferencePropertyDescriptor[] {
            ReferencePropertyDescriptor.AccessDate,
            ReferencePropertyDescriptor.Date,
            ReferencePropertyDescriptor.Date2
        };

        //NOTE: Change the sequence and listing of format strings to look for
        string[] formatsToSearchFor = new string[] { "dd/MM/yyyy", "dd-MM-yyyy", "dd-MMM-yy", "dd-MMM-yyyy" };


        foreach (Reference reference in references)
        {
            foreach (ReferencePropertyDescriptor property in properties)
            {
                var value = reference.GetValue(property.PropertyId) as string;
                if (value == null || string.IsNullOrWhiteSpace(value))
                {
                    continue;
                }

                DateTime dateFound = DateTime.MinValue;

                foreach (CultureInfo culture in cultures)
                {
                    if (DateTime.TryParseExact(value, formatsToSearchFor, culture, DateTimeStyles.None, out dateFound))
                    {
                        reference.SetValue(property.PropertyId, dateFound.ToString(targetFormat, targetCulture));
                        counter++;
                        break;
                    }
                }
            }
        }

        string message = "{0} dates were changed to standard format";

        message = string.Format(message, counter);
        MessageBox.Show(message, "Macro", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
Пример #3
0
        private bool IsConditionMet(Reference reference, ReferencePropertyDescriptor property, Ensure ensure)
        {
            if (ensure == Ensure.Ignore)
            {
                return(true);
            }
            if (reference == null)
            {
                return(true);
            }
            if (property == null)
            {
                return(true);
            }

            #region String

            if (property.DataType == typeof(string))
            {
                string content = (string)reference.GetValue(property.PropertyId);
                if (string.IsNullOrEmpty(content))
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion String

            #region ReferencePersonCollection or NotificationCollection<Person>

            if (property.DataType == typeof(ReferencePersonCollection))
            {
                ReferencePersonCollection persons = (ReferencePersonCollection)reference.GetValue(property.PropertyId);
                if (persons == null || persons.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            if (property.DataType == typeof(NotificationCollection <Person>))
            {
                NotificationCollection <Person> persons = (NotificationCollection <Person>)reference.GetValue(property.PropertyId);
                if (persons == null || persons.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion ReferencePersonCollection or NotificationCollection<Person>

            #region ReferenceCategoryCollection

            if (property.DataType == typeof(ReferenceCategoryCollection))
            {
                if (reference.Categories == null || reference.Categories.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion ReferenceCategoryCollection

            #region ReferenceChildReferenceCollection

            if (property.DataType == typeof(ReferenceChildReferenceCollection))
            {
                if (reference.ChildReferences == null || reference.ChildReferences.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion ReferenceChildReferenceCollection

            #region bool

            if (property.DataType == typeof(bool))
            {
                bool?boolValue = (bool)reference.GetValue(property.PropertyId);
                if (!boolValue.GetValueOrDefault(false))
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion bool

            #region Isbn

            if (property.DataType == typeof(Isbn))
            {
                Isbn isbn = (Isbn)reference.GetValue(property.PropertyId);
                if (string.IsNullOrWhiteSpace(isbn.InputString))
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion Isbn

            #region ReferenceKeywordCollection

            if (property.DataType == typeof(ReferenceKeywordCollection))
            {
                if (reference.Keywords == null || reference.Keywords.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion ReferenceKeywordCollection

            #region ReferenceLocationCollection

            if (property.DataType == typeof(ReferenceLocationCollection))
            {
                if (reference.Locations == null || reference.Locations.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion ReferenceLocationCollection

            #region ParentReference

            if (property.DataType == typeof(Reference))
            {
                if (reference.ParentReference == null)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion ParentReference

            #region Periodical

            if (property.DataType == typeof(Periodical))
            {
                if (reference.Periodical == null)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion Periodical

            #region ReferencePublisherCollection

            if (property.DataType == typeof(ReferencePublisherCollection))
            {
                if (reference.Publishers == null || reference.Publishers.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion ReferencePublisherCollection

            #region RefernceQuotationCollection

            if (property.DataType == typeof(ReferenceQuotationCollection))
            {
                if (reference.Quotations == null || reference.Quotations.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion ReferenceQuotationCollection

            #region SeriesTitle

            if (property.DataType == typeof(SeriesTitle))
            {
                if (reference.SeriesTitle == null)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion SeriesTitle

            #region Editors

            if (property.DataType == SeriesTitlePropertyDescriptor.Editors.DataType)
            {
                if (reference.Editors == null || !reference.Editors.Any())
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion Editors

            #region ReferenceReferenceTaskCollection

            if (property.DataType == typeof(ReferenceReferenceTaskCollection))
            {
                if (reference.Tasks == null || reference.Tasks.Count == 0)
                {
                    return(ensure == Ensure.IsEmpty);
                }
                else
                {
                    return(ensure == Ensure.IsNotEmpty);
                }
            }

            #endregion Reference

            return(true);
        }