Esempio n. 1
0
        public static List <string> Validate(string path, out bool valid)
        {
            var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            var reader = PackageReader.Create(stream);

            valid = true;

            var settings = new PackageValidatorSettings(
                ValidationBehavior.LogWarning,
                ValidationBehavior.LogWarning,
                ValidationBehavior.LogError,
                ValidationBehavior.LogWarning);

            ValidationResults results;

            var messages = new List <string>();

            try
            {
                results = Microsoft.LearningComponents.PackageValidator.Validate(reader, settings);
            }
            catch (InvalidPackageException ex)
            {
                messages.Add(string.Format("Package is invalid.{0}", ex.Message));
                valid = false;
                reader.Dispose();
                stream.Close();
                return(messages);
            }

            foreach (var result in results.Results)
            {
                if (result.IsError)
                {
                    valid = false;
                    messages.Add(string.Format("MLC Error: {0}", result.Message));
                }
                else
                {
                    messages.Add(string.Format("SCORM Warning: {0}", result.Message));
                }
            }

            if (messages.Count == 0)
            {
                messages.Add("Package is valid.");
            }

            reader.Dispose();
            stream.Close();

            return(messages);
        }
        /// <summary>
        /// Validate the package selected by the user.
        /// </summary>
        ///
        void ValidateSelectedPackage()
        {
            // do nothing if no package is selected
            if (packageListBox.SelectedIndex < 0)
            {
                return;
            }

            // execute the remainder of this method with a wait cursor displayed
            Cursor = Cursors.WaitCursor;
            try
            {
                // set <filePath> to the path to the package the user selected
                FileListItem fileListItem = (FileListItem)packageListBox.SelectedItem;
                string       filePath     = fileListItem.FilePath;

                // empty <logTextBox>, and add the file name
                logTextBox.Clear();
                logTextBox.AppendText(String.Format("Validating: {0}\r\n\r\n", filePath));

                // validate the package
                using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    bool anyMessages = false;
                    using (PackageReader packageReader = PackageReader.Create(stream))
                    {
                        // set package validator settings based on the setting of the <additionalScormWarningsCheckBox> control
                        PackageValidatorSettings settings;
                        if (additionalScormWarningsCheckBox.Checked)
                        {
                            // perform the same validation as SLK, plus look for additional SCORM issues
                            settings = new PackageValidatorSettings(
                                ValidationBehavior.LogWarning, ValidationBehavior.LogWarning,
                                ValidationBehavior.LogError, ValidationBehavior.LogWarning);
                        }
                        else
                        {
                            // perform the same validation as SLK
                            settings = new PackageValidatorSettings(
                                ValidationBehavior.LogWarning, ValidationBehavior.None,
                                ValidationBehavior.LogError, ValidationBehavior.LogWarning);
                        }

                        // validate the package; set <results> to the list of results
                        ValidationResults results;
                        try
                        {
                            results = PackageValidator.Validate(packageReader, settings);
                        }
                        catch (InvalidPackageException ex)
                        {
                            logTextBox.AppendText(String.Format("Package is invalid.\r\n\r\n{0}", ex.Message));
                            return;
                        }

                        // display <results>
                        foreach (ValidationResult result in results.Results)
                        {
                            string message;
                            if (result.IsError)
                            {
                                message = String.Format("MLC Error: {0}\r\n\r\n", result.Message);
                            }
                            else
                            {
                                message = String.Format("SCORM Warning: {0}\r\n\r\n", result.Message);
                            }
                            logTextBox.AppendText(message);
                            anyMessages = true;
                        }
                    }

                    // if there were no messages, give feedback to that effect to the user
                    if (!anyMessages)
                    {
                        logTextBox.AppendText("Package is valid.");
                    }
                }
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
    protected void UploadPackageButton_OnClick(object sender, EventArgs e)
    {
        // the user clicked "Upload"...

        // do nothing if the user didn't select a file to upload
        if (!UploadedPackageFile.HasFile)
        {
            return;
        }

        // hide the upload panel and show the message panel; the message panel will hold
        // information about the success or failure of the package upload operation
        UploadPanel.Visible  = false;
        MessagePanel.Visible = true;

        // attempt to import the uploaded file into PackageStore
        try
        {
            // set <currentUser> to information about the current user; we need the current user's
            // UserItemIdentifier
            LStoreUserInfo currentUser = GetCurrentUserInfo();

            // import the package file; set packageId to the ID of the uploaded
            // package; set importLog to a collection of warnings (if any)
            // about the import process
            PackageItemIdentifier packageId;
            ValidationResults     importLog;
            using (PackageReader packageReader = PackageReader.Create(UploadedPackageFile.FileContent))
            {
                // Add package, asking to fix anything that can be fixed.
                AddPackageResult result = PStore.AddPackage(packageReader, new PackageEnforcement(false, false, false));
                packageId = result.PackageId;
                importLog = result.Log;
            }

            // fill in the application-specific columns of the PackageItem table
            LearningStoreJob            job        = LStore.CreateJob();
            Dictionary <string, object> properties = new Dictionary <string, object>();
            properties[Schema.PackageItem.Owner]          = currentUser.Id;
            properties[Schema.PackageItem.FileName]       = UploadedPackageFile.FileName;
            properties[Schema.PackageItem.UploadDateTime] = DateTime.Now;
            job.UpdateItem(packageId, properties);
            job.Execute();

            // retrieve information about the package
            job = LStore.CreateJob();
            RequestMyTraining(job, packageId);
            GetMyTrainingResultsToHtml(job.Execute <DataTable>(), TrainingGrid);

            // when the page loads in the browser, copy information about the from the TrainingGrid
            // hidden table to the main page using client-side script
            UpdateParentPageScript.Visible = true;

            // provide user feedback
            if (importLog.HasWarnings)
            {
                // display warnings
                WarningIntro.Visible    = true;
                WarningMessages.Visible = true;
                foreach (ValidationResult result in importLog.Results)
                {
                    ScrollingMessagesList.Items.Add(new ListItem(result.Message));
                }
            }
            else
            {
                // the operation was successful, and there are no messages to display to the user,
                // so close the dialog
                CloseDialogScript.Visible = true;
            }
        }
        catch (PackageImportException ex)
        {
            // a package import failure occurred -- display the error message
            ErrorIntro.Visible   = true;
            ErrorMessage.Visible = true;

            foreach (ValidationResult result in ex.Log.Results)
            {
                ErrorMessageScrollingList.Items.Add(new ListItem(result.Message));
            }

            if (ex.InnerException != null)
            {
                ErrorMessageScrollingList.Items.Add(new ListItem(
                                                        Server.HtmlEncode(ex.InnerException.Message)));
            }
        }
        catch (Exception ex)
        {
            // an unexpected error occurred -- display a generic message that doesn't include the
            // exception message (since that message may include sensitive information), and write
            // the exception message to the event log
            ErrorIntro.Visible   = true;
            ErrorMessage.Visible = true;
            ErrorMessage.Controls.Add(new System.Web.UI.LiteralControl(
                                          Server.HtmlEncode("A serious error occurred.  Please contact your system administrator.  More information has been written to the server event log.")));
            LogEvent(System.Diagnostics.EventLogEntryType.Error,
                     "An exception occurred while uploading a package:\n\n{0}\n\n", ex.ToString());
        }
    }