Search in sources :

Example 16 with PreparationDTO

use of org.talend.dataprep.api.preparation.PreparationDTO in project data-prep by Talend.

the class PreparationService method getPreparationDetails.

/**
 * Return a preparation details.
 *
 * @param id the wanted preparation id.
 * @param stepId the optional step id.
 * @return the preparation details.
 */
public PreparationDTO getPreparationDetails(String id, String stepId) {
    LOGGER.debug("Get content of preparation details for #{}.", id);
    final PersistentPreparation preparation = preparationRepository.get(id, PersistentPreparation.class);
    if (preparation == null) {
        throw new TDPException(PreparationErrorCodes.PREPARATION_DOES_NOT_EXIST, build().put(ID, id));
    }
    ensurePreparationConsistency(preparation);
    // specify the step id if provided
    if (!StringUtils.equals(HEAD, stepId)) {
        // just make sure the step does exist
        if (Step.ROOT_STEP.id().equals(stepId)) {
            preparation.setSteps(Collections.singletonList(Step.ROOT_STEP.id()));
            preparation.setHeadId(Step.ROOT_STEP.id());
        } else if (preparationRepository.exist(PersistentStep.class, eq(ID, stepId))) {
            preparation.setSteps(preparationUtils.listStepsIds(stepId, preparationRepository));
            preparation.setHeadId(stepId);
        } else {
            throw new TDPException(PREPARATION_STEP_DOES_NOT_EXIST, build().put(ID, preparation).put(STEP_ID, stepId));
        }
    }
    final PreparationDTO details = beanConversionService.convert(preparation, PreparationDTO.class);
    LOGGER.debug("returning details for {} -> {}", id, details);
    return details;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) PreparationDTO(org.talend.dataprep.api.preparation.PreparationDTO) PersistentPreparation(org.talend.dataprep.preparation.store.PersistentPreparation)

Example 17 with PreparationDTO

use of org.talend.dataprep.api.preparation.PreparationDTO in project data-prep by Talend.

the class PreparationService method listAll.

public Stream<PreparationDTO> listAll(PreparationSearchCriterion searchCriterion, Sort sort, Order order) {
    LOGGER.debug("Get list of preparations (with details).");
    Stream<PersistentPreparation> preparationStream;
    Expression filter = null;
    Predicate<PersistentPreparation> deprecatedFolderIdFilter = null;
    if (searchCriterion.getName() != null) {
        filter = getNameFilter(searchCriterion.getName(), searchCriterion.isNameExactMatch());
    }
    if (searchCriterion.getDataSetId() != null) {
        Expression dataSetFilter = eq(DATASET_ID, searchCriterion.getDataSetId());
        filter = filter == null ? dataSetFilter : and(filter, dataSetFilter);
    }
    if (searchCriterion.getFolderId() != null) {
        if (preparationRepository.exist(PersistentPreparation.class, isEmpty(FOLDER_ID))) {
            // filter on folder id (DEPRECATED VERSION - only applies if migration isn't completed yet)
            try (Stream<FolderEntry> folders = folderRepository.entries(searchCriterion.getFolderId(), PREPARATION)) {
                final Set<String> entries = folders.map(// 
                FolderEntry::getContentId).collect(toSet());
                deprecatedFolderIdFilter = p -> entries.contains(p.id());
            }
        } else {
            // Once all preparations all have the folder id,
            Expression folderIdFilter = eq(FOLDER_ID, searchCriterion.getFolderId());
            filter = filter == null ? folderIdFilter : and(filter, folderIdFilter);
        }
    }
    // Handles filtering (prefer database filters)
    if (filter != null) {
        preparationStream = preparationRepository.list(PersistentPreparation.class, filter);
    } else {
        preparationStream = preparationRepository.list(PersistentPreparation.class);
    }
    // migration for preparation after the change from dataset ID to dataset name
    // see TDP-6195 and TDP-5696
    preparationStream = preparationStream.map(dataSetNameInjection::injectDatasetNameBasedOnId);
    if (deprecatedFolderIdFilter != null) {
        // filter on folder id (DEPRECATED VERSION - only applies if migration isn't completed yet)
        preparationStream = // 
        preparationStream.filter(// 
        deprecatedFolderIdFilter).peek(p -> p.setFolderId(searchCriterion.getFolderId()));
    }
    // filter on folder path
    if (searchCriterion.getFolderPath() != null) {
        final Optional<Folder> folder = folderRepository.getFolder(searchCriterion.getFolderPath());
        final Set<String> folderEntries = new HashSet<>();
        folder.ifPresent(f -> {
            try (Stream<String> preparationIds = // 
            folderRepository.entries(f.getId(), // 
            PREPARATION).map(FolderEntry::getContentId)) {
                folderEntries.addAll(// 
                preparationIds.collect(toSet()));
            }
        });
        preparationStream = preparationStream.filter(p -> folderEntries.contains(p.id()));
    }
    return preparationStream.map(preparation -> {
        if (StringUtils.isEmpty(preparation.getName())) {
            preparation.setName((preparation.getDataSetName() != null ? preparation.getDataSetName() + " " : "") + message("preparation.create.suffix"));
            preparationRepository.add(preparation);
        }
        return preparation;
    }).map(p -> beanConversionService.convert(p, PreparationDTO.class, ownerInjection.injectIntoPreparation(), // 
    sharedInjection)).sorted(getPreparationComparator(sort, order));
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PREPARATION(org.talend.dataprep.api.folder.FolderContentType.PREPARATION) TqlBuilder.match(org.talend.tql.api.TqlBuilder.match) ActionFactory(org.talend.dataprep.transformation.actions.common.ActionFactory) Date(java.util.Date) MAX_VALUE(java.lang.Integer.MAX_VALUE) LoggerFactory(org.slf4j.LoggerFactory) TqlBuilder.and(org.talend.tql.api.TqlBuilder.and) Autowired(org.springframework.beans.factory.annotation.Autowired) PreparationRepository(org.talend.dataprep.preparation.store.PreparationRepository) StringUtils(org.apache.commons.lang3.StringUtils) StepRowMetadata(org.talend.dataprep.api.preparation.StepRowMetadata) LockedResourceRepository(org.talend.dataprep.lock.store.LockedResourceRepository) BeanConversionService(org.talend.dataprep.conversions.BeanConversionService) AppendStep(org.talend.dataprep.api.preparation.AppendStep) Collectors.toMap(java.util.stream.Collectors.toMap) Locale(java.util.Locale) Map(java.util.Map) DataprepBundle.message(org.talend.dataprep.i18n.DataprepBundle.message) PREPARATION_NOT_EMPTY(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_NOT_EMPTY) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) Collectors.toSet(java.util.stream.Collectors.toSet) StepDiff(org.talend.dataprep.api.preparation.StepDiff) PREPARATION_ROOT_STEP_CANNOT_BE_DELETED(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_ROOT_STEP_CANNOT_BE_DELETED) Order(org.talend.dataprep.util.SortAndOrderHelper.Order) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Set(java.util.Set) PreparationErrorCodes(org.talend.dataprep.exception.error.PreparationErrorCodes) PREPARATION_STEP_CANNOT_BE_REORDERED(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_STEP_CANNOT_BE_REORDERED) UUID(java.util.UUID) PREPARATION_STEP_DOES_NOT_EXIST(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_STEP_DOES_NOT_EXIST) Objects(java.util.Objects) List(java.util.List) BaseDataprepAuditService(org.talend.dataprep.audit.BaseDataprepAuditService) Stream(java.util.stream.Stream) SortAndOrderHelper.getPreparationComparator(org.talend.dataprep.util.SortAndOrderHelper.getPreparationComparator) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) Security(org.talend.dataprep.security.Security) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) IntStream(java.util.stream.IntStream) TqlBuilder.isEmpty(org.talend.tql.api.TqlBuilder.isEmpty) ImplicitParameters(org.talend.dataprep.transformation.actions.common.ImplicitParameters) TDPException(org.talend.dataprep.exception.TDPException) CREATE_NEW_COLUMN(org.talend.dataprep.transformation.actions.common.ActionsUtils.CREATE_NEW_COLUMN) JsonErrorCodeDescription(org.talend.dataprep.exception.json.JsonErrorCodeDescription) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Deque(java.util.Deque) Expression(org.talend.tql.model.Expression) Function(java.util.function.Function) PersistentPreparation(org.talend.dataprep.preparation.store.PersistentPreparation) DatasetClient(org.talend.dataprep.dataset.adapter.DatasetClient) ArrayList(java.util.ArrayList) PREPARATION_NAME_ALREADY_USED(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_NAME_ALREADY_USED) HashSet(java.util.HashSet) FolderEntry(org.talend.dataprep.api.folder.FolderEntry) ExceptionContext.build(org.talend.daikon.exception.ExceptionContext.build) ExceptionContext(org.talend.daikon.exception.ExceptionContext) Folder(org.talend.dataprep.api.folder.Folder) Service(org.springframework.stereotype.Service) PREPARATION_DOES_NOT_EXIST(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_DOES_NOT_EXIST) VersionService(org.talend.dataprep.api.service.info.VersionService) Action(org.talend.dataprep.api.preparation.Action) OwnerInjection(org.talend.dataprep.conversions.inject.OwnerInjection) Preparation(org.talend.dataprep.api.preparation.Preparation) PreparationDetailsDTO(org.talend.dataprep.api.preparation.PreparationDetailsDTO) FolderRepository(org.talend.dataprep.folder.store.FolderRepository) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ActionRegistry(org.talend.dataprep.transformation.pipeline.ActionRegistry) DecimalFormat(java.text.DecimalFormat) Sort(org.talend.dataprep.util.SortAndOrderHelper.Sort) Step(org.talend.dataprep.api.preparation.Step) PreparationSearchCriterion.filterPreparation(org.talend.dataprep.preparation.service.PreparationSearchCriterion.filterPreparation) ActionForm(org.talend.dataprep.api.action.ActionForm) PreparationDTO(org.talend.dataprep.api.preparation.PreparationDTO) TqlBuilder.eq(org.talend.tql.api.TqlBuilder.eq) Collectors.toList(java.util.stream.Collectors.toList) PreparationUtils(org.talend.dataprep.api.preparation.PreparationUtils) SharedInjection(org.talend.dataprep.preparation.configuration.SharedInjection) ActionMetadataValidation(org.talend.dataprep.transformation.api.action.validation.ActionMetadataValidation) Lookup(org.talend.dataprep.transformation.actions.datablending.Lookup) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) PATH_SEPARATOR(org.talend.dataprep.folder.store.FoldersRepositoriesConstants.PATH_SEPARATOR) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) Folder(org.talend.dataprep.api.folder.Folder) Expression(org.talend.tql.model.Expression) FolderEntry(org.talend.dataprep.api.folder.FolderEntry) PersistentPreparation(org.talend.dataprep.preparation.store.PersistentPreparation) HashSet(java.util.HashSet)

Example 18 with PreparationDTO

use of org.talend.dataprep.api.preparation.PreparationDTO in project data-prep by Talend.

the class PreparationControllerTest method update.

@Test
public void update() throws Exception {
    assertThat(repository.list(Preparation.class).count(), is(0L));
    final String preparationId = clientTest.createPreparation(createTestPreparation("test_name", "1234")).getId();
    final Preparation createdPreparation = repository.list(Preparation.class).iterator().next();
    assertThat(createdPreparation.getId(), is(preparationId));
    final long oldModificationDate = createdPreparation.getLastModificationDate();
    // Test preparation details update
    PreparationDTO forUpdate = new PreparationDTO();
    forUpdate.setName("test_name_updated");
    forUpdate.setDataSetId("1234");
    final String updatedId = given().contentType(// 
    ContentType.JSON).body(// 
    forUpdate).when().put("/preparations/{id}", // 
    preparationId).asString();
    // Preparation id should not change (name is not part of preparation id).
    assertThat(updatedId, is(preparationId));
    final Collection<Preparation> preparations = repository.list(Preparation.class).collect(Collectors.toList());
    assertThat(preparations.size(), is(1));
    final Preparation preparation = preparations.iterator().next();
    assertThat(preparation.getName(), is("test_name_updated"));
    assertThat(preparation.getLastModificationDate(), is(greaterThan(oldModificationDate)));
    assertThat(preparation.getAppVersion(), is(versionService.version().getVersionId()));
}
Also used : PreparationDTO(org.talend.dataprep.api.preparation.PreparationDTO) Preparation(org.talend.dataprep.api.preparation.Preparation) BasePreparationTest(org.talend.dataprep.preparation.BasePreparationTest) Test(org.junit.Test)

Example 19 with PreparationDTO

use of org.talend.dataprep.api.preparation.PreparationDTO in project data-prep by Talend.

the class PreparationControllerTest method updateWithSpecialArguments.

@Test
public void updateWithSpecialArguments() throws Exception {
    // given
    final Preparation createdPreparation = new Preparation("#123", versionService.version().getVersionId());
    createdPreparation.setDataSetId("1234");
    createdPreparation.setName("test_name");
    createdPreparation.setRowMetadata(new RowMetadata());
    final String preparationId = clientTest.createPreparation(createdPreparation).getId();
    // when
    final PreparationDTO forUpdate = new PreparationDTO();
    forUpdate.setName("éàçè");
    forUpdate.setDataSetId("1234");
    final String updatedId = given().contentType(// 
    ContentType.JSON.withCharset(UTF_8)).body(// 
    forUpdate).when().put("/preparations/{id}", preparationId).asString();
    // then
    // Preparation id should not change (new name)
    assertThat(updatedId, is(preparationId));
    final Collection<Preparation> preparations = repository.list(Preparation.class).collect(Collectors.toList());
    assertThat(preparations.size(), is(1));
    final Preparation preparation = preparations.iterator().next();
    assertThat(preparation.id(), is(updatedId));
    assertThat(preparation.getName(), is("éàçè"));
}
Also used : PreparationDTO(org.talend.dataprep.api.preparation.PreparationDTO) Preparation(org.talend.dataprep.api.preparation.Preparation) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) BasePreparationTest(org.talend.dataprep.preparation.BasePreparationTest) Test(org.junit.Test)

Example 20 with PreparationDTO

use of org.talend.dataprep.api.preparation.PreparationDTO in project data-prep by Talend.

the class PreparationServiceTest method updateHeadAndCheckResult.

private void updateHeadAndCheckResult(PreparationDTO prep, List<String> originalStepIds, Integer indexOfStep) {
    preparationService.setPreparationHead(prep.getId(), originalStepIds.get(indexOfStep));
    PreparationDTO updatedPrep = preparationService.getPreparation(prep.getId());
    // we check that headId is the correct one and there is only one step on the list
    assertNotNull(updatedPrep);
    assertEquals(originalStepIds.get(indexOfStep), updatedPrep.getHeadId());
    assertEquals(indexOfStep + 1, updatedPrep.getSteps().size());
    assertEquals(originalStepIds.get(indexOfStep), updatedPrep.getSteps().get(indexOfStep));
}
Also used : PreparationDTO(org.talend.dataprep.api.preparation.PreparationDTO)

Aggregations

PreparationDTO (org.talend.dataprep.api.preparation.PreparationDTO)45 Test (org.junit.Test)22 ExportParameters (org.talend.dataprep.api.export.ExportParameters)9 ApiOperation (io.swagger.annotations.ApiOperation)7 InputStream (java.io.InputStream)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 ArrayList (java.util.ArrayList)6 Action (org.talend.dataprep.api.preparation.Action)6 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)5 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)5 Preparation (org.talend.dataprep.api.preparation.Preparation)5 PreparationDetailsDTO (org.talend.dataprep.api.preparation.PreparationDetailsDTO)5 TransformationCacheKey (org.talend.dataprep.cache.TransformationCacheKey)5 TDPException (org.talend.dataprep.exception.TDPException)5 OutputStream (java.io.OutputStream)4 StringUtils (org.apache.commons.lang3.StringUtils)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Component (org.springframework.stereotype.Component)4