Search in sources :

Example 1 with Folder

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

the class FoldersServiceBean method loadAppFolders.

@Override
public List<AppFolder> loadAppFolders() {
    log.debug("Loading AppFolders");
    StopWatch stopWatch = new Slf4JStopWatch("AppFolders");
    stopWatch.start();
    List<AppFolder> resultList;
    try (Transaction tx = persistence.createTransaction()) {
        String metaClassName = metadata.getExtendedEntities().getEffectiveMetaClass(AppFolder.class).getName();
        TypedQuery<AppFolder> q = persistence.getEntityManager().createQuery("select f from " + metaClassName + " f order by f.sortOrder, f.name", AppFolder.class);
        resultList = q.getResultList();
        // fetch parent folder
        resultList.forEach(Folder::getParent);
        tx.commit();
    } finally {
        stopWatch.stop();
    }
    if (CollectionUtils.isNotEmpty(resultList)) {
        Binding binding = new Binding();
        binding.setVariable("persistence", persistence);
        binding.setVariable("metadata", metadata);
        binding.setVariable("userSession", userSessionSource.getUserSession());
        Iterator<AppFolder> iterator = resultList.iterator();
        while (iterator.hasNext()) {
            AppFolder folder = iterator.next();
            try (Transaction tx = persistence.createTransaction()) {
                boolean evaluatedVisibilityScript = true;
                try {
                    if (!StringUtils.isBlank(folder.getVisibilityScript())) {
                        binding.setVariable("folder", folder);
                        Boolean visible = runScript(folder.getVisibilityScript(), binding);
                        if (BooleanUtils.isFalse(visible)) {
                            iterator.remove();
                            continue;
                        }
                    }
                } catch (Exception e) {
                    log.warn("Unable to evaluate AppFolder visibility script for folder: id: {}  name: {}", folder.getId(), folder.getName(), e);
                    // because EclipseLink Query marks transaction as rollback-only on JPQL syntax errors
                    evaluatedVisibilityScript = false;
                }
                boolean evaluatedQuantityScript = loadFolderQuantity(binding, folder);
                if (evaluatedVisibilityScript && evaluatedQuantityScript) {
                    tx.commit();
                }
            }
        }
    }
    return resultList;
}
Also used : Binding(groovy.lang.Binding) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) AppFolder(com.haulmont.cuba.core.entity.AppFolder) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) Folder(com.haulmont.cuba.core.entity.Folder) IOException(java.io.IOException) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) StopWatch(org.perf4j.StopWatch) AppFolder(com.haulmont.cuba.core.entity.AppFolder) Transaction(com.haulmont.cuba.core.Transaction)

Example 2 with Folder

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

the class MetadataTest method testPersistentAndTransientProperties.

@Test
public void testPersistentAndTransientProperties() throws Exception {
    MetadataTools tools = cont.metadata().getTools();
    // User
    MetaClass metaClass = cont.metadata().getSession().getClassNN(User.class);
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
    assertTrue(tools.isPersistent(metaClass, metaClass.getPropertyNN("id")));
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("createTs")));
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("login")));
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("group")));
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("userRoles")));
    // EntityLogItem
    metaClass = cont.metadata().getSession().getClassNN(EntityLogItem.class);
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("user")));
    assertFalse(tools.isPersistent(metaClass.getPropertyNN("attributes")));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("attributes")));
    // Folder
    metaClass = cont.metadata().getSession().getClassNN(Folder.class);
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("name")));
    assertTrue(tools.isNotPersistent(new Folder(), "itemStyle"));
    // UserSessionEntity
    metaClass = cont.metadata().getSession().getClassNN(UserSessionEntity.class);
    // see JavaDocs on isPersistent() for the reason
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
    assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("id")));
    assertTrue(tools.isNotPersistent(metadata.create(metaClass), "id"));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("login")));
    assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("login")));
    // TestTransientEntity
    metaClass = cont.metadata().getSession().getClassNN(TestNotPersistentEntity.class);
    // see JavaDocs on isPersistent() for the reason
    assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
    assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("id")));
    assertTrue(tools.isNotPersistent(metadata.create(metaClass), "id"));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("name")));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("info")));
    assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("embeddedRef")));
    assertFalse(tools.isEmbedded(metaClass.getPropertyNN("embeddedRef")));
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) MetaClass(com.haulmont.chile.core.model.MetaClass) Folder(com.haulmont.cuba.core.entity.Folder) TestNotPersistentEntity(com.haulmont.cuba.testmodel.TestNotPersistentEntity) Test(org.junit.Test)

Example 3 with Folder

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

the class FolderEditWindow method commit.

protected void commit() {
    SearchFolder folder = (SearchFolder) FolderEditWindow.this.folder;
    if (StringUtils.trimToNull(nameField.getValue()) == null) {
        String msg = messages.getMainMessage("folders.folderEditWindow.emptyName");
        App.getInstance().getWindowManager().showNotification(msg, Frame.NotificationType.TRAY);
        return;
    }
    folder.setName(nameField.getValue());
    folder.setTabName(tabNameField.getValue());
    if (sortOrderField.getValue() == null || "".equals(sortOrderField.getValue())) {
        folder.setSortOrder(null);
    } else {
        String value = sortOrderField.getValue();
        int sortOrder;
        try {
            sortOrder = Integer.parseInt(value);
        } catch (NumberFormatException e) {
            String msg = messages.getMainMessage("folders.folderEditWindow.invalidSortOrder");
            App.getInstance().getWindowManager().showNotification(msg, Frame.NotificationType.WARNING);
            return;
        }
        folder.setSortOrder(sortOrder);
    }
    Object parent = parentSelect.getValue();
    if (parent instanceof Folder)
        folder.setParent((Folder) parent);
    else
        folder.setParent(null);
    folder.setApplyDefault(Boolean.valueOf(applyDefaultCb.getValue().toString()));
    if (globalCb != null) {
        if (BooleanUtils.isTrue(globalCb.getValue())) {
            folder.setUser(null);
        } else {
            folder.setUser(userSessionSource.getUserSession().getCurrentOrSubstitutedUser());
        }
    } else {
        folder.setUser(userSessionSource.getUserSession().getCurrentOrSubstitutedUser());
    }
    if (presentation != null) {
        folder.setPresentation((Presentation) presentation.getValue());
    }
    FolderEditWindow.this.commitHandler.run();
    close();
}
Also used : AbstractSearchFolder(com.haulmont.cuba.core.entity.AbstractSearchFolder) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) AbstractSearchFolder(com.haulmont.cuba.core.entity.AbstractSearchFolder) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) Folder(com.haulmont.cuba.core.entity.Folder)

Example 4 with Folder

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

the class FoldersServiceBean method importFolder.

@Override
public Folder importFolder(Folder parentFolder, byte[] bytes) throws IOException {
    if (!security.isEntityOpPermitted(Folder.class, EntityOp.CREATE)) {
        throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
    }
    Folder folder = null;
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    ZipArchiveInputStream archiveReader;
    archiveReader = new ZipArchiveInputStream(byteArrayInputStream);
    ZipArchiveEntry archiveEntry;
    while (((archiveEntry = archiveReader.getNextZipEntry()) != null) && (folder == null)) {
        if (archiveEntry.getName().equals("folder.xml")) {
            String xml = new String(IOUtils.toByteArray(archiveReader), StandardCharsets.UTF_8);
            folder = (Folder) createXStream().fromXML(xml);
        }
    }
    byteArrayInputStream.close();
    if (folder != null) {
        checkImportPermissions(folder);
        folder.setParent(parentFolder);
        Transaction tx = persistence.createTransaction();
        try {
            EntityManager em = persistence.getEntityManager();
            em.setSoftDeletion(false);
            Folder existingFolder = em.find(Folder.class, folder.getId());
            if (existingFolder != null) {
                checkImportPermissions(existingFolder);
                folder.setVersion(existingFolder.getVersion());
                folder.setCreateTs(existingFolder.getCreateTs());
                folder.setCreatedBy(existingFolder.getCreatedBy());
            } else {
                User user = userSessionSource.getUserSession().getUser();
                folder.setCreatedBy(user.getLoginLowerCase());
                folder.setCreateTs(timeSource.currentTimestamp());
                folder.setUpdatedBy(null);
                folder.setUpdateTs(null);
                folder.setVersion(0);
            }
            em.merge(folder);
            tx.commit();
        } finally {
            tx.end();
        }
    }
    return folder;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) AppFolder(com.haulmont.cuba.core.entity.AppFolder) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) Folder(com.haulmont.cuba.core.entity.Folder)

Example 5 with Folder

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

the class FoldersServiceBean method checkImportPermissions.

protected void checkImportPermissions(Folder folder) {
    UserSession userSession = userSessionSource.getUserSession();
    if (folder instanceof SearchFolder) {
        SearchFolder searchFolder = (SearchFolder) folder;
        User currentUser = userSession.getCurrentOrSubstitutedUser();
        if (searchFolder.getUser() != null && !currentUser.equals(searchFolder.getUser())) {
            throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
        }
        if (searchFolder.getUser() == null && !userSession.isSpecificPermitted("cuba.gui.searchFolder.global")) {
            throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
        }
    }
    if (folder instanceof AppFolder) {
        if (!userSession.isSpecificPermitted("cuba.gui.appFolder.global")) {
            throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
        }
    }
}
Also used : AppFolder(com.haulmont.cuba.core.entity.AppFolder) User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) AppFolder(com.haulmont.cuba.core.entity.AppFolder) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) Folder(com.haulmont.cuba.core.entity.Folder)

Aggregations

Folder (com.haulmont.cuba.core.entity.Folder)7 AppFolder (com.haulmont.cuba.core.entity.AppFolder)5 SearchFolder (com.haulmont.cuba.security.entity.SearchFolder)5 Transaction (com.haulmont.cuba.core.Transaction)2 AbstractSearchFolder (com.haulmont.cuba.core.entity.AbstractSearchFolder)2 User (com.haulmont.cuba.security.entity.User)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 EntityManager (com.haulmont.cuba.core.EntityManager)1 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)1 UserSession (com.haulmont.cuba.security.global.UserSession)1 TestNotPersistentEntity (com.haulmont.cuba.testmodel.TestNotPersistentEntity)1 Binding (groovy.lang.Binding)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)1 Test (org.junit.Test)1 StopWatch (org.perf4j.StopWatch)1 Slf4JStopWatch (org.perf4j.slf4j.Slf4JStopWatch)1