use of io.vertigo.dynamo.domain.metamodel.DtField in project vertigo by KleeGroup.
the class MandatoryPkValidator method checkMultiFieldConstraints.
/**
* NO checkMonoFieldConstraints.
* Can't check that PK was set in a checkMonoFieldConstraints.
* Because it was called for modified fields only, if PK is undefined it will not be checked.
*/
/**
* {@inheritDoc}
*/
@Override
protected void checkMultiFieldConstraints(final E entity, final Set<String> modifiedFieldNameSet, final DtObjectErrors dtObjectErrors) {
final DtDefinition dtDefinition = DtObjectUtil.findDtDefinition(entity);
final DtField idField = dtDefinition.getIdField().get();
final String camelCaseFieldName = getCamelCaseFieldName(idField);
if (!dtObjectErrors.hasError(camelCaseFieldName)) {
if (DtObjectUtil.getId(entity) == null) {
dtObjectErrors.addError(camelCaseFieldName, MessageText.of("Id is mandatory"));
}
}
}
use of io.vertigo.dynamo.domain.metamodel.DtField in project vertigo by KleeGroup.
the class AdvancedTestWebServices method createFilterFunction.
private <C extends DtObject, E extends Entity> Predicate<E> createFilterFunction(final C criteria, final Class<E> resultClass) {
Predicate<E> filter = (o) -> true;
final DtDefinition criteriaDefinition = DtObjectUtil.findDtDefinition(criteria);
final DtDefinition resultDefinition = DtObjectUtil.findDtDefinition(resultClass);
final Set<String> alreadyAddedField = new HashSet<>();
for (final DtField field : criteriaDefinition.getFields()) {
final String fieldName = field.getName();
if (!alreadyAddedField.contains(fieldName)) {
// when we consume two fields at once (min;max)
final Object value = field.getDataAccessor().getValue(criteria);
if (value != null) {
if (fieldName.endsWith("_MIN") || fieldName.endsWith("_MAX")) {
final String filteredField = fieldName.substring(0, fieldName.length() - "_MIN".length());
final DtField resultDtField = resultDefinition.getField(filteredField);
final DtField minField = fieldName.endsWith("_MIN") ? field : criteriaDefinition.getField(filteredField + "_MIN");
final DtField maxField = fieldName.endsWith("_MAX") ? field : criteriaDefinition.getField(filteredField + "_MAX");
final Serializable minValue = (Serializable) minField.getDataAccessor().getValue(criteria);
final Serializable maxValue = (Serializable) maxField.getDataAccessor().getValue(criteria);
filter = filter.and(Criterions.isBetween(() -> resultDtField.getName(), CriterionLimit.ofIncluded(minValue), CriterionLimit.ofExcluded(maxValue)).toPredicate());
} else {
final Predicate predicate = Criterions.isEqualTo(() -> fieldName, Serializable.class.cast(value)).toPredicate();
filter.and(predicate);
}
}
}
// si null, alors on ne filtre pas
}
return filter;
}
use of io.vertigo.dynamo.domain.metamodel.DtField in project vertigo by KleeGroup.
the class RamLuceneIndex method addAll.
/**
* Add element to index.
* @param fullDtc Full Dtc to index
* @param storeValue if data are store in index
* @throws IOException Indexation error
*/
public void addAll(final DtList<D> fullDtc, final boolean storeValue) throws IOException {
Assertion.checkNotNull(fullDtc);
// -----
try (final IndexWriter indexWriter = createIndexWriter()) {
final DtField idField = fullDtc.getDefinition().getIdField().get();
final Collection<DtField> dtFields = fullDtc.getDefinition().getFields();
for (final D dto : fullDtc) {
final Document document = new Document();
final Object pkValue = idField.getDataAccessor().getValue(dto);
Assertion.checkNotNull(pkValue, "Indexed DtObject must have a not null primary key. {0}.{1} was null.", fullDtc.getDefinition().getName(), idField.getName());
final String indexedPkValue = String.valueOf(pkValue);
addKeyword(document, idField.getName(), indexedPkValue, true);
for (final DtField dtField : dtFields) {
final Object value = dtField.getDataAccessor().getValue(dto);
if (value != null && !dtField.equals(idField)) {
if (value instanceof String) {
final String valueAsString = getStringValue(dto, dtField);
addIndexed(document, dtField.getName(), valueAsString, storeValue);
} else if (value instanceof Date) {
final String valueAsString = DateTools.dateToString((Date) value, DateTools.Resolution.DAY);
addKeyword(document, dtField.getName(), valueAsString, storeValue);
} else {
addKeyword(document, dtField.getName(), value.toString(), storeValue);
}
}
}
indexWriter.addDocument(document);
mapDocument(indexedPkValue, dto);
}
}
}
use of io.vertigo.dynamo.domain.metamodel.DtField in project vertigo by KleeGroup.
the class RamLuceneIndex method translateDocs.
private DtList<D> translateDocs(final IndexSearcher searcher, final TopDocs topDocs, final int skip, final int top) throws IOException {
final DtField idField = dtDefinition.getIdField().get();
final DtList<D> dtcResult = new DtList<>(dtDefinition);
final int resultLength = topDocs.scoreDocs.length;
if (resultLength > skip) {
for (int i = skip; i < Math.min(skip + top, resultLength); i++) {
final ScoreDoc scoreDoc = topDocs.scoreDocs[i];
final Document document = searcher.doc(scoreDoc.doc);
dtcResult.add(getDtObjectIndexed(document.get(idField.getName())));
}
}
return dtcResult;
}
use of io.vertigo.dynamo.domain.metamodel.DtField in project vertigo by KleeGroup.
the class RamLuceneIndex method getStringValue.
private static String getStringValue(final DtObject dto, final DtField field) {
final String stringValue;
final Object value = field.getDataAccessor().getValue(dto);
if (value != null) {
if (field.getType() == DtField.FieldType.FOREIGN_KEY && getStoreManager().getMasterDataConfig().containsMasterData(field.getFkDtDefinition())) {
// TODO voir pour mise en cache de cette navigation
final DtListURIForMasterData mdlUri = getStoreManager().getMasterDataConfig().getDtListURIForMasterData(field.getFkDtDefinition());
final DtField displayField = mdlUri.getDtDefinition().getDisplayField().get();
final URI<Entity> uri = new URI<>(field.getFkDtDefinition(), value);
final DtObject fkDto = getStoreManager().getDataStore().readOne(uri);
final Object displayValue = displayField.getDataAccessor().getValue(fkDto);
stringValue = displayField.getDomain().valueToString(displayValue);
} else {
stringValue = String.valueOf(field.getDataAccessor().getValue(dto));
}
return stringValue.trim();
}
return null;
}
Aggregations