use of org.springframework.beans.factory.annotation.Value in project vorto by eclipse.
the class ModelRepositoryController method getModelContentForTargetPlatform.
@ApiOperation(value = "Returns the model content including target platform specific attributes")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/content/{namespace}/{name}/{version:.+}/mapping/{targetplatformKey}", method = RequestMethod.GET)
public AbstractModel getModelContentForTargetPlatform(@ApiParam(value = "The namespace of vorto model, e.g. com.mycompany", required = true) @PathVariable final String namespace, @ApiParam(value = "The name of vorto model, e.g. NewInfomodel", required = true) @PathVariable final String name, @ApiParam(value = "The version of vorto model, e.g. 1.0.0", required = true) @PathVariable final String version, @ApiParam(value = "The key of the targetplatform, e.g. lwm2m", required = true) @PathVariable final String targetplatformKey) {
List<ModelInfo> mappingResource = modelRepository.getMappingModelsForTargetPlatform(new ModelId(name, namespace, version), targetplatformKey);
if (!mappingResource.isEmpty()) {
byte[] mappingContentZip = createZipWithAllDependencies(mappingResource.get(0).getId(), ContentType.DSL);
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(mappingContentZip))).read();
MappingModel mappingModel = (MappingModel) workspace.get().stream().filter(p -> p instanceof MappingModel).findFirst().get();
byte[] modelContent = createZipWithAllDependencies(new ModelId(name, namespace, version), ContentType.DSL);
workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelContent))).read();
return ModelDtoFactory.createResource(workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get(), Optional.of(mappingModel));
} else {
return getModelContent(namespace, name, version);
}
}
use of org.springframework.beans.factory.annotation.Value in project vorto by eclipse.
the class ModelRepositoryController method getModelContent.
@ApiOperation(value = "Returns the model content")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/content/{namespace}/{name}/{version:.+}", method = RequestMethod.GET)
public AbstractModel getModelContent(@ApiParam(value = "The namespace of vorto model, e.g. com.mycompany", required = true) @PathVariable final String namespace, @ApiParam(value = "The name of vorto model, e.g. NewInfomodel", required = true) @PathVariable final String name, @ApiParam(value = "The version of vorto model, e.g. 1.0.0", required = true) @PathVariable final String version) {
byte[] modelContent = createZipWithAllDependencies(new ModelId(name, namespace, version), ContentType.DSL);
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelContent))).read();
return ModelDtoFactory.createResource(workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get(), Optional.empty());
}
use of org.springframework.beans.factory.annotation.Value in project spring-data-commons by spring-projects.
the class SpelEvaluatingMethodInterceptor method potentiallyCreateExpressionsForMethodsOnTargetInterface.
/**
* Eagerly parses {@link Expression} defined on {@link Value} annotations. Returns a map with
* {@code method.hashCode()} as key and the parsed {@link Expression} or an {@link Collections#emptyMap()} if no
* {@code Expressions} were found.
*
* @param parser must not be {@literal null}.
* @param targetInterface must not be {@literal null}.
* @return
*/
private static Map<Integer, Expression> potentiallyCreateExpressionsForMethodsOnTargetInterface(SpelExpressionParser parser, Class<?> targetInterface) {
Map<Integer, Expression> expressions = new HashMap<>();
for (Method method : targetInterface.getMethods()) {
if (!method.isAnnotationPresent(Value.class)) {
continue;
}
Value value = method.getAnnotation(Value.class);
if (!StringUtils.hasText(value.value())) {
throw new IllegalStateException(String.format("@Value annotation on %s contains empty expression!", method));
}
expressions.put(method.hashCode(), parser.parseExpression(value.value(), PARSER_CONTEXT));
}
return Collections.unmodifiableMap(expressions);
}
use of org.springframework.beans.factory.annotation.Value in project molgenis by molgenis.
the class RestControllerV2 method updateAttribute.
/**
* @param entityTypeId The name of the entity to update
* @param attributeName The name of the attribute to update
* @param request EntityCollectionBatchRequestV2
* @param response HttpServletResponse
*/
@PutMapping("/{entityTypeId}/{attributeName}")
@ResponseStatus(OK)
public synchronized void updateAttribute(@PathVariable("entityTypeId") String entityTypeId, @PathVariable("attributeName") String attributeName, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) throws Exception {
final EntityType meta = dataService.getEntityType(entityTypeId);
if (meta == null) {
throw createUnknownEntityException(entityTypeId);
}
try {
Attribute attr = meta.getAttribute(attributeName);
if (attr == null) {
throw new UnknownAttributeException(meta, attributeName);
}
if (attr.isReadOnly()) {
throw createMolgenisDataAccessExceptionReadOnlyAttribute(entityTypeId, attributeName);
}
final List<Entity> entities = request.getEntities().stream().filter(e -> e.size() == 2).map(e -> this.restService.toEntity(meta, e)).collect(toList());
if (entities.size() != request.getEntities().size()) {
throw createMolgenisDataExceptionIdentifierAndValue();
}
final List<Entity> updatedEntities = new ArrayList<>();
int count = 0;
for (Entity entity : entities) {
Object id = checkForEntityId(entity, count);
Entity originalEntity = dataService.findOneById(entityTypeId, id);
if (originalEntity == null) {
throw createUnknownEntityExceptionNotValidId(id);
}
Object value = this.restService.toEntityValue(attr, entity.get(attributeName), id);
originalEntity.set(attributeName, value);
updatedEntities.add(originalEntity);
count++;
}
// update all entities
this.dataService.update(entityTypeId, updatedEntities.stream());
response.setStatus(HttpServletResponse.SC_OK);
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
throw e;
}
}
use of org.springframework.beans.factory.annotation.Value in project molgenis by molgenis.
the class RestControllerV2 method createEntities.
/**
* Try to create multiple entities in one transaction. If one fails all fails.
*
* @param entityTypeId name of the entity where the entities are going to be added.
* @param request EntityCollectionCreateRequestV2
* @param response HttpServletResponse
* @return EntityCollectionCreateResponseBodyV2
*/
@Transactional
@PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE)
public EntityCollectionBatchCreateResponseBodyV2 createEntities(@PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) throws Exception {
final EntityType meta = dataService.getEntityType(entityTypeId);
if (meta == null) {
throw createUnknownEntityException(entityTypeId);
}
try {
final List<Entity> entities = request.getEntities().stream().map(e -> this.restService.toEntity(meta, e)).collect(toList());
final EntityCollectionBatchCreateResponseBodyV2 responseBody = new EntityCollectionBatchCreateResponseBodyV2();
final List<String> ids = new ArrayList<>();
// Add all entities
if (ATTRIBUTE_META_DATA.equals(entityTypeId)) {
entities.stream().map(attribute -> (Attribute) attribute).forEach(attribute -> dataService.getMeta().addAttribute(attribute));
} else {
this.dataService.add(entityTypeId, entities.stream());
}
entities.forEach(entity -> {
restService.updateMappedByEntities(entity);
String id = entity.getIdValue().toString();
ids.add(id);
responseBody.getResources().add(new AutoValue_ResourcesResponseV2(Href.concatEntityHref(RestControllerV2.BASE_URI, entityTypeId, id)));
});
responseBody.setLocation(Href.concatEntityCollectionHref(RestControllerV2.BASE_URI, entityTypeId, meta.getIdAttribute().getName(), ids));
response.setStatus(HttpServletResponse.SC_CREATED);
return responseBody;
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
throw e;
}
}
Aggregations