@BeforeTest public void createEntity() { entityType = when(mock(EntityType.class).getName()).thenReturn("Source").getMock(); Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("Identifier").getMock(); when(idAttr.getDataType()).thenReturn(INT); Attribute intAttr = when(mock(Attribute.class).getName()).thenReturn("Int").getMock(); when(intAttr.getDataType()).thenReturn(INT); Attribute stringAttr = when(mock(Attribute.class).getName()).thenReturn("String").getMock(); when(stringAttr.getDataType()).thenReturn(STRING); Attribute nonNumericStringAttr = when(mock(Attribute.class).getName()).thenReturn("NonNumericString").getMock(); when(nonNumericStringAttr.getDataType()).thenReturn(STRING); Attribute longAttr = when(mock(Attribute.class).getName()).thenReturn("Long").getMock(); when(longAttr.getDataType()).thenReturn(LONG); when(entityType.getIdAttribute()).thenReturn(idAttr); when(entityType.getAttribute("Identifier")).thenReturn(idAttr); when(entityType.getAttribute("Int")).thenReturn(intAttr); when(entityType.getAttribute("String")).thenReturn(stringAttr); when(entityType.getAttribute("NonNumericString")).thenReturn(nonNumericStringAttr); when(entityType.getAttribute("Long")).thenReturn(longAttr); entity = new DynamicEntity(entityType); entity.set("Int", 1); entity.set("String", "12"); entity.set("Long", 10L); entity.set("NonNumericString", "Hello World!"); }
private void validateEntityValueUniqueness( Entity entity, ValidationResource validationResource, ValidationMode validationMode) { validationResource .getUniqueAttrs() .forEach( uniqueAttr -> { Object attrValue = entity.get(uniqueAttr.getName()); if (attrValue != null) { if (uniqueAttr.getDataType() instanceof XrefField) { attrValue = ((Entity) attrValue).getIdValue(); } HugeMap<Object, Object> uniqueAttrValues = validationResource.getUniqueAttrsValues().get(uniqueAttr.getName()); Object existingEntityId = uniqueAttrValues.get(attrValue); if ((validationMode == ValidationMode.ADD && existingEntityId != null) || (validationMode == ValidationMode.UPDATE && existingEntityId != null && !existingEntityId.equals(entity.getIdValue()))) { ConstraintViolation constraintViolation = new ConstraintViolation( format( "Duplicate value '%s' for unique attribute '%s' from entity '%s'", attrValue, uniqueAttr.getName(), getName()), uniqueAttr, Long.valueOf(validationResource.getRow())); validationResource.addViolation(constraintViolation); } else { uniqueAttrValues.put(attrValue, entity.getIdValue()); } } }); }
private EntityMapping toEntityMapping(Entity entityMappingEntity) { String identifier = entityMappingEntity.getString(EntityMappingMetaData.IDENTIFIER); EntityType targetEntityType; try { targetEntityType = dataService.getEntityType( entityMappingEntity.getString(EntityMappingMetaData.TARGET_ENTITY_TYPE)); } catch (UnknownEntityException uee) { LOG.error(uee.getMessage()); targetEntityType = null; } EntityType sourceEntityType; try { sourceEntityType = dataService.getEntityType( entityMappingEntity.getString(EntityMappingMetaData.SOURCE_ENTITY_TYPE)); } catch (UnknownEntityException uee) { LOG.error(uee.getMessage()); sourceEntityType = null; } List<Entity> attributeMappingEntities = Lists.<Entity>newArrayList( entityMappingEntity.getEntities(EntityMappingMetaData.ATTRIBUTE_MAPPINGS)); List<AttributeMapping> attributeMappings = attributeMappingRepository.getAttributeMappings( attributeMappingEntities, sourceEntityType, targetEntityType); return new EntityMapping(identifier, sourceEntityType, targetEntityType, attributeMappings); }
@Override public void populateTestEntity(Entity entity) throws Exception { entity.set("col1", sdf.parse("2012-03-13 23:59:33")); entity.set("col2", sdf.parse("2013-02-09 13:12:11")); assertEquals(entity.getUtilDate("col1"), sdf.parse("2012-03-13 23:59:33")); assertEquals(entity.getUtilDate("col2"), sdf.parse("2013-02-09 13:12:11")); }
@Override public String toString() { StringBuilder strBuilder = new StringBuilder(entityType.getName()).append('{'); strBuilder.append( stream(entityType.getAtomicAttributes().spliterator(), false) .map( attr -> { StringBuilder attrStrBuilder = new StringBuilder(attr.getName()).append('='); if (EntityTypeUtils.isSingleReferenceType(attr)) { Entity refEntity = getEntity(attr.getName()); attrStrBuilder.append(refEntity != null ? refEntity.getIdValue() : null); } else if (EntityTypeUtils.isMultipleReferenceType(attr)) { attrStrBuilder .append('[') .append( stream(getEntities(attr.getName()).spliterator(), false) .map(Entity::getIdValue) .map(Object::toString) .collect(joining(","))) .append(']'); } else { attrStrBuilder.append(get(attr.getName())); } return attrStrBuilder.toString(); }) .collect(Collectors.joining("&"))); strBuilder.append('}'); return strBuilder.toString(); }
@Override public List<Entity> annotateEntity(Entity entity) { HttpGet httpGet = new HttpGet(getServiceUri(entity)); Entity resultEntity = new MapEntity(); if (!annotatedInput.contains(entity.get(UNIPROT_ID))) { annotatedInput.add(entity.get(UNIPROT_ID)); try { HttpResponse response = httpClient.execute(httpGet); BufferedReader br = new BufferedReader( new InputStreamReader( (response.getEntity().getContent()), Charset.forName("UTF-8"))); String output; StringBuilder result = new StringBuilder(); while ((output = br.readLine()) != null) { result.append(output); } resultEntity = parseResult(entity, result.toString()); } catch (Exception e) { httpGet.abort(); // TODO: how to handle exceptions at this point throw new RuntimeException(e); } } return Collections.singletonList(resultEntity); }
@BeforeMethod public void beforeMethod() { owner = new MolgenisUser(); owner.setUsername("flup"); owner.setPassword("geheim"); owner.setId("12345"); owner.setActive(true); owner.setEmail("*****@*****.**"); owner.setFirstName("Flup"); owner.setLastName("de Flap"); DefaultEntityMetaData target1 = new DefaultEntityMetaData("target1"); target1.addAttribute("id", ROLE_ID); DefaultEntityMetaData target2 = new DefaultEntityMetaData("target2"); target2.addAttribute("id", ROLE_ID); mappingProject = new MappingProject("My first mapping project", owner); mappingTarget1 = mappingProject.addTarget(target1); mappingTarget2 = mappingProject.addTarget(target2); Entity mappingTargetEntity = new MapEntity(MappingTargetRepositoryImpl.META_DATA); mappingTargetEntity.set(MappingTargetMetaData.TARGET, "target1"); mappingTargetEntity.set(MappingTargetMetaData.IDENTIFIER, "mappingTargetID1"); Entity mappingTargetEntity2 = new MapEntity(MappingTargetRepositoryImpl.META_DATA); mappingTargetEntity2.set(MappingTargetMetaData.TARGET, "target2"); mappingTargetEntity2.set(MappingTargetMetaData.IDENTIFIER, "mappingTargetID2"); mappingTargetEntities = asList(mappingTargetEntity, mappingTargetEntity2); mappingProjectEntity = new MapEntity(META_DATA); mappingProjectEntity.set(IDENTIFIER, "mappingProjectID"); mappingProjectEntity.set(MAPPINGTARGETS, mappingTargetEntities); mappingProjectEntity.set(OWNER, owner); mappingProjectEntity.set(NAME, "My first mapping project"); }
@Override public Entity createTestEntity() { Entity e = new MapEntity(); e.set("col1", "lorem"); e.set("col2", "ipsum"); return e; }
/** * @param entityName The name of the entity to update * @param attributeName The name of the attribute to update * @param request EntityCollectionBatchRequestV2 * @param response HttpServletResponse * @throws Exception */ @RequestMapping(value = "/{entityName}/{attributeName}", method = PUT) @ResponseStatus(OK) public synchronized void updateAttribute( @PathVariable("entityName") String entityName, @PathVariable("attributeName") String attributeName, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) throws Exception { final EntityMetaData meta = dataService.getEntityMetaData(entityName); if (meta == null) { throw createUnknownEntityException(entityName); } try { AttributeMetaData attr = meta.getAttribute(attributeName); if (attr == null) { throw createUnknownAttributeException(entityName, attributeName); } if (attr.isReadonly()) { throw createMolgenisDataAccessExceptionReadOnlyAttribute(entityName, attributeName); } final List<Entity> entities = request .getEntities() .stream() .filter(e -> e.size() == 2) .map(e -> this.restService.toEntity(meta, e)) .collect(Collectors.toList()); if (entities.size() != request.getEntities().size()) { throw createMolgenisDataExceptionIdentifierAndValue(); } final List<Entity> updatedEntities = new ArrayList<Entity>(); int count = 0; for (Entity entity : entities) { String id = checkForEntityId(entity, count); Entity originalEntity = dataService.findOne(entityName, id); if (originalEntity == null) { throw createUnknownEntityExceptionNotValidId(id); } Object value = this.restService.toEntityValue(attr, entity.get(attributeName)); originalEntity.set(attributeName, value); updatedEntities.add(originalEntity); count++; } // update all entities this.dataService.update(entityName, updatedEntities.stream()); response.setStatus(HttpServletResponse.SC_OK); } catch (Exception e) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); throw e; } }
@Test public void checkRangeMaxOnly() { Entity entity = new DynamicEntity(intRangeMaxMeta); entity.set("id", "123"); entity.set("intrangemin", 0); Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity, intRangeMaxMeta); assertTrue(constraints.isEmpty()); }
@Test public void checkRangeMaxOnlyInvalid() { Entity entity = new DynamicEntity(intRangeMaxMeta); entity.set("id", "123"); entity.set("intrangemin", 2); Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity, intRangeMaxMeta); assertEquals(constraints.size(), 1); }
/** * Creates a internal molgenis id from a vcf entity * * @param vcfEntity * @return the id */ public static String createId(Entity vcfEntity) { StringBuilder id = new StringBuilder(); id.append(StringUtils.strip(vcfEntity.get(CHROM).toString())); id.append("_"); id.append(StringUtils.strip(vcfEntity.get(POS).toString())); id.append("_"); id.append(StringUtils.strip(vcfEntity.get(REF).toString())); id.append("_"); id.append(StringUtils.strip(vcfEntity.get(ALT).toString())); return id.toString(); }
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); } } return defaultSettingsEntity; }
private Entity toEntityMappingEntity( EntityMapping entityMapping, List<Entity> attributeMappingEntities) { Entity entityMappingEntity = new DynamicEntity(entityMappingMetaData); entityMappingEntity.set(EntityMappingMetaData.IDENTIFIER, entityMapping.getIdentifier()); entityMappingEntity.set(EntityMappingMetaData.SOURCE_ENTITY_TYPE, entityMapping.getName()); entityMappingEntity.set( EntityMappingMetaData.TARGET_ENTITY_TYPE, entityMapping.getTargetEntityType() != null ? entityMapping.getTargetEntityType().getName() : null); entityMappingEntity.set(EntityMappingMetaData.ATTRIBUTE_MAPPINGS, attributeMappingEntities); return entityMappingEntity; }
private Entity toEntityMappingEntity( EntityMapping entityMapping, List<Entity> attributeMappingEntities) { Entity entityMappingEntity = new MapEntity(META_DATA); entityMappingEntity.set(EntityMappingMetaData.IDENTIFIER, entityMapping.getIdentifier()); entityMappingEntity.set(EntityMappingMetaData.SOURCEENTITYMETADATA, entityMapping.getName()); entityMappingEntity.set( EntityMappingMetaData.TARGETENTITYMETADATA, entityMapping.getTargetEntityMetaData() != null ? entityMapping.getTargetEntityMetaData().getName() : null); entityMappingEntity.set(EntityMappingMetaData.ATTRIBUTEMAPPINGS, attributeMappingEntities); return entityMappingEntity; }
@SuppressWarnings("unchecked") @Test public void iterator() throws IOException, ServiceException { when(spreadsheetService.getFeed(any(URL.class), (Class<IFeed>) any(Class.class))) .thenReturn(cellFeed) .thenReturn(listFeed); Iterator<Entity> it = spreadsheetRepository.iterator(); assertTrue(it.hasNext()); Entity entity = it.next(); assertEquals(entity.getString("col1"), "val1"); assertEquals(entity.getString("col2"), "val2"); assertEquals(entity.getString("col3"), "val3"); assertFalse(it.hasNext()); }
private void toEntity( EntityMetaData emd, Map<String, Entity> entities, Map<String, Entity> attributes) { // TODO prevent duplicates Entity e = new MapEntity(); e.set("name", emd.getName()); entities.put(emd.getName(), e); logger.debug("entity: " + e); for (AttributeMetaData amd : emd.getAttributes()) { Entity a = new MapEntity(); a.set("name", amd.getName()); a.set("entity", emd.getName()); if (amd.getDataType() != null && amd.getDataType() != MolgenisFieldTypes.STRING) a.set("dataType", amd.getDataType()); if (amd.getDefaultValue() != null) a.set("defaultValue", amd.getDefaultValue()); if (amd.getRefEntity() != null) a.set("refEntity", amd.getRefEntity().getName()); logger.debug("attribute: " + a); attributes.put(emd.getName() + "." + amd.getName(), a); // compound if (amd.getDataType() == MolgenisFieldTypes.COMPOUND && entities.get(amd.getRefEntity().getName()) == null) { this.toEntity(amd.getRefEntity(), entities, attributes); } } }
/** * Get entity id and perform a check, throwing an MolgenisDataException when necessary * * @param entity * @param count * @return */ private String checkForEntityId(Entity entity, int count) { Object id = entity.getIdValue(); if (null == id) { throw createMolgenisDataExceptionUnknownIdentifier(count); } return id.toString(); }
private FileMeta convertFile(Attribute attr, Object paramValue) { Entity entity = convertRef(attr, paramValue); if (entity == null) { return null; } if (!(entity instanceof FileMeta)) { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value is of type [%s] instead of [%s]", attr.getName(), entity.getClass().getSimpleName(), FileMeta.class.getSimpleName()))); } return (FileMeta) entity; }
@RequestMapping( value = "/aggregate", method = RequestMethod.POST, produces = "application/json", consumes = "application/json") @ResponseBody public AggregateResponse aggregate(@Valid @RequestBody AggregateRequest request) { // TODO create utility class to extract info from entity/attribute uris String[] attributeUriTokens = request.getAttributeUri().split("/"); String entityName = attributeUriTokens[3]; String attributeName = attributeUriTokens[5]; QueryImpl q = request.getQ() != null ? new QueryImpl(request.getQ()) : new QueryImpl(); EntityMetaData entityMeta = dataService.getEntityMetaData(entityName); AttributeMetaData attributeMeta = entityMeta.getAttribute(attributeName); FieldTypeEnum dataType = attributeMeta.getDataType().getEnumType(); if (dataType != FieldTypeEnum.BOOL && dataType != FieldTypeEnum.CATEGORICAL) { throw new RuntimeException("Unsupported data type " + dataType); } EntityMetaData refEntityMeta = null; String refAttributeName = null; if (dataType == FieldTypeEnum.CATEGORICAL) { refEntityMeta = attributeMeta.getRefEntity(); refAttributeName = refEntityMeta.getLabelAttribute().getName(); } Map<String, Integer> aggregateMap = new HashMap<String, Integer>(); for (Entity entity : dataService.findAll(entityName, q)) { String val; switch (dataType) { case BOOL: val = entity.getString(attributeName); break; case CATEGORICAL: Entity refEntity = (Entity) entity.get(attributeName); val = refEntity.getString(refAttributeName); break; default: throw new RuntimeException("Unsupported data type " + dataType); } Integer count = aggregateMap.get(val); if (count == null) aggregateMap.put(val, 1); else aggregateMap.put(val, count + 1); } return new AggregateResponse(aggregateMap); }
@Override public Double getDouble(String attributeName) { if (fetch.hasField(attributeName)) { return decoratedEntity.getDouble(attributeName); } else { return entityManager.getReference(getEntityMetaData(), getIdValue()).getDouble(attributeName); } }
private String getServiceUri(Entity entity) { StringBuilder uriStringBuilder = new StringBuilder(); uriStringBuilder.append(EBI_CHEMBLWS_URL); uriStringBuilder.append(entity.get(UNIPROT_ID)); uriStringBuilder.append(".json"); return uriStringBuilder.toString(); }
@Test public void createByUrl() throws IOException { InputStream in = getClass().getResourceAsStream("/testdata.csv"); File csvFile = new File(FileUtils.getTempDirectory(), "testdata.csv"); FileCopyUtils.copy(in, new FileOutputStream(csvFile)); String url = "csv://" + csvFile.getAbsolutePath(); EntitySource entitySource = new CsvEntitySource(url, null); try { assertEquals(entitySource.getUrl(), url); Iterator<String> it = entitySource.getEntityNames().iterator(); assertNotNull(it); assertTrue(it.hasNext()); assertEquals(it.next(), "testdata"); assertFalse(it.hasNext()); Repository<? extends Entity> repo = entitySource.getRepositoryByEntityName("testdata"); assertNotNull(repo); Iterator<AttributeMetaData> itMeta = repo.getAttributes().iterator(); assertNotNull(itMeta); assertTrue(itMeta.hasNext()); AttributeMetaData col1 = itMeta.next(); assertNotNull(col1); assertEquals(col1.getName(), "col1"); AttributeMetaData col2 = itMeta.next(); assertNotNull(col2); assertEquals(col2.getName(), "col2"); assertFalse(itMeta.hasNext()); Iterator<? extends Entity> itEntity = repo.iterator(); assertNotNull(itEntity); assertTrue(itEntity.hasNext()); Entity entity = itEntity.next(); assertNotNull(entity); assertEquals(entity.get("col1"), "val1"); assertEquals(entity.get("col2"), "val2"); } finally { entitySource.close(); } }
public void check(String file, String datasetMatrix) throws IOException { EntitySource entitySource = new ExcelEntitySourceFactory().create(new File(file)); Repository<? extends Entity> repo = entitySource.getRepositoryByEntityName("observablefeature"); List<String> listOfCategoricalFeatures = new ArrayList<String>(); Map<String, List<String>> hashCategories = new HashMap<String, List<String>>(); for (Entity entity : repo) { if ("categorical".equals(entity.getString("dataType"))) { listOfCategoricalFeatures.add(entity.getString("identifier")); hashCategories.put(entity.getString("identifier"), new ArrayList<String>()); } } Repository<? extends Entity> readObservableDataMatrixRepo = entitySource.getRepositoryByEntityName(datasetMatrix); for (Entity entity : readObservableDataMatrixRepo) { for (String category : listOfCategoricalFeatures) { List<String> getList = hashCategories.get(category); if (!hashCategories.get(category).contains(entity.getString(category))) { getList.add(entity.getString(category)); } } } printForCategoryTab(hashCategories); entitySource.close(); }
@SuppressWarnings("unchecked") private void validateEntityValueReadOnly(Entity entity, ValidationResource validationResource) { Entity entityToUpdate = findOne(entity.getIdValue()); validationResource .getReadonlyAttrs() .forEach( readonlyAttr -> { Object value = entity.get(readonlyAttr.getName()); Object existingValue = entityToUpdate.get(readonlyAttr.getName()); if (readonlyAttr.getDataType() instanceof XrefField) { if (value != null) { value = ((Entity) value).getIdValue(); } if (existingValue != null) { existingValue = ((Entity) existingValue).getIdValue(); } } else if (readonlyAttr.getDataType() instanceof MrefField) { List<Object> entityIds = new ArrayList<>(); ((Iterable<Entity>) value) .forEach( mrefEntity -> { entityIds.add(mrefEntity.getIdValue()); }); value = entityIds; List<Object> existingEntityIds = new ArrayList<>(); ((Iterable<Entity>) existingValue) .forEach( mrefEntity -> { existingEntityIds.add(mrefEntity.getIdValue()); }); existingValue = existingEntityIds; } if (value != null && existingValue != null && !value.equals(existingValue)) { validationResource.addViolation( new ConstraintViolation( format( "The attribute '%s' of entity '%s' can not be changed it is readonly.", readonlyAttr.getName(), getName()), Long.valueOf(validationResource.getRow()))); } }); }
@RunAsSystem public FileIngestJob createJob(FileIngestJobExecution fileIngestJobExecution) { dataService.add(FileIngestJobExecutionMetaData.ENTITY_NAME, fileIngestJobExecution); String username = fileIngestJobExecution.getUser(); Progress progress = new ProgressImpl(fileIngestJobExecution, jobExecutionUpdater, mailSender); TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); RunAsUserToken runAsAuthentication = new RunAsUserToken( "Job Execution", username, null, userDetailsService.loadUserByUsername(username).getAuthorities(), null); Entity fileIngestEntity = fileIngestJobExecution.getFileIngest(); Entity targetEntityEntity = fileIngestEntity.getEntity(FileIngestMetaData.ENTITY_META_DATA); String targetEntityName = targetEntityEntity.getString(EntityMetaDataMetaData.FULL_NAME); String url = fileIngestEntity.getString(FileIngestMetaData.URL); String loader = fileIngestEntity.getString(FileIngestMetaData.LOADER); String failureEmail = fileIngestEntity.getString(FileIngestMetaData.FAILURE_EMAIL); return new FileIngestJob( progress, transactionTemplate, runAsAuthentication, fileIngester, targetEntityName, url, loader, failureEmail, fileIngestJobExecution.getIdentifier()); }
private Map<String, Object> createEntityResponse( Entity entity, Fetch fetch, boolean includeMetaData) { Map<String, Object> responseData = new LinkedHashMap<String, Object>(); if (includeMetaData) { createEntityMetaResponse(entity.getEntityMetaData(), fetch, responseData); } createEntityValuesResponse(entity, fetch, responseData); return responseData; }
private Entity parseResult(Entity entity, String json) throws IOException { Entity result = new MapEntity(); if (!"".equals(json)) { Map<String, Object> rootMap = jsonStringToMap(json); Map<String, Object> resultMap = (Map<String, Object>) rootMap.get("target"); resultMap.put(UNIPROT_ID, entity.get(UNIPROT_ID)); result = new MapEntity(resultMap); } return result; }
/** * A helper function that gets identifiers of all the attributes from one entityMetaData * * @param sourceEntityMetaData * @return */ public List<String> getAttributeIdentifiers(EntityMetaData sourceEntityMetaData) { Entity entityMetaDataEntity = dataService.findOne( EntityMetaDataMetaData.ENTITY_NAME, new QueryImpl().eq(EntityMetaDataMetaData.FULL_NAME, sourceEntityMetaData.getName())); if (entityMetaDataEntity == null) throw new MolgenisDataAccessException( "Could not find EntityMetaDataEntity by the name of " + sourceEntityMetaData.getName()); return FluentIterable.from(entityMetaDataEntity.getEntities(EntityMetaDataMetaData.ATTRIBUTES)) .transform( new Function<Entity, String>() { public String apply(Entity attributeEntity) { return attributeEntity.getString(AttributeMetaDataMetaData.IDENTIFIER); } }) .toList(); }
@Override public <E extends Entity> Iterable<E> getEntities(String attributeName, Class<E> clazz) { if (fetch.hasField(attributeName)) { return decoratedEntity.getEntities(attributeName, clazz); } else { return entityManager .getReference(getEntityMetaData(), getIdValue()) .getEntities(attributeName, clazz); } }