From a502d96a860456ec5e8c96761db70f7cabb74751 Mon Sep 17 00:00:00 2001
From: Paul Martin <paul@paulsputer.com>
Date: Sat, 30 Apr 2016 04:19:14 -0400
Subject: [PATCH] Merge pull request #1073 from gitblit/1062-DocEditorUpdates

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

diff --git a/src/main/java/com/gitblit/tickets/FileTicketService.java b/src/main/java/com/gitblit/tickets/FileTicketService.java
index 2247a66..1e82f0d 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;
 
@@ -29,6 +31,7 @@
 
 import com.gitblit.Constants;
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
@@ -39,6 +42,8 @@
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.FileUtils;
 import com.gitblit.utils.StringUtils;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
 
 /**
  * Implementation of a ticket service based on a directory within the repository.
@@ -48,6 +53,7 @@
  * @author James Moger
  *
  */
+@Singleton
 public class FileTicketService extends ITicketService {
 
 	private static final String JOURNAL = "journal.json";
@@ -56,13 +62,16 @@
 
 	private final Map<String, AtomicLong> lastAssignedId;
 
+	@Inject
 	public FileTicketService(
 			IRuntimeManager runtimeManager,
+			IPluginManager pluginManager,
 			INotificationManager notificationManager,
 			IUserManager userManager,
 			IRepositoryManager repositoryManager) {
 
 		super(runtimeManager,
+				pluginManager,
 				notificationManager,
 				userManager,
 				repositoryManager);
@@ -72,6 +81,7 @@
 
 	@Override
 	public FileTicketService start() {
+		log.info("{} started", getClass().getSimpleName());
 		return this;
 	}
 
@@ -143,6 +153,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.
 	 *
@@ -159,18 +194,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);
 					}
 				}
 			}
@@ -281,8 +308,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
@@ -304,6 +330,28 @@
 				ticket.number = ticketId;
 			}
 			return ticket;
+		} finally {
+			db.close();
+		}
+	}
+
+	/**
+	 * 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();
 		}
@@ -445,6 +493,10 @@
 	@Override
 	protected boolean deleteAllImpl(RepositoryModel repository) {
 		Repository db = repositoryManager.getRepository(repository.name);
+		if (db == null) {
+			// the tickets no longer exist because the db no longer exists
+			return true;
+		}
 		try {
 			File dir = new File(db.getDirectory(), TICKETS_PATH);
 			return FileUtils.delete(dir);

--
Gitblit v1.9.1