changeset 65:4dd7d78e19a1

Moved recursive card deletion logic to Database class
author Fox
date Fri, 23 Dec 2022 22:25:27 +0100
parents e8d83f1a6100
children df652edb3c0e
files src/junotu/Card.java src/junotu/Database.java src/junotu/TabBoard.java src/junotu/TabEdit.java
diffstat 4 files changed, 69 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Card.java	Fri Dec 23 21:33:05 2022 +0100
+++ b/src/junotu/Card.java	Fri Dec 23 22:25:27 2022 +0100
@@ -19,6 +19,7 @@
     
     public static final String TAG_BOARD              = TAG_CORE_PREFIX+"board";
     public static final String TAG_BOARD_COLUMNS      = TAG_CORE_PREFIX+"board_columns";
+    public static final String TAG_BOARD_COLUMN       = TAG_CORE_PREFIX+"board_column";
     public static final String TAG_BOARD_COLUMN_CARDS = TAG_CORE_PREFIX+"board_column_cards";
     public static final String TAG_BOARD_COLUMN_CARD  = TAG_CORE_PREFIX+"board_column_card";
 
@@ -165,6 +166,11 @@
 	return value != null ? value : or;
     }
 
+    public boolean tagHas( String tag )
+    {
+	return tags.containsKey(tag);
+    }
+
     public boolean isBoard()
     {
 	return tags.containsKey(TAG_BOARD);
--- a/src/junotu/Database.java	Fri Dec 23 21:33:05 2022 +0100
+++ b/src/junotu/Database.java	Fri Dec 23 22:25:27 2022 +0100
@@ -217,7 +217,64 @@
 	
     }
 
-    public void cardDelete( long identifier )
+    public void cardDelete( Card card )
+    {
+	cardDelete( card, false );
+    }
+
+    public void cardDelete( Card card, boolean cautious )
+    {
+	if( cautious ) {
+	    String tag;
+	    if( (tag = card.<String>tagGetAs(Card.TAG_BOARD_COLUMN_CARD)) != null && tag.equals(Card.VALUE_BOARD_COLUMN_CARD_ONLY) ) {
+		//pass
+	    } else {
+		return; /* Don't delete. */
+	    }
+	}
+	String tag;
+	Card[] cards;
+	if( card.tagHas(Card.TAG_BOARD) ) {
+	    tag = card.<String>tagGetAsOr(Card.TAG_BOARD_COLUMNS, "");
+	    cards = TagUtility.parseCardList(tag);
+	    for( int i = 0; i < cards.length; i++ ) {
+		if( cards[i] == null ) {
+		    continue;
+		}
+		cardDelete( cards[i], false );
+	    }
+	}
+	if( card.tagHas(Card.TAG_BOARD_COLUMN) ) {
+	    tag = card.<String>tagGetAsOr(Card.TAG_BOARD_COLUMN_CARDS, "");
+	    cards = TagUtility.parseCardList(tag);
+	    for( int i = 0; i < cards.length; i++ ) {
+		if( cards[i] == null ) {
+		    continue;
+		}
+		cardDelete( cards[i], true );
+	    }
+	}
+
+	cardDeleteRaw(card.identifierGet());
+	
+    }
+
+    public void cardDeleteByIdentifier( long identifier )
+    {
+	cardDeleteByIdentifier( identifier, false );
+    }
+    
+    public void cardDeleteByIdentifier( long identifier, boolean cautious )
+    {
+	Card card = cardGetByIdentifier(identifier);
+	if( card != null ) {
+	    cardDelete(card);
+	} else {
+	    throw new RuntimeException( "Failed to delete card by identifier "+Long.toString(identifier)+", not found." );
+	}
+    }
+
+    public void cardDeleteRaw( long identifier )
     {
 	TopDocs topDocuments;
 	Query query = NumericRangeQuery.newLongRange( Card.TAG_IDENTIFIER, identifier, identifier, true, true );
--- a/src/junotu/TabBoard.java	Fri Dec 23 21:33:05 2022 +0100
+++ b/src/junotu/TabBoard.java	Fri Dec 23 22:25:27 2022 +0100
@@ -337,6 +337,7 @@
 
 	    card.titleSet( titleGet() );
 	    card.tagValueSetOnly( Card.TAG_BOARD_COLUMN_CARDS, cardIdentifiers );
+	    card.tagValueSetOnly( Card.TAG_BOARD_COLUMN, null );
 
 	    try {
 		if( newCard ) {
@@ -353,18 +354,8 @@
 
 	public void delete()
 	{
-	    Component[] cardList = cards.getComponents();
-	    for( int i = 0; i < cardList.length; i++ ) {
-		ColumnCardWidget cardWidget = (ColumnCardWidget)cardList[i];
-		cardWidget.delete();
-	    }
-	    if( !newCard ) {
-		try {
-		    Main.database.cardDelete(identifier);
-		} catch( Exception e ) {
-		    throw new RuntimeException(e);
-		}
-	    }
+	    save();
+	    Main.database.cardDeleteByIdentifier(identifier);
 	}
 
 	public void actionPerformed( ActionEvent e )
@@ -511,17 +502,7 @@
 		return;
 	    }
 
-	    Card card;
-	    
-	    card = Main.database.cardGetByIdentifier(identifier);
-	    
-	    if( card == null ) {
-		throw new RuntimeException();
-	    }
-
-	    if( card.<String>tagGetAsOr( Card.TAG_BOARD_COLUMN_CARD, "" ).equals(Card.VALUE_BOARD_COLUMN_CARD_ONLY) ) {
-		Main.database.cardDelete(card.identifierGet());
-	    }
+	    Main.database.cardDeleteByIdentifier(identifier, true);
 	}
 
 	public boolean isSelected()
--- a/src/junotu/TabEdit.java	Fri Dec 23 21:33:05 2022 +0100
+++ b/src/junotu/TabEdit.java	Fri Dec 23 22:25:27 2022 +0100
@@ -443,7 +443,7 @@
 	    return;
 	}
 	
-	Main.database.cardDelete( card.identifierGet() );
+	Main.database.cardDeleteByIdentifier( card.identifierGet() );
 	
 	Window window = (Window)this.getTopLevelAncestor();
         window.tabSearch.search();