Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[B03566] Sync on startup to populate DB #216

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
}
}
}