@Transactional @DeleteMapping("/{entityTypeId:^(?!i18n).+}/{id}") @ResponseStatus(NO_CONTENT) public void deleteEntity( @PathVariable("entityTypeId") String entityTypeId, @PathVariable("id") String untypedId) { EntityType entityType = dataService.getEntityType(entityTypeId); Object id = getTypedValue(untypedId, entityType.getIdAttribute()); if (ATTRIBUTE_META_DATA.equals(entityTypeId)) { dataService.getMeta().deleteAttributeById(id); } else { dataService.deleteById(entityTypeId, id); } }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Entity)) return false; return EntityUtils.equals(this, (Entity) o); }
@Override public int hashCode() { return EntityUtils.hashCode(this); }
private Set<ConstraintViolation> checkNullableExpressions(Entity entity, EntityType entityType) { List<String> nullableExpressions = new ArrayList<>(); List<Attribute> expressionAttributes = new ArrayList<>(); for (Attribute attribute : entityType.getAtomicAttributes()) { String nullableExpression = attribute.getNullableExpression(); if (nullableExpression != null) { expressionAttributes.add(attribute); nullableExpressions.add(nullableExpression); } } Set<ConstraintViolation> violations = new LinkedHashSet<>(); if (!nullableExpressions.isEmpty()) { List<Boolean> results = expressionValidator.resolveBooleanExpressions(nullableExpressions, entity); for (int i = 0; i < results.size(); i++) { if (!results.get(i) && EntityUtils.isNullValue(entity, expressionAttributes.get(i))) { violations.add( createConstraintViolation( entity, expressionAttributes.get(i), entityType, format("Offended nullable expression: %s", nullableExpressions.get(i)))); } } } return violations; }
@Override @Transactional public EntityImportReport doImport( RepositoryCollection source, MetadataAction metadataAction, DataAction dataAction, @Nullable String packageId) { if (dataAction != DataAction.ADD) { throw new IllegalArgumentException("Only ADD is supported"); } EntityImportReport report = new EntityImportReport(); for (String entityTypeId : source.getEntityTypeIds()) { try (Repository<Entity> sourceRepository = source.getRepository(entityTypeId)) { Repository<Entity> targetRepository = dataService.getRepository(entityTypeId); Integer count = targetRepository.add(asStream(sourceRepository)); report.addEntityCount(entityTypeId, count); } catch (IOException e) { LOG.error("", e); throw new MolgenisDataException(e); } } return report; }
/** Delete multiple entities of the given entity type */ @DeleteMapping("/{entityTypeId}") @ResponseStatus(NO_CONTENT) public void deleteEntityCollection( @PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid EntityCollectionDeleteRequestV2 request) { EntityType entityType = dataService.getEntityType(entityTypeId); if (entityType.isAbstract()) { throw new MolgenisDataException( format("Cannot delete entities because type [%s] is abstract.", entityTypeId)); } Attribute idAttribute = entityType.getIdAttribute(); Stream<Object> typedIds = request.getEntityIds().stream().map(entityId -> getTypedValue(entityId, idAttribute)); dataService.deleteAll(entityTypeId, typedIds); }
private void delete(String entityTypeId, String untypedId) { EntityType entityType = dataService.getEntityType(entityTypeId); Object id = getTypedValue(untypedId, entityType.getIdAttribute()); if (ATTRIBUTE_META_DATA.equals(entityTypeId)) { dataService.getMeta().deleteAttributeById(id); } else { dataService.deleteById(entityTypeId, id); } }
/** Same as retrieveEntity (GET) only tunneled through POST. */ @PostMapping( value = "/{entityTypeId}/{id}", params = "_method=GET", produces = APPLICATION_JSON_VALUE) @ResponseBody public Map<String, Object> retrieveEntity( @PathVariable("entityTypeId") String entityTypeId, @PathVariable("id") String untypedId, @Valid @RequestBody EntityTypeRequest request) { Set<String> attributesSet = toAttributeSet(request != null ? request.getAttributes() : null); Map<String, Set<String>> attributeExpandSet = toExpandMap(request != null ? request.getExpand() : null); EntityType meta = dataService.getEntityType(entityTypeId); Object id = getTypedValue(untypedId, meta.getIdAttribute()); Entity entity = dataService.findOneById(entityTypeId, id); if (entity == null) { throw new UnknownEntityException(meta, untypedId); } return getEntityAsMap(entity, meta, attributesSet, attributeExpandSet); }
private void registerIndexAction(IndexAction action) { String entityTypeId = action.getEntityTypeId(); EntityType entityType = dataService.findOneById(ENTITY_TYPE_META_DATA, entityTypeId, EntityType.class); if (entityType != null) { Object typedEntityId = getTypedValue(action.getEntityId(), entityType.getIdAttribute()); indexActionRegisterService.register(entityType, typedEntityId); } } }
/** * Get's an entity by it's id * * <p>Examples: * * <p>/api/v1/person/99 Retrieves a person with id 99 */ @GetMapping(value = "/{entityTypeId}/{id}", produces = APPLICATION_JSON_VALUE) public Map<String, Object> retrieveEntity( @PathVariable("entityTypeId") String entityTypeId, @PathVariable("id") String untypedId, @RequestParam(value = "attributes", required = false) String[] attributes, @RequestParam(value = "expand", required = false) String[] attributeExpands) { Set<String> attributesSet = toAttributeSet(attributes); Map<String, Set<String>> attributeExpandSet = toExpandMap(attributeExpands); EntityType meta = dataService.getEntityType(entityTypeId); if (meta == null) { throw new UnknownEntityTypeException(entityTypeId); } Object id = getTypedValue(untypedId, meta.getIdAttribute()); Entity entity = dataService.findOneById(entityTypeId, id); if (entity == null) { throw new UnknownEntityException(meta, id); } return getEntityAsMap(entity, meta, attributesSet, attributeExpandSet); }
@PostMapping(value = "/{entityTypeId}/{id}/{attributeName}", params = "_method=PUT") @ResponseStatus(OK) public synchronized void updateAttribute( @PathVariable("entityTypeId") String entityTypeId, @PathVariable("attributeName") String attributeName, @PathVariable("id") String untypedId, @RequestBody(required = false) Object paramValue) { EntityType entityType = dataService.getEntityType(entityTypeId); if (entityType == null) { throw new UnknownEntityTypeException(entityTypeId); } Object id = getTypedValue(untypedId, entityType.getIdAttribute()); Entity entity = dataService.findOneById(entityTypeId, id); if (entity == null) { throw new UnknownEntityException(entityType, id); } Attribute attr = entityType.getAttribute(attributeName); if (attr == null) { throw new UnknownAttributeException(entityType, attributeName); } if (attr.isReadOnly()) { throw new MolgenisDataAccessException( "Attribute '" + attributeName + "' of entity '" + entityTypeId + "' is readonly"); } Object value = this.restService.toEntityValue(attr, paramValue, id); entity.set(attributeName, value); dataService.update(entityTypeId, entity); }
Object entityId = getTypedValue(untypedEntityId, entityType.getIdAttribute()); String entityFullName = entityType.getId();
Entity getDefaultSettings() { Entity defaultSettingsEntity = new DynamicEntity(this); for (Attribute attr : this.getAtomicAttributes()) { // default values are stored/retrieved as strings, so we convert them to the required type // here. String defaultValue = attr.getDefaultValue(); if (defaultValue != null) { Object typedDefaultValue = getTypedValue(defaultValue, attr, entityManager); defaultSettingsEntity.set(attr.getName(), typedDefaultValue); } } defaultSettingsEntity.set(ATTR_ID, getSettingsEntityId()); return defaultSettingsEntity; }
private Map<String, Object> getEntityResponse( String entityTypeId, String untypedId, AttributeFilter attributeFilter, boolean includeCategories) { EntityType entityType = dataService.getEntityType(entityTypeId); Object id = getTypedValue(untypedId, entityType.getIdAttribute()); Fetch fetch = AttributeFilterToFetchConverter.convert( attributeFilter, entityType, LanguageService.getCurrentUserLanguageCode()); Entity entity = dataService.findOneById(entityTypeId, id, fetch); if (entity == null) { throw new UnknownEntityException(entityType, id); } return createEntityResponse(entity, fetch, true, includeCategories); }
throw new UnknownEntityTypeException(entityTypeId); Object id = getTypedValue(untypedId, meta.getIdAttribute());
private void updateInternal( String entityTypeId, String untypedId, Map<String, Object> entityMap) { EntityType meta = dataService.getEntityType(entityTypeId); if (meta.getIdAttribute() == null) { throw new IllegalArgumentException(entityTypeId + " does not have an id attribute"); } Object id = getTypedValue(untypedId, meta.getIdAttribute()); Entity existing = dataService.findOneById( entityTypeId, id, new Fetch().field(meta.getIdAttribute().getName())); if (existing == null) { throw new UnknownEntityException(meta, id); } Entity entity = this.restService.toEntity(meta, entityMap); dataService.update(entityTypeId, entity); restService.updateMappedByEntities(entity, existing); }
typedValue = EntityUtils.getTypedValue(value, attr, entityManager); } catch (NumberFormatException e) { throw new MolgenisValidationException(