Search in sources :

Example 21 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class LoadSubstitutionsTest method testQuerySubstitutions.

@Test
public void testQuerySubstitutions() throws Exception {
    ViewRepository viewRepository = AppBeans.get(ViewRepository.NAME);
    View userView = new View(new View.ViewParams().src(viewRepository.getView(User.class, View.LOCAL)));
    View substitutedUserView = new View(User.class);
    substitutedUserView.addProperty("login");
    View substitutionsView = new View(UserSubstitution.class);
    substitutionsView.addProperty("substitutedUser", substitutedUserView);
    substitutionsView.addProperty("startDate");
    userView.addProperty("substitutions", substitutionsView);
    User loadedUser;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        loadedUser = em.find(User.class, user.getId(), userView);
        tx.commit();
    }
    assertNotNull(loadedUser);
    assertNotNull(loadedUser.getSubstitutions());
    Assert.assertEquals(1, loadedUser.getSubstitutions().size());
    UserSubstitution loadedSubstitution = loadedUser.getSubstitutions().iterator().next();
    assertEquals(user, loadedSubstitution.getUser());
    assertEquals(substitutedUser, loadedSubstitution.getSubstitutedUser());
}
Also used : ViewRepository(com.haulmont.cuba.core.global.ViewRepository) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) View(com.haulmont.cuba.core.global.View)

Example 22 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class SystemInfoWindow method generateInsert.

public void generateInsert() {
    scriptArea.setEditable(true);
    if (item instanceof Role) {
        View localView = metadata.getViewRepository().getView(Role.class, View.LOCAL);
        View roleView = new View(localView, Role.class, "role-export-view", true).addProperty("permissions", metadata.getViewRepository().getView(Permission.class, View.LOCAL));
        item = getDsContext().getDataSupplier().reload(item, roleView);
        StringBuilder result = new StringBuilder();
        result.append(sqlGenerationService.generateInsertScript(item)).append("\n");
        for (Permission permission : ((Role) item).getPermissions()) {
            result.append(sqlGenerationService.generateInsertScript(permission)).append("\n");
        }
        scriptArea.setValue(result.toString());
    } else {
        scriptArea.setValue(sqlGenerationService.generateInsertScript(item));
    }
    showScriptArea();
}
Also used : Role(com.haulmont.cuba.security.entity.Role) Permission(com.haulmont.cuba.security.entity.Permission) View(com.haulmont.cuba.core.global.View)

Example 23 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class PropertyDatasourceImpl method getView.

@Override
public View getView() {
    if (view == null) {
        MetaClass metaMetaClass = masterDs.getMetaClass();
        if (metadata.getTools().isPersistent(metaMetaClass) || metadata.getTools().isEmbeddable(metaMetaClass)) {
            View masterView = masterDs.getView();
            if (masterView == null) {
                throw new DevelopmentException("No view for datasource " + masterDs.getId(), ParamsMap.of("masterDs", masterDs.getId(), "propertyDs", getId()));
            }
            ViewProperty property = masterView.getProperty(metaProperty.getName());
            if (property == null) {
                return null;
            }
            if (property.getView() == null) {
                throw new DevelopmentException(String.format("Invalid view definition: %s. Property '%s' must have a view", masterView, property), ParamsMap.of("masterDs", masterDs.getId(), "propertyDs", getId(), "masterView", masterView, "property", property));
            }
            view = metadata.getViewRepository().findView(getMetaClass(), property.getView().getName());
            // anonymous (nameless) view
            if (view == null)
                view = property.getView();
        }
    }
    return view;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) ViewProperty(com.haulmont.cuba.core.global.ViewProperty) View(com.haulmont.cuba.core.global.View) DevelopmentException(com.haulmont.cuba.core.global.DevelopmentException)

Example 24 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class RuntimePropsDatasourceImpl method initMetaClass.

protected void initMetaClass(Entity entity) {
    if (!(entity instanceof BaseGenericIdEntity)) {
        throw new IllegalStateException("This datasource can contain only entity with subclass of BaseGenericIdEntity");
    }
    BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity;
    if (PersistenceHelper.isNew(baseGenericIdEntity) && baseGenericIdEntity.getDynamicAttributes() == null) {
        baseGenericIdEntity.setDynamicAttributes(new HashMap<>());
    }
    @SuppressWarnings("unchecked") Map<String, CategoryAttributeValue> dynamicAttributes = baseGenericIdEntity.getDynamicAttributes();
    Preconditions.checkNotNullArgument(dynamicAttributes, "Dynamic attributes should be loaded explicitly");
    if (baseGenericIdEntity instanceof Categorized) {
        category = ((Categorized) baseGenericIdEntity).getCategory();
    }
    if (!initializedBefore && category == null) {
        category = getDefaultCategory();
        if (baseGenericIdEntity.getMetaClass().getProperty("category") != null) {
            baseGenericIdEntity.setValue("category", category);
        }
    }
    item = new DynamicAttributesEntity(baseGenericIdEntity, attributes);
    if (PersistenceHelper.isNew(entity) || categoryChanged) {
        dynamicAttributesGuiTools.initDefaultAttributeValues(baseGenericIdEntity, resolveCategorizedEntityClass());
    }
    view = new View(DynamicAttributesEntity.class, false);
    Collection<MetaProperty> properties = metaClass.getProperties();
    for (MetaProperty property : properties) {
        view.addProperty(property.getName());
    }
    valid();
    initializedBefore = true;
    fireItemChanged(null);
}
Also used : DynamicAttributesMetaProperty(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty) MetaProperty(com.haulmont.chile.core.model.MetaProperty) View(com.haulmont.cuba.core.global.View)

Example 25 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class ViewRepositoryInfo method dump.

protected void dump(ViewProperty viewProperty, StringBuilder content, String indent) {
    content.append("\n");
    content.append(indent);
    content.append("- ").append(viewProperty.getName());
    View innerView = viewProperty.getView();
    if (innerView != null) {
        if (StringUtils.isNotEmpty(innerView.getName())) {
            content.append(" -> ").append(metadata.getSession().getClass(innerView.getEntityClass()).getName()).append("/").append(innerView.getName());
        } else {
            for (ViewProperty innerProperty : innerView.getProperties()) {
                dump(innerProperty, content, "  " + indent);
            }
        }
    }
}
Also used : ViewProperty(com.haulmont.cuba.core.global.ViewProperty) View(com.haulmont.cuba.core.global.View)

Aggregations

View (com.haulmont.cuba.core.global.View)55 Test (org.junit.Test)24 EntityManager (com.haulmont.cuba.core.EntityManager)22 Transaction (com.haulmont.cuba.core.Transaction)21 User (com.haulmont.cuba.security.entity.User)19 Group (com.haulmont.cuba.security.entity.Group)10 MetaClass (com.haulmont.chile.core.model.MetaClass)5 ViewProperty (com.haulmont.cuba.core.global.ViewProperty)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 Entity (com.haulmont.cuba.core.entity.Entity)2 ViewRepository (com.haulmont.cuba.core.global.ViewRepository)2 Role (com.haulmont.cuba.security.entity.Role)2 SoftDeleteOneToOneB (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneB)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)1 QueryResult (com.haulmont.cuba.core.entity.QueryResult)1 Server (com.haulmont.cuba.core.entity.Server)1