Example #1
0
        /// <summary> Constructor for a new instance of the Default_Metadata_AdminViewer class </summary>
        /// <param name="RequestSpecificValues"> All the necessary, non-global data specific to the current request </param>
        /// <remarks> Postback from handling a new project is handled here in the constructor </remarks>
        public Default_Metadata_AdminViewer(RequestCache RequestSpecificValues) : base(RequestSpecificValues)
        {
            RequestSpecificValues.Tracer.Add_Trace("Default_Metadata_AdminViewer.Constructor", String.Empty);

            // Set action message and entered values to nothing to start
            actionMessage = String.Empty;
            entered_code  = String.Empty;
            entered_base  = String.Empty;
            entered_name  = String.Empty;
            entered_desc  = String.Empty;

            // If the RequestSpecificValues.Current_User cannot edit this, go back
            if ((!RequestSpecificValues.Current_User.Is_System_Admin) && (!RequestSpecificValues.Current_User.Is_Portal_Admin))
            {
                RequestSpecificValues.Current_Mode.Mode          = Display_Mode_Enum.My_Sobek;
                RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // If this is a postback, handle any events first
            if (RequestSpecificValues.Current_Mode.isPostBack)
            {
                try
                {
                    // Pull the standard values
                    NameValueCollection form = HttpContext.Current.Request.Form;

                    string save_value   = form["admin_project_tosave"].ToUpper().Trim();
                    string delete_value = form["admin_project_delete"].ToUpper().Trim();
                    string code_value   = form["admin_project_code"].ToUpper().Trim();

                    // Was this a delete request?
                    if ((RequestSpecificValues.Current_User.Is_System_Admin) && (delete_value.Length > 0))
                    {
                        RequestSpecificValues.Tracer.Add_Trace("Default_Metadata_AdminViewer.Constructor", "Delete default metadata '" + delete_value + "'");

                        // Try to delete in the database
                        if (SobekCM_Database.Delete_Default_Metadata(delete_value, RequestSpecificValues.Tracer))
                        {
                            // Set the message
                            actionMessage = "Deleted default metadata '" + delete_value + "'";

                            // Look for the file to delete as well
                            string pmets_file = UI_ApplicationCache_Gateway.Settings.Servers.Base_MySobek_Directory + "projects\\" + delete_value + ".pmets";
                            if (File.Exists(pmets_file))
                            {
                                try
                                {
                                    File.Delete(pmets_file);
                                }
                                catch
                                {
                                    actionMessage = "Deleted default metadata '" + delete_value + "' but failed to delete associated pmets file";
                                }
                            }
                        }
                        else
                        {
                            actionMessage = "Error encountered deleting default metadata '" + delete_value + "' from the database";
                        }



                        Engine_ApplicationCache_Gateway.RefreshCollectionAliases();
                    }
                    else if (save_value.Length > 0) // Or.. was this a save request
                    {
                        RequestSpecificValues.Tracer.Add_Trace("Default_Metadata_AdminViewer.Constructor", "Save default metadata '" + save_value + "'");

                        // Was this to save a new project (from the main page) or rename an existing (from the popup form)?
                        if (save_value == code_value)
                        {
                            entered_code = code_value;
                            entered_base = form["admin_project_base"].ToUpper().Trim();
                            entered_name = form["admin_project_name"].Trim().Replace("\"", "'");
                            entered_desc = form["admin_project_desc"].Trim().Replace("\"", "'");

                            if (entered_name.Length == 0)
                            {
                                entered_name = entered_code;
                            }
                            if (entered_desc.Length == 0)
                            {
                                entered_desc = entered_name;
                            }

                            // Ensure the code is valid first
                            bool result = entered_code.All(C => Char.IsLetterOrDigit(C) || C == '_');
                            if (!result)
                            {
                                actionMessage = "ERROR: Default metadata CODE must contain only letters and numbers";
                                entered_code  = entered_code.Replace("\"", "");
                            }
                            else if (entered_code.Length == 0)
                            {
                                actionMessage = "ERROR: Default metadata CODE is required";
                            }
                            else if (entered_code.Length > 20)
                            {
                                actionMessage = "ERROR: Default metadata CODE cannot be more than twenty characters long";
                                entered_code  = entered_code.Substring(0, 20);
                            }
                            else if (entered_name.Length > 50)
                            {
                                actionMessage = "ERROR: Default metadata NAME cannot be more than fifty characters long";
                                entered_name  = entered_name.Substring(0, 50);
                            }
                            else
                            {
                                // Save this new interface
                                if (SobekCM_Database.Save_Default_Metadata(entered_code.ToUpper(), entered_name, entered_desc, -1, RequestSpecificValues.Tracer))
                                {
                                    actionMessage = "Saved new default metadata <i>" + save_value + "</i>";

                                    entered_code = String.Empty;
                                    entered_base = String.Empty;
                                    entered_name = String.Empty;
                                    entered_desc = String.Empty;
                                }
                                else
                                {
                                    actionMessage = "Unable to save new default metadata <i>" + save_value + "</i>";
                                }

                                Engine_ApplicationCache_Gateway.RefreshCollectionAliases();

                                // Try to creating the PMETS file if there was a base PMETS code provided
                                try
                                {
                                    if (entered_base.Length > 0)
                                    {
                                        string pmets_file      = UI_ApplicationCache_Gateway.Settings.Servers.Base_MySobek_Directory + "projects\\" + code_value + ".pmets";
                                        string base_pmets_file = UI_ApplicationCache_Gateway.Settings.Servers.Base_MySobek_Directory + "projects\\" + entered_base + ".pmets";

                                        if (File.Exists(base_pmets_file))
                                        {
                                            File.Copy(base_pmets_file, pmets_file, true);
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    actionMessage = "Error copying new default metadata METS to the project folder";
                                }
                            }
                        }
                        else
                        {
                            string edit_name        = form["form_project_name"].Trim().Replace("\"", "'");
                            string edit_description = form["form_project_desc"].Trim().Replace("\"", "'");

                            // Save this existing interface
                            if (SobekCM_Database.Save_Default_Metadata(save_value.ToUpper(), edit_name, edit_description, -1, RequestSpecificValues.Tracer))
                            {
                                actionMessage = "Renamed existing default metadata <i>" + save_value + "</i>";
                            }
                            else
                            {
                                actionMessage = "Unable to rename existing default metadata <i>" + save_value + "</i>";
                            }

                            Engine_ApplicationCache_Gateway.RefreshCollectionAliases();
                        }
                    }
                }
                catch (Exception)
                {
                    actionMessage = "Unknown exception occurred while processing your request";
                }
            }

            UI_ApplicationCache_Gateway.ResetDefaultMetadataTemplates();
        }