use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.
the class WebLookupField method initNullEntity.
protected void initNullEntity() {
// noinspection IncorrectCreateEntity
nullEntity = new BaseUuidEntity() {
@Override
public String getInstanceName() {
if (nullOption instanceof Instance) {
return InstanceUtils.getInstanceName((Instance) nullOption);
}
if (nullOption == null) {
return "";
} else {
return nullOption.toString();
}
}
// Used for captionProperty of null entity
@Override
public <T> T getValue(String s) {
return (T) getInstanceName();
}
};
component.setNullSelectionItemId(nullEntity);
}
use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.
the class EntityFetcher method fetch.
@SuppressWarnings("unchecked")
protected void fetch(Entity entity, View view, Map<Instance, Set<View>> visited, boolean optimizeForDetached) {
Set<View> views = visited.get(entity);
if (views == null) {
views = new HashSet<>();
visited.put(entity, views);
} else if (views.contains(view)) {
return;
}
views.add(view);
if (log.isTraceEnabled())
log.trace("Fetching instance " + entity);
MetaClass metaClass = metadata.getClassNN(entity.getClass());
for (ViewProperty property : view.getProperties()) {
MetaProperty metaProperty = metaClass.getPropertyNN(property.getName());
if (!metaProperty.getRange().isClass() && !metadata.getTools().isLazyFetchedLocalAttribute(metaProperty) || metadata.getTools().isNotPersistent(metaClass, metaProperty))
continue;
if (log.isTraceEnabled())
log.trace("Fetching property " + property.getName());
Object value = entity.getValue(property.getName());
View propertyView = property.getView();
if (value != null && propertyView != null) {
if (value instanceof Collection) {
for (Object item : new ArrayList(((Collection) value))) {
if (item instanceof Entity) {
Entity e = (Entity) item;
if (entityStates.isDetached(e)) {
fetchReloaded(e, propertyView, visited, optimizeForDetached, managed -> {
if (value instanceof List) {
List list = (List) value;
list.set(list.indexOf(e), managed);
} else {
Collection collection = (Collection) value;
collection.remove(e);
collection.add(managed);
}
});
} else {
fetch((Entity) item, propertyView, visited, optimizeForDetached);
}
}
}
} else if (value instanceof Entity) {
Entity e = (Entity) value;
if (!metaProperty.isReadOnly() && entityStates.isDetached(e) && !(e instanceof EmbeddableEntity)) {
fetchReloaded(e, propertyView, visited, optimizeForDetached, managed -> {
entity.setValue(property.getName(), managed);
});
} else {
fetch(e, propertyView, visited, optimizeForDetached);
}
}
}
}
}
use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.
the class AppliedFilter method formatParamValue.
protected String formatParamValue(Param param) {
Object value = param.getValue();
if (value == null)
return "";
if (param.isDateInterval()) {
DateIntervalValue dateIntervalValue = AppBeans.getPrototype(DateIntervalValue.NAME, (String) value);
return dateIntervalValue.getLocalizedValue();
}
if (value instanceof Instance)
return ((Instance) value).getInstanceName();
if (value instanceof Enum)
return messages.getMessage((Enum) value);
if (value instanceof ArrayList) {
ArrayList<String> names = new ArrayList<>();
ArrayList list = ((ArrayList) value);
for (Object obj : list) {
if (obj instanceof Instance)
names.add(((Instance) obj).getInstanceName());
else {
names.add(FilterConditionUtils.formatParamValue(param, obj));
}
}
return names.toString();
}
return FilterConditionUtils.formatParamValue(param, value);
}
use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.
the class DataContextImpl method mergeState.
protected void mergeState(Entity srcEntity, Entity dstEntity, Set<Entity> mergedSet, boolean isRoot, MergeOptions options) {
EntityStates entityStates = getEntityStates();
boolean srcNew = entityStates.isNew(srcEntity);
boolean dstNew = entityStates.isNew(dstEntity);
mergeSystemState(srcEntity, dstEntity, isRoot, options);
MetaClass metaClass = getMetadata().getClassNN(srcEntity.getClass());
MetaProperty primaryKeyProperty = getMetadataTools().getPrimaryKeyProperty(metaClass);
for (MetaProperty property : metaClass.getProperties()) {
String propertyName = property.getName();
if (// local
!property.getRange().isClass() && // not PK
property != primaryKeyProperty && // loaded src
(srcNew || entityStates.isLoaded(srcEntity, propertyName)) && (dstNew || entityStates.isLoaded(dstEntity, propertyName))) {
// loaded dst
Object value = srcEntity.getValue(propertyName);
// ignore null values in non-root source entities
if (!isRoot && !options.isFresh() && value == null) {
continue;
}
setPropertyValue(dstEntity, property, value);
}
}
for (MetaProperty property : metaClass.getProperties()) {
String propertyName = property.getName();
if (// refs and collections
property.getRange().isClass() && // not PK
property != primaryKeyProperty && // loaded src
(srcNew || entityStates.isLoaded(srcEntity, propertyName)) && (dstNew || entityStates.isLoaded(dstEntity, propertyName))) {
// loaded dst
Object value = srcEntity.getValue(propertyName);
// ignore null values in non-root source entities
if (!isRoot && !options.isFresh() && value == null) {
continue;
}
if (value == null) {
setPropertyValue(dstEntity, property, null);
continue;
}
if (value instanceof Collection) {
if (value instanceof List) {
mergeList((List) value, dstEntity, property, isRoot, options, mergedSet);
} else if (value instanceof Set) {
mergeSet((Set) value, dstEntity, property, isRoot, options, mergedSet);
} else {
throw new UnsupportedOperationException("Unsupported collection type: " + value.getClass().getName());
}
} else {
Entity srcRef = (Entity) value;
if (!mergedSet.contains(srcRef)) {
Entity managedRef = internalMerge(srcRef, mergedSet, false, options);
setPropertyValue(dstEntity, property, managedRef, false);
if (getMetadataTools().isEmbedded(property)) {
EmbeddedPropertyChangeListener listener = new EmbeddedPropertyChangeListener(dstEntity);
managedRef.addPropertyChangeListener(listener);
embeddedPropertyListeners.computeIfAbsent(dstEntity, e -> new HashMap<>()).put(propertyName, listener);
}
} else {
Entity managedRef = find(srcRef.getClass(), srcRef.getId());
if (managedRef != null) {
setPropertyValue(dstEntity, property, managedRef, false);
} else {
// should never happen
log.debug("Instance was merged but managed instance is null: {}", srcRef);
}
}
}
}
}
}
use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.
the class ListEditorHelper method getValueCaption.
@Nullable
public static String getValueCaption(@Nullable Object v, ListEditor.ItemType itemType, @Nullable TimeZone timeZone) {
if (v == null)
return null;
switch(itemType) {
case ENTITY:
if (v instanceof Instance)
return ((Instance) v).getInstanceName();
else
return v.toString();
case STRING:
return (String) v;
case DATE:
return Datatypes.getNN(java.sql.Date.class).format(v);
case DATETIME:
UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
UserSession userSession = userSessionSource.getUserSession();
if (timeZone != null) {
return ((TimeZoneAwareDatatype) Datatypes.getNN(Date.class)).format(v, userSession.getLocale(), timeZone);
} else {
return Datatypes.getNN(Date.class).format(v, userSession.getLocale());
}
case INTEGER:
return Datatypes.getNN(Integer.class).format(v);
case LONG:
return Datatypes.getNN(Long.class).format(v);
case BIGDECIMAL:
return Datatypes.getNN(BigDecimal.class).format(v);
case DOUBLE:
return Datatypes.getNN(Double.class).format(v);
case ENUM:
return AppBeans.get(Messages.class).getMessage((Enum) v);
case UUID:
return Datatypes.getNN(java.util.UUID.class).format(v);
default:
throw new IllegalStateException("Unknown item type");
}
}
Aggregations