use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.
the class AggregatableDelegate method doAggregation.
protected Map<AggregationInfo, String> doAggregation(Collection<K> itemIds, AggregationInfo[] aggregationInfos) {
final Map<AggregationInfo, String> aggregationResults = new HashMap<>();
for (AggregationInfo aggregationInfo : aggregationInfos) {
final Object value = doPropertyAggregation(aggregationInfo, itemIds);
String formattedValue;
if (aggregationInfo.getFormatter() != null) {
// noinspection unchecked
formattedValue = aggregationInfo.getFormatter().format(value);
} else {
MetaPropertyPath propertyPath = aggregationInfo.getPropertyPath();
final Range range = propertyPath.getRange();
if (range.isDatatype()) {
if (aggregationInfo.getType() != AggregationInfo.Type.COUNT) {
Class resultClass;
if (aggregationInfo.getStrategy() == null) {
Class rangeJavaClass = propertyPath.getRangeJavaClass();
Aggregation aggregation = Aggregations.get(rangeJavaClass);
resultClass = aggregation.getResultClass();
} else {
resultClass = aggregationInfo.getStrategy().getResultClass();
}
UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
Locale locale = userSessionSource.getLocale();
formattedValue = Datatypes.getNN(resultClass).format(value, locale);
} else {
formattedValue = value.toString();
}
} else {
if (aggregationInfo.getStrategy() != null) {
Class resultClass = aggregationInfo.getStrategy().getResultClass();
UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
Locale locale = userSessionSource.getLocale();
formattedValue = Datatypes.getNN(resultClass).format(value, locale);
} else {
formattedValue = value.toString();
}
}
}
aggregationResults.put(aggregationInfo, formattedValue);
}
return aggregationResults;
}
use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.
the class EntityDiffManager method getPropertyDifference.
/**
* Return difference between property values
*
* @param firstValue First value
* @param secondValue Second value
* @param metaProperty Meta Property
* @param viewProperty View property
* @param diffBranch Branch with passed diffs
* @return Diff
*/
protected EntityPropertyDiff getPropertyDifference(Object firstValue, Object secondValue, MetaProperty metaProperty, ViewProperty viewProperty, Stack<Object> diffBranch) {
EntityPropertyDiff propertyDiff = null;
Range range = metaProperty.getRange();
if (range.isDatatype() || range.isEnum()) {
if (!Objects.equals(firstValue, secondValue)) {
propertyDiff = new EntityBasicPropertyDiff(firstValue, secondValue, metaProperty);
}
} else if (range.getCardinality().isMany()) {
propertyDiff = getCollectionDiff(firstValue, secondValue, viewProperty, metaProperty, diffBranch);
} else if (range.isClass()) {
propertyDiff = getClassDiff(firstValue, secondValue, viewProperty, metaProperty, diffBranch);
}
return propertyDiff;
}
use of com.haulmont.chile.core.model.Range 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.Range in project cuba by cuba-platform.
the class EntityRestore method buildLayout.
protected void buildLayout() {
Object value = entities.getValue();
if (value != null) {
MetaClass metaClass = (MetaClass) value;
MetaProperty deleteTsMetaProperty = metaClass.getProperty("deleteTs");
if (deleteTsMetaProperty != null) {
if (entitiesTable != null) {
tablePanel.remove(entitiesTable);
}
if (filter != null) {
tablePanel.remove(filter);
}
ComponentsFactory componentsFactory = AppConfig.getFactory();
entitiesTable = componentsFactory.createComponent(Table.class);
entitiesTable.setFrame(frame);
restoreButton = componentsFactory.createComponent(Button.class);
restoreButton.setId("restore");
restoreButton.setCaption(getMessage("entityRestore.restore"));
ButtonsPanel buttonsPanel = componentsFactory.createComponent(ButtonsPanel.class);
buttonsPanel.add(restoreButton);
entitiesTable.setButtonsPanel(buttonsPanel);
RowsCount rowsCount = componentsFactory.createComponent(RowsCount.class);
entitiesTable.setRowsCount(rowsCount);
final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
Formatter dateTimeFormatter = propertyValue -> {
if (propertyValue == null) {
return StringUtils.EMPTY;
}
return dateTimeFormat.format(propertyValue);
};
// collect properties in order to add non-system columns first
LinkedList<Table.Column> nonSystemPropertyColumns = new LinkedList<>();
LinkedList<Table.Column> systemPropertyColumns = new LinkedList<>();
List<MetaProperty> metaProperties = new ArrayList<>();
for (MetaProperty metaProperty : metaClass.getProperties()) {
// don't show embedded & multiple referred entities
Range range = metaProperty.getRange();
if (isEmbedded(metaProperty) || range.getCardinality().isMany() || metadataTools.isSystemLevel(metaProperty) || (range.isClass() && metadataTools.isSystemLevel(range.asClass()))) {
continue;
}
metaProperties.add(metaProperty);
Table.Column column = new Table.Column(metaClass.getPropertyPath(metaProperty.getName()));
if (!metadataTools.isSystem(metaProperty)) {
column.setCaption(getPropertyCaption(metaClass, metaProperty));
nonSystemPropertyColumns.add(column);
} else {
column.setCaption(metaProperty.getName());
systemPropertyColumns.add(column);
}
if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
column.setFormatter(dateTimeFormatter);
}
}
for (Table.Column column : nonSystemPropertyColumns) {
entitiesTable.addColumn(column);
}
for (Table.Column column : systemPropertyColumns) {
entitiesTable.addColumn(column);
}
DsContext dsContext = getDsContext();
if (entitiesDs != null) {
((DsContextImplementation) dsContext).unregister(entitiesDs);
}
entitiesDs = DsBuilder.create(dsContext).setId("entitiesDs").setMetaClass(metaClass).setView(buildView(metaClass, metaProperties)).buildGroupDatasource();
entitiesDs.setQuery("select e from " + metaClass.getName() + " e " + "where e.deleteTs is not null order by e.deleteTs");
entitiesDs.setSoftDeletion(false);
entitiesDs.refresh();
entitiesTable.setDatasource(entitiesDs);
String filterId = metaClass.getName().replace("$", "") + "GenericFilter";
filter = componentsFactory.createComponent(Filter.class);
filter.setId(filterId);
filter.setFrame(getFrame());
StringBuilder sb = new StringBuilder("");
for (MetaProperty property : metaClass.getProperties()) {
AnnotatedElement annotatedElement = property.getAnnotatedElement();
if (annotatedElement.getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class) != null) {
sb.append(property.getName()).append("|");
}
}
Element filterElement = Dom4j.readDocument(String.format("<filter id=\"%s\">\n" + " <properties include=\".*\" exclude=\"\"/>\n" + "</filter>", filterId)).getRootElement();
String excludedProperties = sb.toString();
if (StringUtils.isNotEmpty(excludedProperties)) {
Element properties = filterElement.element("properties");
properties.attribute("exclude").setValue(excludedProperties.substring(0, excludedProperties.lastIndexOf("|")));
}
filter.setXmlDescriptor(filterElement);
filter.setUseMaxResults(true);
filter.setDatasource(entitiesDs);
entitiesTable.setSizeFull();
entitiesTable.setMultiSelect(true);
Action restoreAction = new ItemTrackingAction("restore").withCaption(getMessage("entityRestore.restore")).withHandler(event -> showRestoreDialog());
entitiesTable.addAction(restoreAction);
restoreButton.setAction(restoreAction);
tablePanel.add(filter);
tablePanel.add(entitiesTable);
tablePanel.expand(entitiesTable, "100%", "100%");
entitiesTable.refresh();
((FilterImplementation) filter).loadFiltersAndApplyDefault();
}
}
}
use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.
the class AbstractViewRepository method loadView.
protected void loadView(Element rootElem, Element viewElem, View view, boolean systemProperties, Set<ViewInfo> visited) {
final MetaClass metaClass = metadata.getClassNN(view.getEntityClass());
final String viewName = view.getName();
Set<String> propertyNames = new HashSet<>();
for (Element propElem : (List<Element>) viewElem.elements("property")) {
String propertyName = propElem.attributeValue("name");
if (propertyNames.contains(propertyName)) {
throw new DevelopmentException(String.format("View %s/%s definition error: view declared property %s twice", metaClass.getName(), viewName, propertyName));
}
propertyNames.add(propertyName);
MetaProperty metaProperty = metaClass.getProperty(propertyName);
if (metaProperty == null) {
throw new DevelopmentException(String.format("View %s/%s definition error: property %s doesn't exists", metaClass.getName(), viewName, propertyName));
}
View refView = null;
String refViewName = propElem.attributeValue("view");
MetaClass refMetaClass;
Range range = metaProperty.getRange();
if (range == null) {
throw new RuntimeException("cannot find range for meta property: " + metaProperty);
}
final List<Element> propertyElements = Dom4j.elements(propElem, "property");
boolean inlineView = !propertyElements.isEmpty();
if (!range.isClass() && (refViewName != null || inlineView)) {
throw new DevelopmentException(String.format("View %s/%s definition error: property %s is not an entity", metaClass.getName(), viewName, propertyName));
}
if (refViewName != null) {
refMetaClass = getMetaClass(propElem, range);
refView = retrieveView(refMetaClass, refViewName, visited);
if (refView == null) {
for (Element e : Dom4j.elements(rootElem, "view")) {
if (refMetaClass.equals(getMetaClass(e.attributeValue("entity"), e.attributeValue("class"))) && refViewName.equals(e.attributeValue("name"))) {
refView = deployView(rootElem, e, visited);
break;
}
}
if (refView == null) {
MetaClass originalMetaClass = metadata.getExtendedEntities().getOriginalMetaClass(refMetaClass);
if (originalMetaClass != null) {
refView = retrieveView(originalMetaClass, refViewName, visited);
}
}
if (refView == null) {
throw new DevelopmentException(String.format("View %s/%s definition error: unable to find/deploy referenced view %s/%s", metaClass.getName(), viewName, range.asClass().getName(), refViewName));
}
}
}
if (inlineView) {
// try to import anonymous views
Class rangeClass = range.asClass().getJavaClass();
if (refView != null) {
// system properties are already in the source view
refView = new View(refView, rangeClass, "", false);
} else {
ViewProperty existingProperty = view.getProperty(propertyName);
if (existingProperty != null && existingProperty.getView() != null) {
refView = new View(existingProperty.getView(), rangeClass, "", systemProperties);
} else {
refView = new View(rangeClass, systemProperties);
}
}
loadView(rootElem, propElem, refView, systemProperties, visited);
}
FetchMode fetchMode = FetchMode.AUTO;
String fetch = propElem.attributeValue("fetch");
if (fetch != null)
fetchMode = FetchMode.valueOf(fetch);
view.addProperty(propertyName, refView, fetchMode);
}
}
Aggregations