Exemple #1
0
        private ExportPropertiesDictionary CreateExportProperties()
        {
            ExportPropertiesDictionary exportProps = new ExportPropertiesDictionary(string.Empty);

            if (ShowImages)
            {
                exportProps.Add("ShowImage", "true");
            }
            if (ShowDescription)
            {
                exportProps.Add("ShowDescription", "true");
            }
            if (ShowVideo)
            {
                exportProps.Add("ShowVideo", "true");
            }
            if (ShowPages)
            {
                exportProps.Add("ShowPages", "true");
            }

            return(exportProps);
        }
        protected override bool ProcessWorkItem(SPContentDatabase contentDatabase, 
            SPWorkItemCollection workItems, SPWorkItem workItem, SPJobState jobState)
        {
            Stopwatch sw = Stopwatch.StartNew(); //used to time how long the export took
            bool processingAlready = false;
            
            try
            {
                if (workItem != null)
                {
                    //process the workItem
                    using (SPSite site = new SPSite(workItem.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb(workItem.WebId))
                        {
                            // processing logic
                            // 1. SET GlymaExport list Item to ExportStatus.Processing
                            // 2. START export based on type in GlymaExport list item
                            // 3. STORE the export in the GlymaExport list against the item as an attachment
                            // 4. SET GlymaExport list item to ExportStatus.Completed
                            SPList exportsList = null;
                            SPListItem exportItem = null;
                            try
                            {
                                exportsList = web.Lists[workItem.ParentId];
                                exportItem = exportsList.GetItemByUniqueId(workItem.ItemGuid);
                                if (exportItem != null)
                                {
                                    string exportStatus = exportItem["ExportStatus"] as string;
                                    if (exportStatus == ExportStatus.Scheduled.ToString())
                                    {
                                        // If the export was still marked as scheduled then set it to processing
                                        WriteExportStatus(workItem, ExportStatus.Processing);

                                        exportItem = exportsList.GetItemByUniqueId(workItem.ItemGuid);

                                        if (exportItem != null)
                                        {
                                            // read the type of export that we should produce
                                            ExportType exportType = (ExportType)Enum.Parse(typeof(ExportType), exportItem["ExportType"] as string);
                                            MapType mapType = (MapType)Enum.Parse(typeof(MapType), exportItem["MapType"] as string);

                                            Dictionary<string, string> exportProperties = null;
                                            string exportPropertiesStr = exportItem["ExportProperties"] as string;
                                            if (!string.IsNullOrEmpty(exportPropertiesStr) && exportPropertiesStr.Trim() != string.Empty)
                                            {
                                                try
                                                {
                                                    ExportPropertiesDictionary exportDict = new ExportPropertiesDictionary(exportPropertiesStr);
                                                    exportProperties = exportDict as Dictionary<string, string>;
                                                }
                                                catch (XmlSchemaValidationException)
                                                {
                                                    exportProperties = null;
                                                }
                                            }

                                            bool useVerboseLogging = false;
                                            if (exportProperties != null)
                                            {
                                                if (exportProperties.ContainsKey("Verbose"))
                                                {
                                                    if (exportProperties["Verbose"].ToLower() == "true")
                                                    {
                                                        useVerboseLogging = true;
                                                    }
                                                }
                                            }

                                            if (useVerboseLogging)
                                            {
                                                LogMessage(workItem, "Creating Map Manager for the web: {0}.", web.Url);
                                            }

                                            // create the appropriate IExportUtility for the ExportType
                                            MapManagerFactory mapManagerFactory = new MapManagerFactory();
                                            IMapManager mapManager = mapManagerFactory.GetMapManager(web);
                                            if (mapManager != null)
                                            {
                                                if (useVerboseLogging)
                                                {
                                                    LogMessage(workItem, "Created Map Manager for the web: {0}.", web.Url);
                                                }

                                                ExportUtilityFactory exportUtilityFactory = new ExportUtilityFactory(mapManager);
                                                IExportUtility exportUtility = exportUtilityFactory.CreateExportUtility(mapType, exportType);

                                                if (exportUtility != null)
                                                {
                                                    if (useVerboseLogging)
                                                    {
                                                        LogMessage(workItem, "Created Export Utility for {0} map type to {1}.", mapType.ToString(), exportType.ToString());
                                                    }

                                                    exportItem = exportsList.GetItemByUniqueId(workItem.ItemGuid);
                                                    if (exportItem != null)
                                                    {
                                                        Guid rootMapUid = Guid.Empty;
                                                        Guid domainUid = Guid.Empty;
                                                        try
                                                        {
                                                            string rootMapUidValue = exportItem["RootMapUid"] as string;
                                                            string domainUidValue = exportItem["DomainUid"] as string;
                                                            domainUid = new Guid(domainUidValue);
                                                            rootMapUid = new Guid(rootMapUidValue);
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            //The GUIDs were not parsed correctly
                                                        }
                                                        if (rootMapUid != Guid.Empty && domainUid != Guid.Empty)
                                                        {
                                                            //The export utility will do the grunt work and provide a URL to the temp file created
                                                            GlymaExportUserState userState = new GlymaExportUserState(workItem);
                                                            userState.UseVerboseLogging = useVerboseLogging; //store the level of logging to use in the event handlers
                                                            exportUtility.ExportCompleted += exportUtility_ExportCompleted;
                                                            exportUtility.ProgressChanged += exportUtility_ProgressChanged;
                                                            exportUtility.ExceptionRaised += exportUtility_ExceptionRaised;

                                                            if (useVerboseLogging)
                                                            {
                                                                LogMessage(workItem, "Starting export.");
                                                            }

                                                            exportUtility.ExportMap(domainUid, rootMapUid, exportProperties, new List<Guid>(), userState);

                                                            if (useVerboseLogging)
                                                            {
                                                                LogMessage(workItem, "Waiting for export to complete.");
                                                            }

                                                            // Wait for the export to complete (up to the timeout)
                                                            bool signalled = userState.Completed.WaitOne(TIMEOUT_MS);
                                                            if (!signalled)
                                                            {
                                                                if (useVerboseLogging)
                                                                {
                                                                    LogMessage(workItem, "The export timed out after {0}ms", TIMEOUT_MS);
                                                                }
                                                                // The timeout occurred so don't process the completed or progress changed events
                                                                exportUtility.ExportCompleted -= exportUtility_ExportCompleted;
                                                                exportUtility.ProgressChanged -= exportUtility_ProgressChanged;
                                                                exportUtility.ExceptionRaised -= exportUtility_ExceptionRaised;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            throw new Exception(string.Format("The DomainUid and/or RootMapUid were not valid Guid's. DomainUid: {0} RootMapUid: {1}.", domainUid.ToString(), rootMapUid.ToString()));
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    throw new Exception(string.Format("Unable to create export utility for the export type: {0}.", exportType.ToString()));
                                                }
                                            }
                                            else
                                            {
                                                throw new Exception(string.Format("Unable to build an IMapManager for the current SPWeb. {0}", mapManagerFactory.ErrorMessage));
                                            }
                                        }
                                    }
                                    else if (exportStatus == ExportStatus.Processing.ToString())
                                    {
                                        //shouldn't do anything unless it's scheduled.
                                        processingAlready = true;
                                    }
                                }
                                else
                                {
                                    throw new Exception("The Export Job did not exist in the list.");
                                }
                            }
                            catch (TargetInvocationException tiex)
                            {
                                if (workItem != null)
                                {
                                    WriteExportStatus(workItem, ExportStatus.Error);
                                    LogMessage(workItem, "Error during export: {0}", tiex.Message);

                                    if (tiex.InnerException != null)
                                    {
                                        LogMessage(workItem, "Inner Exception({0}): {1}.\r\nStackTrace: {2}", tiex.InnerException.GetType().ToString(), tiex.InnerException.Message, tiex.InnerException.StackTrace);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                //exception handling
                                if (workItem != null)
                                {
                                    WriteExportStatus(workItem, ExportStatus.Error);
                                    LogMessage(workItem, "Error during export: {0}.", ex.Message);
                                }
                            }
                            finally
                            {
                                if (!processingAlready)
                                {
                                    // delete the workItem after we've processed it
                                    workItems.SubCollection(site, web, 0, (uint)workItems.Count).DeleteWorkItem(workItem.Id);
                                }

                                //Update the progress of the timer job
                                Progress += (100 / workItems.Count); //estimate only, some maps will export faster than others and different types may export faster
                                if (Progress > 100)
                                {
                                    Progress = 100;
                                }
                                this.UpdateProgress(Progress); //The base classes timer job's overall progress
                                
                                if (!processingAlready) //this is if the timer job ran and started processing the item that was already being processed
                                {
                                    exportItem = exportsList.GetItemByUniqueId(workItem.ItemGuid);
                                    if (exportItem != null)
                                    {
                                        string exportStatus = exportItem["ExportStatus"] as string;
                                        if (exportStatus == ExportStatus.Processing.ToString())
                                        {
                                            //if it's still processing and at this point then something has failed.
                                            WriteExportStatus(workItem, ExportStatus.Error);
                                            LogMessage(workItem, "The export failed as it was still in a processing state when it had apparently completed.");
                                        }

                                        LogMessage(workItem, "Completed in {0:0.000}s.", sw.Elapsed.TotalSeconds);//add in a metric for how long the export took (successful or failure)
                                    }
                                 }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //exception handling
                throw new Exception("Failed to while processing Glyma Export Work Item Timer Job.", ex);
            }

            return true;
        }
        private ExportPropertiesDictionary CreateExportProperties()
        {
            ExportPropertiesDictionary exportProps = new ExportPropertiesDictionary(string.Empty);
            if (ShowImages)
            {
                exportProps.Add("ShowImage", "true");
            }
            if (ShowDescription)
            {
                exportProps.Add("ShowDescription", "true");
            }
            if (ShowVideo)
            {
                exportProps.Add("ShowVideo", "true");
            }
            if (ShowPages)
            {
                exportProps.Add("ShowPages", "true");
            }

            return exportProps;
        }
Exemple #4
0
        private void OnInitialiseMapManagerCompleted(object sender, InitialiseMapManagerEventArgs e)
        {
            if (e.IsInitialised)
            {
                ProgressRecord progressRecord = new ProgressRecord(0, "Exporting Glyma Map", string.Format("Initialising {0} export processor", ExportFormat.ToString()));
                progressRecord.SecondsRemaining = -1;
                progressRecord.PercentComplete  = 5;
                progressRecord.RecordType       = ProgressRecordType.Processing;

                lock (pr_lock)
                {
                    this.ProgressRecord = progressRecord;
                    ProgressEvent.Set(); //notify of change to progress
                }

                ExportUtilityFactory exportFactory = new ExportUtilityFactory(MapManager);
                IExportUtility       exportUtil    = null;
                switch (MapSchema)
                {
                case "IBIS":
                    switch (ExportFormat)
                    {
                    case "Compendium":
                        exportUtil = exportFactory.CreateExportUtility(MapType.IBIS, ExportType.Compendium);
                        break;

                    case "Word":
                        exportUtil = exportFactory.CreateExportUtility(MapType.IBIS, ExportType.Word);
                        break;

                    case "PDF":
                        exportUtil = exportFactory.CreateExportUtility(MapType.IBIS, ExportType.PDF);
                        break;

                    case "GlymaXML":
                        //exportUtil = exportFactory.CreateExportUtility(MapType.IBIS, ExportType.GlymaXml);
                        lock (msg_lock)
                        {
                            Message = "Exporting to GlymaXML format is currently not supported";
                            LogMessageEvent.Set();
                        }
                        break;
                    }
                    break;
                }
                exportUtil.ExportCompleted += OnExportCompleted;
                exportUtil.ProgressChanged += OnProgressChanged;


                ExportPropertiesDictionary exportProps = CreateExportProperties();
                GlymaExportUserState       userState   = new GlymaExportUserState(Output);

                progressRecord = new ProgressRecord(0, "Exporting Glyma Map", "Export Progress: 0%");
                progressRecord.SecondsRemaining = -1;
                progressRecord.PercentComplete  = 10;
                progressRecord.RecordType       = ProgressRecordType.Processing;

                lock (pr_lock)
                {
                    this.ProgressRecord = progressRecord;
                    ProgressEvent.Set(); //notify of change to progress
                }

                exportUtil.ExportMap(Domain, Map, exportProps, null, userState);
                bool signalled = userState.Completed.WaitOne(Timeout.Infinite); //wait for the export to completed
                if (!signalled)
                {
                    Completed.Set(); //since it's inifinite timeout this shouldn't happen
                }
            }
            else
            {
                lock (msg_lock)
                {
                    this.Message = string.Format("Failed to initialise the web service client: {0}", e.ErrorMessage);
                    LogMessageEvent.Set();
                }

                Completed.Set();
            }
        }
        private ExportJob GetExportJob(SPListItem export)
        {
            ExportJob exportJob = new ExportJob();
            try
            {
                string workItemId = export[SPBuiltInFieldId.Title] as string;
                workItemId.Trim(); //ensure it is just the GUID in case anyone has messed with the list
                exportJob.Id = new Guid(workItemId);

                exportJob.Created = (DateTime)export[SPBuiltInFieldId.Created];
                string createdBy = export[SPBuiltInFieldId.Author] as string;
                exportJob.CreatedBy = new GlymaUser() { Name = createdBy };

                
                if (export.Attachments.Count > 0)
                {
                    string fileName = export.Attachments[0] as string;
                    string linkAddress = export.Attachments.UrlPrefix + fileName;
                    exportJob.Link = linkAddress;
                }
                string exportStatus = export["ExportStatus"] as string;
                exportJob.Status = (ExportStatus)Enum.Parse(typeof(ExportStatus), exportStatus, true);

                string exportType = export["ExportType"] as string;
                exportJob.Type = (ExportType)Enum.Parse(typeof(ExportType), exportType, true);

                string mapType = export["MapType"] as string;
                exportJob.MapType = (MapType)Enum.Parse(typeof(MapType), mapType, true); ;

                string exportProperties = export["ExportProperties"] as string;
                if (!string.IsNullOrEmpty(exportProperties))
                {
                    try
                    {
                        ExportPropertiesDictionary serializableDict = new ExportPropertiesDictionary(export["ExportProperties"] as string);
                        exportJob.ExportProperties = serializableDict as IDictionary<string, string>;
                    }
                    catch (Exception)
                    {
                        //failed to parse the ExportProperties so assume they are null
                    }
                }

                try
                {
                    Double exportPercentageComplete = (Double)export["PercentageComplete"];
                    exportPercentageComplete = exportPercentageComplete * 100;
                    exportJob.PercentageComplete = (int)exportPercentageComplete;
                }
                catch (Exception)
                {
                    exportJob.PercentageComplete = 0;
                }

                //TODO: determine if the export job is current (have any changes occurred to the map since last export
                exportJob.IsCurrent = true;
            }
            catch (Exception)
            {
                //Failed to read the Export Job from the SPListItem, could be a Guid ID wasn't of the correct format
            }
            
            return exportJob;
        }
        /// <summary>
        /// Creates an ExportJob and schedules the WorkItem for the timer job that processes the exports.
        /// </summary>
        /// <param name="domainUid">The DominUid for the map being exported</param>
        /// <param name="rootMapUid">The RootMapUid for the map being exported</param>
        /// <param name="exportProperties">The export properties for the export</param>
        /// <param name="mapType">The map type (schema) for the map being exported</param>
        /// <param name="exportType">The output format for the export</param>
        /// <returns>The ExportJob that was created</returns>
        public ExportJobResponse CreateExportJob(Guid domainUid, Guid rootMapUid, IDictionary<string, string> exportProperties, MapType mapType, ExportType exportType)
        {
            ExportJobResponse response = new ExportJobResponse();

            try
            {
                Guid webID = SPContext.Current.Web.ID;
                Guid siteID = SPContext.Current.Site.ID;
                SPUser currentUser = null;
                using (SPSite site = new SPSite(siteID)) 
                {
                    using (SPWeb web = site.OpenWeb(webID))
                    {
                        if (web != null)
                        {
                            currentUser = web.CurrentUser;
                        }
                    }
                }
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    int userId = -1;
                    SPList exportsList = null;
                    int listItemIdNum = -1;
                    
                    using (SPSite site = new SPSite(siteID)) 
                    {
                        using (SPWeb web = site.OpenWeb(webID))
                        {
                            if (web != null)
                            {
                                if (currentUser == null)
                                {
                                    //The current user shouldn't be null, it should have been resolved outside of this RunWithElevatedPrivileges delegate
                                    currentUser = web.CurrentUser;
                                }
                                if (currentUser != null)
                                {
                                    userId = currentUser.ID;

                                    exportsList = web.TryGetList(web.GetServerRelativeListUrlPrefix() + "GlymaExports"); //TODO get the name from a constant
                                    if (exportsList != null)
                                    {

                                        //the text payload will contain the properties serialized into a simple XML format
                                        ExportPropertiesDictionary serializableDict = new ExportPropertiesDictionary(exportProperties);
                                        string textPayload = serializableDict.ConvertToXml();

                                        Guid workItemId = Guid.NewGuid(); // Create a unique id for the work item and export job
                                        listItemIdNum = CreateExportJobListEntry(exportsList, domainUid, rootMapUid, mapType, exportType, workItemId, textPayload, userId);

                                        // if the list item was created then create the export job, if it wasn't it was because an export
                                        // for this particular root map of this type was already scheduled (changing the properties doesn't have effect)
                                        if (listItemIdNum != -1)
                                        {
                                            site.AddWorkItem(workItemId, //gWorkItemId - A Guid that identifies the work item
                                                DateTime.Now.ToUniversalTime(), //schdDateTime - represents a time in universal time for when the work item should take place
                                                GlymaExportWorkItemTimerJob.WorkItemTypeId, //gWorkItemType - this must be the GUID used in the GlymaExportWorkItemTimerJob
                                                web.ID, //gWebId - The identifier of the web containing the list
                                                exportsList.ID, //gParentId - The list ID
                                                listItemIdNum, //nItemId - The list item ID number
                                                true, //fSetWebId - true to set the Web identifier
                                                exportsList.Items.GetItemById(listItemIdNum).UniqueId, //gItemGuid - The unique identifier of the list item
                                                domainUid, //gBatchId - A Guid context identifier for the work item engine
                                                userId, //nUserId - SPUser ID number
                                                null, //rgbBinaryPayload - not used
                                                textPayload, //strTextPayload
                                                Guid.Empty); //gProcessingId - needs to be Guid.Empty

                                            ExportJob scheduledJob = new ExportJob();
                                            scheduledJob.Created = (DateTime)exportsList.Items.GetItemById(listItemIdNum)[SPBuiltInFieldId.Created];
                                            scheduledJob.CreatedBy = new GlymaUser() { Name = currentUser.Name };
                                            scheduledJob.Id = workItemId;
                                            scheduledJob.IsCurrent = true;
                                            scheduledJob.Status = ExportStatus.Scheduled;
                                            scheduledJob.Type = exportType;
                                            scheduledJob.MapType = mapType;
                                            scheduledJob.ExportProperties = exportProperties;
                                            scheduledJob.PercentageComplete = 0;
                                            response.ExportJob = scheduledJob;
                                        }
                                        else
                                        {
                                            //already scheduled so throw an exception to be handled with an error
                                            throw new Exception(string.Format("A scheduled export job already exists for the Glyma map: DomainUid: {0}, RootMapUid: {1}, Export Type: {2}, Map Type: {3}",
                                                domainUid.ToString(), rootMapUid.ToString(), exportType.ToString(), mapType.ToString()));
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception("Failed to find the Glyma Exports list.");
                                    }
                                }
                                else
                                {
                                    throw new Exception("The current user was not able to be determined.");
                                }
                            }
                            else
                            {
                                throw new Exception("The SPSite and/or the SPWeb were null.");
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExportError error = new ExportError() { ErrorMessage = "Failed to create the Glyma map export job." };
                throw new FaultException<ExportError>(error, ex.ToString());
            }

            return response;
        }
Exemple #7
0
        private ExportJob GetExportJob(SPListItem export)
        {
            ExportJob exportJob = new ExportJob();

            try
            {
                string workItemId = export[SPBuiltInFieldId.Title] as string;
                workItemId.Trim(); //ensure it is just the GUID in case anyone has messed with the list
                exportJob.Id = new Guid(workItemId);

                exportJob.Created = (DateTime)export[SPBuiltInFieldId.Created];
                string createdBy = export[SPBuiltInFieldId.Author] as string;
                exportJob.CreatedBy = new GlymaUser()
                {
                    Name = createdBy
                };


                if (export.Attachments.Count > 0)
                {
                    string fileName    = export.Attachments[0] as string;
                    string linkAddress = export.Attachments.UrlPrefix + fileName;
                    exportJob.Link = linkAddress;
                }
                string exportStatus = export["ExportStatus"] as string;
                exportJob.Status = (ExportStatus)Enum.Parse(typeof(ExportStatus), exportStatus, true);

                string exportType = export["ExportType"] as string;
                exportJob.Type = (ExportType)Enum.Parse(typeof(ExportType), exportType, true);

                string mapType = export["MapType"] as string;
                exportJob.MapType = (MapType)Enum.Parse(typeof(MapType), mapType, true);;

                string exportProperties = export["ExportProperties"] as string;
                if (!string.IsNullOrEmpty(exportProperties))
                {
                    try
                    {
                        ExportPropertiesDictionary serializableDict = new ExportPropertiesDictionary(export["ExportProperties"] as string);
                        exportJob.ExportProperties = serializableDict as IDictionary <string, string>;
                    }
                    catch (Exception)
                    {
                        //failed to parse the ExportProperties so assume they are null
                    }
                }

                try
                {
                    Double exportPercentageComplete = (Double)export["PercentageComplete"];
                    exportPercentageComplete     = exportPercentageComplete * 100;
                    exportJob.PercentageComplete = (int)exportPercentageComplete;
                }
                catch (Exception)
                {
                    exportJob.PercentageComplete = 0;
                }

                //TODO: determine if the export job is current (have any changes occurred to the map since last export
                exportJob.IsCurrent = true;
            }
            catch (Exception)
            {
                //Failed to read the Export Job from the SPListItem, could be a Guid ID wasn't of the correct format
            }

            return(exportJob);
        }
Exemple #8
0
        /// <summary>
        /// Creates an ExportJob and schedules the WorkItem for the timer job that processes the exports.
        /// </summary>
        /// <param name="domainUid">The DominUid for the map being exported</param>
        /// <param name="rootMapUid">The RootMapUid for the map being exported</param>
        /// <param name="exportProperties">The export properties for the export</param>
        /// <param name="mapType">The map type (schema) for the map being exported</param>
        /// <param name="exportType">The output format for the export</param>
        /// <returns>The ExportJob that was created</returns>
        public ExportJobResponse CreateExportJob(Guid domainUid, Guid rootMapUid, IDictionary <string, string> exportProperties, MapType mapType, ExportType exportType)
        {
            ExportJobResponse response = new ExportJobResponse();

            try
            {
                Guid   webID       = SPContext.Current.Web.ID;
                Guid   siteID      = SPContext.Current.Site.ID;
                SPUser currentUser = null;
                using (SPSite site = new SPSite(siteID))
                {
                    using (SPWeb web = site.OpenWeb(webID))
                    {
                        if (web != null)
                        {
                            currentUser = web.CurrentUser;
                        }
                    }
                }
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    int userId         = -1;
                    SPList exportsList = null;
                    int listItemIdNum  = -1;

                    using (SPSite site = new SPSite(siteID))
                    {
                        using (SPWeb web = site.OpenWeb(webID))
                        {
                            if (web != null)
                            {
                                if (currentUser == null)
                                {
                                    //The current user shouldn't be null, it should have been resolved outside of this RunWithElevatedPrivileges delegate
                                    currentUser = web.CurrentUser;
                                }
                                if (currentUser != null)
                                {
                                    userId = currentUser.ID;

                                    exportsList = web.TryGetList(web.GetServerRelativeListUrlPrefix() + "GlymaExports"); //TODO get the name from a constant
                                    if (exportsList != null)
                                    {
                                        //the text payload will contain the properties serialized into a simple XML format
                                        ExportPropertiesDictionary serializableDict = new ExportPropertiesDictionary(exportProperties);
                                        string textPayload = serializableDict.ConvertToXml();

                                        Guid workItemId = Guid.NewGuid(); // Create a unique id for the work item and export job
                                        listItemIdNum   = CreateExportJobListEntry(exportsList, domainUid, rootMapUid, mapType, exportType, workItemId, textPayload, userId);

                                        // if the list item was created then create the export job, if it wasn't it was because an export
                                        // for this particular root map of this type was already scheduled (changing the properties doesn't have effect)
                                        if (listItemIdNum != -1)
                                        {
                                            site.AddWorkItem(workItemId,                                            //gWorkItemId - A Guid that identifies the work item
                                                             DateTime.Now.ToUniversalTime(),                        //schdDateTime - represents a time in universal time for when the work item should take place
                                                             GlymaExportWorkItemTimerJob.WorkItemTypeId,            //gWorkItemType - this must be the GUID used in the GlymaExportWorkItemTimerJob
                                                             web.ID,                                                //gWebId - The identifier of the web containing the list
                                                             exportsList.ID,                                        //gParentId - The list ID
                                                             listItemIdNum,                                         //nItemId - The list item ID number
                                                             true,                                                  //fSetWebId - true to set the Web identifier
                                                             exportsList.Items.GetItemById(listItemIdNum).UniqueId, //gItemGuid - The unique identifier of the list item
                                                             domainUid,                                             //gBatchId - A Guid context identifier for the work item engine
                                                             userId,                                                //nUserId - SPUser ID number
                                                             null,                                                  //rgbBinaryPayload - not used
                                                             textPayload,                                           //strTextPayload
                                                             Guid.Empty);                                           //gProcessingId - needs to be Guid.Empty

                                            ExportJob scheduledJob = new ExportJob();
                                            scheduledJob.Created   = (DateTime)exportsList.Items.GetItemById(listItemIdNum)[SPBuiltInFieldId.Created];
                                            scheduledJob.CreatedBy = new GlymaUser()
                                            {
                                                Name = currentUser.Name
                                            };
                                            scheduledJob.Id                 = workItemId;
                                            scheduledJob.IsCurrent          = true;
                                            scheduledJob.Status             = ExportStatus.Scheduled;
                                            scheduledJob.Type               = exportType;
                                            scheduledJob.MapType            = mapType;
                                            scheduledJob.ExportProperties   = exportProperties;
                                            scheduledJob.PercentageComplete = 0;
                                            response.ExportJob              = scheduledJob;
                                        }
                                        else
                                        {
                                            //already scheduled so throw an exception to be handled with an error
                                            throw new Exception(string.Format("A scheduled export job already exists for the Glyma map: DomainUid: {0}, RootMapUid: {1}, Export Type: {2}, Map Type: {3}",
                                                                              domainUid.ToString(), rootMapUid.ToString(), exportType.ToString(), mapType.ToString()));
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception("Failed to find the Glyma Exports list.");
                                    }
                                }
                                else
                                {
                                    throw new Exception("The current user was not able to be determined.");
                                }
                            }
                            else
                            {
                                throw new Exception("The SPSite and/or the SPWeb were null.");
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExportError error = new ExportError()
                {
                    ErrorMessage = "Failed to create the Glyma map export job."
                };
                throw new FaultException <ExportError>(error, ex.ToString());
            }

            return(response);
        }
Exemple #9
0
        protected override bool ProcessWorkItem(SPContentDatabase contentDatabase,
                                                SPWorkItemCollection workItems, SPWorkItem workItem, SPJobState jobState)
        {
            Stopwatch sw = Stopwatch.StartNew(); //used to time how long the export took
            bool      processingAlready = false;

            try
            {
                if (workItem != null)
                {
                    //process the workItem
                    using (SPSite site = new SPSite(workItem.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb(workItem.WebId))
                        {
                            // processing logic
                            // 1. SET GlymaExport list Item to ExportStatus.Processing
                            // 2. START export based on type in GlymaExport list item
                            // 3. STORE the export in the GlymaExport list against the item as an attachment
                            // 4. SET GlymaExport list item to ExportStatus.Completed
                            SPList     exportsList = null;
                            SPListItem exportItem  = null;
                            try
                            {
                                exportsList = web.Lists[workItem.ParentId];
                                exportItem  = exportsList.GetItemByUniqueId(workItem.ItemGuid);
                                if (exportItem != null)
                                {
                                    string exportStatus = exportItem["ExportStatus"] as string;
                                    if (exportStatus == ExportStatus.Scheduled.ToString())
                                    {
                                        // If the export was still marked as scheduled then set it to processing
                                        WriteExportStatus(workItem, ExportStatus.Processing);

                                        exportItem = exportsList.GetItemByUniqueId(workItem.ItemGuid);

                                        if (exportItem != null)
                                        {
                                            // read the type of export that we should produce
                                            ExportType exportType = (ExportType)Enum.Parse(typeof(ExportType), exportItem["ExportType"] as string);
                                            MapType    mapType    = (MapType)Enum.Parse(typeof(MapType), exportItem["MapType"] as string);

                                            Dictionary <string, string> exportProperties = null;
                                            string exportPropertiesStr = exportItem["ExportProperties"] as string;
                                            if (!string.IsNullOrEmpty(exportPropertiesStr) && exportPropertiesStr.Trim() != string.Empty)
                                            {
                                                try
                                                {
                                                    ExportPropertiesDictionary exportDict = new ExportPropertiesDictionary(exportPropertiesStr);
                                                    exportProperties = exportDict as Dictionary <string, string>;
                                                }
                                                catch (XmlSchemaValidationException)
                                                {
                                                    exportProperties = null;
                                                }
                                            }

                                            bool useVerboseLogging = false;
                                            if (exportProperties != null)
                                            {
                                                if (exportProperties.ContainsKey("Verbose"))
                                                {
                                                    if (exportProperties["Verbose"].ToLower() == "true")
                                                    {
                                                        useVerboseLogging = true;
                                                    }
                                                }
                                            }

                                            if (useVerboseLogging)
                                            {
                                                LogMessage(workItem, "Creating Map Manager for the web: {0}.", web.Url);
                                            }

                                            // create the appropriate IExportUtility for the ExportType
                                            MapManagerFactory mapManagerFactory = new MapManagerFactory();
                                            IMapManager       mapManager        = mapManagerFactory.GetMapManager(web);
                                            if (mapManager != null)
                                            {
                                                if (useVerboseLogging)
                                                {
                                                    LogMessage(workItem, "Created Map Manager for the web: {0}.", web.Url);
                                                }

                                                ExportUtilityFactory exportUtilityFactory = new ExportUtilityFactory(mapManager);
                                                IExportUtility       exportUtility        = exportUtilityFactory.CreateExportUtility(mapType, exportType);

                                                if (exportUtility != null)
                                                {
                                                    if (useVerboseLogging)
                                                    {
                                                        LogMessage(workItem, "Created Export Utility for {0} map type to {1}.", mapType.ToString(), exportType.ToString());
                                                    }

                                                    exportItem = exportsList.GetItemByUniqueId(workItem.ItemGuid);
                                                    if (exportItem != null)
                                                    {
                                                        Guid rootMapUid = Guid.Empty;
                                                        Guid domainUid  = Guid.Empty;
                                                        try
                                                        {
                                                            string rootMapUidValue = exportItem["RootMapUid"] as string;
                                                            string domainUidValue  = exportItem["DomainUid"] as string;
                                                            domainUid  = new Guid(domainUidValue);
                                                            rootMapUid = new Guid(rootMapUidValue);
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            //The GUIDs were not parsed correctly
                                                        }
                                                        if (rootMapUid != Guid.Empty && domainUid != Guid.Empty)
                                                        {
                                                            //The export utility will do the grunt work and provide a URL to the temp file created
                                                            GlymaExportUserState userState = new GlymaExportUserState(workItem);
                                                            userState.UseVerboseLogging    = useVerboseLogging; //store the level of logging to use in the event handlers
                                                            exportUtility.ExportCompleted += exportUtility_ExportCompleted;
                                                            exportUtility.ProgressChanged += exportUtility_ProgressChanged;
                                                            exportUtility.ExceptionRaised += exportUtility_ExceptionRaised;

                                                            if (useVerboseLogging)
                                                            {
                                                                LogMessage(workItem, "Starting export.");
                                                            }

                                                            exportUtility.ExportMap(domainUid, rootMapUid, exportProperties, new List <Guid>(), userState);

                                                            if (useVerboseLogging)
                                                            {
                                                                LogMessage(workItem, "Waiting for export to complete.");
                                                            }

                                                            // Wait for the export to complete (up to the timeout)
                                                            bool signalled = userState.Completed.WaitOne(TIMEOUT_MS);
                                                            if (!signalled)
                                                            {
                                                                if (useVerboseLogging)
                                                                {
                                                                    LogMessage(workItem, "The export timed out after {0}ms", TIMEOUT_MS);
                                                                }
                                                                // The timeout occurred so don't process the completed or progress changed events
                                                                exportUtility.ExportCompleted -= exportUtility_ExportCompleted;
                                                                exportUtility.ProgressChanged -= exportUtility_ProgressChanged;
                                                                exportUtility.ExceptionRaised -= exportUtility_ExceptionRaised;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            throw new Exception(string.Format("The DomainUid and/or RootMapUid were not valid Guid's. DomainUid: {0} RootMapUid: {1}.", domainUid.ToString(), rootMapUid.ToString()));
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    throw new Exception(string.Format("Unable to create export utility for the export type: {0}.", exportType.ToString()));
                                                }
                                            }
                                            else
                                            {
                                                throw new Exception(string.Format("Unable to build an IMapManager for the current SPWeb. {0}", mapManagerFactory.ErrorMessage));
                                            }
                                        }
                                    }
                                    else if (exportStatus == ExportStatus.Processing.ToString())
                                    {
                                        //shouldn't do anything unless it's scheduled.
                                        processingAlready = true;
                                    }
                                }
                                else
                                {
                                    throw new Exception("The Export Job did not exist in the list.");
                                }
                            }
                            catch (TargetInvocationException tiex)
                            {
                                if (workItem != null)
                                {
                                    WriteExportStatus(workItem, ExportStatus.Error);
                                    LogMessage(workItem, "Error during export: {0}", tiex.Message);

                                    if (tiex.InnerException != null)
                                    {
                                        LogMessage(workItem, "Inner Exception({0}): {1}.\r\nStackTrace: {2}", tiex.InnerException.GetType().ToString(), tiex.InnerException.Message, tiex.InnerException.StackTrace);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                //exception handling
                                if (workItem != null)
                                {
                                    WriteExportStatus(workItem, ExportStatus.Error);
                                    LogMessage(workItem, "Error during export: {0}.", ex.Message);
                                }
                            }
                            finally
                            {
                                if (!processingAlready)
                                {
                                    // delete the workItem after we've processed it
                                    workItems.SubCollection(site, web, 0, (uint)workItems.Count).DeleteWorkItem(workItem.Id);
                                }

                                //Update the progress of the timer job
                                Progress += (100 / workItems.Count); //estimate only, some maps will export faster than others and different types may export faster
                                if (Progress > 100)
                                {
                                    Progress = 100;
                                }
                                this.UpdateProgress(Progress); //The base classes timer job's overall progress

                                if (!processingAlready)        //this is if the timer job ran and started processing the item that was already being processed
                                {
                                    exportItem = exportsList.GetItemByUniqueId(workItem.ItemGuid);
                                    if (exportItem != null)
                                    {
                                        string exportStatus = exportItem["ExportStatus"] as string;
                                        if (exportStatus == ExportStatus.Processing.ToString())
                                        {
                                            //if it's still processing and at this point then something has failed.
                                            WriteExportStatus(workItem, ExportStatus.Error);
                                            LogMessage(workItem, "The export failed as it was still in a processing state when it had apparently completed.");
                                        }

                                        LogMessage(workItem, "Completed in {0:0.000}s.", sw.Elapsed.TotalSeconds);//add in a metric for how long the export took (successful or failure)
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //exception handling
                throw new Exception("Failed to while processing Glyma Export Work Item Timer Job.", ex);
            }

            return(true);
        }