@Override
  @RunAsSystem
  public void initialize() {
    Resource resource = new ClassPathResource("roc-curve.R");
    if (resource.exists()) {
      long count =
          dataService.count(
              Script.ENTITY_NAME, new QueryImpl().eq(Script.NAME, ROC_CURVE_SCRIPT_NAME));
      if (count == 0) {
        Entity scriptType =
            dataService.findOne(ScriptType.ENTITY_NAME, new QueryImpl().eq(ScriptType.NAME, "R"));

        if (scriptType == null) throw new UnknownEntityException("ScriptType R does not exist!");

        String scriptContent;
        try {
          scriptContent =
              FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream(), "UTF-8"));

        } catch (IOException e) {
          throw new UncheckedIOException(e);
        }

        if (dataService.count(
                ScriptParameter.ENTITY_NAME,
                new QueryImpl().eq(ScriptParameter.NAME, ROC_CURVE_SCRIPT_PARAMETER))
            == 0) {
          dataService.add(
              ScriptParameter.ENTITY_NAME,
              new MapEntity(ImmutableMap.of(ScriptParameter.NAME, ROC_CURVE_SCRIPT_PARAMETER)));
        }

        Entity scriptParameterEntity =
            dataService.findOne(
                ScriptParameter.ENTITY_NAME,
                new QueryImpl().eq(ScriptParameter.NAME, ROC_CURVE_SCRIPT_PARAMETER));

        MapEntity scriptEntity = new MapEntity();
        scriptEntity.set(Script.NAME, ROC_CURVE_SCRIPT_NAME);
        scriptEntity.set(Script.GENERATE_TOKEN, true);
        scriptEntity.set(Script.TYPE, scriptType);
        scriptEntity.set(Script.RESULT_FILE_EXTENSION, "png");
        scriptEntity.set(Script.CONTENT, scriptContent);
        scriptEntity.set(Script.PARAMETERS, Arrays.asList(scriptParameterEntity));
        dataService.add(Script.ENTITY_NAME, scriptEntity);

        LOG.info("Script entity \"roc\" has been added to the database!");
      } else {
        LOG.info("Script entity \"roc\" already exists in the database!");
      }
    } else {
      LOG.info("R script \"roc-curve.R\" does not exist on classpath!");
    }
  }
  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);
    }
  }