use of org.molgenis.js.magma.WithJsMagmaScriptContext 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
*/
// getEntities is guaranteed to be not empty
@SuppressWarnings("java:S2259")
@Transactional
@WithJsMagmaScriptContext
@PutMapping("/{entityTypeId}/{attributeName}")
@ResponseStatus(OK)
public synchronized void updateAttribute(@PathVariable("entityTypeId") String entityTypeId, @PathVariable("attributeName") String attributeName, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) {
final EntityType meta = dataService.getEntityType(entityTypeId);
try {
Attribute attr = meta.getAttribute(attributeName);
if (attr == null) {
throw new UnknownAttributeException(meta, attributeName);
}
if (attr.isReadOnly()) {
throw createMolgenisDataAccessExceptionReadOnlyAttribute(entityTypeId, attributeName);
}
// transform request entities to real Entity objects
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();
}
// update original entities
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 new UnknownEntityException(meta, id);
}
originalEntity.set(attributeName, entity.get(attributeName));
updatedEntities.add(originalEntity);
count++;
}
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.molgenis.js.magma.WithJsMagmaScriptContext in project molgenis by molgenis.
the class CopyServiceImpl method copy.
@Override
@SuppressWarnings(// Exception types should not be tested using "instanceof" in catch blocks
"java:S1193")
@Transactional(isolation = Isolation.SERIALIZABLE)
@WithJsMagmaScriptContext
public Void copy(List<ResourceIdentifier> resources, @Nullable @CheckForNull String targetPackageId, Progress progress) {
try {
ResourceCollection resourceCollection = resourceCollector.get(resources);
Package targetPackage = getPackage(targetPackageId);
CopyState state = CopyState.create(targetPackage, progress);
copyResources(resourceCollection, state);
} catch (RuntimeException e) {
if (e instanceof ErrorCoded) {
throw e;
} else {
throw new UnknownCopyFailedException(e);
}
}
return null;
}
use of org.molgenis.js.magma.WithJsMagmaScriptContext in project molgenis by molgenis.
the class NegotiatorController method validateNegotiatorExport.
/**
* Validates a {@link NegotiatorRequest}.
*
* <p>Used by the data explorer to verify the request. Since the data explorer does not allow
* filtering on more than one repository, this endpoint does *NOT* support providing biobankRsql.
*
* <ul>
* <li>Checks if disabled collections are selected.
* <li>Checks if at least one collection is left after filtering out the disabled ones.
* </ul>
*
* @param request the request to validate
* @return ExportValidationResponse
* @throws MolgenisDataException if the negotiator is not configured for this entity type
*/
@PostMapping("/validate")
@ResponseBody
@WithJsMagmaScriptContext
public ExportValidationResponse validateNegotiatorExport(@RequestBody NegotiatorRequest request) {
if (request.getBiobankRsql() != null || request.getBiobankId() != null) {
throw new IllegalArgumentException("Cannot verify requests with biobank filters.");
}
boolean isValidRequest = true;
String message = "";
List<String> enabledCollectionsLabels;
List<String> disabledCollectionLabels;
Optional<NegotiatorEntityConfig> entityConfigOptional = getNegotiatorEntityConfig(request.getEntityId());
if (entityConfigOptional.isPresent()) {
NegotiatorEntityConfig entityConfig = entityConfigOptional.get();
LOG.info("Validating negotiator request\n\n{}", request);
List<Entity> collectionEntities = getCollectionEntities(request);
List<Entity> disabledCollections = getDisabledCollections(collectionEntities, entityConfig);
Function<Entity, String> getLabel = entity -> entity.getLabelValue().toString();
disabledCollectionLabels = disabledCollections.stream().map(getLabel).collect(toList());
enabledCollectionsLabels = collectionEntities.stream().filter(e -> !disabledCollections.contains(e)).map(getLabel).collect(toList());
if (!disabledCollections.isEmpty()) {
message = messageSource.getMessage("dataexplorer_directory_disabled", new Object[] { disabledCollections.size(), collectionEntities.size() }, getLocale());
}
if (collectionEntities.isEmpty() || (collectionEntities.size() == disabledCollections.size())) {
isValidRequest = false;
message = messageSource.getMessage("dataexplorer_directory_no_rows", new Object[] {}, getLocale());
}
} else {
throw new MolgenisDataException(messageSource.getMessage("dataexplorer_directory_no_config", new Object[] {}, getLocale()));
}
return ExportValidationResponse.create(isValidRequest, message, enabledCollectionsLabels, disabledCollectionLabels);
}
use of org.molgenis.js.magma.WithJsMagmaScriptContext in project molgenis by molgenis.
the class MappingServiceController method attributeMappingFeedback.
@WithJsMagmaScriptContext
@PostMapping("/attributemappingfeedback")
public String attributeMappingFeedback(@RequestParam() String mappingProjectId, @RequestParam() String target, @RequestParam() String source, @RequestParam() String targetAttribute, @RequestParam() String algorithm, Model model) {
MappingProject project = mappingService.getMappingProject(mappingProjectId);
MappingTarget mappingTarget = project.getMappingTarget(target);
EntityMapping entityMapping = mappingTarget.getMappingForSource(source);
AttributeMapping algorithmTest;
if (entityMapping.getAttributeMapping(targetAttribute) == null) {
algorithmTest = entityMapping.addAttributeMapping(targetAttribute);
algorithmTest.setAlgorithm(algorithm);
} else {
algorithmTest = entityMapping.getAttributeMapping(targetAttribute);
algorithmTest.setAlgorithm(algorithm);
}
try {
Collection<String> sourceAttributeNames = algorithmService.getSourceAttributeNames(algorithm);
if (!sourceAttributeNames.isEmpty()) {
List<Attribute> sourceAttributes = sourceAttributeNames.stream().map(attributeName -> {
EntityType sourceEntityType = entityMapping.getSourceEntityType();
return sourceEntityType.getAttribute(attributeName);
}).filter(Objects::nonNull).collect(toList());
model.addAttribute("sourceAttributes", sourceAttributes);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
model.addAttribute("mappingProjectId", mappingProjectId);
model.addAttribute("target", target);
model.addAttribute("source", source);
model.addAttribute("targetAttribute", dataService.getEntityType(target).getAttribute(targetAttribute));
Stream<Entity> sourceEntities = dataService.findAll(source).limit(10);
List<AlgorithmResult> algorithmResults = sourceEntities.map(sourceEntity -> {
try {
algorithmService.bind(sourceEntity);
return AlgorithmResult.createSuccess(algorithmService.apply(algorithmTest), sourceEntity);
} catch (Exception e) {
return AlgorithmResult.createFailure(e, sourceEntity);
}
}).collect(toList());
model.addAttribute("feedbackRows", algorithmResults);
long missing = algorithmResults.stream().filter(r -> r.isSuccess() && r.getValue() == null).count();
long success = algorithmResults.stream().filter(AlgorithmResult::isSuccess).count() - missing;
long error = algorithmResults.size() - success - missing;
model.addAttribute("success", success);
model.addAttribute("missing", missing);
model.addAttribute("error", error);
model.addAttribute("dataexplorerUri", menuReaderService.findMenuItemPath(DataExplorerController.ID));
return VIEW_ATTRIBUTE_MAPPING_FEEDBACK;
}
use of org.molgenis.js.magma.WithJsMagmaScriptContext in project molgenis by molgenis.
the class MappingServiceImpl method applyMappings.
@Override
@Transactional
@WithJsMagmaScriptContext
public long applyMappings(String mappingProjectId, String entityTypeId, Boolean addSourceAttribute, String packageId, String label, Progress progress) {
MappingProject mappingProject = getMappingProject(mappingProjectId);
MappingTarget mappingTarget = mappingProject.getMappingTargets().get(0);
progress.setProgressMax(calculateMaxProgress(mappingTarget));
progress.progress(0, format("Checking target repository [%s]...", entityTypeId));
EntityType targetMetadata = createTargetMetadata(mappingTarget, entityTypeId, packageId, label, addSourceAttribute);
Repository<Entity> targetRepo = getTargetRepository(entityTypeId, targetMetadata);
return applyMappingsInternal(mappingTarget, targetRepo, progress);
}
Aggregations