use of org.eclipse.sirius.web.services.api.projects.ProjectManifest in project sirius-web by eclipse-sirius.
the class ProjectImportService method importProject.
/**
* Returns {@link UploadProjectSuccessPayload} if the project import has been successful, {@link ErrorPayload}
* otherwise.
*
* <p>
* Unzip the given {@link UploadFile}, then creates a project with the name of the root directory in the zip file,
* then use {@link ProjectImporter} to create documents and representations. If the project has not been imported,
* it disposes the {@link IEditingContextEventProcessor} used to create documents and representations then delete
* the created project in order to keep the server in the same state before the project upload attempt.
* </p>
*
* @param inputId
* The identifier of the input which has triggered the upload
* @param file
* the file to upload
* @return {@link UploadProjectSuccessPayload} whether the project import has been successful, {@link ErrorPayload}
* otherwise
*/
@Override
public IPayload importProject(UUID inputId, UploadFile file) {
IPayload payload = new ErrorPayload(inputId, this.messageService.unexpectedError());
ProjectUnzipper unzipper = new ProjectUnzipper(file.getInputStream(), this.objectMapper);
Optional<UnzippedProject> optionalUnzippedProject = unzipper.unzipProject();
if (optionalUnzippedProject.isEmpty()) {
return new ErrorPayload(inputId, this.messageService.unexpectedError());
}
UnzippedProject unzippedProject = optionalUnzippedProject.get();
ProjectManifest manifest = unzippedProject.getProjectManifest();
String projectName = unzippedProject.getProjectName();
CreateProjectInput createProjectInput = new CreateProjectInput(inputId, projectName, Visibility.PRIVATE);
IPayload createProjectPayload = this.projectService.createProject(createProjectInput);
if (createProjectPayload instanceof CreateProjectSuccessPayload) {
Project project = ((CreateProjectSuccessPayload) createProjectPayload).getProject();
Optional<IEditingContextEventProcessor> optionalEditingContextEventProcessor = this.editingContextEventProcessorRegistry.getOrCreateEditingContextEventProcessor(project.getId().toString());
if (optionalEditingContextEventProcessor.isPresent()) {
IEditingContextEventProcessor editingContextEventProcessor = optionalEditingContextEventProcessor.get();
Map<String, UploadFile> documents = unzippedProject.getDocumentIdToUploadFile();
List<RepresentationDescriptor> representations = unzippedProject.getRepresentationDescriptors();
ProjectImporter projectImporter = new ProjectImporter(project.getId().toString(), editingContextEventProcessor, documents, representations, manifest, this.idMappingRepository);
boolean hasBeenImported = projectImporter.importProject(inputId);
if (!hasBeenImported) {
this.editingContextEventProcessorRegistry.disposeEditingContextEventProcessor(project.getId().toString());
this.projectService.delete(project.getId());
} else {
payload = new UploadProjectSuccessPayload(inputId, project);
}
}
}
return payload;
}
use of org.eclipse.sirius.web.services.api.projects.ProjectManifest in project sirius-web by eclipse-sirius.
the class ProjectUnzipper method getProjectManifest.
/**
* Selects and deserializes the {@link ProjectManifest} from the given map thanks to the given manifest path in zip.
*
* @param zipEntryNameToContent
* The map of zipEntry name to their content
* @param manifestPathInZip
* The path of the manifest file in zip
* @return The {@link ProjectManifest} whether it is present in the given map and it has been deserialized
* successfully, {@link Optional#empty()} otherwise
*/
private Optional<ProjectManifest> getProjectManifest(Map<String, ByteArrayOutputStream> zipEntryNameToContent, String manifestPathInZip) {
Optional<ProjectManifest> optionalProjectManifest = Optional.empty();
// @formatter:off
byte[] manifestBytes = zipEntryNameToContent.entrySet().stream().filter(e -> e.getKey().equals(manifestPathInZip)).map(Entry::getValue).map(ByteArrayOutputStream::toByteArray).findFirst().orElse(new byte[0]);
// @formatter:on
if (manifestBytes.length <= 0) {
return Optional.empty();
}
try {
ProjectManifest projectManifest = this.objectMapper.readValue(manifestBytes, ProjectManifest.class);
optionalProjectManifest = Optional.of(projectManifest);
} catch (IOException exception) {
this.logger.warn(exception.getMessage(), exception);
}
return optionalProjectManifest;
}
use of org.eclipse.sirius.web.services.api.projects.ProjectManifest in project sirius-web by eclipse-sirius.
the class ProjectExportService method addManifest.
/**
* Adds a {@link ZipEntry} for the manifest file in the given {@link ZipOutputStream}.
*
* <p>
* The name of the {@link ZipEntry} is [projectName]/manifest.json, where '/' are used as path separator in the zip.
* </p>
*
* @param projectId
* The id of the project we want to export
* @param projectName
* The name of the project we want to export
* @param id2DocumentName
* The map of document id to document name
* @param representationsManifests
* The map of representation id to {@link RepresentationManifest}
* @param zippedOut
* The {@link ZipOutputStream} used to build the zip
* @return the mapping between document id and document name
* @throws IOException
* if an I/O error occurred
*/
private void addManifest(String projectId, String projectName, Map<String, String> id2DocumentName, Map<String, RepresentationManifest> representationsManifests, ZipOutputStream zippedout) throws IOException {
// @formatter:off
List<String> metamodels = this.editingContextEPackageService.getEPackages(projectId).stream().map(EPackage::getNsURI).collect(Collectors.toList());
ProjectManifest projectManifest = ProjectManifest.newProjectManifest(CURRENT_MANIFEST_VERSION, this.buildProperties.getVersion()).metamodels(metamodels).documentIdsToName(id2DocumentName).representations(representationsManifests).build();
// @formatter:on
byte[] manifestContent = this.objectMapper.writeValueAsBytes(projectManifest);
// $NON-NLS-1$
ZipEntry zipEntry = new ZipEntry(projectName + "/manifest.json");
zippedout.putNextEntry(zipEntry);
zippedout.write(manifestContent);
zippedout.closeEntry();
}
use of org.eclipse.sirius.web.services.api.projects.ProjectManifest in project sirius-web by eclipse-sirius.
the class ProjectUnzipper method unzipProject.
/**
* Unzip the project.
*
* <p>
* The zip must have the following structure:
* <ul>
* <li>Documents should be in [projectName]/documents/*</li>
* <li>Representations should be in [projectName]/representations/*</li>
* <li>The manifest.json should be in [projectName]/*</li>
* </ul>
* </p>
*
* @return <code>true</code> whether the unzip went well, <code>false</code> otherwise.
*/
public Optional<UnzippedProject> unzipProject() {
Optional<UnzippedProject> optionalUnzippedProject = Optional.empty();
Map<String, ByteArrayOutputStream> zipEntryNameToContent = this.readZipFile();
Optional<String> optionalProjectName = this.handleProjectName(zipEntryNameToContent);
if (optionalProjectName.isPresent()) {
String projectName = optionalProjectName.get();
String documentsFolderInZip = projectName + ZIP_FOLDER_SEPARATOR + DOCUMENTS_FOLDER + ZIP_FOLDER_SEPARATOR;
String representationsFolderInZip = projectName + ZIP_FOLDER_SEPARATOR + REPRESENTATIONS_FOLDER + ZIP_FOLDER_SEPARATOR;
String manifestPathInZip = projectName + ZIP_FOLDER_SEPARATOR + MANIFEST_JSON_FILE;
Optional<ProjectManifest> optionalManifest = this.getProjectManifest(zipEntryNameToContent, manifestPathInZip);
Map<String, ByteArrayOutputStream> documentIdToDocumentContent = this.selectAndTransformIntoDocumentIdToDocumentContent(zipEntryNameToContent, documentsFolderInZip);
List<ByteArrayOutputStream> representationDescritorsContent = this.selectAndTransformIntoRepresentationDescriptorsContent(zipEntryNameToContent, representationsFolderInZip);
if (!optionalManifest.isEmpty() && this.validateDocuments(documentIdToDocumentContent, optionalManifest.get())) {
ProjectManifest manifest = optionalManifest.get();
Map<String, UploadFile> documentIdToUploadFile = this.getUploadFiles(documentIdToDocumentContent, manifest);
List<RepresentationDescriptor> representationDescriptors;
try {
representationDescriptors = this.getRepresentationDescriptors(representationDescritorsContent);
// @formatter:off
UnzippedProject unzippedProject = UnzippedProject.newUnzippedProject(projectName).projectManifest(manifest).documentIdToUploadFile(documentIdToUploadFile).representationDescriptors(representationDescriptors).build();
// @formatter:on
optionalUnzippedProject = Optional.of(unzippedProject);
} catch (IOException exception) {
this.logger.warn(exception.getMessage(), exception);
}
}
}
return optionalUnzippedProject;
}
Aggregations