use of org.talend.dataprep.preparation.store.PersistentPreparation in project data-prep by Talend.
the class PreparationService method getVersionedAction.
/**
* Get all the actions of a preparation at given version.
*
* @param id the wanted preparation id.
* @param version the wanted preparation version.
* @return the list of actions.
*/
public List<Action> getVersionedAction(final String id, final String version) {
LOGGER.debug("Get list of actions of preparation #{} at version {}.", id, version);
final PersistentPreparation preparation = preparationRepository.get(id, PersistentPreparation.class);
if (preparation != null) {
return getVersionedAction(preparation, version);
} else {
throw new TDPException(PREPARATION_DOES_NOT_EXIST, build().put(ID, id));
}
}
use of org.talend.dataprep.preparation.store.PersistentPreparation in project data-prep by Talend.
the class PreparationService method updateAction.
/**
* Update a step in a preparation <b>Strategy</b><br/>
* The goal here is to rewrite the preparation history from 'the step to modify' (STM) to the head, with STM
* containing the new action.<br/>
* <ul>
* <li>1. Extract the actions from STM (excluded) to the head</li>
* <li>2. Insert the new actions before the other extracted actions. The actions list contains all the actions from
* the <b>NEW</b> STM to the head</li>
* <li>3. Set preparation head to STM's parent, so STM will be excluded</li>
* <li>4. Append each action (one step is created by action) after the new preparation head</li>
* </ul>
*/
public void updateAction(final String preparationId, final String stepToModifyId, final AppendStep newStep) {
checkActionStepConsistency(newStep);
LOGGER.debug("Modifying actions in preparation #{}", preparationId);
final PersistentPreparation preparation = lockPreparation(preparationId);
try {
LOGGER.debug("Current head for preparation #{}: {}", preparationId, preparation.getHeadId());
// Get steps from "step to modify" to the head
// throws an exception if stepId is
final List<String> steps = extractSteps(preparation, stepToModifyId);
// not in
// the preparation
LOGGER.debug("Rewriting history for {} steps.", steps.size());
// Extract created columns ids diff info
final PersistentStep stm = getStep(stepToModifyId);
final List<String> originalCreatedColumns = stm.getDiff().getCreatedColumns();
final List<String> updatedCreatedColumns = newStep.getDiff().getCreatedColumns();
final List<String> deletedColumns = //
originalCreatedColumns.stream().filter(//
id -> !updatedCreatedColumns.contains(id)).collect(toList());
final int columnsDiffNumber = updatedCreatedColumns.size() - originalCreatedColumns.size();
final int maxCreatedColumnIdBeforeUpdate = //
!originalCreatedColumns.isEmpty() ? originalCreatedColumns.stream().mapToInt(Integer::parseInt).max().getAsInt() : MAX_VALUE;
// Build list of actions from modified one to the head
final List<AppendStep> actionsSteps = getStepsWithShiftedColumnIds(steps, stepToModifyId, deletedColumns, maxCreatedColumnIdBeforeUpdate, columnsDiffNumber);
actionsSteps.add(0, newStep);
// Rebuild history from modified step
final PersistentStep stepToModify = getStep(stepToModifyId);
replaceHistory(preparation, stepToModify.getParentId(), actionsSteps);
LOGGER.debug("Modified head of preparation #{}: head is now {}", preparationId, preparation.getHeadId());
if (auditService.isActive()) {
//
auditService.auditPreparationUpdateStep(preparationId, stepToModifyId, //
newStep.getActions().stream().collect(toMap(Action::getName, Action::getParameters)));
}
} finally {
unlockPreparation(preparationId);
}
}
use of org.talend.dataprep.preparation.store.PersistentPreparation 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));
}
use of org.talend.dataprep.preparation.store.PersistentPreparation in project data-prep by Talend.
the class PreparationService method update.
/**
* Update a preparation.
*
* @param preparationId the preparation id to update.
* @param preparation the updated preparation.
* @return the updated preparation id.
*/
public String update(String preparationId, final PreparationDTO preparation) {
lockPreparation(preparationId);
try {
final PersistentPreparation previousPreparation = preparationRepository.get(preparationId, PersistentPreparation.class);
LOGGER.debug("Updating preparation with id {}: {}", preparation.getId(), previousPreparation);
PersistentPreparation updated = previousPreparation.merge(preparation);
if (!updated.id().equals(preparationId)) {
preparationRepository.remove(previousPreparation);
}
updated.setAppVersion(versionService.version().getVersionId());
updated.setLastModificationDate(System.currentTimeMillis());
preparationRepository.add(updated);
LOGGER.info("Preparation {} updated -> {}", preparationId, updated);
auditService.auditPreparationRename(preparationId, updated.getName());
return updated.id();
} finally {
unlockPreparation(preparationId);
}
}
use of org.talend.dataprep.preparation.store.PersistentPreparation in project data-prep by Talend.
the class PreparationService method create.
/**
* Create a preparation from the http request body.
*
* @param preparation the preparation to create.
* @param folderId where to store the preparation.
* @return the created preparation id.
*/
public String create(final Preparation preparation, String folderId) {
LOGGER.debug("Create new preparation for data set {} in {}", preparation.getDataSetId(), folderId);
PersistentPreparation toCreate = new PersistentPreparation();
toCreate.setId(UUID.randomUUID().toString());
toCreate.setAppVersion(versionService.version().getVersionId());
toCreate.setHeadId(Step.ROOT_STEP.id());
toCreate.setAuthor(security.getUserId());
toCreate.setName(preparation.getName());
toCreate.setDataSetId(preparation.getDataSetId());
toCreate.setFolderId(folderId);
toCreate.setRowMetadata(preparation.getRowMetadata());
toCreate.setDataSetName(preparation.getDataSetName());
preparationRepository.add(toCreate);
final String id = toCreate.id();
// create associated folderEntry
FolderEntry folderEntry = new FolderEntry(PREPARATION, id);
folderRepository.addFolderEntry(folderEntry, folderId);
auditService.auditPreparationCreation(toCreate.getName(), id, toCreate.getDataSetName(), toCreate.getDataSetId(), folderId);
LOGGER.info("New preparation {} created and stored in {} ", preparation, folderId);
return id;
}
Aggregations