public static void setSecurityState(Entity entity, SecurityState securityState) { Preconditions.checkNotNullArgument(entity, "Entity is null"); if (entity instanceof BaseGenericIdEntity) { BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity; baseGenericIdEntity.__securityState = securityState; } else if (entity instanceof EmbeddableEntity) { EmbeddableEntity embeddableEntity = (EmbeddableEntity) entity; embeddableEntity.__securityState = securityState; } else { throw new IllegalArgumentException(String.format("Entity with type [%s] does not support security state", entity.getMetaClass().getName())); } }
protected void checkCompositePrimaryKey(Entity entity) { if (metadata.getTools().hasCompositePrimaryKey(entity.getMetaClass()) && !(entity instanceof HasUuid)) { throw new UnsupportedOperationException(format("Entity %s has no persistent UUID attribute", entity)); } }
@Override protected void loadData(Map<String, Object> params) { if (instance != null && instanceMetaClass != null && instance.getMetaClass() != null) { data.clear(); compileInfo(); } }
protected boolean isPermitted(Entity entity, Predicate<ConstraintData> predicate) { List<ConstraintData> constraints = getConstraints(entity.getMetaClass(), predicate); for (ConstraintData constraint : constraints) { if (!isPermitted(entity, constraint)) { return false; } } return true; }
protected Collection getFilteredIds(Entity entity, String propertyName) { if (entity instanceof BaseGenericIdEntity) { String storeName = metadata.getTools().getStoreName(entity.getMetaClass()); DataStore dataStore = storeFactory.get(storeName); if (dataStore instanceof RdbmsStore) { persistenceSecurity.restoreSecurityState(entity); return Optional.ofNullable(BaseEntityInternalAccess.getFilteredData(entity)) .map(v -> v.get(propertyName)) .orElse(Collections.emptyList()); } } return Collections.emptyList(); }
@Override public void actionPerform(com.haulmont.cuba.gui.components.Component component) { if (component instanceof Component.BelongToFrame && component instanceof ListComponent) { Entity selectedItem = ((ListComponent) component).getSingleSelected(); if (selectedItem != null) { showInfo(selectedItem, selectedItem.getMetaClass(), (Component.BelongToFrame) component); } } }
@Override public void restoreSecurityState(Entity entity) { try { securityTokenManager.readSecurityToken(entity); } catch (SecurityTokenException e) { throw new RowLevelSecurityException( format("Could not restore security state for entity [%s] because security token isn't valid.", entity), entity.getMetaClass().getName()); } }
@Override protected Collection<EntitySnapshot> getEntities(Map<String, Object> params) { if (entity != null) { EntitySnapshotService snapshotService = AppBeans.get(EntitySnapshotService.NAME); snapshots = snapshotService.getSnapshots(entity.getMetaClass(), entity.getId()); return snapshots; } return Collections.emptyList(); }
protected void assertTokenForAttributeAccess(Entity entity) { MetaClass metaClass = metadata.getClassNN(entity.getClass()); if (attributeSecuritySupport.isAttributeAccessEnabled(metaClass)) { throw new RowLevelSecurityException(format("Could not read security token from entity %s, " + "even though there are active attribute access for the entity.", entity), entity.getMetaClass().getName()); } }
protected void checkOperationPermitted(Entity entity, ConstraintOperationType operationType) { if (userSessionSource.getUserSession().hasConstraints() && security.hasConstraints(entity.getMetaClass()) && !security.isPermitted(entity, operationType)) { throw new RowLevelSecurityException( operationType + " is not permitted for entity " + entity, entity.getMetaClass().getName(), operationType); } }
protected void checkRequiredAttributes(Entity entity) { SecurityState securityState = getSecurityState(entity); if (securityState != null && !securityState.getRequiredAttributes().isEmpty()) { for (MetaProperty metaProperty : entity.getMetaClass().getProperties()) { String propertyName = metaProperty.getName(); if (BaseEntityInternalAccess.isRequired(securityState, propertyName) && entity.getValue(propertyName) == null) { throw new RowLevelSecurityException(format("Attribute [%s] is required for entity %s", propertyName, entity), entity.getMetaClass().getName()); } } } }
protected boolean doNotRegister(Entity entity) { if (entity == null) { return true; } if (entity instanceof EntityLogItem) { return true; } if (metadata.getTools().hasCompositePrimaryKey(entity.getMetaClass()) && !(entity instanceof HasUuid)) { return true; } return !isEnabled(); }
/** * Check that all properties of the view are loaded from DB for the passed entity. * * @param entity entity * @param viewName view name * @return false if at least one of properties is not loaded */ @SuppressWarnings("unchecked") public boolean isLoadedWithView(Entity entity, String viewName) { checkNotNullArgument(viewName); return isLoadedWithView(entity, viewRepository.getView(entity.getMetaClass(), viewName)); }
@SuppressWarnings("unchecked") public void listenCategoryChanges(Datasource ds) { ds.addItemPropertyChangeListener(e -> { if ("category".equals(e.getProperty())) { initDefaultAttributeValues((BaseGenericIdEntity) e.getItem(), e.getItem().getMetaClass()); } }); }
public WindowInfo getEditorScreen(Entity entity) { MetaClass metaClass = entity.getMetaClass(); MetaClass originalMetaClass = metadata.getExtendedEntities().getOriginalOrThisMetaClass(metaClass); WindowInfo windowInfo = primaryEditors.get(originalMetaClass.getJavaClass()); if (windowInfo != null) { return windowInfo; } String editorScreenId = getEditorScreenId(metaClass); return getWindowInfo(editorScreenId); }
@Override public ResolvableType getResolvableType() { ExtendedEntities extendedEntities = AppBeans.get(ExtendedEntities.NAME); MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(getEntity().getMetaClass()); return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forClass(metaClass.getJavaClass())); } }
protected Entity reloadInstance(Entity instance) { View reloadView = new View(instance.getMetaClass().getJavaClass(), true); LoadContext loadContext = new LoadContext(instance.getMetaClass()); loadContext.setSoftDeletion(false); loadContext.setId(instance.getId()); loadContext.setView(reloadView); DataSupplier supplier = getDataSupplier(); return supplier.load(loadContext); }
protected MetaClass getMetaClassForLocking(Entity item) { // lock original metaClass, if any, because by convention all the configuration is based on original entities MetaClass metaClass = AppBeans.get(ExtendedEntities.class).getOriginalMetaClass(item.getMetaClass()); if (metaClass == null) { metaClass = getTable().getDatasource().getMetaClass(); } return metaClass; }
protected void fetchDynamicAttributes(Entity entity) { if (entity instanceof BaseGenericIdEntity) { LoadContext<BaseGenericIdEntity> loadContext = new LoadContext<>(entity.getMetaClass()); loadContext.setId(entity.getId()).setLoadDynamicAttributes(true); DataService dataService = AppBeans.get(DataService.NAME, DataService.class); BaseGenericIdEntity reloaded = dataService.load(loadContext); if (reloaded != null) { ((BaseGenericIdEntity) entity).setDynamicAttributes(reloaded.getDynamicAttributes()); } else { ((BaseGenericIdEntity) entity).setDynamicAttributes(new HashMap<>()); } } } }
protected Entity getReference(Entity entity, MetaProperty property) { if (PersistenceHelper.isLoaded(entity, property.getName())) return entity.getValue(property.getName()); else { Query query = entityManager.createQuery( "select e." + property.getName() + " from " + entity.getMetaClass().getName() + " e where e." + primaryKeyName + " = ?1"); query.setParameter(1, entity.getId()); Object refEntity = query.getFirstResult(); return (Entity) refEntity; } }