@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!");
    }
  }
  @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 Entity upsert(EntityMapping entityMapping) {
   List<Entity> attributeMappingEntities =
       attributeMappingRepository.upsert(entityMapping.getAttributeMappings());
   Entity entityMappingEntity;
   if (entityMapping.getIdentifier() == null) {
     entityMapping.setIdentifier(idGenerator.generateId());
     entityMappingEntity = toEntityMappingEntity(entityMapping, attributeMappingEntities);
     dataService.add(entityMappingMetaData.getName(), entityMappingEntity);
   } else {
     entityMappingEntity = toEntityMappingEntity(entityMapping, attributeMappingEntities);
     dataService.update(entityMappingMetaData.getName(), entityMappingEntity);
   }
   return entityMappingEntity;
 }
  private void replaceUserPermissions(
      List<UserAuthority> entityAuthorities, String userId, String authorityType) {
    MolgenisUser molgenisUser =
        dataService.findOne(MolgenisUser.ENTITY_NAME, userId, MolgenisUser.class);
    if (molgenisUser == null) throw new RuntimeException("unknown user id [" + userId + "]");

    // inject user
    for (UserAuthority entityAuthority : entityAuthorities)
      entityAuthority.setMolgenisUser(molgenisUser);

    // delete old plugin authorities
    List<? extends Authority> oldEntityAuthorities =
        getUserPermissions(molgenisUser, authorityType);
    if (oldEntityAuthorities != null && !oldEntityAuthorities.isEmpty())
      dataService.delete(UserAuthority.ENTITY_NAME, oldEntityAuthorities);

    // insert new plugin authorities
    if (!entityAuthorities.isEmpty()) dataService.add(UserAuthority.ENTITY_NAME, entityAuthorities);
  }
  private void replaceGroupPermissions(
      List<GroupAuthority> entityAuthorities, String groupId, String authorityPrefix) {
    MolgenisGroup molgenisGroup =
        dataService.findOne(MolgenisGroup.ENTITY_NAME, groupId, MolgenisGroup.class);
    if (molgenisGroup == null) throw new RuntimeException("unknown group id [" + groupId + "]");

    // inject user
    for (GroupAuthority entityAuthority : entityAuthorities)
      entityAuthority.setMolgenisGroup(molgenisGroup);

    // delete old plugin authorities
    List<Authority> oldEntityAuthorities = getGroupPermissions(molgenisGroup, authorityPrefix);
    if (oldEntityAuthorities != null && !oldEntityAuthorities.isEmpty())
      dataService.delete(GroupAuthority.ENTITY_NAME, oldEntityAuthorities);

    // insert new plugin authorities
    if (!entityAuthorities.isEmpty())
      dataService.add(GroupAuthority.ENTITY_NAME, entityAuthorities);
  }
  @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;
  }