Search in sources :

Example 6 with UiSetting

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

the class UserSettingServiceImpl method loadSetting.

@Override
public String loadSetting(String name) {
    Preconditions.checkNotNullArgument(name);
    String value = transaction.execute(status -> {
        UiSetting us = findUserSettings(name);
        return us == null ? null : us.getValue();
    });
    return value;
}
Also used : UiSetting(io.jmix.uidata.entity.UiSetting)

Example 7 with UiSetting

use of io.jmix.uidata.entity.UiSetting 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

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