/// <summary> Constructor for a new instance of the Delete_Item_MySobekViewer class </summary> /// <param name="User"> Authenticated user information </param> /// <param name="Current_Mode"> Mode / navigation information for the current request</param> /// <param name="All_Items_Lookup"> Allows individual items to be retrieved by various methods as <see cref="SobekCM.Library.Application_State.Single_Item"/> objects.</param> /// <param name="Tracer">Trace object keeps a list of each method executed and important milestones in rendering</param> public Delete_Item_MySobekViewer(User_Object User, SobekCM_Navigation_Object Current_Mode, Item_Lookup_Object All_Items_Lookup, Custom_Tracer Tracer) : base(User) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Delete this item"); // Save mode and set defaults currentMode = Current_Mode; errorCode = -1; // First, ensure this is a logged on user and system administrator before continuing Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Validate user permissions"); if ((User == null) || ((!User.Is_System_Admin) && (User.UserName.ToLower() != "neldamaxs"))) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "User does not have delete permissions", Custom_Trace_Type_Enum.Error); errorCode = 1; } // Second, ensure the item is valid if (errorCode == -1) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Validate item exists"); if (!All_Items_Lookup.Contains_BibID_VID(Current_Mode.BibID, Current_Mode.VID)) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Item indicated is not valid", Custom_Trace_Type_Enum.Error); errorCode = 2; } } // Get the current item details string bib_location = String.Empty; string vid_location = String.Empty; if (errorCode == -1) { // Get item details DataSet itemDetails = SobekCM_Database.Get_Item_Details(currentMode.BibID, currentMode.VID, Tracer); // If the itemdetails was null, this item is somehow invalid item then if (itemDetails == null) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Item indicated is not valid", Custom_Trace_Type_Enum.Error); errorCode = 2; } else { // Get the location for this METS file from the returned value DataRow mainItemRow = itemDetails.Tables[2].Rows[0]; bib_location = SobekCM_Library_Settings.Image_Server_Network + mainItemRow["File_Location"].ToString().Replace("/", "\\"); vid_location = bib_location + "\\" + currentMode.VID; } } // If this is a postback, handle any events first if ((currentMode.isPostBack) && (errorCode < 0)) { Debug.Assert(User != null, "User != null"); // Pull the standard values string save_value = HttpContext.Current.Request.Form["admin_delete_item"]; string text_value = HttpContext.Current.Request.Form["admin_delete_confirm"]; // Better say "DELETE", or just send back to the item if ((save_value == null) || (save_value.ToUpper() != "DELETE") || (text_value.ToUpper() != "DELETE")) { HttpContext.Current.Response.Redirect(Current_Mode.Base_URL + currentMode.BibID + "/" + currentMode.VID); } else { errorCode = 0; // Perform the database delete Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Perform database update"); bool database_result = SobekCM_Database.Delete_SobekCM_Item(currentMode.BibID, currentMode.VID, User.Is_System_Admin, String.Empty); if (!database_result) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Error performing delete in the database", Custom_Trace_Type_Enum.Error); errorCode = 3; } else { // Move the folder to deletes try { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Move resource files to DELETED folder"); // Make sure upper deleted folder exists, or create it string delete_folder = SobekCM_Library_Settings.Image_Server_Network + "DELETED"; if (!Directory.Exists(delete_folder)) { Directory.CreateDirectory(delete_folder); } // Create the bib level folder next string bib_folder = SobekCM_Library_Settings.Image_Server_Network + "DELETED\\" + currentMode.BibID; if (!Directory.Exists(bib_folder)) { Directory.CreateDirectory(bib_folder); } // Ensure the VID folder does not exist string vid_folder = SobekCM_Library_Settings.Image_Server_Network + "DELETED\\" + currentMode.BibID + "\\" + currentMode.VID; if (Directory.Exists(vid_folder)) { Directory.Move(vid_folder, vid_folder + "_OLD"); } // Move the VID folder over now Directory.Move(vid_location, vid_folder); // Check if this was the last VID under this BIB if (Directory.GetDirectories(bib_location).Length == 0) { // Move all files over to the bib folder then string[] bib_files = Directory.GetFiles(bib_location); foreach (string thisFile in bib_files) { string fileName = (new FileInfo(thisFile)).Name; string new_file = bib_folder + "\\" + fileName; File.Move(thisFile, new_file); } } } catch (Exception ee) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Error moving the folder and files to the DELETED folder", Custom_Trace_Type_Enum.Error); Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", ee.Message, Custom_Trace_Type_Enum.Error); Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", ee.StackTrace, Custom_Trace_Type_Enum.Error); errorCode = 4; } // Remove from the item list All_Items_Lookup.Remove_Item(currentMode.BibID, currentMode.VID); // Also remove from the cache Cached_Data_Manager.Remove_Digital_Resource_Object(currentMode.BibID, currentMode.VID, Tracer); } } } }
private void Delete_Item(User_Object User, Item_Lookup_Object All_Items_Lookup, Custom_Tracer Tracer) { errorCode = 0; // Get the current item details string vid_location = item.Source_Directory; string bib_location = (new DirectoryInfo(vid_location)).Parent.FullName; //if (errorCode == -1) //{ // // Get item details // DataSet itemDetails = SobekCM_Database.Get_Item_Details(currentMode.BibID, currentMode.VID, Tracer); // // If the itemdetails was null, this item is somehow invalid item then // if (itemDetails == null) // { // Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Item indicated is not valid", Custom_Trace_Type_Enum.Error); // errorCode = 2; // } // else // { // // Get the location for this METS file from the returned value // DataRow mainItemRow = itemDetails.Tables[2].Rows[0]; // bib_location = SobekCM_Library_Settings.Image_Server_Network + mainItemRow["File_Location"].ToString().Replace("/", "\\"); // vid_location = bib_location + "\\" + currentMode.VID; // } //} // Perform the database delete Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Perform database update"); bool database_result2 = SobekCM_Database.Delete_SobekCM_Item(currentMode.BibID, currentMode.VID, User.Is_System_Admin, String.Empty); // Perform the SOLR delete Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Perform solr delete"); Solr.Solr_Controller.Delete_Resource_From_Index(SobekCM_Library_Settings.Document_Solr_Index_URL, SobekCM_Library_Settings.Page_Solr_Index_URL, currentMode.BibID, currentMode.VID); if (!database_result2) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Error performing delete in the database", Custom_Trace_Type_Enum.Error); errorCode = 3; } else { // Move the folder to deletes try { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Move resource files to RECYCLE BIN folder"); // Make sure upper RECYCLE BIN folder exists, or create it string delete_folder = SobekCM_Library_Settings.Image_Server_Network + "RECYCLE BIN"; if (!Directory.Exists(delete_folder)) { Directory.CreateDirectory(delete_folder); } // Create the bib level folder next string bib_folder = SobekCM_Library_Settings.Image_Server_Network + "RECYCLE BIN\\" + currentMode.BibID; if (!Directory.Exists(bib_folder)) { Directory.CreateDirectory(bib_folder); } // Ensure the VID folder does not exist string vid_folder = SobekCM_Library_Settings.Image_Server_Network + "RECYCLE BIN\\" + currentMode.BibID + "\\" + currentMode.VID; if (Directory.Exists(vid_folder)) { Directory.Move(vid_folder, vid_folder + "_OLD"); } // Move the VID folder over now Directory.Move(vid_location, vid_folder); // Check if this was the last VID under this BIB if (Directory.GetDirectories(bib_location).Length == 0) { // Move all files over to the bib folder then string[] bib_files = Directory.GetFiles(bib_location); foreach (string thisFile in bib_files) { string fileName = (new FileInfo(thisFile)).Name; string new_file = bib_folder + "\\" + fileName; File.Move(thisFile, new_file); } } } catch (Exception ee) { Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", "Error moving the folder and files to the RECYCLE BIN folder", Custom_Trace_Type_Enum.Error); Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", ee.Message, Custom_Trace_Type_Enum.Error); Tracer.Add_Trace("Delete_Item_MySobekViewer.Constructor", ee.StackTrace, Custom_Trace_Type_Enum.Error); errorCode = 4; } // Remove from the item list All_Items_Lookup.Remove_Item(currentMode.BibID, currentMode.VID); // Also remove from the cache Cached_Data_Manager.Remove_Digital_Resource_Object(currentMode.BibID, currentMode.VID, Tracer); } }