Пример #1
0
 public SPInstance(ScriptEngine engine, SPBaristaContext context, SPFarm farmContext, SPServer serverContext)
     : base(engine)
 {
     m_context     = new SPContextInstance(Engine, context);
     m_farm        = new SPFarmInstance(Engine.Object.InstancePrototype, farmContext);
     m_server      = new SPServerInstance(Engine.Object.InstancePrototype, serverContext);
     m_secureStore = new SPSecureStoreInstance(Engine.Object.InstancePrototype);
     PopulateFunctions();
 }
        public SPContextInstance(ScriptEngine engine, SPBaristaContext context)
            : base(engine)
        {
            m_context = context;

            if (m_context.Site != null)
            {
                this.Site = new SPSiteInstance(this.Engine.Object.InstancePrototype, m_context.Site);
            }

            if (m_context.Web != null)
            {
                this.Web = new SPWebInstance(this.Engine, m_context.Web);
            }

            try
            {
                if (m_context.List != null)
                {
                    this.List = new SPListInstance(this.Engine, null, null, m_context.List);
                }
            }
            catch (NullReferenceException) { /* Do Nothing */ }

            try
            {
                if (m_context.ListItem != null)
                {
                    this.ListItem = new SPListItemInstance(this.Engine, m_context.ListItem);
                }
            }
            catch (NullReferenceException) { /* Do Nothing */ }

            try
            {
                if (m_context.View != null)
                {
                    this.View = new SPViewInstance(this.Engine.Object.InstancePrototype, m_context.View);
                }
            }
            catch (NullReferenceException) { /* Do Nothing */ }

            this.PopulateFields();
            this.PopulateFunctions();
        }
 /// <summary>
 /// Occurs when an attachment is deleted from an entity.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="entityId"></param>
 /// <param name="fileName"></param>
 protected virtual void AttachmentDeleted(SPBaristaContext context, Guid entityId, string fileName)
 {
 }
 /// <summary>
 /// Occurs when an entity is moved to a different folder.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="entity"></param>
 /// <param name="oldFolder"></param>
 /// <param name="newFolder"></param>
 protected virtual void EntityMoved(SPBaristaContext context, Entity entity, Folder oldFolder, Folder newFolder)
 {
 }
 /// <summary>
 /// Occurs when an entity is deleted from a SPDocumentStore.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="entityId"></param>
 protected virtual void EntityDeleted(SPBaristaContext context, Guid entityId)
 {
 }
 /// <summary>
 /// Occurs when an entity part is deleted from an entity.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="entityId"></param>
 /// <param name="partName"></param>
 protected virtual void EntityPartDeleted(SPBaristaContext context, Guid entityId, string partName)
 {
 }
 /// <summary>
 /// Occurs when an attachment is updated.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="attachment"></param>
 protected virtual void AttachmentUpdated(SPBaristaContext context, Attachment attachment)
 {
 }
 /// <summary>
 /// Occurs when an entity part is updated.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="entityPart"></param>
 protected virtual void EntityPartUpdated(SPBaristaContext context, EntityPart entityPart)
 {
 }
 /// <summary>
 /// Occurs when an entity is updated.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="entity"></param>
 protected virtual void EntityUpdated(SPBaristaContext context, Entity entity)
 {
 }
        /// <summary>
        /// An item was deleted.
        /// </summary>
        public override void ItemDeleted(SPItemEventProperties properties)
        {
            base.ItemDeleted(properties);

            string guidString;

            //If the Url ends with "default.dsep", presume it's an entity deletion.
            if (properties.BeforeUrl.EndsWith(Constants.DocumentStoreDefaultEntityPartFileName, StringComparison.InvariantCultureIgnoreCase))
            {
                guidString =
                    properties.BeforeUrl
                    .Replace("/" + Constants.DocumentStoreDefaultEntityPartFileName, "")
                    .Substring(properties.BeforeUrl.LastIndexOf("/", StringComparison.InvariantCulture) + 1);

                try
                {
                    var entityId = new Guid(guidString);
                    using (var site = properties.OpenSite())
                    {
                        using (var web = properties.OpenWeb())
                        {
                            var context = new SPBaristaContext(site, web);
                            EntityDeleted(context, entityId);
                            return;
                        }
                    }
                }
                catch (Exception) { /* Do Nothing */ }
            }

            //If the above failed, and the Url ends with ".dsep", presume it's an entity part deletion.
            if (properties.BeforeUrl.EndsWith(Constants.DocumentSetEntityPartExtension, StringComparison.InvariantCultureIgnoreCase))
            {
                var partName =
                    properties.BeforeUrl
                    .Substring(properties.BeforeUrl.LastIndexOf("/", System.StringComparison.InvariantCulture) + 1)
                    .Replace(Constants.DocumentSetEntityPartExtension, "");

                var urlWithoutPartName =
                    properties.BeforeUrl
                    .Replace("/" + partName + Constants.DocumentSetEntityPartExtension, "");

                guidString =
                    urlWithoutPartName.Substring(urlWithoutPartName.LastIndexOf("/", StringComparison.InvariantCulture) + 1);

                try
                {
                    var entityId = new Guid(guidString);

                    using (var site = properties.OpenSite())
                    {
                        using (var web = properties.OpenWeb())
                        {
                            var context = new SPBaristaContext(site, web);
                            EntityPartDeleted(context, entityId, partName);
                            return;
                        }
                    }
                }
                catch (Exception) { /* Do Nothing */ }
            }

            //If the last fragment is a Guid, presume it's an entity deletion.
            guidString =
                properties.BeforeUrl
                .Substring(properties.BeforeUrl.LastIndexOf("/", StringComparison.InvariantCulture) + 1);

            try
            {
                var entityId = new Guid(guidString);

                using (var site = properties.OpenSite())
                {
                    using (var web = properties.OpenWeb())
                    {
                        var context = new SPBaristaContext(site, web);
                        EntityDeleted(context, entityId);
                        return;
                    }
                }
            }
            catch (Exception) { /* Do Nothing */ }

            //If it wasn't a guid, presume an attachment.
            var fileName =
                properties.BeforeUrl
                .Substring(properties.BeforeUrl.LastIndexOf("/", System.StringComparison.InvariantCulture) + 1);

            var urlWithoutFileName =
                properties.BeforeUrl
                .Replace("/" + fileName, "");

            guidString =
                urlWithoutFileName.Substring(urlWithoutFileName.LastIndexOf("/", StringComparison.InvariantCulture) + 1);

            try
            {
                var attachmentEntityId = new Guid(guidString);
                using (var site = properties.OpenSite())
                {
                    using (var web = properties.OpenWeb())
                    {
                        var context = new SPBaristaContext(site, web);
                        AttachmentDeleted(context, attachmentEntityId, fileName);
// ReSharper disable RedundantJumpStatement
                        return;
// ReSharper restore RedundantJumpStatement
                    }
                }
            }
            catch (Exception) { /* Do Nothing */ }

            //At this point, I don't know what it is (Folder or an additional file not in an document set), continue...
        }
        /// <summary>
        /// Processes events.
        /// </summary>
        /// <param name="properties"></param>
        protected virtual void ProcessEvent(SPItemEventProperties properties)
        {
            //TODO: Should there be folder events too? What happens to the files in a folder when a folder is deleted?

            var documentStoreEntityContentTypeId     = new SPContentTypeId(Constants.DocumentStoreEntityContentTypeId);
            var documentStoreEntityPartContentTypeId = new SPContentTypeId(Constants.DocumentStoreEntityPartContentTypeId);
            var attachmentContentTypeId = new SPContentTypeId(Constants.DocumentStoreEntityAttachmentContentTypeId);

            if (properties.ListItem.ContentTypeId.IsChildOf(documentStoreEntityContentTypeId))
            {
                //Note: This is the actual Document Set.
                switch (properties.EventType)
                {
                case SPEventReceiverType.ItemFileMoved:
                {
                    var documentSet = DocumentSet.GetDocumentSet(properties.ListItem.Folder);
                    var entity      = SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, null);

                    using (var site = properties.OpenSite())
                    {
                        using (var web = properties.OpenWeb())
                        {
                            var before    = web.GetFolder(properties.BeforeUrl);
                            var after     = web.GetFolder(properties.AfterUrl);
                            var oldFolder = SPDocumentStoreHelper.MapFolderFromSPFolder(before.ParentFolder);
                            var newFolder = SPDocumentStoreHelper.MapFolderFromSPFolder(after.ParentFolder);
                            var context   = new SPBaristaContext(site, web);
                            EntityMoved(context, entity, oldFolder, newFolder);
                        }
                    }
                    break;
                }
                }
            }
            else if (properties.ListItem.ContentTypeId.IsChildOf(documentStoreEntityPartContentTypeId))
            {
                if (
                    String.Compare(properties.ListItem.File.Name, Constants.DocumentStoreDefaultEntityPartFileName,
                                   StringComparison.InvariantCulture) == 0)
                {
                    switch (properties.EventType)
                    {
                    case SPEventReceiverType.ItemAdded:
                    {
                        //TODO: See if this is appropraite.
                        if (properties.ListItem.Folder == null)
                        {
                            return;
                        }

                        var documentSet = DocumentSet.GetDocumentSet(properties.ListItem.Folder);
                        var entity      = SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, properties.ListItem.File,
                                                                                         null);
                        using (var site = properties.OpenSite())
                        {
                            using (var web = properties.OpenWeb())
                            {
                                var context = new SPBaristaContext(site, web);
                                EntityAdded(context, entity);
                            }
                        }
                    }
                    break;

                    case SPEventReceiverType.ItemUpdated:
                    {
                        if (properties.ListItem.Folder != null)
                        {
                            var documentSet = DocumentSet.GetDocumentSet(properties.ListItem.Folder);
                            var entity      = SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, properties.ListItem.File,
                                                                                             null);
                            using (var site = properties.OpenSite())
                            {
                                using (var web = properties.OpenWeb())
                                {
                                    var context = new SPBaristaContext(site, web);
                                    EntityUpdated(context, entity);
                                }
                            }
                        }
                    }
                    break;
                    }
                }
                else
                {
                    var entityPart = SPDocumentStoreHelper.MapEntityPartFromSPFile(properties.ListItem.File, null);

                    switch (properties.EventType)
                    {
                    case SPEventReceiverType.ItemAdded:
                        using (var site = properties.OpenSite())
                        {
                            using (var web = properties.OpenWeb())
                            {
                                var context = new SPBaristaContext(site, web);
                                EntityPartAdded(context, entityPart);
                            }
                        }
                        break;

                    case SPEventReceiverType.ItemUpdated:
                        using (var site = properties.OpenSite())
                        {
                            using (var web = properties.OpenWeb())
                            {
                                var context = new SPBaristaContext(site, web);
                                EntityPartUpdated(context, entityPart);
                            }
                        }
                        break;
                    }
                }
            }
            else if (properties.ListItem.ContentTypeId.IsChildOf(attachmentContentTypeId))
            {
                var attachment = SPDocumentStoreHelper.MapAttachmentFromSPFile(properties.ListItem.File);

                switch (properties.EventType)
                {
                case SPEventReceiverType.ItemAdded:
                    using (var site = properties.OpenSite())
                    {
                        using (var web = properties.OpenWeb())
                        {
                            var context = new SPBaristaContext(site, web);
                            AttachmentAdded(context, attachment);
                        }
                    }
                    break;

                case SPEventReceiverType.ItemUpdated:
                    using (var site = properties.OpenSite())
                    {
                        using (var web = properties.OpenWeb())
                        {
                            var context = new SPBaristaContext(site, web);
                            AttachmentUpdated(context, attachment);
                        }
                    }
                    break;
                }
            }
            else if (properties.ListItem.File != null &&
                     properties.ListItem.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Document) &&
                     String.Compare(properties.ListItem.File.Name, Constants.DocumentStoreDefaultEntityPartFileName,
                                    StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                //Apparently the Default documents in a Doc Set are initially added as a "Document"
                var documentSet = DocumentSet.GetDocumentSet(properties.ListItem.File.ParentFolder);
                var entity      = SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, properties.ListItem.File, null);
                switch (properties.EventType)
                {
                case SPEventReceiverType.ItemAdded:
                    using (var site = properties.OpenSite())
                    {
                        using (var web = properties.OpenWeb())
                        {
                            var context = new SPBaristaContext(site, web);
                            EntityAdded(context, entity);
                        }
                    }
                    break;
                }
            }
        }