use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class RelatedEntitiesServiceBean method getRelatedIds.
@SuppressWarnings("unchecked")
@Override
public List<Object> getRelatedIds(List<Object> parentIds, String parentMetaClass, String relationProperty) {
checkNotNullArgument(parentIds, "parents argument is null");
checkNotNullArgument(parentMetaClass, "parentMetaClass argument is null");
checkNotNullArgument(relationProperty, "relationProperty argument is null");
MetaClass metaClass = extendedEntities.getEffectiveMetaClass(metadata.getClassNN(parentMetaClass));
Class parentClass = metaClass.getJavaClass();
MetaProperty metaProperty = metaClass.getPropertyNN(relationProperty);
// return empty list only after all argument checks
if (parentIds.isEmpty()) {
return Collections.emptyList();
}
MetaClass propertyMetaClass = extendedEntities.getEffectiveMetaClass(metaProperty.getRange().asClass());
Class propertyClass = propertyMetaClass.getJavaClass();
List<Object> relatedIds = new ArrayList<>();
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
String parentPrimaryKey = metadata.getTools().getPrimaryKeyName(metaClass);
String queryString = "select x from " + parentMetaClass + " x where x." + parentPrimaryKey + " in :ids";
Query query = em.createQuery(queryString);
String relatedPrimaryKey = metadata.getTools().getPrimaryKeyName(propertyMetaClass);
View view = new View(parentClass);
view.addProperty(relationProperty, new View(propertyClass).addProperty(relatedPrimaryKey));
query.setView(view);
query.setParameter("ids", parentIds);
List<Entity> resultList = query.getResultList();
for (Entity e : resultList) {
Object value = e.getValue(relationProperty);
if (value instanceof Entity) {
relatedIds.add(((Entity) value).getId());
} else if (value instanceof Collection) {
for (Object collectionItem : (Collection) value) {
relatedIds.add(((Entity) collectionItem).getId());
}
}
}
tx.commit();
} finally {
tx.end();
}
return relatedIds;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class EntityInspectorBrowse method createEntityImportView.
protected EntityImportView createEntityImportView(MetaClass metaClass) {
@SuppressWarnings("unchecked") Class<? extends Entity> javaClass = metaClass.getJavaClass();
EntityImportView entityImportView = new EntityImportView(javaClass);
for (MetaProperty metaProperty : metaClass.getProperties()) {
if (!metadata.getTools().isPersistent(metaProperty))
continue;
switch(metaProperty.getType()) {
case DATATYPE:
case ENUM:
entityImportView.addLocalProperty(metaProperty.getName());
break;
case ASSOCIATION:
case COMPOSITION:
Range.Cardinality cardinality = metaProperty.getRange().getCardinality();
switch(cardinality) {
case MANY_TO_ONE:
entityImportView.addManyToOneProperty(metaProperty.getName(), ReferenceImportBehaviour.IGNORE_MISSING);
break;
case ONE_TO_ONE:
entityImportView.addOneToOneProperty(metaProperty.getName(), ReferenceImportBehaviour.IGNORE_MISSING);
break;
}
break;
default:
throw new IllegalStateException("unknown property type");
}
}
return entityImportView;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class EntityInspectorBrowse method createEntitiesTable.
protected void createEntitiesTable(MetaClass meta) {
if (entitiesTable != null)
tableBox.remove(entitiesTable);
if (filter != null) {
filterBox.remove(filter);
}
entitiesTable = componentsFactory.createComponent(Table.class);
entitiesTable.setFrame(frame);
if (companion != null) {
companion.setHorizontalScrollEnabled(entitiesTable, true);
}
ClientType clientType = AppConfig.getClientType();
if (clientType == ClientType.WEB) {
textSelection.setVisible(true);
textSelection.addValueChangeListener(e -> changeTableTextSelectionEnabled());
}
final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
Formatter dateTimeFormatter = value -> {
if (value == null) {
return StringUtils.EMPTY;
}
return dateTimeFormat.format(value);
};
// collect properties in order to add non-system columns first
List<Table.Column> nonSystemPropertyColumns = new ArrayList<>(10);
List<Table.Column> systemPropertyColumns = new ArrayList<>(10);
for (MetaProperty metaProperty : meta.getProperties()) {
// don't show embedded, transient & multiple referred entities
if (isEmbedded(metaProperty) || metadata.getTools().isNotPersistent(metaProperty))
continue;
Range range = metaProperty.getRange();
if (range.getCardinality().isMany())
continue;
Table.Column column = new Table.Column(meta.getPropertyPath(metaProperty.getName()));
if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
column.setFormatter(dateTimeFormatter);
}
if (metaProperty.getJavaType().equals(String.class)) {
column.setMaxTextLength(MAX_TEXT_LENGTH);
}
if (!metadata.getTools().isSystem(metaProperty)) {
column.setCaption(getPropertyCaption(meta, metaProperty));
nonSystemPropertyColumns.add(column);
} else {
column.setCaption(metaProperty.getName());
systemPropertyColumns.add(column);
}
}
for (Table.Column column : nonSystemPropertyColumns) {
entitiesTable.addColumn(column);
}
for (Table.Column column : systemPropertyColumns) {
entitiesTable.addColumn(column);
}
if (entitiesDs != null) {
((DsContextImplementation) getDsContext()).unregister(entitiesDs);
}
entitiesDs = DsBuilder.create(getDsContext()).setId("entitiesDs").setMetaClass(meta).setView(createView(meta)).buildCollectionDatasource();
entitiesDs.setLoadDynamicAttributes(true);
entitiesDs.setSoftDeletion(!removedRecords.isChecked());
entitiesDs.setQuery(String.format("select e from %s e", meta.getName()));
entitiesTable.setDatasource(entitiesDs);
tableBox.add(entitiesTable);
entitiesTable.setSizeFull();
createButtonsPanel(entitiesTable);
RowsCount rowsCount = componentsFactory.createComponent(RowsCount.class);
rowsCount.setDatasource(entitiesDs);
entitiesTable.setRowsCount(rowsCount);
entitiesTable.setEnterPressAction(entitiesTable.getAction("edit"));
entitiesTable.setItemClickAction(entitiesTable.getAction("edit"));
entitiesTable.setMultiSelect(true);
createFilter();
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class EntityInspectorBrowse method createView.
protected View createView(MetaClass meta) {
// noinspection unchecked
View view = new View(meta.getJavaClass(), false);
for (MetaProperty metaProperty : meta.getProperties()) {
switch(metaProperty.getType()) {
case DATATYPE:
case ENUM:
view.addProperty(metaProperty.getName());
break;
case ASSOCIATION:
case COMPOSITION:
if (!metaProperty.getRange().getCardinality().isMany()) {
View minimal = metadata.getViewRepository().getView(metaProperty.getRange().asClass(), View.MINIMAL);
View propView = new View(minimal, metaProperty.getName() + "Ds", false);
view.addProperty(metaProperty.getName(), propView);
}
break;
default:
throw new IllegalStateException("unknown property type");
}
}
return view;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class EntityInspectorEditor method createNestedEmbeddedDatasources.
protected void createNestedEmbeddedDatasources(MetaClass metaClass, String fqnPrefix, Datasource masterDs) {
for (MetaProperty metaProperty : metaClass.getProperties()) {
if (MetaProperty.Type.ASSOCIATION == metaProperty.getType() || MetaProperty.Type.COMPOSITION == metaProperty.getType()) {
if (isEmbedded(metaProperty)) {
String fqn = fqnPrefix + "." + metaProperty.getName();
MetaClass propertyMetaClass = metaProperty.getRange().asClass();
NestedDatasource propertyDs = new EmbeddedDatasourceImpl();
propertyDs.setup(fqn + "Ds", masterDs, metaProperty.getName());
createNestedEmbeddedDatasources(propertyMetaClass, fqn, propertyDs);
datasources.put(fqn, propertyDs);
dsContext.register(propertyDs);
}
}
}
}
Aggregations