Skip to content

Commit

Permalink
Merge pull request #212 from TAMULib/dame-sprint-oct-b03566-sync_service
Browse files Browse the repository at this point in the history
[B-03566] sync single project based on project name
  • Loading branch information
jsavell committed Oct 30, 2018
2 parents 685bcb1 + aad840f commit b5c05b7
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 76 deletions.
6 changes: 0 additions & 6 deletions src/main/java/edu/tamu/app/Initialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import edu.tamu.app.observer.FileObserverRegistry;
import edu.tamu.app.observer.MapFileListener;
import edu.tamu.app.observer.ProjectListener;
import edu.tamu.app.service.SyncService;
import edu.tamu.app.utilities.FileSystemUtility;

@Component
Expand All @@ -37,9 +36,6 @@ public class Initialization implements CommandLineRunner {
@Value("${app.assets.folders}")
private String[] assetsFolders;

@Autowired
private SyncService syncService;

@Autowired
private ResourceLoader resourceLoader;

Expand All @@ -65,8 +61,6 @@ public void run(String... args) throws Exception {
fileObserverRegistry.register(new ProjectListener(ASSETS_PATH, "projects"));
fileObserverRegistry.register(new MapFileListener(ASSETS_PATH, "maps"));

syncService.sync();

// NOTE: this must be last on startup, otherwise it will invoke all file observers
fileMonitorManager.start();
}
Expand Down
30 changes: 1 addition & 29 deletions src/main/java/edu/tamu/app/controller/AdminController.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,19 @@
package edu.tamu.app.controller;

import static edu.tamu.weaver.response.ApiStatus.SUCCESS;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.tamu.app.service.SyncService;
import edu.tamu.weaver.response.ApiResponse;

/**
* Admin Controller.
*
*
* @author
*
*/
@RestController
@RequestMapping("/admin")
public class AdminController {

private static final Logger logger = Logger.getLogger(AdminController.class);

@Autowired
private SyncService syncService;

// TODO: handle exception gracefully
/**
* Synchronizes the project directory with the database.
*
* @return ApiResponse
* @throws IOException
*
*/
@RequestMapping("/sync")
@PreAuthorize("hasRole('ADMIN')")
public ApiResponse syncDocuments() throws IOException {
logger.info("Syncronizing projects with database.");
syncService.sync();
return new ApiResponse(SUCCESS, "Syncronized projects with database.");
}

}
30 changes: 30 additions & 0 deletions src/main/java/edu/tamu/app/controller/ProjectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import edu.tamu.app.model.repo.FieldProfileRepo;
import edu.tamu.app.model.repo.MetadataFieldLabelRepo;
import edu.tamu.app.model.repo.ProjectRepo;
import edu.tamu.app.service.SyncService;
import edu.tamu.app.service.registry.MagpieServiceRegistry;
import edu.tamu.app.service.repository.Destination;
import edu.tamu.weaver.response.ApiResponse;
Expand All @@ -57,6 +58,9 @@ public class ProjectController {
@Autowired
private ObjectMapper objectMapper;

@Autowired
private SyncService syncService;

/**
* Endpoint to return list of projects.
*
Expand Down Expand Up @@ -210,4 +214,30 @@ public ApiResponse publishBatch(@PathVariable Long projectId, @PathVariable Long
return new ApiResponse(ERROR, "There was an error with the batch publish");
}

// TODO: handle exception gracefully
/**
* Synchronizes the project directory with the database for a single project.
*
* @param Long id
* The ID of the specific project to synchronize.
*
* @return ApiResponse
* @throws IOException
*
*/
@RequestMapping("/sync/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ApiResponse syncDocuments(@PathVariable Long id) throws IOException {
ApiResponse response = null;
logger.info("Syncronizing projects with database.");
if (id == null) {
response = new ApiResponse(ERROR, "No valid project ID specified.");
}
else {
syncService.sync(id);
response = new ApiResponse(SUCCESS, "Syncronized project ID " + id + " with database.");
}
return response;
}

}
41 changes: 33 additions & 8 deletions src/main/java/edu/tamu/app/service/ProjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private void registerListeners(Project project) {
}

// @formatter:off
// TODO: generalize against ProjectService commonalities
// TODO: generalize against ProjectService commonalities
/*
public Map<String, List<String>> getProjectServiceTypes(ProjectService projectService) {
Map<String, List<String>> scaffolds = new HashMap<String, List<String>>();
Expand Down Expand Up @@ -415,16 +415,41 @@ public List<MetadataFieldGroup> getProjectFields(String projectName) {
}

public void startProjectFileListeners() {
String projectsPath = ASSETS_PATH + File.separator + "projects";
projectRepo.findAll().forEach(project -> {
if (project.isHeadless()) {
logger.info("Registering headless document listener: " + projectsPath + File.separator + project.getName());
fileObserverRegistry.register(new HeadlessDocumentListener(projectsPath, project.getName()));
} else {
logger.info("Registering standard document listener: " + projectsPath + File.separator + project.getName());
fileObserverRegistry.register(new StandardDocumentListener(projectsPath, project.getName()));
if (project != null) {
this.startProjectFileListener(project);
}
});
}

/**
* Initiate project file listener for single project.
*
* @param id
* ID of the project to initiate listener for.
*/
public void startProjectFileListener(Long id) {
Project project = projectRepo.findOne(id);
if (project != null) {
this.startProjectFileListener(project);
}
}

/**
* Initiate project file listener for single project.
*
* @param project
* The project to initiate listener for.
*/
private void startProjectFileListener(Project project) {
String projectsPath = ASSETS_PATH + File.separator + "projects";
if (project.isHeadless()) {
logger.info("Registering headless document listener: " + projectsPath + File.separator + project.getName());
fileObserverRegistry.register(new HeadlessDocumentListener(projectsPath, project.getName()));
} else {
logger.info("Registering standard document listener: " + projectsPath + File.separator + project.getName());
fileObserverRegistry.register(new StandardDocumentListener(projectsPath, project.getName()));
}
}

}
37 changes: 23 additions & 14 deletions src/main/java/edu/tamu/app/service/SyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import edu.tamu.app.model.IngestType;
import edu.tamu.app.model.Project;
import edu.tamu.app.model.repo.DocumentRepo;
import edu.tamu.app.model.repo.ProjectRepo;
import edu.tamu.app.utilities.FileSystemUtility;

/**
* Sync Service. Synchronizes project database with projects folders.
*
*
* @author
*/
@Service
Expand All @@ -33,6 +34,9 @@ public class SyncService {
@Autowired
private DocumentRepo documentRepo;

@Autowired
private ProjectRepo projectRepo;

@Autowired
private ProjectFactory projectFactory;

Expand All @@ -43,22 +47,26 @@ public SyncService() {

}

public void sync() throws IOException {
/**
* Synchronizes the project directory with the database for a single project.
*
* @param Long projectId
* The ID of the specific project to synchronize.
*
* @throws IOException
*/
public void sync(Long projectId) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Running Sync Service");
logger.debug("Running Sync Service for ID " + projectId);
}

projectFactory.startProjectFileListeners();

List<Path> projects = FileSystemUtility.directoryList(ASSETS_PATH + File.separator + "projects");
Project project = projectRepo.getOne(projectId);

for (Path projectPath : projects) {
if (project != null) {
projectFactory.startProjectFileListener(projectId);

logger.info("Found project: " + projectPath);

String projectName = projectPath.getFileName().toString();

Project project = projectFactory.getOrCreateProject(projectName);
String projectPath = ASSETS_PATH + File.separator + "projects" + File.separator + project.getName();
logger.info("Found project: " + project.getName());

if (project.isHeadless()) {
logger.info(project.getName() + " is headless. Headless projects do not support manual sync!");
Expand All @@ -71,7 +79,7 @@ public void sync() throws IOException {

String documentName = documentPath.getFileName().toString();

Document document = documentRepo.findByProjectNameAndName(projectName, documentName);
Document document = documentRepo.findByProjectNameAndName(project.getName(), documentName);

try {
if (document == null) {
Expand Down Expand Up @@ -108,7 +116,8 @@ public void sync() throws IOException {
}

}
logger.info("Sync Service Finished");

logger.info("Sync Service Finished for ID " + projectId);
}

}
19 changes: 0 additions & 19 deletions src/test/java/edu/tamu/app/controller/AdminControllerTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -62,4 +63,10 @@ private JsonNode getFieldProfileData() {
return data;
}

@Test
public void testSyncDocuments() throws IOException {
response = projectController.syncDocuments(TEST_PROJECT1.getId());
assertEquals(" The response was not successful ", ApiStatus.SUCCESS, response.getMeta().getStatus());
}

}

0 comments on commit b5c05b7

Please sign in to comment.