use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class ConditionDescriptorsTreeBuilder method addMultiplePropertyDescriptors.
protected void addMultiplePropertyDescriptors(String includeRe, String excludeRe, List<AbstractConditionDescriptor> descriptors, Filter filter) {
List<String> includedProps = new ArrayList<>();
Pattern inclPattern = Pattern.compile(includeRe.replace(" ", ""));
MetaClass metaClass = filter.getDatasource().getMetaClass();
for (MetaProperty property : metaClass.getProperties()) {
if (!isPropertyAllowed(metaClass, property)) {
continue;
}
if (inclPattern.matcher(property.getName()).matches()) {
includedProps.add(property.getName());
}
}
Pattern exclPattern = null;
if (!StringUtils.isBlank(excludeRe)) {
exclPattern = Pattern.compile(excludeRe.replace(" ", ""));
}
for (String prop : includedProps) {
if (exclPattern == null || !exclPattern.matcher(prop).matches()) {
AbstractConditionDescriptor conditionDescriptor = new PropertyConditionDescriptor(prop, null, filter.getFrame().getMessagesPack(), filterComponentName, filter.getDatasource());
descriptors.add(conditionDescriptor);
}
}
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class CollectionDsHelper method createProperties.
public static List<MetaPropertyPath> createProperties(View view, MetaClass metaClass) {
List<MetaPropertyPath> properties = new ArrayList<>();
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
if (view != null && metadataTools.isPersistent(metaClass)) {
for (ViewProperty property : view.getProperties()) {
final String name = property.getName();
final MetaProperty metaProperty = metaClass.getProperty(name);
if (metaProperty == null) {
String message = String.format("Unable to find property %s for entity %s", name, metaClass.getName());
throw new DevelopmentException(message);
}
if (!metadataTools.isPersistent(metaProperty)) {
String message = String.format("Specified transient property %s in view for datasource with persistent entity %s", name, metaClass.getName());
LoggerFactory.getLogger(CollectionDsHelper.class).warn(message);
continue;
}
final Range range = metaProperty.getRange();
if (range == null) {
continue;
}
final Range.Cardinality cardinality = range.getCardinality();
if (!cardinality.isMany()) {
properties.add(new MetaPropertyPath(metaClass, metaProperty));
}
}
// add all non-persistent properties
for (MetaProperty metaProperty : metaClass.getProperties()) {
if (metadataTools.isNotPersistent(metaProperty)) {
properties.add(new MetaPropertyPath(metaClass, metaProperty));
}
}
} else {
if (view != null) {
LoggerFactory.getLogger(CollectionDsHelper.class).warn("Specified view {} for datasource with not persistent entity {}", view.getName(), metaClass.getName());
}
for (MetaProperty metaProperty : metaClass.getProperties()) {
final Range range = metaProperty.getRange();
if (range == null)
continue;
final Range.Cardinality cardinality = range.getCardinality();
if (!cardinality.isMany()) {
properties.add(new MetaPropertyPath(metaClass, metaProperty));
}
}
}
return properties;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class DsContextImpl method repairReferences.
@SuppressWarnings("unchecked")
protected void repairReferences(Entity entity, Entity contextEntity) {
MetaClass metaClass = metadata.getClassNN(entity.getClass());
MetaClass contextEntityMetaClass = metadata.getClassNN(contextEntity.getClass());
for (MetaProperty property : metaClass.getProperties()) {
if (!property.getRange().isClass() || !property.getRange().asClass().equals(contextEntityMetaClass) || !PersistenceHelper.isLoaded(entity, property.getName()))
continue;
Object value = entity.getValue(property.getName());
if (value != null) {
if (property.getRange().getCardinality().isMany()) {
Collection collection = (Collection) value;
for (Object item : new ArrayList(collection)) {
if (contextEntity.equals(item) && contextEntity != item) {
if (collection instanceof List) {
List list = (List) collection;
list.set(list.indexOf(item), contextEntity);
} else {
collection.remove(item);
collection.add(contextEntity);
}
}
}
} else {
if (contextEntity.equals(value) && contextEntity != value) {
entity.setValue(property.getName(), contextEntity);
}
}
}
}
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class DsContextImpl method replaceMasterCopies.
// Replace the reference to master entity with actual entity containing in the master datasource,
// because in case of nested property datasources there may be references to cloned master entities.
protected void replaceMasterCopies(Entity entity, NestedDatasource datasource) {
Datasource masterDs = datasource.getMaster();
MetaProperty metaProperty = datasource.getProperty();
if (masterDs != null && metaProperty != null) {
MetaProperty inverseProp = metaProperty.getInverse();
if (inverseProp != null && !inverseProp.getRange().getCardinality().isMany()) {
MetaClass metaClass = metadata.getExtendedEntities().getEffectiveMetaClass(inverseProp.getDomain());
if (metaClass.equals(datasource.getMetaClass()) && (PersistenceHelper.isLoaded(entity, inverseProp.getName()) && // replace master only if it's already set
entity.getValue(inverseProp.getName()) != null)) {
Object masterItem = null;
if (masterDs instanceof CollectionDatasource) {
Entity value = entity.getValue(inverseProp.getName());
if (value != null) {
Object id = value.getId();
// noinspection unchecked
masterItem = ((CollectionDatasource) masterDs).getItem(id);
}
} else {
masterItem = masterDs.getItem();
}
if (masterItem != null) {
// CAUTION need to rework this mechanism in case of two or more nested collection datasources
((AbstractInstance) entity).setValue(inverseProp.getName(), masterItem, false);
}
}
}
}
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class PropertyDatasourceImpl method commit.
@SuppressWarnings("unchecked")
@Override
public void commit() {
if (!allowCommit) {
return;
}
if (getCommitMode() == CommitMode.PARENT) {
if (parentDs == null) {
throw new IllegalStateException("parentDs is null while commitMode=PARENT");
}
if (parentDs instanceof CollectionDatasource) {
CollectionDatasource parentCollectionDs = (CollectionDatasource) parentDs;
for (Entity item : itemsToCreate) {
parentCollectionDs.addItem(item);
}
for (Entity item : itemsToUpdate) {
parentCollectionDs.modifyItem(item);
}
for (Entity item : itemsToDelete) {
parentCollectionDs.removeItem(item);
}
// after repeated edit of new items the parent datasource can contain items-to-create which are deleted
// in this datasource, so we need to delete them
Collection<Entity> parentItemsToCreate = ((DatasourceImplementation) parentCollectionDs).getItemsToCreate();
for (Entity createdItem : new ArrayList<>(parentItemsToCreate)) {
if (!this.itemsToCreate.contains(createdItem)) {
MetaProperty inverseProp = metaProperty.getInverse();
// delete only if they have the same master item
if (inverseProp != null && PersistenceHelper.isLoaded(createdItem, inverseProp.getName()) && Objects.equals(createdItem.getValue(inverseProp.getName()), masterDs.getItem())) {
parentCollectionDs.removeItem(createdItem);
}
}
}
} else {
Entity item = null;
if (!itemsToCreate.isEmpty()) {
item = itemsToCreate.iterator().next();
} else if (!itemsToUpdate.isEmpty()) {
item = itemsToUpdate.iterator().next();
} else if (!itemsToDelete.isEmpty()) {
item = itemsToDelete.iterator().next();
}
if (item != null) {
parentDs.setItem(item);
}
}
clearCommitLists();
modified = false;
}
}
Aggregations