diff --git a/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/project/CreateNewProjectPresenter.java b/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/project/CreateNewProjectPresenter.java index 0411b96fb..1060c13c3 100644 --- a/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/project/CreateNewProjectPresenter.java +++ b/webprotege-gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/project/CreateNewProjectPresenter.java @@ -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.*; @@ -103,7 +100,7 @@ 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); } @@ -111,18 +108,79 @@ private void submitCreateProjectRequest(ProjectCreatedHandler 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( + 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, @@ -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) { @@ -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); } @@ -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( - 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( + 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()); + } + } +} \ No newline at end of file diff --git a/webprotege-gwt-ui-shared/src/main/java/edu/stanford/bmir/protege/web/shared/icd/CreateNewProjectFromProjectBackupAction.java b/webprotege-gwt-ui-shared/src/main/java/edu/stanford/bmir/protege/web/shared/icd/CreateNewProjectFromProjectBackupAction.java new file mode 100644 index 000000000..e00d1e218 --- /dev/null +++ b/webprotege-gwt-ui-shared/src/main/java/edu/stanford/bmir/protege/web/shared/icd/CreateNewProjectFromProjectBackupAction.java @@ -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 { + 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(); + } +} diff --git a/webprotege-gwt-ui-shared/src/main/java/edu/stanford/bmir/protege/web/shared/icd/CreateNewProjectFromProjectBackupResult.java b/webprotege-gwt-ui-shared/src/main/java/edu/stanford/bmir/protege/web/shared/icd/CreateNewProjectFromProjectBackupResult.java new file mode 100644 index 000000000..4da51f88d --- /dev/null +++ b/webprotege-gwt-ui-shared/src/main/java/edu/stanford/bmir/protege/web/shared/icd/CreateNewProjectFromProjectBackupResult.java @@ -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(); + } +}