From 3f5b8f5d9203aa7ffb7fbe9cdbaf9dba3da6cae6 Mon Sep 17 00:00:00 2001
From: Hybris95 <hybris_95@hotmail.com>
Date: Thu, 01 May 2014 16:14:15 -0400
Subject: [PATCH] Fixes sort, page building and search functions on "my tickets" page.

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

diff --git a/src/main/java/com/gitblit/tickets/FileTicketService.java b/src/main/java/com/gitblit/tickets/FileTicketService.java
index 8375a2b..7fd9675 100644
--- a/src/main/java/com/gitblit/tickets/FileTicketService.java
+++ b/src/main/java/com/gitblit/tickets/FileTicketService.java
@@ -25,12 +25,11 @@
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicLong;
 
-import javax.inject.Inject;
-
 import org.eclipse.jgit.lib.Repository;
 
 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;
@@ -58,14 +57,15 @@
 
 	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);
@@ -207,71 +207,96 @@
 	@Override
 	public List<TicketModel> getTickets(RepositoryModel repository, TicketFilter filter) {
 		List<TicketModel> list = new ArrayList<TicketModel>();
-
-		Repository db = repositoryManager.getRepository(repository.name);
-		try {
-			// Collect the set of all json files
-			File dir = new File(db.getDirectory(), TICKETS_PATH);
-			List<File> journals = findAll(dir, JOURNAL);
-
-			// Deserialize each ticket and optionally filter out unwanted tickets
-			for (File journal : journals) {
-				String json = null;
-				try {
-					json = new String(FileUtils.readContent(journal), Constants.ENCODING);
-				} catch (Exception e) {
-					log.error(null, e);
-				}
-				if (StringUtils.isEmpty(json)) {
-					// journal was touched but no changes were written
-					continue;
-				}
-				try {
-					// 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);
-					List<Change> changes = TicketSerializer.deserializeJournal(json);
-					if (ArrayUtils.isEmpty(changes)) {
-						log.warn("Empty journal for {}:{}", repository, journal);
+		List<Repository> databases = new ArrayList<Repository>();
+		List<RepositoryModel> models = new ArrayList<RepositoryModel>();
+		
+		if(repository == null)
+		{
+			List<String> allRepo = repositoryManager.getRepositoryList();
+			for(int i = 0; i < allRepo.size(); i++)
+			{
+				databases.add(repositoryManager.getRepository(allRepo.get(i)));
+				models.add(repositoryManager.getRepositoryModel(allRepo.get(i)));
+			}
+		}
+		else
+		{
+			databases.add(repositoryManager.getRepository(repository.name));
+			models.add(repository);
+		}
+		
+		for(int i = 0; i < databases.size(); i++)
+		{
+			Repository db = databases.get(i);
+			RepositoryModel model = models.get(i);
+			try {
+				// Collect the set of all json files
+				File dir = new File(db.getDirectory(), TICKETS_PATH);
+				List<File> journals = findAll(dir, JOURNAL);
+	
+				// Deserialize each ticket and optionally filter out unwanted tickets
+				for (File journal : journals) {
+					String json = null;
+					try {
+						json = new String(FileUtils.readContent(journal), Constants.ENCODING);
+					} catch (Exception e) {
+						log.error(null, e);
+					}
+					if (StringUtils.isEmpty(json)) {
+						// journal was touched but no changes were written
 						continue;
 					}
-					TicketModel ticket = TicketModel.buildTicket(changes);
-					ticket.project = repository.projectPath;
-					ticket.repository = repository.name;
-					ticket.number = ticketId;
-
-					// add the ticket, conditionally, to the list
-					if (filter == null) {
-						list.add(ticket);
-					} else {
-						if (filter.accept(ticket)) {
-							list.add(ticket);
+					try {
+						// 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);
+						List<Change> changes = TicketSerializer.deserializeJournal(json);
+						if (ArrayUtils.isEmpty(changes)) {
+							log.warn("Empty journal for {}:{}", model, journal);
+							continue;
 						}
+						TicketModel ticket = TicketModel.buildTicket(changes);
+						ticket.project = model.projectPath;
+						ticket.repository = model.name;
+						ticket.number = ticketId;
+	
+						// add the ticket, conditionally, to the list
+						if (filter == null) {
+							list.add(ticket);
+						} else {
+							if (filter.accept(ticket)) {
+								list.add(ticket);
+							}
+						}
+					} catch (Exception e) {
+						log.error("failed to deserialize {}/{}\n{}",
+								new Object [] { model, journal, e.getMessage()});
+						log.error(null, e);
 					}
-				} catch (Exception e) {
-					log.error("failed to deserialize {}/{}\n{}",
-							new Object [] { repository, journal, e.getMessage()});
-					log.error(null, e);
 				}
+			} finally {
+				db.close();
 			}
-
-			// sort the tickets by creation
-			Collections.sort(list);
-			return list;
-		} finally {
-			db.close();
 		}
+		
+		// sort the tickets by creation
+		Collections.sort(list);
+		return list;
 	}
 
 	private List<File> findAll(File dir, String filename) {
 		List<File> list = new ArrayList<File>();
-		for (File file : dir.listFiles()) {
+		File [] files = dir.listFiles();
+		if (files == null) {
+			return list;
+		}
+		for (File file : files) {
 			if (file.isDirectory()) {
 				list.addAll(findAll(file, filename));
 			} else if (file.isFile()) {
-				if (file.getName().equals(filename)) {
+				if (file.getName().equalsIgnoreCase(filename)) {
 					list.add(file);
 				}
 			}

--
Gitblit v1.9.1