use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class EntityLoadInfoBuilder method parse.
/**
* Parse an info from the string.
*
* @param str string representation of the info. See {@link EntityLoadInfo} for formats.
* @return info instance or null if the string can not be parsed. Any exception is silently swallowed.
*/
@Nullable
public EntityLoadInfo parse(String str) {
boolean isNew = false;
if (str.startsWith(EntityLoadInfo.NEW_PREFIX)) {
str = str.substring("NEW-".length());
isNew = true;
}
int idDashPos = str.indexOf('-');
if (idDashPos == -1) {
if (isNew) {
MetaClass metaClass = metadata.getSession().getClass(str);
if (metaClass == null) {
return null;
}
Entity entity = metadata.create(metaClass);
MetaProperty primaryKeyProp = metadata.getTools().getPrimaryKeyProperty(metaClass);
boolean stringKey = primaryKeyProp != null && primaryKeyProp.getJavaType().equals(String.class);
return new EntityLoadInfo(entity.getId(), metaClass, null, stringKey, true);
}
return null;
}
String entityName = str.substring(0, idDashPos);
MetaClass metaClass = metadata.getSession().getClass(entityName);
if (metaClass == null) {
return null;
}
Object id;
String viewName;
boolean stringKey = false;
MetaProperty primaryKeyProp = metadata.getTools().getPrimaryKeyProperty(metaClass);
if (primaryKeyProp == null)
return null;
if (primaryKeyProp.getJavaType().equals(UUID.class)) {
int viewDashPos = -1;
int dashCount = StringUtils.countMatches(str, "-");
if (dashCount < 5) {
return null;
}
if (dashCount >= 6) {
int i = 0;
while (i < 6) {
viewDashPos = str.indexOf('-', viewDashPos + 1);
i++;
}
viewName = str.substring(viewDashPos + 1);
} else {
viewDashPos = str.length();
viewName = null;
}
String entityIdStr = str.substring(idDashPos + 1, viewDashPos);
try {
id = UuidProvider.fromString(entityIdStr);
} catch (Exception e) {
return null;
}
} else {
String entityIdStr;
if (primaryKeyProp.getJavaType().equals(String.class)) {
stringKey = true;
int viewDashPos = str.indexOf("}-", idDashPos + 2);
if (viewDashPos > -1) {
viewName = str.substring(viewDashPos + 2);
} else {
viewDashPos = str.length() - 1;
viewName = null;
}
entityIdStr = str.substring(idDashPos + 2, viewDashPos);
} else {
int viewDashPos = str.indexOf('-', idDashPos + 1);
if (viewDashPos > -1) {
viewName = str.substring(viewDashPos + 1);
} else {
viewDashPos = str.length();
viewName = null;
}
entityIdStr = str.substring(idDashPos + 1, viewDashPos);
}
try {
if (primaryKeyProp.getJavaType().equals(Long.class)) {
id = Long.valueOf(entityIdStr);
} else if (primaryKeyProp.getJavaType().equals(Integer.class)) {
id = Integer.valueOf(entityIdStr);
} else {
id = entityIdStr;
}
} catch (Exception e) {
return null;
}
}
return new EntityLoadInfo(id, metaClass, viewName, stringKey, isNew);
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class MetadataTools method deepCopy.
/**
* Copies all property values from source to destination excluding null values.
*/
public void deepCopy(Entity source, Entity destination, EntitiesHolder entitiesHolder) {
for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
String name = srcProperty.getName();
if (srcProperty.isReadOnly() || !persistentAttributesLoadChecker.isLoaded(source, name)) {
continue;
}
Object value = source.getValue(name);
if (value == null) {
continue;
}
if (srcProperty.getRange().isClass()) {
Class refClass = srcProperty.getRange().asClass().getJavaClass();
if (!isPersistent(refClass)) {
continue;
}
if (srcProperty.getRange().getCardinality().isMany()) {
// noinspection unchecked
Collection<Entity> srcCollection = (Collection) value;
Collection<Entity> dstCollection = value instanceof List ? new ArrayList<>() : new LinkedHashSet<>();
for (Entity srcRef : srcCollection) {
Entity reloadedRef = entitiesHolder.find(srcRef.getId());
if (reloadedRef == null) {
reloadedRef = entitiesHolder.create(srcRef.getClass(), srcRef.getId());
deepCopy(srcRef, reloadedRef, entitiesHolder);
}
dstCollection.add(reloadedRef);
}
destination.setValue(name, dstCollection);
} else {
Entity srcRef = (Entity) value;
Entity reloadedRef = entitiesHolder.find(srcRef.getId());
if (reloadedRef == null) {
reloadedRef = entitiesHolder.create(srcRef.getClass(), srcRef.getId());
deepCopy(srcRef, reloadedRef, entitiesHolder);
}
destination.setValue(name, reloadedRef);
}
} else {
destination.setValue(name, value);
}
}
if (source instanceof BaseGenericIdEntity && destination instanceof BaseGenericIdEntity) {
((BaseGenericIdEntity) destination).setDynamicAttributes(((BaseGenericIdEntity<?>) source).getDynamicAttributes());
}
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class MetadataTools method deepCopy.
/**
* Make deep copy of the source entity: all referred entities ant collections will be copied as well
*/
public <T extends Entity> T deepCopy(T source) {
CachingEntitiesHolder entityFinder = new CachingEntitiesHolder();
Entity destination = entityFinder.create(source.getClass(), source.getId());
deepCopy(source, destination, entityFinder);
return (T) destination;
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class MetadataTools method getEntityName.
/**
* @param entityClass entity class
* @return entity name as defined in {@link javax.persistence.Entity} annotation
*/
public String getEntityName(Class<?> entityClass) {
Annotation annotation = entityClass.getAnnotation(javax.persistence.Entity.class);
if (annotation == null)
throw new IllegalArgumentException("Class " + entityClass + " is not a persistent entity");
String name = ((javax.persistence.Entity) annotation).name();
if (!StringUtils.isEmpty(name))
return name;
else
return entityClass.getSimpleName();
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
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 = ((Editor) frame).getItem();
frame.showNotification(messages.formatMessage(AppConfig.getMessagesPack(), "info.EntitySave", messages.getTools().getEntityCaption(entity.getMetaClass()), entity.getInstanceName()), NotificationType.TRAY);
}
postInit();
}
return true;
}
Aggregations