コード例 #1
0
 @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();
 }
コード例 #2
0
 private EntityType createCategoricalRefEntityType(String entityName) {
   EntityType targetRefEntityType = entityTypeFactory.create(entityName);
   Attribute targetCodeAttribute = attrMetaFactory.create().setName("code").setDataType(INT);
   Attribute targetLabelAttribute = attrMetaFactory.create().setName("label");
   targetRefEntityType.addAttribute(targetCodeAttribute, ROLE_ID);
   targetRefEntityType.addAttribute(targetLabelAttribute, ROLE_LABEL);
   return targetRefEntityType;
 }
コード例 #3
0
 @Override
 public void setIdValue(Object id) {
   Attribute idAttr = entityType.getIdAttribute();
   if (idAttr == null) {
     throw new IllegalArgumentException(
         format("Entity [%s] doesn't have an id attribute", entityType.getName()));
   }
   set(idAttr.getName(), id);
 }
コード例 #4
0
ファイル: QueryValidator.java プロジェクト: molgenis/molgenis
  private static Attribute getQueryRuleAttribute(QueryRule queryRule, EntityType entityType) {
    String queryRuleField = queryRule.getField();
    if (queryRuleField == null) {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Query rule with operator [%s] is missing required field",
                  queryRule.getOperator().toString())));
    }

    Attribute attr = entityType.getAttribute(queryRuleField);
    if (attr == null) {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Query rule field [%s] refers to unknown attribute in entity type [%s]",
                  queryRuleField, entityType.getName())));
    }
    return attr;
  }
コード例 #5
0
  @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!");
  }
コード例 #6
0
  @Test
  public void testGenerateRules() {
    EntityType targetRefEntityType = createCategoricalRefEntityType("HOP_HYPERTENSION");
    Entity targetEntity1 =
        new DynamicEntity(
            targetRefEntityType, of("code", 0, "label", "Never had high blood pressure "));
    Entity targetEntity2 =
        new DynamicEntity(
            targetRefEntityType, of("code", 1, "label", "Ever had high blood pressure "));
    Entity targetEntity3 =
        new DynamicEntity(targetRefEntityType, of("code", 9, "label", "Missing"));
    Mockito.when(dataService.findAll(targetRefEntityType.getName()))
        .thenReturn(Stream.of(targetEntity1, targetEntity2, targetEntity3));
    targetAttribute =
        attrMetaFactory.create().setName("History of Hypertension").setDataType(CATEGORICAL);
    targetAttribute.setRefEntity(targetRefEntityType);

    EntityType sourceRefEntityType = createCategoricalRefEntityType("High_blood_pressure_ref");
    Entity sourceEntity1 = new DynamicEntity(targetRefEntityType, of("code", 1, "label", "yes"));
    Entity sourceEntity2 = new DynamicEntity(targetRefEntityType, of("code", 2, "label", "no"));
    Entity sourceEntity3 =
        new DynamicEntity(targetRefEntityType, of("code", 3, "label", "I do not know"));
    Mockito.when(dataService.findAll(sourceRefEntityType.getName()))
        .thenReturn(Stream.of(sourceEntity1, sourceEntity2, sourceEntity3));

    sourceAttribute =
        attrMetaFactory.create().setName("High_blood_pressure").setDataType(CATEGORICAL);
    sourceAttribute.setRefEntity(sourceRefEntityType);

    String generatedAlgorithm =
        categoryAlgorithmGenerator.generate(
            targetAttribute, singletonList(sourceAttribute), targetEntityType, sourceEntityType);

    String expectedAlgorithm =
        "$('High_blood_pressure').map({\"1\":\"1\",\"2\":\"0\",\"3\":\"9\"}, null, null).value();";

    Assert.assertEquals(generatedAlgorithm, expectedAlgorithm);
  }
コード例 #7
0
  ElasticsearchEntityIterable(
      Query<Entity> q,
      EntityType entityType,
      ElasticsearchUtils elasticsearchFacade,
      ElasticsearchEntityFactory elasticsearchEntityFactory,
      SearchRequestGenerator searchRequestGenerator,
      String indexName) {
    super(BATCH_SIZE, q);
    this.entityType = requireNonNull(entityType);
    this.elasticsearchFacade = requireNonNull(elasticsearchFacade);
    this.elasticsearchEntityFactory = requireNonNull(elasticsearchEntityFactory);
    this.searchRequestGenerator = requireNonNull(searchRequestGenerator);
    this.indexName = requireNonNull(indexName);

    this.type = sanitizeMapperType(entityType.getName());
  }
コード例 #8
0
 @Override
 protected List<Entity> getBatch(Query<Entity> q) {
   Consumer<SearchRequestBuilder> searchRequestBuilderConsumer =
       searchRequestBuilder ->
           searchRequestGenerator.buildSearchRequest(
               searchRequestBuilder,
               type,
               SearchType.QUERY_AND_FETCH,
               q,
               null,
               null,
               null,
               entityType);
   return elasticsearchFacade
       .searchForIds(searchRequestBuilderConsumer, q.toString(), type, indexName)
       .map(idString -> convert(idString, entityType.getIdAttribute()))
       .map(idObject -> elasticsearchEntityFactory.getReference(entityType, idObject))
       .collect(toList());
 }
コード例 #9
0
 @Override
 public Iterable<String> getAttributeNames() {
   return EntityTypeUtils.getAttributeNames(entityType.getAtomicAttributes());
 }
コード例 #10
0
  @BeforeMethod
  public void init() {
    dataService = Mockito.mock(DataService.class);

    categoryAlgorithmGenerator = new OneToOneCategoryAlgorithmGenerator(dataService);

    EntityType targetRefEntityType = createCategoricalRefEntityType("POTATO_REF");
    Entity targetEntity1 =
        new DynamicEntity(targetRefEntityType, of("code", 1, "label", "Almost daily + daily"));
    Entity targetEntity2 =
        new DynamicEntity(targetRefEntityType, of("code", 2, "label", "Several times a week"));
    Entity targetEntity3 =
        new DynamicEntity(targetRefEntityType, of("code", 3, "label", "About once a week"));
    Entity targetEntity4 =
        new DynamicEntity(
            targetRefEntityType, of("code", 4, "label", "Never + fewer than once a week"));
    Entity targetEntity5 =
        new DynamicEntity(targetRefEntityType, of("code", 9, "label", "missing"));

    targetAttribute =
        attrMetaFactory
            .create()
            .setName("Current Consumption Frequency of Potatoes")
            .setDataType(CATEGORICAL);
    targetAttribute.setRefEntity(targetRefEntityType);

    Mockito.when(dataService.findAll(targetRefEntityType.getName()))
        .thenReturn(
            Stream.of(targetEntity1, targetEntity2, targetEntity3, targetEntity4, targetEntity5));

    targetEntityType = entityTypeFactory.create("target");
    targetEntityType.addAttribute(targetAttribute);

    EntityType sourceRefEntityType = createCategoricalRefEntityType("LifeLines_POTATO_REF");
    Entity sourceEntity1 =
        new DynamicEntity(targetRefEntityType, of("code", 1, "label", "Not this month"));
    Entity sourceEntity2 =
        new DynamicEntity(targetRefEntityType, of("code", 2, "label", "1 day per month"));
    Entity sourceEntity3 =
        new DynamicEntity(targetRefEntityType, of("code", 3, "label", "2-3 days per month"));
    Entity sourceEntity4 =
        new DynamicEntity(targetRefEntityType, of("code", 4, "label", "1 day per week"));
    Entity sourceEntity5 =
        new DynamicEntity(targetRefEntityType, of("code", 5, "label", "2-3 days per week"));
    Entity sourceEntity6 =
        new DynamicEntity(targetRefEntityType, of("code", 6, "label", "4-5 days per week"));
    Entity sourceEntity7 =
        new DynamicEntity(targetRefEntityType, of("code", 7, "label", "6-7 days per week"));
    Entity sourceEntity8 =
        new DynamicEntity(targetRefEntityType, of("code", 8, "label", "9 days per week"));

    sourceAttribute = attrMetaFactory.create().setName("MESHED_POTATO").setDataType(CATEGORICAL);
    sourceAttribute.setLabel(
        "How often did you eat boiled or mashed potatoes (also in stew) in the past month? Baked potatoes are asked later");
    sourceAttribute.setRefEntity(sourceRefEntityType);

    Mockito.when(dataService.findAll(sourceRefEntityType.getName()))
        .thenReturn(
            Stream.of(
                sourceEntity1,
                sourceEntity2,
                sourceEntity3,
                sourceEntity4,
                sourceEntity5,
                sourceEntity6,
                sourceEntity7,
                sourceEntity8));

    sourceEntityType = entityTypeFactory.create("source");
    sourceEntityType.addAttributes(Lists.newArrayList(sourceAttribute));
  }
コード例 #11
0
 @Override
 public Object getLabelValue() {
   // abstract entities might not have an label attribute
   Attribute labelAttr = entityType.getLabelAttribute();
   return labelAttr != null ? get(labelAttr.getName()) : null;
 }
コード例 #12
0
 @Override
 public Object getIdValue() {
   // abstract entities might not have an id attribute
   Attribute idAttr = entityType.getIdAttribute();
   return idAttr != null ? get(idAttr.getName()) : null;
 }
コード例 #13
0
 @Override
 public Iterable<String> getAttributeNames() {
   return stream(entityType.getAtomicAttributes().spliterator(), false).map(Attribute::getName)
       ::iterator;
 }
コード例 #14
0
  /**
   * Validate is value is of the type defined by the attribute data type.
   *
   * @param attrName attribute name
   * @param value value (must be of the type defined by the attribute data type.)
   */
  protected void validateValueType(String attrName, Object value) {
    if (value == null) {
      return;
    }

    Attribute attr = entityType.getAttribute(attrName);
    if (attr == null) {
      throw new UnknownAttributeException(format("Unknown attribute [%s]", attrName));
    }

    AttributeType dataType = attr.getDataType();
    switch (dataType) {
      case BOOL:
        if (!(value instanceof Boolean)) {
          throw new MolgenisDataException(
              format(
                  "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]",
                  value.toString(),
                  value.getClass().getSimpleName(),
                  Boolean.class.getSimpleName(),
                  attrName));
        }
        break;
      case CATEGORICAL:
        // expected type is FileMeta. validation is not possible because molgenis-data does not
        // depend on molgenis-file
      case FILE:
      case XREF:
        if (!(value instanceof Entity)) {
          throw new MolgenisDataException(
              format(
                  "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]",
                  value.toString(),
                  value.getClass().getSimpleName(),
                  Entity.class.getSimpleName(),
                  attrName));
        }
        break;
      case CATEGORICAL_MREF:
      case MREF:
      case ONE_TO_MANY:
        if (!(value instanceof Iterable)) {
          throw new MolgenisDataException(
              format(
                  "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]",
                  value.toString(),
                  value.getClass().getSimpleName(),
                  Iterable.class.getSimpleName(),
                  attrName));
        }
        break;
      case COMPOUND:
        throw new IllegalArgumentException(
            format("Unexpected data type [%s] for attribute: [%s]", dataType.toString(), attrName));
      case DATE:
      case DATE_TIME:
        if (!(value instanceof java.util.Date)) {
          throw new MolgenisDataException(
              format(
                  "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]",
                  value.toString(),
                  value.getClass().getSimpleName(),
                  java.util.Date.class.getSimpleName(),
                  attrName));
        }
        break;
      case DECIMAL:
        if (!(value instanceof Double)) {
          throw new MolgenisDataException(
              format(
                  "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]",
                  value.toString(),
                  value.getClass().getSimpleName(),
                  Double.class.getSimpleName(),
                  attrName));
        }
        break;
      case EMAIL:
      case ENUM:
      case HTML:
      case HYPERLINK:
      case SCRIPT:
      case STRING:
      case TEXT:
        if (!(value instanceof String)) {
          throw new MolgenisDataException(
              format(
                  "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]",
                  value.toString(),
                  value.getClass().getSimpleName(),
                  String.class.getSimpleName(),
                  attrName));
        }
        break;
      case INT:
        if (!(value instanceof Integer)) {
          throw new MolgenisDataException(
              format(
                  "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]",
                  value.toString(),
                  value.getClass().getSimpleName(),
                  Integer.class.getSimpleName(),
                  attrName));
        }
        break;
      case LONG:
        if (!(value instanceof Long)) {
          throw new MolgenisDataException(
              format(
                  "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]",
                  value.toString(),
                  value.getClass().getSimpleName(),
                  Long.class.getSimpleName(),
                  attrName));
        }
        break;
      default:
        throw new RuntimeException(format("Unknown data type [%s]", dataType.toString()));
    }
  }