Search in sources :

Example 1 with UiTablePresentation

use of io.jmix.uidata.entity.UiTablePresentation in project jmix by jmix-framework.

the class TablePresentationsImpl method checkLoad.

protected void checkLoad() {
    if (presentations == null) {
        LoadContext<UiTablePresentation> ctx = new LoadContext<>(metadata.getClass(UiTablePresentation.class));
        ctx.setFetchPlan(fetchPlanRepository.getFetchPlan(UiTablePresentation.class, "app"));
        UserDetails user = currentUserSubstitution.getEffectiveUser();
        ctx.setQueryString("select p from ui_TablePresentation p " + "where p.componentId = :component and (p.username is null or p.username = :username)").setParameter("component", name).setParameter("username", user.getUsername());
        final List<UiTablePresentation> list = dataManager.loadList(ctx);
        presentations = new LinkedHashMap<>(list.size());
        for (final TablePresentation p : list) {
            presentations.put(EntityValues.<UUID>getId(p), p);
        }
    }
}
Also used : TablePresentation(io.jmix.ui.presentation.model.TablePresentation) UiTablePresentation(io.jmix.uidata.entity.UiTablePresentation) UserDetails(org.springframework.security.core.userdetails.UserDetails) UiTablePresentation(io.jmix.uidata.entity.UiTablePresentation)

Example 2 with UiTablePresentation

use of io.jmix.uidata.entity.UiTablePresentation in project jmix by jmix-framework.

the class UserSettingServiceImpl method onUserRemove.

@Transactional
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, fallbackExecution = true)
private void onUserRemove(UserRemovedEvent event) {
    String username = event.getUsername();
    List<UiSetting> settings = entityManager.createQuery("select s from ui_Setting s where s.username = ?1", UiSetting.class).setParameter(1, username).getResultList();
    for (UiSetting setting : settings) {
        entityManager.remove(setting);
    }
    List<UiTablePresentation> presentations = entityManager.createQuery("select p from ui_TablePresentation p where p.username = ?1", UiTablePresentation.class).setParameter(1, username).getResultList();
    for (UiTablePresentation presentation : presentations) {
        entityManager.remove(presentation);
    }
}
Also used : UiTablePresentation(io.jmix.uidata.entity.UiTablePresentation) UiSetting(io.jmix.uidata.entity.UiSetting) TransactionalEventListener(org.springframework.transaction.event.TransactionalEventListener) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UiTablePresentation

use of io.jmix.uidata.entity.UiTablePresentation in project jmix by jmix-framework.

the class UserSettingServiceImpl method copyPresentations.

protected Map<UUID, UiTablePresentation> copyPresentations(UserDetails fromUser, UserDetails toUser) {
    Map<UUID, UiTablePresentation> resultMap = transaction.execute(status -> {
        // delete existing
        Query delete = entityManager.createQuery("delete from ui_TablePresentation p where p.username = ?1");
        delete.setParameter(1, toUser.getUsername());
        delete.executeUpdate();
        // copy settings
        TypedQuery<UiTablePresentation> selectQuery = entityManager.createQuery("select p from ui_TablePresentation p where p.username = ?1", UiTablePresentation.class);
        selectQuery.setParameter(1, fromUser.getUsername());
        List<UiTablePresentation> presentations = selectQuery.getResultList();
        Map<UUID, UiTablePresentation> presentationMap = new HashMap<>();
        for (UiTablePresentation presentation : presentations) {
            UiTablePresentation newPresentation = metadata.create(UiTablePresentation.class);
            newPresentation.setUsername(toUser.getUsername());
            newPresentation.setComponentId(presentation.getComponentId());
            newPresentation.setAutoSave(presentation.getAutoSave());
            newPresentation.setName(presentation.getName());
            newPresentation.setSettings(presentation.getSettings());
            presentationMap.put(presentation.getId(), newPresentation);
            entityManager.persist(newPresentation);
        }
        return presentationMap;
    });
    return resultMap == null ? Collections.emptyMap() : resultMap;
}
Also used : TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) UiTablePresentation(io.jmix.uidata.entity.UiTablePresentation)

Example 4 with UiTablePresentation

use of io.jmix.uidata.entity.UiTablePresentation in project jmix by jmix-framework.

the class UserSettingServiceBean method copySettings.

@Override
public void copySettings(UserDetails fromUser, UserDetails toUser) {
    Preconditions.checkNotNullArgument(fromUser);
    Preconditions.checkNotNullArgument(toUser);
    MetaClass metaClass = metadata.getClass(UiSetting.class);
    CrudEntityContext entityContext = new CrudEntityContext(metaClass);
    accessManager.applyRegisteredConstraints(entityContext);
    if (!entityContext.isCreatePermitted()) {
        throw new AccessDeniedException("entity", metaClass.getName());
    }
    transaction.executeWithoutResult(status -> {
        Query deleteSettingsQuery = entityManager.createQuery("delete from ui_Setting s where s.username = ?1");
        deleteSettingsQuery.setParameter(1, toUser.getUsername());
        deleteSettingsQuery.executeUpdate();
    });
    Map<UUID, UiTablePresentation> presentationsMap = copyPresentations(fromUser, toUser);
    copyUserFolders(fromUser, toUser, presentationsMap);
    Map<UUID, FilterEntity> filtersMap = copyFilters(fromUser, toUser);
    transaction.executeWithoutResult(status -> {
        TypedQuery<UiSetting> q = entityManager.createQuery("select s from ui_Setting s where s.username = ?1", UiSetting.class);
        q.setParameter(1, fromUser.getUsername());
        List<UiSetting> fromUserSettings = q.getResultList();
        for (UiSetting currSetting : fromUserSettings) {
            UiSetting newSetting = metadata.create(UiSetting.class);
            newSetting.setUsername(toUser.getUsername());
            newSetting.setName(currSetting.getName());
            try {
                Document doc = dom4JTools.readDocument(currSetting.getValue());
                List<Element> components = doc.getRootElement().element("components").elements("component");
                for (Element component : components) {
                    Attribute presentationAttr = component.attribute("presentation");
                    if (presentationAttr != null) {
                        UUID presentationId = UuidProvider.fromString(presentationAttr.getValue());
                        UiTablePresentation newPresentation = presentationsMap.get(presentationId);
                        if (newPresentation != null) {
                            presentationAttr.setValue(newPresentation.getId().toString());
                        }
                    }
                    Element defaultFilterEl = component.element("defaultFilter");
                    if (defaultFilterEl != null) {
                        Attribute idAttr = defaultFilterEl.attribute("id");
                        if (idAttr != null) {
                            UUID filterId = UuidProvider.fromString(idAttr.getValue());
                            FilterEntity newFilter = filtersMap.get(filterId);
                            if (newFilter != null) {
                                idAttr.setValue(newFilter.getId().toString());
                            }
                        }
                    }
                }
                newSetting.setValue(dom4JTools.writeDocument(doc, true));
            } catch (Exception e) {
                newSetting.setValue(currSetting.getValue());
            }
            entityManager.persist(newSetting);
        }
    });
}
Also used : AccessDeniedException(io.jmix.core.security.AccessDeniedException) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Document(org.dom4j.Document) AccessDeniedException(io.jmix.core.security.AccessDeniedException) MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) UiTablePresentation(io.jmix.uidata.entity.UiTablePresentation) UiSetting(io.jmix.uidata.entity.UiSetting) UUID(java.util.UUID)

Example 5 with UiTablePresentation

use of io.jmix.uidata.entity.UiTablePresentation in project jmix by jmix-framework.

the class UserSettingServiceImpl method copySettings.

@Override
public void copySettings(UserDetails fromUser, UserDetails toUser) {
    Preconditions.checkNotNullArgument(fromUser);
    Preconditions.checkNotNullArgument(toUser);
    MetaClass metaClass = metadata.getClass(UiSetting.class);
    CrudEntityContext entityContext = new CrudEntityContext(metaClass);
    accessManager.applyRegisteredConstraints(entityContext);
    if (!entityContext.isCreatePermitted()) {
        throw new AccessDeniedException("entity", metaClass.getName());
    }
    transaction.executeWithoutResult(status -> {
        Query deleteSettingsQuery = entityManager.createQuery("delete from ui_Setting s where s.username = ?1");
        deleteSettingsQuery.setParameter(1, toUser.getUsername());
        deleteSettingsQuery.executeUpdate();
    });
    Map<UUID, UiTablePresentation> presentationsMap = copyPresentations(fromUser, toUser);
    transaction.executeWithoutResult(status -> {
        TypedQuery<UiSetting> q = entityManager.createQuery("select s from ui_Setting s where s.username = ?1", UiSetting.class);
        q.setParameter(1, fromUser.getUsername());
        List<UiSetting> fromUserSettings = q.getResultList();
        for (UiSetting currSetting : fromUserSettings) {
            UiSetting newSetting = metadata.create(UiSetting.class);
            newSetting.setUsername(toUser.getUsername());
            newSetting.setName(currSetting.getName());
            try {
                Document doc = dom4JTools.readDocument(currSetting.getValue());
                List<Element> components = doc.getRootElement().element("components").elements("component");
                for (Element component : components) {
                    Attribute presentationAttr = component.attribute("presentation");
                    if (presentationAttr != null) {
                        UUID presentationId = UuidProvider.fromString(presentationAttr.getValue());
                        UiTablePresentation newPresentation = presentationsMap.get(presentationId);
                        if (newPresentation != null) {
                            presentationAttr.setValue(newPresentation.getId().toString());
                        }
                    }
                }
                newSetting.setValue(dom4JTools.writeDocument(doc, true));
            } catch (Exception e) {
                newSetting.setValue(currSetting.getValue());
            }
            entityManager.persist(newSetting);
        }
    });
}
Also used : AccessDeniedException(io.jmix.core.security.AccessDeniedException) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Document(org.dom4j.Document) AccessDeniedException(io.jmix.core.security.AccessDeniedException) MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) UiTablePresentation(io.jmix.uidata.entity.UiTablePresentation) UiSetting(io.jmix.uidata.entity.UiSetting)

Aggregations

UiTablePresentation (io.jmix.uidata.entity.UiTablePresentation)5 UiSetting (io.jmix.uidata.entity.UiSetting)3 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 CrudEntityContext (io.jmix.core.accesscontext.CrudEntityContext)2 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 AccessDeniedException (io.jmix.core.security.AccessDeniedException)2 Attribute (org.dom4j.Attribute)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 FilterEntity (com.haulmont.cuba.security.entity.FilterEntity)1 TablePresentation (io.jmix.ui.presentation.model.TablePresentation)1 UUID (java.util.UUID)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1 Transactional (org.springframework.transaction.annotation.Transactional)1 TransactionalEventListener (org.springframework.transaction.event.TransactionalEventListener)1