use of org.talend.dataprep.api.user.UserData in project data-prep by Talend.
the class DataSetConversions method doWith.
@Override
public BeanConversionService doWith(BeanConversionService conversionService, String beanName, ApplicationContext applicationContext) {
conversionService.register(//
fromBean(DataSetMetadata.class).toBeans(//
UserDataSetMetadata.class).using(UserDataSetMetadata.class, (dataSetMetadata, userDataSetMetadata) -> {
final Security security = applicationContext.getBean(Security.class);
final UserDataRepository userDataRepository = applicationContext.getBean(UserDataRepository.class);
String userId = security.getUserId();
// update the dataset favorites
final UserData userData = userDataRepository.get(userId);
if (userData != null) {
userDataSetMetadata.setFavorite(userData.getFavoritesDatasets().contains(dataSetMetadata.getId()));
}
// and the owner (if not already present).
if (userDataSetMetadata.getOwner() == null) {
userDataSetMetadata.setOwner(new Owner(userId, security.getUserDisplayName(), StringUtils.EMPTY));
}
return userDataSetMetadata;
}).build());
return conversionService;
}
use of org.talend.dataprep.api.user.UserData in project data-prep by Talend.
the class DataSetService method list.
@RequestMapping(value = "/datasets", method = RequestMethod.GET)
@ApiOperation(value = "List all data sets and filters on certified, or favorite or a limited number when asked", notes = "Returns the list of data sets (and filters) the current user is allowed to see. Creation date is a Epoch time value (in UTC time zone).")
@Timed
public Stream<UserDataSetMetadata> list(@ApiParam(value = "Sort key (by name, creation or modification date)") @RequestParam(defaultValue = "creationDate") Sort sort, @ApiParam(value = "Order for sort key (desc or asc or modif)") @RequestParam(defaultValue = "desc") Order order, @ApiParam(value = "Filter on name containing the specified name") @RequestParam(required = false) String name, @ApiParam(value = "Filter on name containing the specified name strictness") @RequestParam(defaultValue = "false") boolean nameStrict, @ApiParam(value = "Filter on certified data sets") @RequestParam(defaultValue = "false") boolean certified, @ApiParam(value = "Filter on favorite data sets") @RequestParam(defaultValue = "false") boolean favorite, @ApiParam(value = "Only return a limited number of data sets") @RequestParam(defaultValue = "false") boolean limit) {
// Build filter for data sets
String userId = security.getUserId();
final UserData userData = userDataRepository.get(userId);
final List<String> predicates = new ArrayList<>();
predicates.add("lifecycle.importing = false");
if (favorite) {
if (userData != null && !userData.getFavoritesDatasets().isEmpty()) {
predicates.add("id in [" + userData.getFavoritesDatasets().stream().map(ds -> '\'' + ds + '\'').collect(Collectors.joining(",")) + "]");
} else {
// Wants favorites but user has no favorite
return Stream.empty();
}
}
if (certified) {
predicates.add("governance.certificationStep = '" + Certification.CERTIFIED + "'");
}
if (StringUtils.isNotEmpty(name)) {
final String regex = "(?i)" + Pattern.quote(name);
final String filter;
if (nameStrict) {
filter = "name ~ '^" + regex + "$'";
} else {
filter = "name ~ '.*" + regex + ".*'";
}
predicates.add(filter);
}
final String tqlFilter = predicates.stream().collect(Collectors.joining(" and "));
LOG.debug("TQL Filter in use: {}", tqlFilter);
// Get all data sets according to filter
try (Stream<DataSetMetadata> stream = dataSetMetadataRepository.list(tqlFilter, sort, order)) {
Stream<UserDataSetMetadata> userDataSetMetadataStream = stream.map(m -> conversionService.convert(m, UserDataSetMetadata.class));
if (sort == Sort.AUTHOR || sort == Sort.NAME) {
// As theses are not well handled by mongo repository
userDataSetMetadataStream = userDataSetMetadataStream.sorted(getDataSetMetadataComparator(sort, order));
}
return userDataSetMetadataStream.limit(limit ? datasetListLimit : Long.MAX_VALUE);
}
}
use of org.talend.dataprep.api.user.UserData in project data-prep by Talend.
the class DataSetService method favorites.
/**
* list all the favorites dataset for the current user
*
* @return a list of the dataset Ids of all the favorites dataset for the current user or an empty list if none
* found
*/
@RequestMapping(value = "/datasets/favorites", method = RequestMethod.GET)
@ApiOperation(value = "return all favorites datasets of the current user", notes = "Returns the list of favorites datasets.")
@Timed
public Iterable<String> favorites() {
String userId = security.getUserId();
UserData userData = userDataRepository.get(userId);
return userData != null ? userData.getFavoritesDatasets() : emptyList();
}
use of org.talend.dataprep.api.user.UserData in project data-prep by Talend.
the class DataSetService method setFavorites.
/**
* update the current user data dataset favorites list by adding or removing the dataSetId according to the unset
* flag. The user data for the current will be created if it does not exist. If no data set exists for given id, a
* {@link TDPException} is thrown.
*
* @param unset, if true this will remove the dataSetId from the list of favorites, if false then it adds the
* dataSetId to the favorite list
* @param dataSetId, the id of the favorites data set. If the data set does not exists nothing is done.
*/
@RequestMapping(value = "/datasets/{id}/favorite", method = PUT)
@ApiOperation(value = "set or unset a dataset as favorite", notes = "Specify if a dataset is or is not a favorite for the current user.")
@Timed
public void setFavorites(@RequestParam(defaultValue = "false") @ApiParam(name = "unset", value = "if true then unset the dataset as favorite, if false (default value) set the favorite flag") boolean unset, @PathVariable(value = "id") @ApiParam(name = "id", value = "Id of the favorite data set, do nothing is the id does not exist.") String dataSetId) {
String userId = security.getUserId();
// check that dataset exists
DataSetMetadata dataSetMetadata = dataSetMetadataRepository.get(dataSetId);
if (dataSetMetadata != null) {
// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
LOG.debug("{} favorite dataset for #{} for user {}", unset ? "Unset" : "Set", dataSetId, userId);
UserData userData = userDataRepository.get(userId);
if (unset) {
// unset the favorites
if (userData != null) {
userData.getFavoritesDatasets().remove(dataSetId);
userDataRepository.save(userData);
}
// no user data for this user so nothing to unset
} else {
// set the favorites
if (userData == null) {
// let's create a new UserData
userData = new UserData(userId, versionService.version().getVersionId());
}
// else already created so just update it.
userData.addFavoriteDataset(dataSetId);
userDataRepository.save(userData);
}
} else {
// no dataset found so throws an error
throw new TDPException(DataSetErrorCodes.DATASET_DOES_NOT_EXIST, build().put("id", dataSetId));
}
}
use of org.talend.dataprep.api.user.UserData in project data-prep by Talend.
the class DataSetServiceTest method getMetadata.
@Test
public void getMetadata() throws Exception {
DataSetMetadataBuilder builder = metadataBuilder.metadata().id("1234");
builder.row(//
ColumnMetadata.Builder.column().id(//
1234).name(//
"id").empty(//
0).invalid(//
0).valid(//
0).type(//
Type.STRING)).created(//
0).name(//
"name").author(//
"author").footerSize(//
0).headerSize(//
1).qualityAnalyzed(//
true).schemaAnalyzed(//
true).formatFamilyId(//
new CSVFormatFamily().getBeanId()).mediaType("text/csv");
DataSetMetadata metadata = builder.build();
metadata.getContent().addParameter(CSVFormatFamily.SEPARATOR_PARAMETER, ";");
dataSetMetadataRepository.save(metadata);
String contentAsString = when().get("/datasets/{id}/metadata", "1234").asString();
InputStream expected = this.getClass().getResourceAsStream("../metadata1.json");
assertThat(contentAsString, sameJSONAsFile(expected));
Boolean isFavorites = from(contentAsString).get("metadata.favorite");
assertFalse(isFavorites);
// add favorite
UserData userData = new UserData(security.getUserId(), versionService.version().getVersionId());
HashSet<String> favorites = new HashSet<>();
favorites.add("1234");
userData.setFavoritesDatasets(favorites);
userDataRepository.save(userData);
contentAsString = when().get("/datasets/{id}/metadata", "1234").asString();
isFavorites = from(contentAsString).get("metadata.favorite");
assertTrue(isFavorites);
}
Aggregations