Search in sources :

Example 1 with FileRefDatatype

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;
}
Also used : AbstractMap(java.util.AbstractMap) MultipartFile(org.springframework.web.multipart.MultipartFile) FileRefDatatype(io.jmix.core.metamodel.datatype.impl.FileRefDatatype) FileRef(io.jmix.core.FileRef) CoercingParseValueException(graphql.schema.CoercingParseValueException) FileNotFoundException(java.io.FileNotFoundException) FileStorage(io.jmix.core.FileStorage) IOException(java.io.IOException) CoercingSerializeException(graphql.schema.CoercingSerializeException) IOException(java.io.IOException) CoercingParseValueException(graphql.schema.CoercingParseValueException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with FileRefDatatype

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);
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) FileRefDatatype(io.jmix.core.metamodel.datatype.impl.FileRefDatatype) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

FileRefDatatype (io.jmix.core.metamodel.datatype.impl.FileRefDatatype)2 CoercingParseValueException (graphql.schema.CoercingParseValueException)1 CoercingSerializeException (graphql.schema.CoercingSerializeException)1 FileRef (io.jmix.core.FileRef)1 FileStorage (io.jmix.core.FileStorage)1 MetaClass (io.jmix.core.metamodel.model.MetaClass)1 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1