Skip to content

Commit

Permalink
Added CreateNewProjectFromProjectBackupAction
Browse files Browse the repository at this point in the history
  • Loading branch information
soimugeo committed Dec 6, 2024
1 parent 7ff4326 commit de4d40b
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

import com.google.web.bindery.event.shared.EventBus;
import edu.stanford.bmir.protege.web.client.app.ApplicationEnvironmentManager;
import edu.stanford.bmir.protege.web.client.dispatch.DispatchErrorMessageDisplay;
import edu.stanford.bmir.protege.web.client.dispatch.DispatchServiceCallbackWithProgressDisplay;
import edu.stanford.bmir.protege.web.client.dispatch.DispatchServiceManager;
import edu.stanford.bmir.protege.web.client.dispatch.ProgressDisplay;
import edu.stanford.bmir.protege.web.client.dispatch.*;
import edu.stanford.bmir.protege.web.client.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.projectmanager.ProjectCreatedEvent;
import edu.stanford.bmir.protege.web.client.upload.FileUploader;
import edu.stanford.bmir.protege.web.client.user.LoggedInUserManager;
import edu.stanford.bmir.protege.web.client.uuid.UuidV4;
import edu.stanford.bmir.protege.web.shared.csv.DocumentId;
import edu.stanford.bmir.protege.web.shared.dispatch.actions.GetUserInfoAction;
import edu.stanford.bmir.protege.web.shared.dispatch.actions.GetUserInfoResult;
import edu.stanford.bmir.protege.web.shared.dispatch.actions.*;
import edu.stanford.bmir.protege.web.shared.icd.*;
import edu.stanford.bmir.protege.web.shared.permissions.PermissionDeniedException;
import edu.stanford.bmir.protege.web.shared.project.*;

Expand Down Expand Up @@ -103,26 +100,87 @@ private void submitCreateProjectRequest(ProjectCreatedHandler handler) {
if (fileSourceType.equals("owl") && view.isFileUploadSpecified()) {
uploadSourcesAndCreateProject(handler);
} else if (fileSourceType.equals("backup") && view.isFileUploadSpecified()) {
//do stuff here
uploadProjectBackupAndCreateProjectFromBackup(handler);
} else {
createEmptyProject(handler);
}
}

private void createEmptyProject(ProjectCreatedHandler projectCreatedHandler) {
NewProjectSettings newProjectSettings = NewProjectSettings.get(loggedInUserManager.getLoggedInUserId(),
view.getProjectName(),
view.getProjectLanguage(),
view.getProjectDescription());
view.getProjectName(),
view.getProjectLanguage(),
view.getProjectDescription());
submitCreateNewProjectRequest(newProjectSettings, projectCreatedHandler);
}

private void uploadProjectBackupAndCreateProjectFromBackup(@Nonnull ProjectCreatedHandler projectCreatedHandler) {
checkNotNull(projectCreatedHandler);
progressDisplay.displayProgress("Uploading file", "Uploading file. Please wait.");
dispatchServiceManager.execute(new GetUserInfoAction(),
info -> createProjectFromProjectBackupForUser(info, projectCreatedHandler));
}

private void createProjectFromProjectBackupForUser(GetUserInfoResult userInfo,
ProjectCreatedHandler projectCreatedHandler) {
String fileUploadElementId = view.getFileUploadElementId();
FileUploader fileUploader = new FileUploader(applicationEnvironmentManager.getAppEnvVariables().getFileUploadUrl());
fileUploader.uploadFile(fileUploadElementId,
userInfo.getToken(),
fileSubmissionId -> handleProjectBackupFilesSubmissionSuccess(projectCreatedHandler, fileSubmissionId),
this::handleFileSubmissionError);
}

private void handleProjectBackupFilesSubmissionSuccess(ProjectCreatedHandler projectCreatedHandler, String fileSubmissionId) {
progressDisplay.hideProgress();
handleCreateNewProjectFromProjectBackup(projectCreatedHandler,
new DocumentId(fileSubmissionId));
}

private void handleCreateNewProjectFromProjectBackup(ProjectCreatedHandler projectCreatedHandler, DocumentId documentId) {
NewProjectSettings newProjectSettings = NewProjectSettings.get(loggedInUserManager.getLoggedInUserId(),
view.getProjectName(),
view.getProjectLanguage(),
view.getProjectDescription(),
documentId);
submitCreateNewProjectFromProjectBackupRequest(newProjectSettings, projectCreatedHandler);
}

private void submitCreateNewProjectFromProjectBackupRequest(NewProjectSettings newProjectSettings, ProjectCreatedHandler projectCreatedHandler) {
String uuid = UuidV4.uuidv4();
ProjectId newProjectId = ProjectId.get(uuid);
dispatchServiceManager.execute(new CreateNewProjectFromProjectBackupAction(newProjectId, newProjectSettings),
new DispatchServiceCallbackWithProgressDisplay<CreateNewProjectFromProjectBackupResult>(
errorDisplay,
progressDisplay) {
@Override
public String getProgressDisplayTitle() {
return "Creating project";
}

@Override
public String getProgressDisplayMessage() {
return "Please wait.";
}

@Override
public void handleSuccess(CreateNewProjectFromProjectBackupResult result) {
handleProjectCreationSuccess(projectCreatedHandler, result.getProjectDetails());
}

@Override
public void handleExecutionException(Throwable cause) {
handleProjectCreationException(cause);
}
});
}


private void uploadSourcesAndCreateProject(@Nonnull ProjectCreatedHandler projectCreatedHandler) {
checkNotNull(projectCreatedHandler);
progressDisplay.displayProgress("Uploading file", "Uploading file. Please wait.");
dispatchServiceManager.execute(new GetUserInfoAction(),
info -> createProjectForUser(info, projectCreatedHandler));
info -> createProjectForUser(info, projectCreatedHandler));
}

private void createProjectForUser(GetUserInfoResult userInfo,
Expand All @@ -131,9 +189,9 @@ private void createProjectForUser(GetUserInfoResult userInfo,
String fileUploadElementId = view.getFileUploadElementId();
FileUploader fileUploader = new FileUploader(applicationEnvironmentManager.getAppEnvVariables().getFileUploadUrl());
fileUploader.uploadFile(fileUploadElementId,
userInfo.getToken(),
fileSubmissionId -> handleFileSubmissionSuccess(projectCreatedHandler, fileSubmissionId),
this::handleFileSubmissionError);
userInfo.getToken(),
fileSubmissionId -> handleFileSubmissionSuccess(projectCreatedHandler, fileSubmissionId),
this::handleFileSubmissionError);
}

private void handleFileSubmissionError(int errorCode) {
Expand All @@ -145,15 +203,15 @@ private void handleFileSubmissionSuccess(CreateNewProjectPresenter.ProjectCreate
String fileSubmissionId) {
progressDisplay.hideProgress();
handleCreateNewProjectFromUploadedSources(projectCreatedHandler,
new DocumentId(fileSubmissionId));
new DocumentId(fileSubmissionId));
}

private void handleCreateNewProjectFromUploadedSources(ProjectCreatedHandler projectCreatedHandler, DocumentId documentId) {
NewProjectSettings newProjectSettings = NewProjectSettings.get(loggedInUserManager.getLoggedInUserId(),
view.getProjectName(),
view.getProjectLanguage(),
view.getProjectDescription(),
documentId);
view.getProjectName(),
view.getProjectLanguage(),
view.getProjectDescription(),
documentId);
submitCreateNewProjectRequest(newProjectSettings, projectCreatedHandler);
}

Expand All @@ -162,46 +220,51 @@ private void submitCreateNewProjectRequest(@Nonnull NewProjectSettings newProjec
String uuid = UuidV4.uuidv4();
ProjectId newProjectId = ProjectId.get(uuid);
dispatchServiceManager.execute(new CreateNewProjectAction(newProjectId, newProjectSettings),
new DispatchServiceCallbackWithProgressDisplay<CreateNewProjectResult>(
errorDisplay,
progressDisplay) {
@Override
public String getProgressDisplayTitle() {
return "Creating project";
}

@Override
public String getProgressDisplayMessage() {
return "Please wait.";
}

@Override
public void handleSuccess(CreateNewProjectResult result) {
projectCreatedHandler.handleProjectCreated();
eventBus.fireEvent(new ProjectCreatedEvent(result.getProjectDetails()));
}

@Override
public void handleExecutionException(Throwable cause) {
if (cause instanceof PermissionDeniedException) {
messageBox.showMessage(
"You do not have permission to create new projects");
}
else if (cause instanceof ProjectAlreadyRegisteredException) {
ProjectAlreadyRegisteredException ex = (ProjectAlreadyRegisteredException) cause;
String projectName = ex.getProjectId().getId();
messageBox.showMessage("The project name " + projectName + " is already registered. Please try a different name.");
}
else if (cause instanceof ProjectDocumentExistsException) {
ProjectDocumentExistsException ex = (ProjectDocumentExistsException) cause;
String projectName = ex.getProjectId().getId();
messageBox.showMessage(
"There is already a non-empty project on the server with the id " + projectName + ". This project has NOT been overwritten. Please contact the administrator to resolve this issue.");
}
else {
messageBox.showMessage(cause.getMessage());
}
}
});
}
}
new DispatchServiceCallbackWithProgressDisplay<CreateNewProjectResult>(
errorDisplay,
progressDisplay) {
@Override
public String getProgressDisplayTitle() {
return "Creating project";
}

@Override
public String getProgressDisplayMessage() {
return "Please wait.";
}

@Override
public void handleSuccess(CreateNewProjectResult result) {
handleProjectCreationSuccess(projectCreatedHandler, result.getProjectDetails());
}

@Override
public void handleExecutionException(Throwable cause) {
handleProjectCreationException(cause);
}
});
}

private void handleProjectCreationSuccess(ProjectCreatedHandler projectCreatedHandler, ProjectDetails projectDetails) {
projectCreatedHandler.handleProjectCreated();
eventBus.fireEvent(new ProjectCreatedEvent(projectDetails));
}

private void handleProjectCreationException(Throwable cause) {
if (cause instanceof PermissionDeniedException) {
messageBox.showMessage(
"You do not have permission to create new projects");
} else if (cause instanceof ProjectAlreadyRegisteredException) {
ProjectAlreadyRegisteredException ex = (ProjectAlreadyRegisteredException) cause;
String projectName = ex.getProjectId().getId();
messageBox.showMessage("The project name " + projectName + " is already registered. Please try a different name.");
} else if (cause instanceof ProjectDocumentExistsException) {
ProjectDocumentExistsException ex = (ProjectDocumentExistsException) cause;
String projectName = ex.getProjectId().getId();
messageBox.showMessage(
"There is already a non-empty project on the server with the id " + projectName + ". This project has NOT been overwritten. Please contact the administrator to resolve this issue.");
} else {
messageBox.showMessage(cause.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package edu.stanford.bmir.protege.web.shared.icd;

import com.fasterxml.jackson.annotation.*;
import com.google.common.base.Objects;
import edu.stanford.bmir.protege.web.shared.dispatch.Action;
import edu.stanford.bmir.protege.web.shared.project.*;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;

@JsonTypeName("webprotege.projects.CreateNewProjectFromProjectBackup")
public class CreateNewProjectFromProjectBackupAction implements Action<CreateNewProjectFromProjectBackupResult> {
private ProjectId newProjectId;

private NewProjectSettings newProjectSettings;

/**
* For serialization purposes only
*/
private CreateNewProjectFromProjectBackupAction() {
}

@JsonCreator
public CreateNewProjectFromProjectBackupAction(@JsonProperty("projectId") ProjectId newProjectId,
@JsonProperty("newProjectSettings") NewProjectSettings newProjectSettings) {
this.newProjectId = newProjectId;
this.newProjectSettings = checkNotNull(newProjectSettings);
}

public ProjectId getNewProjectId() {
return newProjectId;
}

public NewProjectSettings getNewProjectSettings() {
return newProjectSettings;
}

@Override
public int hashCode() {
return Objects.hashCode(newProjectId, newProjectSettings);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof CreateNewProjectFromProjectBackupAction)) {
return false;
}
CreateNewProjectFromProjectBackupAction other = (CreateNewProjectFromProjectBackupAction) obj;
return this.newProjectId.equals(other.newProjectId) && this.newProjectSettings.equals(other.newProjectSettings);
}

@Override
public String toString() {
return toStringHelper("CreateNewProjectFromProjectBackupAction")
.addValue(newProjectId)
.addValue(newProjectSettings)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package edu.stanford.bmir.protege.web.shared.icd;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.base.Objects;
import edu.stanford.bmir.protege.web.shared.dispatch.Result;
import edu.stanford.bmir.protege.web.shared.project.*;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;

@JsonTypeName("webprotege.projects.CreateNewProjectFromProjectBackup")
public class CreateNewProjectFromProjectBackupResult implements Result {

private ProjectDetails projectDetails;

/**
* For serialization purposes only
*/
private CreateNewProjectFromProjectBackupResult() {
}

public CreateNewProjectFromProjectBackupResult(ProjectDetails projectDetails) {
this.projectDetails = checkNotNull(projectDetails);
}

public ProjectDetails getProjectDetails() {
return projectDetails;
}

@Override
public int hashCode() {
return Objects.hashCode(projectDetails);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof CreateNewProjectFromProjectBackupResult)) {
return false;
}
CreateNewProjectFromProjectBackupResult other = (CreateNewProjectFromProjectBackupResult) obj;
return this.projectDetails.equals(other.projectDetails);
}


@Override
public String toString() {
return toStringHelper("CreateNewProjectFromProjectBackupResult")
.addValue(projectDetails)
.toString();
}
}

0 comments on commit de4d40b

Please sign in to comment.