Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Filter component should allow filtering by references to entities fro…
Browse files Browse the repository at this point in the history
…m other data stores #636
  • Loading branch information
Flaurite committed Sep 20, 2021
1 parent 4c15f14 commit 4c9d47b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
7 changes: 5 additions & 2 deletions ui-data/src/main/java/io/jmix/uidata/UiDataConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.jmix.uidata;

import io.jmix.core.AccessManager;
import io.jmix.core.Metadata;
import io.jmix.core.MetadataTools;
import io.jmix.core.annotation.JmixModule;
import io.jmix.core.impl.scanning.AnnotationScanMetadataReaderFactory;
Expand Down Expand Up @@ -111,8 +112,10 @@ public FilterSupport filterSupport() {
public FilterMetadataTools filterMetadataTools(MetadataTools metadataTools,
UiComponentProperties uiComponentProperties,
AccessManager accessManager,
QueryTransformerFactory queryTransformerFactory) {
return new UiDataFilterMetadataTools(metadataTools, uiComponentProperties, accessManager, queryTransformerFactory);
QueryTransformerFactory queryTransformerFactory,
Metadata metadata) {
return new UiDataFilterMetadataTools(metadataTools, uiComponentProperties, accessManager,
queryTransformerFactory, metadata);
}

@Bean("ui_RelativeDateTimeMomentProvider")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package io.jmix.uidata.filter;

import com.google.common.base.Strings;
import io.jmix.core.AccessManager;
import io.jmix.core.Metadata;
import io.jmix.core.MetadataTools;
import io.jmix.core.annotation.Internal;
import io.jmix.core.metamodel.model.MetaClass;
Expand All @@ -41,13 +43,18 @@ public class UiDataFilterMetadataTools extends FilterMetadataTools {
public UiDataFilterMetadataTools(MetadataTools metadataTools,
UiComponentProperties uiComponentProperties,
AccessManager accessManager,
QueryTransformerFactory queryTransformerFactory) {
super(metadataTools, uiComponentProperties, accessManager);
QueryTransformerFactory queryTransformerFactory,
Metadata metadata) {
super(metadataTools, uiComponentProperties, accessManager, metadata);
this.queryTransformerFactory = queryTransformerFactory;
}

@Override
protected boolean isAggregateFunction(MetaPropertyPath propertyPath, String query) {
if (Strings.isNullOrEmpty(query)) {
return false;
}

MetaClass filterMetaClass = propertyPath.getMetaClass();
int index = new ArrayList<>(filterMetaClass.getProperties()).indexOf(propertyPath.getMetaProperty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.base.Strings;
import io.jmix.core.AccessManager;
import io.jmix.core.Metadata;
import io.jmix.core.MetadataTools;
import io.jmix.core.annotation.Internal;
import io.jmix.core.impl.keyvalue.KeyValueMetaClass;
Expand All @@ -39,16 +40,19 @@
public class FilterMetadataTools {

protected MetadataTools metadataTools;
protected Metadata metadata;
protected UiComponentProperties uiComponentProperties;
protected AccessManager accessManager;

@Autowired
public FilterMetadataTools(MetadataTools metadataTools,
UiComponentProperties uiComponentProperties,
AccessManager accessManager) {
AccessManager accessManager,
Metadata metadata) {
this.metadataTools = metadataTools;
this.uiComponentProperties = uiComponentProperties;
this.accessManager = accessManager;
this.metadata = metadata;
}

public List<MetaPropertyPath> getPropertyPaths(MetaClass filterMetaClass,
Expand Down Expand Up @@ -97,10 +101,15 @@ protected List<MetaPropertyPath> getPropertyPaths(MetaClass filterMetaClass,
protected boolean isMetaPropertyPathAllowed(MetaPropertyPath propertyPath, String query) {
UiEntityAttributeContext context = new UiEntityAttributeContext(propertyPath);
accessManager.applyRegisteredConstraints(context);

return context.canView()
&& !metadataTools.isSystemLevel(propertyPath.getMetaProperty())
&& (metadataTools.isJpa(propertyPath)
|| (propertyPath.getMetaClass() instanceof KeyValueMetaClass && !isAggregateFunction(propertyPath, query)))
&& ((metadataTools.isJpa(propertyPath)
|| (propertyPath.getMetaClass() instanceof KeyValueMetaClass
&& !isAggregateFunction(propertyPath, query)
&& isKeyValueCrossDataStoreReferenceAllowed(propertyPath, query)))
|| (isCrossDataStoreReference(propertyPath.getMetaProperty())
&& !(propertyPath.getMetaClass() instanceof KeyValueMetaClass)))
&& !propertyPath.getMetaProperty().getRange().getCardinality().isMany()
&& !(byte[].class.equals(propertyPath.getMetaProperty().getJavaType()));
}
Expand All @@ -109,4 +118,37 @@ protected boolean isMetaPropertyPathAllowed(MetaPropertyPath propertyPath, Strin
protected boolean isAggregateFunction(MetaPropertyPath propertyPath, String query) {
return false;
}

protected boolean isCrossDataStoreReference(MetaProperty metaProperty) {
return metadataTools.getCrossDataStoreReferenceIdProperty(
metaProperty.getDomain().getStore().getName(),
metaProperty) != null;
}

protected boolean isKeyValueCrossDataStoreReferenceAllowed(MetaPropertyPath propertyPath, String query) {
MetaClass filterMetaClass = propertyPath.getMetaClass();
if (!(filterMetaClass instanceof KeyValueMetaClass) || Strings.isNullOrEmpty(query)) {
return true;
}

MetaClass domainMetaClass = propertyPath.getMetaProperty().getDomain();
MetaClass propertyMetaClass = propertyPath.getMetaProperty().getRange().isClass()
? propertyPath.getMetaProperty().getRange().asClass()
: null;

if (!domainMetaClass.equals(filterMetaClass)) {
return propertyMetaClass == null
|| domainMetaClass.getStore().getName().equals(propertyMetaClass.getStore().getName());
} else if (propertyMetaClass != null) {
String entityName = query.substring(query.indexOf("from") + 4)
.trim()
.split(" ")[0];

MetaClass mainFromMetaClass = metadata.getClass(entityName);
return mainFromMetaClass.getStore().getName().equals(propertyMetaClass.getStore().getName())
|| propertyMetaClass instanceof KeyValueMetaClass;
} else {
return true;
}
}
}

0 comments on commit 4c9d47b

Please sign in to comment.