private EntityMapping toEntityMapping(Entity entityMappingEntity) {
    String identifier = entityMappingEntity.getString(EntityMappingMetaData.IDENTIFIER);

    EntityMetaData targetEntityMetaData;
    try {
      targetEntityMetaData =
          dataService.getEntityMetaData(
              entityMappingEntity.getString(EntityMappingMetaData.TARGETENTITYMETADATA));
    } catch (UnknownEntityException uee) {
      LOG.error(uee.getMessage());
      targetEntityMetaData = null;
    }

    EntityMetaData sourceEntityMetaData;
    try {
      sourceEntityMetaData =
          dataService.getEntityMetaData(
              entityMappingEntity.getString(EntityMappingMetaData.SOURCEENTITYMETADATA));
    } catch (UnknownEntityException uee) {
      LOG.error(uee.getMessage());
      sourceEntityMetaData = null;
    }

    List<Entity> attributeMappingEntities =
        Lists.<Entity>newArrayList(
            entityMappingEntity.getEntities(EntityMappingMetaData.ATTRIBUTEMAPPINGS));
    List<AttributeMapping> attributeMappings =
        attributeMappingRepository.getAttributeMappings(
            attributeMappingEntities, sourceEntityMetaData, targetEntityMetaData);

    return new EntityMapping(
        identifier, sourceEntityMetaData, targetEntityMetaData, attributeMappings);
  }
  /**
   * @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;
    }
  }
Exemplo n.º 3
0
 private Map<String, String> getGenomeBrowserSetsToModel() {
   Map<String, String> genomeBrowserSets = new HashMap<String, String>();
   for (String entityName : dataService.getEntityNames()) {
     EntityMetaData entityMetaData = dataService.getEntityMetaData(entityName);
     AttributeMetaData attributeStartPosition =
         entityMetaData.getAttribute(MUTATION_START_POSITION);
     AttributeMetaData attributeId = entityMetaData.getAttribute(MUTATION_ID);
     AttributeMetaData attributeChromosome = entityMetaData.getAttribute(MUTATION_CHROMOSOME);
     if (attributeStartPosition != null && attributeId != null && attributeChromosome != null) {
       genomeBrowserSets.put(entityName, entityMetaData.getLabel());
     }
   }
   return genomeBrowserSets;
 }
  private AttributeMetaDataResponseV2 createAttributeMetaDataResponse(
      String entityName, String attributeName) {
    EntityMetaData entity = dataService.getEntityMetaData(entityName);
    if (entity == null) {
      throw new UnknownEntityException(entityName + " not found");
    }

    AttributeMetaData attribute = entity.getAttribute(attributeName);
    if (attribute == null) {
      throw new RuntimeException("attribute : " + attributeName + " does not exist!");
    }

    return new AttributeMetaDataResponseV2(
        entityName, entity, attribute, null, permissionService, dataService, languageService);
  }
  /**
   * Try to create multiple entities in one transaction. If one fails all fails.
   *
   * @param entityName name of the entity where the entities are going to be added.
   * @param request EntityCollectionCreateRequestV2
   * @param response HttpServletResponse
   * @return EntityCollectionCreateResponseBodyV2
   * @throws Exception
   */
  @RequestMapping(value = "/{entityName}", method = POST, produces = APPLICATION_JSON_VALUE)
  @ResponseBody
  public EntityCollectionBatchCreateResponseBodyV2 createEntities(
      @PathVariable("entityName") String entityName,
      @RequestBody @Valid EntityCollectionBatchRequestV2 request,
      HttpServletResponse response)
      throws Exception {
    final EntityMetaData meta = dataService.getEntityMetaData(entityName);
    if (meta == null) {
      throw createUnknownEntityException(entityName);
    }

    try {
      final List<Entity> entities =
          request
              .getEntities()
              .stream()
              .map(e -> this.restService.toEntity(meta, e))
              .collect(Collectors.toList());
      final EntityCollectionBatchCreateResponseBodyV2 responseBody =
          new EntityCollectionBatchCreateResponseBodyV2();
      final List<String> ids = new ArrayList<String>();

      // Add all entities
      this.dataService.add(entityName, entities.stream());

      entities.forEach(
          entity -> {
            String id = entity.getIdValue().toString();
            ids.add(id.toString());
            responseBody
                .getResources()
                .add(
                    new AutoValue_ResourcesResponseV2(
                        Href.concatEntityHref(RestControllerV2.BASE_URI, entityName, id)));
          });

      responseBody.setLocation(
          Href.concatEntityCollectionHref(
              RestControllerV2.BASE_URI, entityName, meta.getIdAttribute().getName(), ids));

      response.setStatus(HttpServletResponse.SC_CREATED);
      return responseBody;
    } catch (Exception e) {
      response.setStatus(HttpServletResponse.SC_NO_CONTENT);
      throw e;
    }
  }
Exemplo n.º 6
0
  @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);
  }
  @RequestMapping(value = "/{entityName}/{id:.+}", method = POST, params = "_method=GET")
  @ResponseBody
  public Map<String, Object> retrieveEntityPost(
      @PathVariable("entityName") String entityName,
      @PathVariable("id") Object id,
      @RequestParam(value = "attrs", required = false) AttributeFilter attributeFilter) {
    EntityMetaData entityMeta = dataService.getEntityMetaData(entityName);
    Fetch fetch =
        AttributeFilterToFetchConverter.convert(
            attributeFilter, entityMeta, languageService.getCurrentUserLanguageCode());

    Entity entity = dataService.findOne(entityName, id, fetch);
    if (entity == null) {
      throw new UnknownEntityException(entityName + " [" + id + "] not found");
    }

    return createEntityResponse(entity, fetch, true);
  }
Exemplo n.º 8
0
  @RequestMapping(value = "/download", method = POST)
  public void download(
      @RequestParam("dataRequest") String dataRequestStr, HttpServletResponse response)
      throws IOException {
    // Workaround because binding with @RequestBody is not possible:
    // http://stackoverflow.com/a/9970672
    dataRequestStr = URLDecoder.decode(dataRequestStr, "UTF-8");
    logger.info("Download request: [" + dataRequestStr + "]");
    DataRequest dataRequest =
        new GsonHttpMessageConverter().getGson().fromJson(dataRequestStr, DataRequest.class);

    String entityName = dataRequest.getEntityName();
    EntityMetaData entityMetaData = dataService.getEntityMetaData(entityName);
    final Set<String> attributes = new HashSet<String>(dataRequest.getAttributeNames());
    String fileName =
        entityName + '_' + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".csv";

    response.setContentType("text/csv");
    response.addHeader("Content-Disposition", "attachment; filename=" + fileName);

    CsvWriter csvWriter = new CsvWriter(response.getOutputStream());
    try {
      csvWriter.writeAttributeNames(
          Iterables.transform(
              Iterables.filter(
                  entityMetaData.getAtomicAttributes(),
                  new Predicate<AttributeMetaData>() {
                    @Override
                    public boolean apply(AttributeMetaData attributeMetaData) {
                      return attributes.contains(attributeMetaData.getName());
                    }
                  }),
              new Function<AttributeMetaData, String>() {
                @Override
                public String apply(AttributeMetaData attributeMetaData) {
                  return attributeMetaData.getName();
                }
              }));
      csvWriter.add(dataService.findAll(entityName, dataRequest.getQuery()));
    } finally {
      csvWriter.close();
    }
  }
  /**
   * Try to update multiple entities in one transaction. If one fails all fails.
   *
   * @param entityName name of the entity where the entities are going to be added.
   * @param request EntityCollectionCreateRequestV2
   * @param response HttpServletResponse
   * @throws Exception
   */
  @RequestMapping(value = "/{entityName}", method = PUT)
  public synchronized void updateEntities(
      @PathVariable("entityName") String entityName,
      @RequestBody @Valid EntityCollectionBatchRequestV2 request,
      HttpServletResponse response)
      throws Exception {
    final EntityMetaData meta = dataService.getEntityMetaData(entityName);
    if (meta == null) {
      throw createUnknownEntityException(entityName);
    }

    try {
      final Stream<Entity> entities =
          request.getEntities().stream().map(e -> this.restService.toEntity(meta, e));

      // update all entities
      this.dataService.update(entityName, entities);
      response.setStatus(HttpServletResponse.SC_OK);
    } catch (Exception e) {
      response.setStatus(HttpServletResponse.SC_NO_CONTENT);
      throw e;
    }
  }
Exemplo n.º 10
0
  private EntityCollectionResponseV2 createEntityCollectionResponse(
      String entityName, EntityCollectionRequestV2 request, HttpServletRequest httpRequest) {
    EntityMetaData meta = dataService.getEntityMetaData(entityName);

    Query q = request.getQ() != null ? request.getQ().createQuery(meta) : new QueryImpl();
    q.pageSize(request.getNum()).offset(request.getStart()).sort(request.getSort());
    Fetch fetch =
        AttributeFilterToFetchConverter.convert(
            request.getAttrs(), meta, languageService.getCurrentUserLanguageCode());
    if (fetch != null) {
      q.fetch(fetch);
    }

    if (request.getAggs() != null) {
      // return aggregates for aggregate query
      AggregateQuery aggsQ = request.getAggs().createAggregateQuery(meta, q);
      AttributeMetaData xAttr = aggsQ.getAttributeX();
      AttributeMetaData yAttr = aggsQ.getAttributeY();
      if (xAttr == null && yAttr == null) {
        throw new MolgenisQueryException("Aggregate query is missing 'x' or 'y' attribute");
      }
      AggregateResult aggs = dataService.aggregate(entityName, aggsQ);
      AttributeMetaDataResponseV2 xAttrResponse =
          xAttr != null
              ? new AttributeMetaDataResponseV2(
                  entityName, meta, xAttr, fetch, permissionService, dataService, languageService)
              : null;
      AttributeMetaDataResponseV2 yAttrResponse =
          yAttr != null
              ? new AttributeMetaDataResponseV2(
                  entityName, meta, yAttr, fetch, permissionService, dataService, languageService)
              : null;
      return new EntityAggregatesResponse(
          aggs, xAttrResponse, yAttrResponse, BASE_URI + '/' + entityName);
    } else {
      Long count = dataService.count(entityName, q);
      Iterable<Entity> it;
      if (count > 0) {
        it =
            new Iterable<Entity>() {
              @Override
              public Iterator<Entity> iterator() {
                return dataService.findAll(entityName, q).iterator();
              }
            };
      } else {
        it = Collections.emptyList();
      }
      EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it);

      List<Map<String, Object>> entities = new ArrayList<>();
      for (Entity entity : it) {
        Map<String, Object> responseData = new LinkedHashMap<String, Object>();
        createEntityValuesResponse(entity, fetch, responseData);
        entities.add(responseData);
      }

      UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getFullURL(httpRequest));

      String prevHref = null;
      if (pager.getPrevStart() != null) {
        builder.replaceQueryParam("start", pager.getPrevStart());
        prevHref = builder.build(false).toUriString();
      }

      String nextHref = null;
      if (pager.getNextStart() != null) {
        builder.replaceQueryParam("start", pager.getNextStart());
        nextHref = builder.build(false).toUriString();
      }

      return new EntityCollectionResponseV2(
          pager,
          entities,
          fetch,
          BASE_URI + '/' + entityName,
          meta,
          permissionService,
          dataService,
          languageService,
          prevHref,
          nextHref);
    }
  }
  @Override
  @Transactional(rollbackFor = IOException.class)
  public EntityImportReport doImport(
      RepositoryCollection repositories, DatabaseAction databaseAction) throws IOException {
    // All new repository identifiers
    List<String> newRepoIdentifiers = new ArrayList<String>();

    // First import entities, the data sheets are ignored in the entitiesimporter
    EntityImportReport importReport = entitiesImporter.importEntities(repositories, databaseAction);

    // RULE: Feature can only belong to one Protocol in a DataSet. Check it (see issue #1136)
    checkFeatureCanOnlyBelongToOneProtocolForOneDataSet();

    // Import data sheets
    for (String name : repositories.getEntityNames()) {
      Repository repository = repositories.getRepositoryByEntityName(name);

      if (repository.getName().startsWith(DATASET_SHEET_PREFIX)) {
        // Import DataSet sheet, create new OmxRepository
        String identifier = repository.getName().substring(DATASET_SHEET_PREFIX.length());

        if (!dataService.hasRepository(identifier)) {

          dataService.addRepository(
              new AggregateableCrudRepositorySecurityDecorator(
                  new OmxRepository(dataService, searchService, identifier, entityValidator)));
          newRepoIdentifiers.add(identifier);

          DataSet dataSet =
              dataService.findOne(
                  DataSet.ENTITY_NAME,
                  new QueryImpl().eq(DataSet.IDENTIFIER, identifier),
                  DataSet.class);

          List<Protocol> protocols =
              ProtocolUtils.getProtocolDescendants(dataSet.getProtocolUsed());
          List<ObservableFeature> categoricalFeatures = new ArrayList<ObservableFeature>();
          for (Protocol protocol : protocols) {
            List<ObservableFeature> observableFeatures = protocol.getFeatures();
            if (observableFeatures != null) {
              for (ObservableFeature observableFeature : observableFeatures) {
                String dataType = observableFeature.getDataType();
                FieldType type = MolgenisFieldTypes.getType(dataType);
                if (type.getEnumType() == FieldTypeEnum.CATEGORICAL) {
                  categoricalFeatures.add(observableFeature);
                }
              }
            }
          }
          for (ObservableFeature categoricalFeature : categoricalFeatures) {
            if (!dataService.hasRepository(
                OmxLookupTableEntityMetaData.createOmxLookupTableEntityMetaDataName(
                    categoricalFeature.getIdentifier()))) {
              dataService.addRepository(
                  new OmxLookupTableRepository(
                      dataService, categoricalFeature.getIdentifier(), queryResolver));
              newRepoIdentifiers.add(
                  OmxLookupTableEntityMetaData.createOmxLookupTableEntityMetaDataName(
                      categoricalFeature.getIdentifier()));
            }
          }
        }

        // Check if all column names in the excel sheet exist as attributes of the entity
        Set<ConstraintViolation> violations = Sets.newLinkedHashSet();
        EntityMetaData meta = dataService.getEntityMetaData(identifier);
        for (AttributeMetaData attr : repository.getEntityMetaData().getAttributes()) {
          if (meta.getAttribute(attr.getName()) == null) {
            String message =
                String.format(
                    "Unknown attributename '%s' for entity '%s'. Sheet: '%s'",
                    attr.getName(), meta.getName(), repository.getName());
            violations.add(new ConstraintViolation(message, attr.getName(), null, null, meta, 0));
          }
        }

        if (!violations.isEmpty()) {
          throw new MolgenisValidationException(violations);
        }

        // Import data into new OmxRepository
        try {
          dataService.add(identifier, repository);
        } catch (MolgenisValidationException e) {
          // Add sheet info
          for (ConstraintViolation violation : e.getViolations()) {
            if (violation.getRownr() > 0) {

              // Rownr +1 for header
              violation.setImportInfo(
                  String.format(
                      "Sheet: '%s', row: %d", repository.getName(), violation.getRownr() + 1));
            } else {
              violation.setImportInfo(String.format("Sheet: '%s'", repository.getName()));
            }
          }

          for (String newRepoIdentifier : newRepoIdentifiers) {
            dataService.removeRepository(newRepoIdentifier);
          }

          throw e;
        }

        int count = (int) RepositoryUtils.count(repository);
        importReport.addEntityCount(identifier, count);
        importReport.addNrImported(count);
      }
    }

    return importReport;
  }