@SuppressWarnings("unchecked") protected static Entity createInstanceWithId(Class<? extends Entity> entityClass, Object id) { Entity entity = createInstance(entityClass); if (entity instanceof BaseGenericIdEntity) { ((BaseGenericIdEntity) entity).setId(id); } return entity; }
protected void fetchDynamicAttributes(Entity entity) { if (entity instanceof BaseGenericIdEntity) { LoadContext<BaseGenericIdEntity> loadContext = new LoadContext<>(entity.getMetaClass()); loadContext.setId(entity.getId()).setLoadDynamicAttributes(true); DataService dataService = AppBeans.get(DataService.NAME, DataService.class); BaseGenericIdEntity reloaded = dataService.load(loadContext); if (reloaded != null) { ((BaseGenericIdEntity) entity).setDynamicAttributes(reloaded.getDynamicAttributes()); } else { ((BaseGenericIdEntity) entity).setDynamicAttributes(new HashMap<>()); } } } }
@Override @SuppressWarnings("unchecked") public void setValue(String name, Object value) { mainItem.setValue(name, value); //if we set an attribute from another type of entity, we need to set reference to CategoryAttribute manually //this is workaround to make #PL-5770 logic works with modern RuntimePropertiesDatasource String attributeCode = DynamicAttributesUtils.decodeAttributeCode(name); Map<String, CategoryAttributeValue> dynamicAttributes = mainItem.getDynamicAttributes(); if (dynamicAttributes != null) { CategoryAttributeValue categoryAttributeValue = dynamicAttributes.get(attributeCode); if (categoryAttributeValue != null && categoryAttributeValue.getCategoryAttribute() == null) { categoryAttributeValue.setCategoryAttribute(attributesMap.get(attributeCode)); } } }
/** * Reload dynamic attributes on the entity */ @SuppressWarnings("unchecked") public void reloadDynamicAttributes(BaseGenericIdEntity entity) { MetaClass metaClass = entity.getMetaClass(); View view = new View(metaClass.getJavaClass(), false) .addProperty(metadata.getTools().getPrimaryKeyName(metaClass)); LoadContext loadContext = new LoadContext(metaClass) .setView(view) .setLoadDynamicAttributes(true) .setId(entity.getId()); BaseGenericIdEntity reloadedEntity = (BaseGenericIdEntity) dataManager.load(loadContext); if (reloadedEntity != null) { entity.setDynamicAttributes(reloadedEntity.getDynamicAttributes()); } } }
if (PersistenceHelper.isNew(baseGenericIdEntity) && baseGenericIdEntity.getDynamicAttributes() == null) { baseGenericIdEntity.setDynamicAttributes(new HashMap<>()); Map<String, CategoryAttributeValue> dynamicAttributes = baseGenericIdEntity.getDynamicAttributes(); checkNotNullArgument(dynamicAttributes, "Dynamic attributes should be loaded explicitly"); if (baseGenericIdEntity.getMetaClass().getProperty("category") != null) { baseGenericIdEntity.setValue("category", category);
Collection<CategoryAttribute> attributes = dynamicAttributes.getAttributesForMetaClass(metaClass); if (item.getDynamicAttributes() == null) { item.setDynamicAttributes(new HashMap<>()); String code = DynamicAttributesUtils.encodeAttributeCode(categoryAttribute.getCode()); if (entityIsCategorized && !categoryAttribute.getCategory().equals(((Categorized) item).getCategory())) { item.setValue(code, null);//cleanup attributes from not dedicated category continue; if (item.getValue(code) != null) { continue;//skip not null attributes .setParameter("entityId", categoryAttribute.getDefaultValue()); Entity defaultEntity = dataManager.load(lc); item.setValue(code, defaultEntity); } else if (Boolean.TRUE.equals(categoryAttribute.getIsCollection())) { List<Object> list = new ArrayList<>(); list.add(categoryAttribute.getDefaultValue()); item.setValue(code, list); } else { item.setValue(code, categoryAttribute.getDefaultValue()); item.setValue(code, currentTimestamp);
protected boolean entityHasDynamicAttributes(Entity entity) { return entity instanceof BaseGenericIdEntity && ((BaseGenericIdEntity) entity).getDynamicAttributes() != null; }
@SuppressWarnings("unchecked") protected void doStoreDynamicAttributes(BaseGenericIdEntity entity) { final EntityManager em = persistence.getEntityManager(); Map<String, CategoryAttributeValue> dynamicAttributes = entity.getDynamicAttributes(); if (dynamicAttributes != null) { Map<String, CategoryAttributeValue> mergedDynamicAttributes = new HashMap<>(); && categoryAttributeValue.getCode() != null) { CategoryAttribute attribute = getAttributeForMetaClass(entity.getMetaClass(), categoryAttributeValue.getCode()); categoryAttributeValue.setCategoryAttribute(attribute); entity.setDynamicAttributes(mergedDynamicAttributes);
public InstanceRef(EntityLoadInfo loadInfo) throws InstantiationException, IllegalAccessException { if (loadInfo == null) throw new NullPointerException("No load info passed"); this.loadInfo = loadInfo; MetaClass childMetaClass = loadInfo.getMetaClass(); if (!Strings.isNullOrEmpty(loadInfo.getViewName()) && !loadInfo.isNewEntity()) { DataService dataService = AppBeans.get(DataService.class); LoadContext<BaseGenericIdEntity<Object>> ctx = new LoadContext<>(loadInfo.getMetaClass()); ctx.setId(loadInfo.getId()).setView(loadInfo.getViewName()); instance = dataService.load(ctx); if (instance == null) { throw new RuntimeException("Entity with loadInfo " + loadInfo + " not found"); } } else { //noinspection unchecked instance = (BaseGenericIdEntity<Object>) AppBeans.get(Metadata.class).create(childMetaClass); if (!loadInfo.isNewEntity()) { for (MetaProperty metaProperty : childMetaClass.getProperties()) { if (!metaProperty.getRange().isClass()) { try { instance.setValue(metaProperty.getName(), null); } catch (Exception ignored) { } } } } } instance.setId(this.loadInfo.getId()); }
for (BaseGenericIdEntity entity : entities) { Map<String, CategoryAttributeValue> map = new HashMap<>(); entity.setDynamicAttributes(map); for (BaseGenericIdEntity entity : entities) { Map<String, CategoryAttributeValue> map = new HashMap<>(); entity.setDynamicAttributes(map); Collection<CategoryAttributeValue> entityAttributeValues = allAttributeValues.get(referenceToEntitySupport.getReferenceId(entity)); Map<String, CategoryAttributeValue> map = new HashMap<>(); entity.setDynamicAttributes(map); if (CollectionUtils.isNotEmpty(entityAttributeValues)) { for (CategoryAttributeValue categoryAttributeValue : entityAttributeValues) {
categoryAttributeValue.setValue(null); categoryAttributeValue.setDeleteTs(AppBeans.get(TimeSource.class).currentTimestamp()); propertyChanged(property, oldValue, null); DynamicAttributes dynamicAttributesBean = AppBeans.get(DynamicAttributes.NAME); categoryAttributeValue.setCategoryAttribute( dynamicAttributesBean.getAttributeForMetaClass(getMetaClass(), attributeCode)); dynamicAttributes.put(attributeCode, categoryAttributeValue); propertyChanged(property, oldValue, newValue);
@Override public MetaClass getMetaClass() { return mainItem.getMetaClass(); }
@Override @SuppressWarnings("unchecked") public <T> T getValue(String name) { return (T) mainItem.getValue(name); }
if (filteredData != null && filteredData.containsEntry(property.getName(), referenceToEntitySupport.getReferenceId((Entity) value))) { baseGenericIdEntity.setValue(property.getName(), null); } else { applyConstraints((Entity) value, handled);
protected boolean entityHasDynamicAttributes(Entity entity) { return entity instanceof BaseGenericIdEntity && ((BaseGenericIdEntity) entity).getDynamicAttributes() != null; }
((BaseGenericIdEntity) instance).setDynamicAttributes(new HashMap<>());
toProcess.forEach(e -> entitiesByType.put(e.getMetaClass().getName(), e)); entitiesByType.keySet().forEach(entityType -> { MetaClass metaClass = metadata.getExtendedEntities().getOriginalOrThisMetaClass(metadata.getClass(entityType));
return RefId.createNotLoaded(property); else { Entity refEntity = (Entity) entity.getValue(property); return RefId.create(property, refEntity == null ? null : refEntity.getId());
public static void fetchDynamicAttributes(Entity entity){ if (entity instanceof BaseGenericIdEntity) { LoadContext<BaseGenericIdEntity> loadContext = new LoadContext<>(entity.getMetaClass()); loadContext.setId(entity.getId()).setLoadDynamicAttributes(true); DataService dataService = AppBeans.get(DataService.NAME, DataService.class); BaseGenericIdEntity reloaded = dataService.load(loadContext); if (reloaded != null) { ((BaseGenericIdEntity) entity).setDynamicAttributes(reloaded.getDynamicAttributes()); } else { ((BaseGenericIdEntity) entity).setDynamicAttributes(new HashMap<>()); } } } }