use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class OriginalEntityLoadInfo method create.
/**
* Create a new info instance.
* @param entity entity instance
* @return info instance
*/
public static OriginalEntityLoadInfo create(Entity entity) {
Objects.requireNonNull(entity, "entity is null");
Metadata metadata = AppBeans.get(Metadata.class);
MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
ExtendedEntities extendedEntities = AppBeans.get(ExtendedEntities.class);
MetaClass metaClass = metadata.getSession().getClass(entity.getClass());
MetaClass originalMetaClass = extendedEntities.getOriginalMetaClass(metaClass);
if (originalMetaClass != null) {
metaClass = originalMetaClass;
}
MetaProperty primaryKeyProperty = metadataTools.getPrimaryKeyProperty(metaClass);
boolean stringKey = primaryKeyProperty != null && primaryKeyProperty.getJavaType().equals(String.class);
return new OriginalEntityLoadInfo((UUID) EntityValues.getId(entity), metaClass, stringKey);
}
use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class ContainerValueSource method setApplicationContext.
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
MetaClass metaClass = container.getEntityMetaClass();
MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
this.metaPropertyPath = metadataTools.resolveMetaPropertyPath(metaClass, property);
this.container.addItemChangeListener(this::containerItemChanged);
this.container.addItemPropertyChangeListener(this::containerItemPropertyChanged);
@SuppressWarnings("unchecked") InstanceContainer<?> parentCont = container;
for (int i = 1; i < this.metaPropertyPath.length(); i++) {
MetaPropertyPath intermediatePath = new MetaPropertyPath(this.metaPropertyPath.getMetaClass(), Arrays.copyOf(this.metaPropertyPath.getMetaProperties(), i));
String pathToTarget = Joiner.on('.').join(Arrays.copyOfRange(this.metaPropertyPath.getPath(), i, this.metaPropertyPath.length()));
DataComponents dataComponents = applicationContext.getBean(DataComponents.class);
@SuppressWarnings("unchecked") InstanceContainer<Object> propertyCont = dataComponents.createInstanceContainer(intermediatePath.getRangeJavaClass());
parentCont.addItemChangeListener(event -> {
if (event.getItem() != null) {
setState(BindingState.ACTIVE);
} else {
setState(BindingState.INACTIVE);
}
propertyCont.setItem(event.getItem() != null ? EntityValues.getValueEx(event.getItem(), intermediatePath.getMetaProperty().getName()) : null);
});
parentCont.addItemPropertyChangeListener(event -> {
if (Objects.equals(event.getProperty(), intermediatePath.getMetaProperty().getName())) {
Object entity = event.getValue();
Object prevEntity = event.getPrevValue();
propertyCont.setItem(entity);
V prevValue = prevEntity != null ? EntityValues.getValueEx(prevEntity, pathToTarget) : null;
V value = entity != null ? EntityValues.getValueEx(entity, pathToTarget) : null;
events.publish(ValueChangeEvent.class, new ValueChangeEvent<>(this, prevValue, value));
}
});
if (i == this.metaPropertyPath.length() - 1) {
propertyCont.addItemPropertyChangeListener(event -> {
if (Objects.equals(event.getProperty(), this.metaPropertyPath.getMetaProperty().getName())) {
events.publish(ValueChangeEvent.class, new ValueChangeEvent<>(this, (V) event.getPrevValue(), (V) event.getValue()));
}
});
}
parentCont = propertyCont;
}
}
use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class AbstractEditor method postCommit.
/**
* Hook to be implemented in subclasses. Called by the framework after committing datasources.
* The default implementation notifies about commit and calls {@link #postInit()} if the window is not closing.
*
* @param committed whether any data were actually changed and committed
* @param close whether the window is going to be closed
* @return true to continue, false to abort
*/
protected boolean postCommit(boolean committed, boolean close) {
if (committed && !close) {
if (showSaveNotification) {
Entity entity = getItem();
MetadataTools metadataTools = getApplicationContext().getBean(MetadataTools.class);
showNotification(messages.formatMessage("", "info.EntitySave", messageTools.getEntityCaption(metadata.getClass(entity)), metadataTools.getInstanceName(entity)), NotificationType.TRAY);
}
postInit();
afterWindowApplyPostInit();
}
return true;
}
use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class FilterConditionUtils method getPropertyLocCaption.
public static String getPropertyLocCaption(MetaClass metaClass, String propertyPath) {
MessageTools messageTools = AppBeans.get(MessageTools.class);
MetaPropertyPath mpp = metaClass.getPropertyPath(propertyPath);
if (mpp == null) {
return propertyPath;
} else {
MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
MetaProperty[] metaProperties = mpp.getMetaProperties();
StringBuilder sb = new StringBuilder();
MetaPropertyPath parentMpp = null;
MetaClass tempMetaClass;
for (int i = 0; i < metaProperties.length; i++) {
if (i == 0) {
parentMpp = new MetaPropertyPath(metaClass, metaProperties[i]);
tempMetaClass = metaClass;
} else {
parentMpp = new MetaPropertyPath(parentMpp, metaProperties[i]);
tempMetaClass = metadataTools.getPropertyEnclosingMetaClass(parentMpp);
}
sb.append(messageTools.getPropertyCaption(tempMetaClass, metaProperties[i].getName()));
if (i < metaProperties.length - 1) {
sb.append(".");
}
}
return sb.toString();
}
}
use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class PropertyCondition method updateText.
@Override
protected void updateText() {
Metadata metadata = AppBeans.get(Metadata.class);
MetadataTools metadataTools = metadata.getTools();
String nameToUse = !Strings.isNullOrEmpty(propertiesPath) ? propertiesPath : name;
boolean useCrossDataStoreRefId = false;
boolean stringType = false;
String thisStore = metaClass.getStore().getName();
MetaPropertyPath propertyPath = metaClass.getPropertyPath(name);
if (propertyPath != null) {
MetaProperty metaProperty = propertyPath.getMetaProperty();
String refIdProperty = metadataTools.getCrossDataStoreReferenceIdProperty(thisStore, propertyPath.getMetaProperty());
if (refIdProperty != null) {
useCrossDataStoreRefId = true;
int lastdDot = nameToUse.lastIndexOf('.');
if (lastdDot == -1) {
nameToUse = refIdProperty;
} else {
nameToUse = nameToUse.substring(0, lastdDot + 1) + refIdProperty;
}
}
stringType = String.class.equals(metaProperty.getJavaType());
}
if (operator == Op.DATE_INTERVAL) {
text = dateIntervalConditionToJpql(nameToUse);
return;
}
StringBuilder sb = new StringBuilder();
StringBuilder sbJoin = new StringBuilder();
if (operator == Op.NOT_IN) {
sb.append("((");
}
String path = (metaClass instanceof KeyValueMetaClass && Strings.isNullOrEmpty(propertiesPath)) ? entityAlias : entityAlias + "." + nameToUse;
String joinAlias = nameToUse.replace(".", "_") + RandomStringUtils.randomAlphabetic(5);
if (Param.Type.ENTITY == param.getType() && !useCrossDataStoreRefId) {
String primaryKeyName = metadataTools.getPrimaryKeyName(metadata.getClassNN(param.getJavaClass()));
if (operator == Op.NOT_IN) {
sbJoin.append("left join ").append(path).append(" ").append(joinAlias);
sb.append(joinAlias).append(".").append(primaryKeyName);
} else {
sb.append(path).append(".").append(primaryKeyName);
}
} else {
sb.append(path);
}
if (operator != Op.NOT_EMPTY) {
PersistenceManagerClient persistenceManager = AppBeans.get(PersistenceManagerClient.class);
if (operator == Op.EQUAL && stringType) /*&& persistenceManager.emulateEqualsByLike(thisStore)*/
{
sb.append(" ").append(Op.CONTAINS.forJpql());
} else if (operator == Op.NOT_EQUAL && stringType) /*&& persistenceManager.emulateEqualsByLike(thisStore)*/
{
sb.append(" ").append(Op.DOES_NOT_CONTAIN.forJpql());
} else {
sb.append(" ").append(operator.forJpql());
}
}
if (!operator.isUnary()) {
sb.append(" :").append(param.getName());
if (operator == Op.ENDS_WITH || operator == Op.STARTS_WITH || operator == Op.CONTAINS || operator == Op.DOES_NOT_CONTAIN) {
CubaProperties properties = AppBeans.get(CubaProperties.class);
if (properties.getDisableEscapingLikeForDataStores() == null || !properties.getDisableEscapingLikeForDataStores().contains(thisStore)) {
sb.append(" ESCAPE '").append(QueryUtils.ESCAPE_CHARACTER).append("' ");
}
}
if (operator == Op.NOT_IN) {
if (Param.Type.ENTITY == param.getType() && !useCrossDataStoreRefId) {
String primaryKeyName = metadataTools.getPrimaryKeyName(metadata.getClassNN(param.getJavaClass()));
sb.append(") or (").append(joinAlias).append(".").append(primaryKeyName).append(" is null)) ");
} else {
sb.append(") or (").append(path).append(" is null)) ");
}
}
}
if (operator == Op.NOT_EMPTY) {
sb.append(BooleanUtils.isTrue((Boolean) param.getValue()) ? " is not null" : " is null");
}
text = sb.toString();
join = sbJoin.toString();
}
Aggregations