use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class EntityCopyUtils method copyCompositions.
public static void copyCompositions(Entity source, Entity dest) {
Preconditions.checkNotNullArgument(source, "source is null");
Preconditions.checkNotNullArgument(dest, "dest is null");
if (source instanceof BaseDbGeneratedIdEntity && dest instanceof BaseDbGeneratedIdEntity) {
((BaseDbGeneratedIdEntity) dest).setId(((BaseDbGeneratedIdEntity) source).getId());
}
for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
String name = srcProperty.getName();
MetaProperty dstProperty = dest.getMetaClass().getProperty(name);
if (dstProperty != null && !dstProperty.isReadOnly()) {
try {
Object value = source.getValue(name);
if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
// noinspection unchecked
Collection<Entity> srcCollection = (Collection) value;
// Copy first to a Set to remove duplicates that could be created on repeated editing newly
// added items
Collection<Entity> tmpCollection = new LinkedHashSet<>();
for (Entity item : srcCollection) {
Entity copy = copyCompositions(item);
tmpCollection.add(copy);
}
Collection<Entity> dstCollection;
if (value instanceof List)
dstCollection = new ArrayList<>(tmpCollection);
else
dstCollection = tmpCollection;
dest.setValue(name, dstCollection);
} else {
dest.setValue(name, source.getValue(name));
}
} catch (RuntimeException e) {
Throwable cause = ExceptionUtils.getRootCause(e);
if (cause == null)
cause = e;
// ignore exception on copy for not loaded fields
if (!isNotLoadedAttributeException(cause))
throw e;
}
}
}
if (source instanceof BaseGenericIdEntity && dest instanceof BaseGenericIdEntity) {
BaseGenericIdEntity destGenericEntity = (BaseGenericIdEntity) dest;
BaseGenericIdEntity<?> sourceGenericEntity = (BaseGenericIdEntity<?>) source;
BaseEntityInternalAccess.setDetached(destGenericEntity, BaseEntityInternalAccess.isDetached(sourceGenericEntity));
BaseEntityInternalAccess.setNew(destGenericEntity, BaseEntityInternalAccess.isNew(sourceGenericEntity));
destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());
}
}
use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class PersistenceTools method getReferenceId.
/**
* Returns an ID of directly referenced entity without loading it from DB.
*
* If the view does not contain the reference and {@link View#loadPartialEntities()} is true,
* the returned {@link RefId} will have {@link RefId#isLoaded()} = false.
*
* <p>Usage example:
* <pre>
* PersistenceTools.RefId refId = persistenceTools.getReferenceId(doc, "currency");
* if (refId.isLoaded()) {
* String currencyCode = (String) refId.getValue();
* }
* </pre>
*
* @param entity entity instance in managed state
* @param property name of reference property
* @return {@link RefId} instance which contains the referenced entity ID
* @throws IllegalArgumentException if the specified property is not a reference
* @throws IllegalStateException if the entity is not in Managed state
* @throws RuntimeException if anything goes wrong when retrieving the ID
*/
public RefId getReferenceId(BaseGenericIdEntity entity, String property) {
MetaClass metaClass = metadata.getClassNN(entity.getClass());
MetaProperty metaProperty = metaClass.getPropertyNN(property);
if (!metaProperty.getRange().isClass() || metaProperty.getRange().getCardinality().isMany())
throw new IllegalArgumentException("Property is not a reference");
if (!PersistenceHelper.isManaged(entity))
throw new IllegalStateException("Entity must be in managed state");
String[] inaccessibleAttributes = BaseEntityInternalAccess.getInaccessibleAttributes(entity);
if (inaccessibleAttributes != null) {
for (String inaccessibleAttr : inaccessibleAttributes) {
if (inaccessibleAttr.equals(property))
return RefId.createNotLoaded(property);
}
}
if (entity instanceof FetchGroupTracker) {
FetchGroup fetchGroup = ((FetchGroupTracker) entity)._persistence_getFetchGroup();
if (fetchGroup != null) {
if (!fetchGroup.getAttributeNames().contains(property))
return RefId.createNotLoaded(property);
else {
Entity refEntity = (Entity) entity.getValue(property);
return RefId.create(property, refEntity == null ? null : refEntity.getId());
}
}
}
try {
Class<?> declaringClass = metaProperty.getDeclaringClass();
if (declaringClass == null) {
throw new RuntimeException("Property does not belong to persistent class");
}
Method vhMethod = declaringClass.getDeclaredMethod(String.format("_persistence_get_%s_vh", property));
vhMethod.setAccessible(true);
ValueHolderInterface vh = (ValueHolderInterface) vhMethod.invoke(entity);
if (vh instanceof DatabaseValueHolder) {
AbstractRecord row = ((DatabaseValueHolder) vh).getRow();
if (row != null) {
Session session = persistence.getEntityManager().getDelegate().unwrap(Session.class);
ClassDescriptor descriptor = session.getDescriptor(entity);
DatabaseMapping mapping = descriptor.getMappingForAttributeName(property);
Vector<DatabaseField> fields = mapping.getFields();
if (fields.size() != 1) {
throw new IllegalStateException("Invalid number of columns in mapping: " + fields);
}
Object value = row.get(fields.get(0));
if (value != null) {
ClassDescriptor refDescriptor = mapping.getReferenceDescriptor();
DatabaseMapping refMapping = refDescriptor.getMappingForAttributeName(metadata.getTools().getPrimaryKeyName(metaClass));
if (refMapping instanceof AbstractColumnMapping) {
Converter converter = ((AbstractColumnMapping) refMapping).getConverter();
if (converter != null) {
return RefId.create(property, converter.convertDataValueToObjectValue(value, session));
}
}
}
return RefId.create(property, value);
} else {
return RefId.create(property, null);
}
}
return RefId.createNotLoaded(property);
} catch (Exception e) {
throw new RuntimeException(String.format("Error retrieving reference ID from %s.%s", entity.getClass().getSimpleName(), property), e);
}
}
use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class ObjectsCache method copy.
// Items passed to update method can be managed we send only their copies
protected Entity copy(BaseGenericIdEntity entity) {
BaseGenericIdEntity result = (BaseGenericIdEntity) AppBeans.get(Metadata.class).create(entity.getMetaClass());
result.setId(entity.getId());
return result;
}
use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class BulkEditorWindow method init.
@Override
public void init(Map<String, Object> params) {
super.init(params);
String width = themeConstants.get("cuba.gui.BulkEditorWindow.width");
String height = themeConstants.get("cuba.gui.BulkEditorWindow.height");
getDialogOptions().setWidth(width).setHeight(height);
checkNotNullArgument(metaClass);
checkNotNullArgument(selected);
if (StringUtils.isNotBlank(exclude)) {
excludeRegex = Pattern.compile(exclude);
}
for (ManagedField managedField : getManagedFields(metaClass)) {
managedFields.put(managedField.getFqn(), managedField);
}
View view = createView(metaClass);
items = loadItems(view);
dsContext = new DsContextImpl(dataSupplier);
dsContext.setFrameContext(getDsContext().getFrameContext());
setDsContext(dsContext);
datasource = new DatasourceImpl<>();
datasource.setup(dsContext, dataSupplier, metaClass.getName() + "Ds", metaClass, view);
((DatasourceImpl) datasource).valid();
dsContext.register(datasource);
createNestedEmbeddedDatasources(datasource, metaClass, "");
Entity instance = metadata.create(metaClass);
if (loadDynamicAttributes && (instance instanceof BaseGenericIdEntity)) {
((BaseGenericIdEntity) instance).setDynamicAttributes(new HashMap<>());
}
createEmbeddedFields(metaClass, instance, "");
datasource.setItem(instance);
datasource.setAllowCommit(false);
createDataComponents();
}
use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class GlobalPersistentAttributesLoadChecker method isLoaded.
@Override
public boolean isLoaded(Object entity, String property) {
MetaClass metaClass = metadata.getClassNN(entity.getClass());
if (isDynamicAttribute(property)) {
@SuppressWarnings("unchecked") Map<String, CategoryAttributeValue> dynamicAttributes = ((BaseGenericIdEntity) entity).getDynamicAttributes();
if (dynamicAttributes == null) {
return false;
}
String attributeCode = decodeAttributeCode(property);
return dynamicAttributes.containsKey(attributeCode);
}
MetaProperty metaProperty = metaClass.getPropertyNN(property);
if (!metadataTools.isPersistent(metaProperty)) {
List<String> relatedProperties = metadataTools.getRelatedProperties(metaProperty);
if (relatedProperties.isEmpty()) {
return true;
} else {
for (String relatedProperty : relatedProperties) {
if (!isLoaded(entity, relatedProperty))
return false;
}
return true;
}
}
PropertyLoadedState isLoaded = isLoadedCommonCheck(entity, property);
if (isLoaded != PropertyLoadedState.UNKNOWN) {
return isLoaded == PropertyLoadedState.YES;
}
return isLoadedSpecificCheck(entity, property, metaClass, metaProperty);
}
Aggregations