use of io.jmix.core.metamodel.datatype.impl.FileRefDatatype in project jmix by jmix-framework.
the class FileRefCoercing method parseValue.
@Override
public Object parseValue(Object o) throws CoercingParseValueException {
String storageName = null;
try {
if (o instanceof AbstractMap.SimpleEntry) {
storageName = ((AbstractMap.SimpleEntry<String, MultipartFile>) o).getKey();
FileStorage fileStorage = fileService.getFileStorage(storageName);
MultipartFile multipartFile = ((AbstractMap.SimpleEntry<String, MultipartFile>) o).getValue();
FileRefDatatype refDatatype = new FileRefDatatype();
FileRef fileRef = fileService.saveFileIntoStorage(multipartFile, fileStorage);
return refDatatype.format(fileRef);
}
if (o instanceof String) {
FileRef fileRef = FileRef.fromString((String) o);
FileStorage fileStorage = fileService.getFileStorage(fileRef.getStorageName());
if (!fileStorage.fileExists(fileRef)) {
throw new FileNotFoundException();
}
return o;
}
} catch (FileNotFoundException e) {
throw new CoercingParseValueException("File with name " + FileRef.fromString((String) o).getFileName() + " not found");
} catch (IOException e) {
throw new CoercingParseValueException("Exception with saving file");
} catch (Exception e) {
throw new CoercingParseValueException("File storage with name " + storageName + " not found");
}
return null;
}
use of io.jmix.core.metamodel.datatype.impl.FileRefDatatype in project jmix by jmix-framework.
the class SearchResultsScreen method formatFieldCaption.
protected String formatFieldCaption(String entityName, String fieldName) {
List<String> captionParts = new ArrayList<>();
String[] parts = fieldName.split("\\.");
MetaClass currentMetaClass = metadata.getClass(entityName);
for (int i = 0; i < parts.length; i++) {
String part = parts[i];
MetaProperty currentMetaProperty = currentMetaClass.findProperty(part);
if (currentMetaProperty == null) {
break;
}
if (currentMetaProperty.getRange().isDatatype() && (Datatype<?>) currentMetaProperty.getRange().asDatatype() instanceof FileRefDatatype && i + 1 < parts.length) {
String propertyCaption = messageTools.getPropertyCaption(currentMetaProperty);
String nextPart = parts[i + 1];
String labelKey = systemFieldLabels.get(nextPart);
if (labelKey != null) {
String labelValue = messages.getMessage(SearchResultsScreen.class, labelKey);
propertyCaption = propertyCaption + "[" + labelValue + "]";
}
captionParts.add(propertyCaption);
} else {
captionParts.add(messageTools.getPropertyCaption(currentMetaProperty));
if (currentMetaProperty.getRange().isClass()) {
currentMetaClass = currentMetaProperty.getRange().asClass();
} else {
break;
}
}
}
return Joiner.on(".").join(captionParts);
}
Aggregations