Skip to content

Commit

Permalink
Merge pull request #216 from TAMULib/dame-sprint-oct-b03566-sync_service
Browse files Browse the repository at this point in the history
[B03566] Sync on startup to populate DB
  • Loading branch information
kaladay committed Nov 8, 2018
2 parents 6025927 + ba37190 commit 4fd2a72
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
6 changes: 6 additions & 0 deletions src/main/java/edu/tamu/app/Initialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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 Down Expand Up @@ -45,6 +46,9 @@ public class Initialization implements CommandLineRunner {
@Autowired
private FileObserverRegistry fileObserverRegistry;

@Autowired
private SyncService syncService;

@Override
public void run(String... args) throws Exception {

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

syncService.syncStartup();

// NOTE: this must be last on startup, otherwise it will invoke all file observers
fileMonitorManager.start();
}
Expand Down
91 changes: 56 additions & 35 deletions src/main/java/edu/tamu/app/service/SyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,61 +63,82 @@ public void sync(Long projectId) throws IOException {
Project project = projectRepo.getOne(projectId);

if (project != null) {
projectFactory.startProjectFileListener(projectId);

String projectPath = ASSETS_PATH + File.separator + "projects" + File.separator + project.getName();
logger.info("Found project: " + project.getName());
processSync(project);
}

if (project.isHeadless()) {
logger.info(project.getName() + " is headless. Headless projects do not support manual sync!");
} else {
List<Path> documents = FileSystemUtility.directoryList(projectPath.toString());
logger.info("Sync Service Finished for ID " + projectId);
}

documents.parallelStream().forEach(documentPath -> {
private void processSync(Project project) {
projectFactory.startProjectFileListener(project.getId());
String projectPath = ASSETS_PATH + File.separator + "projects" + File.separator + project.getName();
if (project.isHeadless()) {
logger.info(project.getName() + " is headless. Headless projects do not support manual sync!");
} else {
List<Path> documents = FileSystemUtility.directoryList(projectPath.toString());

logger.info("Found document: " + documentPath);
documents.parallelStream().forEach(documentPath -> {

String documentName = documentPath.getFileName().toString();
logger.info("Found document: " + documentPath);

Document document = documentRepo.findByProjectNameAndName(project.getName(), documentName);
String documentName = documentPath.getFileName().toString();

try {
if (document == null) {
document = documentFactory.createDocument(documentPath.toFile());
if (!project.getIngestType().equals(IngestType.SAF)) {
Document document = documentRepo.findByProjectNameAndName(project.getName(), documentName);

List<Path> resources = FileSystemUtility.fileList(documentPath.toString());
try {
if (document == null) {
document = documentFactory.createDocument(documentPath.toFile());
if (!project.getIngestType().equals(IngestType.SAF)) {

for (Path resourcePath : resources) {
List<Path> resources = FileSystemUtility.fileList(documentPath.toString());

logger.info("Found resource: " + resourcePath);
for (Path resourcePath : resources) {

File file = resourcePath.toFile();
if (file.isFile() && !file.isHidden()) {
documentFactory.addResource(document, file);
}
logger.info("Found resource: " + resourcePath);

File file = resourcePath.toFile();
if (file.isFile() && !file.isHidden()) {
documentFactory.addResource(document, file);
}

} else {
logger.info("SAF ingest type cannot sync resources at this time. Please use listener.");
}
}

} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} else {
logger.info("SAF ingest type cannot sync resources at this time. Please use listener.");
}
}

});
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}

});
}

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

public void syncStartup() throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Running Initial Sync");
}

List<Path> projects = FileSystemUtility.directoryList(ASSETS_PATH + File.separator + "projects");

for (Path projectPath : projects) {

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

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

Project project = projectFactory.getOrCreateProject(projectName);
if (project != null) {
processSync(project);
}
logger.info("Initial Sync Finished");
}
}
}

0 comments on commit 4fd2a72

Please sign in to comment.