Search in sources :

Example 1 with ModelAlreadyExistsException

use of org.eclipse.vorto.repository.core.ModelAlreadyExistsException in project vorto by eclipse.

the class ModelRepositoryController method createModel.

@ApiOperation(value = "Creates a model in the repository with the given model ID and model type.")
@PostMapping(value = "/{modelId:.+}/{modelType}", produces = "application/json")
public ResponseEntity<ModelInfo> createModel(@ApiParam(value = "modelId", required = true) @PathVariable String modelId, @ApiParam(value = "modelType", required = true) @PathVariable ModelType modelType, @RequestBody(required = false) List<ModelProperty> properties) throws WorkflowException {
    final ModelId modelID = ModelId.fromPrettyFormat(modelId);
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    IUserContext userContext = UserContext.user(authentication, getWorkspaceId(modelId));
    IModelRepository modelRepo = getModelRepository(modelID);
    if (modelRepo.exists(modelID)) {
        throw new ModelAlreadyExistsException();
    } else {
        String modelTemplate = null;
        if (modelType == ModelType.InformationModel && properties != null) {
            modelTemplate = new InfomodelTemplate().createModelTemplate(modelID, properties);
        } else {
            modelTemplate = new ModelTemplate().createModelTemplate(modelID, modelType);
        }
        ModelInfo savedModel = modelRepo.save(modelID, modelTemplate.getBytes(), modelID.getName() + modelType.getExtension(), userContext);
        this.workflowService.start(modelID, userContext);
        return new ResponseEntity<>(savedModel, HttpStatus.CREATED);
    }
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ModelAlreadyExistsException(org.eclipse.vorto.repository.core.ModelAlreadyExistsException) ModelTemplate(org.eclipse.vorto.repository.web.core.templates.ModelTemplate) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ResponseEntity(org.springframework.http.ResponseEntity) Authentication(org.springframework.security.core.Authentication) InfomodelTemplate(org.eclipse.vorto.repository.web.core.templates.InfomodelTemplate) ModelId(org.eclipse.vorto.model.ModelId) PostMapping(org.springframework.web.bind.annotation.PostMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 ModelId (org.eclipse.vorto.model.ModelId)1 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)1 IUserContext (org.eclipse.vorto.repository.core.IUserContext)1 ModelAlreadyExistsException (org.eclipse.vorto.repository.core.ModelAlreadyExistsException)1 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)1 InfomodelTemplate (org.eclipse.vorto.repository.web.core.templates.InfomodelTemplate)1 ModelTemplate (org.eclipse.vorto.repository.web.core.templates.ModelTemplate)1 ResponseEntity (org.springframework.http.ResponseEntity)1 Authentication (org.springframework.security.core.Authentication)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1