From 685d8311066c673a864fd5645027dd0eecb4550b Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 02 May 2014 15:44:08 -0400
Subject: [PATCH] Merged #19 "Implement Ticket service migration tool"

---
 src/main/java/com/gitblit/tickets/FileTicketService.java |   68 +++++++++++++++++++++++++++-------
 1 files changed, 54 insertions(+), 14 deletions(-)

diff --git a/src/main/java/com/gitblit/tickets/FileTicketService.java b/src/main/java/com/gitblit/tickets/FileTicketService.java
index 4386020..b3d8838 100644
--- a/src/main/java/com/gitblit/tickets/FileTicketService.java
+++ b/src/main/java/com/gitblit/tickets/FileTicketService.java
@@ -22,6 +22,8 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -146,6 +148,31 @@
 		return hasTicket;
 	}
 
+	@Override
+	public synchronized Set<Long> getIds(RepositoryModel repository) {
+		Set<Long> ids = new TreeSet<Long>();
+		Repository db = repositoryManager.getRepository(repository.name);
+		try {
+			// identify current highest ticket id by scanning the paths in the tip tree
+			File dir = new File(db.getDirectory(), TICKETS_PATH);
+			dir.mkdirs();
+			List<File> journals = findAll(dir, JOURNAL);
+			for (File journal : journals) {
+				// Reconstruct ticketId from the path
+				// id/26/326/journal.json
+				String path = FileUtils.getRelativePath(dir, journal);
+				String tid = path.split("/")[1];
+				long ticketId = Long.parseLong(tid);
+				ids.add(ticketId);
+			}
+		} finally {
+			if (db != null) {
+				db.close();
+			}
+		}
+		return ids;
+	}
+
 	/**
 	 * Assigns a new ticket id.
 	 *
@@ -162,18 +189,10 @@
 			}
 			AtomicLong lastId = lastAssignedId.get(repository.name);
 			if (lastId.get() <= 0) {
-				// identify current highest ticket id by scanning the paths in the tip tree
-				File dir = new File(db.getDirectory(), TICKETS_PATH);
-				dir.mkdirs();
-				List<File> journals = findAll(dir, JOURNAL);
-				for (File journal : journals) {
-					// Reconstruct ticketId from the path
-					// id/26/326/journal.json
-					String path = FileUtils.getRelativePath(dir, journal);
-					String tid = path.split("/")[1];
-					long ticketId = Long.parseLong(tid);
-					if (ticketId > lastId.get()) {
-						lastId.set(ticketId);
+				Set<Long> ids = getIds(repository);
+				for (long id : ids) {
+					if (id > lastId.get()) {
+						lastId.set(id);
 					}
 				}
 			}
@@ -284,8 +303,7 @@
 	}
 
 	/**
-	 * Retrieves the ticket from the repository by first looking-up the changeId
-	 * associated with the ticketId.
+	 * Retrieves the ticket from the repository.
 	 *
 	 * @param repository
 	 * @param ticketId
@@ -313,6 +331,28 @@
 	}
 
 	/**
+	 * Retrieves the journal for the ticket.
+	 *
+	 * @param repository
+	 * @param ticketId
+	 * @return a journal, if it exists, otherwise null
+	 */
+	@Override
+	protected List<Change> getJournalImpl(RepositoryModel repository, long ticketId) {
+		Repository db = repositoryManager.getRepository(repository.name);
+		try {
+			List<Change> changes = getJournal(db, ticketId);
+			if (ArrayUtils.isEmpty(changes)) {
+				log.warn("Empty journal for {}:{}", repository, ticketId);
+				return null;
+			}
+			return changes;
+		} finally {
+			db.close();
+		}
+	}
+
+	/**
 	 * Returns the journal for the specified ticket.
 	 *
 	 * @param db

--
Gitblit v1.9.1