Search in sources :

Example 31 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project rtcab-ordermanagement by mariodavid.

the class OrderEdit method generateInvoice.

public void generateInvoice() {
    Report invoiceReport = reportLoadService.loadReportBySystemcode("order_invoice");
    String fileName = "myFile";
    Map<String, Object> params = new HashMap<>();
    params.put("entity", getItem());
    FileDescriptor invoiceFile = reportService.createAndSaveReport(invoiceReport, params, fileName);
    Document invoiceDocument = metadata.create(Document.class);
    invoiceDocument.setFile(invoiceFile);
    invoiceDocument.setName(fileName);
    invoiceDocument.setType(DocumentType.INVOICE);
    invoiceDocument.setOrder(getItem());
    documentsDs.addItem(invoiceDocument);
}
Also used : Report(com.haulmont.reports.entity.Report) HashMap(java.util.HashMap) Document(com.roadtocubaandbeyond.ordermanagement.entity.Document) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 32 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class EntityListenerTest method testEntityManager.

@Test
public void testEntityManager() throws Exception {
    Server server;
    UUID serverId;
    Transaction tx = cont.persistence().createTransaction();
    try {
        // create
        server = new Server();
        server.setName("server1");
        serverId = server.getId();
        cont.persistence().getEntityManager().persist(server);
        tx.commitRetaining();
        assertNotNull(server.getData());
        UUID relatedId = UUID.fromString(server.getData());
        FileDescriptor related = cont.persistence().getEntityManager().find(FileDescriptor.class, relatedId);
        assertNotNull(related);
        assertEquals("Related", related.getName());
        tx.commitRetaining();
        // update
        server = cont.persistence().getEntityManager().find(Server.class, serverId);
        assertNotNull(server);
        server.setName("server1 updated");
        tx.commitRetaining();
        related = cont.persistence().getEntityManager().find(FileDescriptor.class, relatedId);
        assertNotNull(related);
        assertEquals("Related updated", related.getName());
        tx.commitRetaining();
        // remove
        server = cont.persistence().getEntityManager().find(Server.class, serverId);
        assertNotNull(server);
        cont.persistence().getEntityManager().remove(server);
        tx.commitRetaining();
        related = cont.persistence().getEntityManager().find(FileDescriptor.class, relatedId);
        assertNull(related);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Server(com.haulmont.cuba.core.entity.Server) UUID(java.util.UUID) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) Test(org.junit.jupiter.api.Test)

Example 33 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class FileStorage method findOrphanDescriptors.

@Override
public String findOrphanDescriptors() {
    com.haulmont.cuba.core.app.filestorage.FileStorage fileStorage;
    FileStorageAPI fileStorageAPI = AppBeans.get(FileStorageAPI.class);
    if (fileStorageAPI instanceof com.haulmont.cuba.core.app.filestorage.FileStorage) {
        fileStorage = (com.haulmont.cuba.core.app.filestorage.FileStorage) fileStorageAPI;
    } else {
        return "<not supported>";
    }
    File[] roots = getStorageRoots();
    if (roots.length == 0)
        return "No storage directories defined";
    StringBuilder sb = new StringBuilder();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<FileDescriptor> query = em.createQuery("select fd from sys$FileDescriptor fd", FileDescriptor.class);
        List<FileDescriptor> fileDescriptors = query.getResultList();
        for (FileDescriptor fileDescriptor : fileDescriptors) {
            File dir = fileStorage.getStorageDir(roots[0], fileDescriptor);
            File file = new File(dir, com.haulmont.cuba.core.app.filestorage.FileStorage.getFileName(fileDescriptor));
            if (!file.exists()) {
                sb.append(fileDescriptor.getId()).append(", ").append(fileDescriptor.getName()).append(", ").append(fileDescriptor.getCreateDate()).append("\n");
            }
        }
        tx.commit();
    } catch (Exception e) {
        return ExceptionUtils.getStackTrace(e);
    } finally {
        tx.end();
    }
    return sb.toString();
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileStorageAPI(com.haulmont.cuba.core.app.FileStorageAPI) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) File(java.io.File)

Example 34 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class FileStorage method findOrphanFiles.

@Override
public String findOrphanFiles() {
    FileStorageAPI fileStorageAPI = AppBeans.get(FileStorageAPI.class);
    if (!(fileStorageAPI instanceof com.haulmont.cuba.core.app.filestorage.FileStorage)) {
        return "<not supported>";
    }
    File[] roots = getStorageRoots();
    if (roots.length == 0)
        return "No storage directories defined";
    StringBuilder sb = new StringBuilder();
    File storageFolder = roots[0];
    if (!storageFolder.exists())
        return ExceptionUtils.getStackTrace(new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, storageFolder.getAbsolutePath()));
    Collection<File> systemFiles = FileUtils.listFiles(storageFolder, null, true);
    Collection<File> filesInRootFolder = FileUtils.listFiles(storageFolder, null, false);
    // remove files of root storage folder (e.g. storage.log) from files collection
    systemFiles.removeAll(filesInRootFolder);
    List<FileDescriptor> fileDescriptors;
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<FileDescriptor> query = em.createQuery("select fd from sys$FileDescriptor fd", FileDescriptor.class);
        fileDescriptors = query.getResultList();
        tx.commit();
    } catch (Exception e) {
        return ExceptionUtils.getStackTrace(e);
    } finally {
        tx.end();
    }
    Set<String> descriptorsFileNames = new HashSet<>();
    for (FileDescriptor fileDescriptor : fileDescriptors) {
        descriptorsFileNames.add(com.haulmont.cuba.core.app.filestorage.FileStorage.getFileName(fileDescriptor));
    }
    for (File file : systemFiles) {
        if (!descriptorsFileNames.contains(file.getName()))
            // Encode file path if it contains non-ASCII characters
            if (!file.getPath().matches("\\p{ASCII}+")) {
                String encodedFilePath = URLEncodeUtils.encodeUtf8(file.getPath());
                sb.append(encodedFilePath).append("\n");
            } else {
                sb.append(file.getPath()).append("\n");
            }
    }
    return sb.toString();
}
Also used : FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileStorageAPI(com.haulmont.cuba.core.app.FileStorageAPI) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) File(java.io.File) HashSet(java.util.HashSet)

Example 35 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class DesktopFileUploadField method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    this.datasource = datasource;
    if (datasource == null) {
        setValue(null);
        return;
    }
    MetaClass metaClass = datasource.getMetaClass();
    resolveMetaPropertyPath(metaClass, property);
    itemChangeListener = e -> {
        if (updatingInstance)
            return;
        FileDescriptor descriptor = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
        updateComponent(descriptor);
        fireChangeListeners(descriptor);
    };
    // noinspection unchecked
    datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
    itemPropertyChangeListener = e -> {
        if (updatingInstance)
            return;
        if (e.getProperty().equals(metaPropertyPath.toString())) {
            updateComponent((FileDescriptor) e.getValue());
            fireChangeListeners(e.getValue());
        }
    };
    // noinspection unchecked
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    initRequired(metaPropertyPath);
    if ((datasource.getState() == Datasource.State.VALID) && (datasource.getItem() != null)) {
        Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
        FileDescriptor fileDescriptor = (FileDescriptor) newValue;
        updateComponent(fileDescriptor);
        fireChangeListeners(newValue);
    }
    if (metaProperty.isReadOnly()) {
        setEditable(false);
    }
    handleFilteredAttributes(this, this.datasource, metaPropertyPath);
    securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
    // noinspection unchecked
    this.datasource.addItemChangeListener(new WeakItemChangeListener(this.datasource, securityItemChangeListener));
    initBeanValidator();
}
Also used : WeakItemChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemChangeListener) MetaClass(com.haulmont.chile.core.model.MetaClass) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Aggregations

FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)47 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)12 UUID (java.util.UUID)9 EntityManager (com.haulmont.cuba.core.EntityManager)5 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)5 UserSession (com.haulmont.cuba.security.global.UserSession)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Transaction (com.haulmont.cuba.core.Transaction)3 ExportDisplay (com.haulmont.cuba.gui.export.ExportDisplay)3 FileDataProvider (com.haulmont.cuba.gui.export.FileDataProvider)3 RestAPIException (com.haulmont.restapi.exception.RestAPIException)3 File (java.io.File)3 FileStorageAPI (com.haulmont.cuba.core.app.FileStorageAPI)2 FileStorageService (com.haulmont.cuba.core.app.FileStorageService)2 LoadContext (com.haulmont.cuba.core.global.LoadContext)2 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)2 TaskLifeCycle (com.haulmont.cuba.gui.executors.TaskLifeCycle)2 ByteArrayDataProvider (com.haulmont.cuba.gui.export.ByteArrayDataProvider)2