/** * @param id id of an entity to be loaded * @return this instance for chaining */ public LoadContext<E> setId(Object id) { this.id = id instanceof Entity ? ((Entity) id).getId() : id; // for compatibility with legacy code relying on implicit conversions return this; }
@Override protected Collection<EntitySnapshot> getEntities(Map<String, Object> params) { if (entity != null) { EntitySnapshotService snapshotService = AppBeans.get(EntitySnapshotService.NAME); snapshots = snapshotService.getSnapshots(entity.getMetaClass(), entity.getId()); return snapshots; } return Collections.emptyList(); }
@Nonnull @Override public String format(@Nullable Object value) { return ((Entity) value).getInstanceName(); }
protected void removeListeners(Entity entity) { entity.removePropertyChangeListener(propertyChangeListener); Map<String, EmbeddedPropertyChangeListener> listenerMap = embeddedPropertyListeners.get(entity); if (listenerMap != null) { for (Map.Entry<String, EmbeddedPropertyChangeListener> entry : listenerMap.entrySet()) { Entity embedded = entity.getValue(entry.getKey()); if (embedded != null) { embedded.removePropertyChangeListener(entry.getValue()); embedded.removePropertyChangeListener(propertyChangeListener); } } embeddedPropertyListeners.remove(entity); } }
public static void setSecurityState(Entity entity, SecurityState securityState) { Preconditions.checkNotNullArgument(entity, "Entity is null"); if (entity instanceof BaseGenericIdEntity) { BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity; baseGenericIdEntity.__securityState = securityState; } else if (entity instanceof EmbeddableEntity) { EmbeddableEntity embeddableEntity = (EmbeddableEntity) entity; embeddableEntity.__securityState = securityState; } else { throw new IllegalArgumentException(String.format("Entity with type [%s] does not support security state", entity.getMetaClass().getName())); } }
protected Entity getReference(Entity entity, MetaProperty property) { if (PersistenceHelper.isLoaded(entity, property.getName())) return entity.getValue(property.getName()); else { Query query = entityManager.createQuery( "select e." + property.getName() + " from " + entity.getMetaClass().getName() + " e where e." + primaryKeyName + " = ?1"); query.setParameter(1, entity.getId()); Object refEntity = query.getFirstResult(); return (Entity) refEntity; } }
/** * Tries to initialize entity fields included in entity name pattern by default values * * @param entity instance */ protected void initNamePatternFields(Entity entity) { Collection<MetaProperty> properties = metadata.getTools().getNamePatternProperties(entity.getMetaClass()); for (MetaProperty property : properties) { if (entity.getValue(property.getName()) == null && property.getType() == MetaProperty.Type.DATATYPE) { try { entity.setValue(property.getName(), property.getJavaType().newInstance()); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException("Unable to set value of name pattern field", e); } } } }
protected void setParentField(Entity item, String parentProperty, Entity parent) { if (parentProperty != null && parent != null && item != null) item.setValue(parentProperty, parent); }
@Nullable @Override public E getParent(E item) { Preconditions.checkNotNullArgument(item); return item.getValue(hierarchyProperty); } }
@Override protected Collection<KeyValueEntity> getEntities(Map<String, Object> params) { Tag tag = (Tag) params.get("tag"); Collection<Entity> entitiesWithTag = taggingService.getEntitiesWithTag(tag); Collection<KeyValueEntity> result = new ArrayList<>(); for (Entity entity : entitiesWithTag) { KeyValueEntity keyValueEntity = metadata.create(KeyValueEntity.class); keyValueEntity.setMetaClass(entity.getMetaClass()); keyValueEntity.setValue("instanceName", entity.getInstanceName()); keyValueEntity.setValue("entity", entity); result.add(keyValueEntity); } return result; } }
@Override public String apply(Object o) { if (!(o instanceof Entity)) { return ""; } Entity entity = (Entity) o; if (captionMode == CaptionMode.PROPERTY && captionProperty != null) { if (entity.getMetaClass().getPropertyPath(captionProperty) == null) { throw new IllegalArgumentException(String.format("Couldn't find property with name '%s'", captionProperty)); } Object propertyValue = entity.getValueEx(captionProperty); return propertyValue != null ? propertyValue.toString() : " "; } return metadata.getTools().getInstanceName(entity); }
protected void checkRequiredAttributes(Entity entity) { SecurityState securityState = getSecurityState(entity); if (securityState != null && !securityState.getRequiredAttributes().isEmpty()) { for (MetaProperty metaProperty : entity.getMetaClass().getProperties()) { String propertyName = metaProperty.getName(); if (BaseEntityInternalAccess.isRequired(securityState, propertyName) && entity.getValue(propertyName) == null) { throw new RowLevelSecurityException(format("Attribute [%s] is required for entity %s", propertyName, entity), entity.getMetaClass().getName()); } } } }
@Override public T apply(E entity) { return propertyPath != null ? entity.getValueEx(propertyPath) : null; } }
protected void importReference(Entity srcEntity, Entity dstEntity, EntityImportViewProperty importViewProperty, View regularView, CommitContext commitContext, Collection<ReferenceInfo> referenceInfoList, boolean optimisticLocking) { Entity srcPropertyValue = srcEntity.getValue(importViewProperty.getName()); Entity dstPropertyValue = dstEntity.getValue(importViewProperty.getName()); if (importViewProperty.getView() == null) { ReferenceInfo referenceInfo = new ReferenceInfo(dstEntity, null, importViewProperty, srcPropertyValue, dstPropertyValue); referenceInfoList.add(referenceInfo); } else { dstPropertyValue = importEntity(srcPropertyValue, dstPropertyValue, importViewProperty.getView(), regularView, commitContext, referenceInfoList, optimisticLocking); dstEntity.setValue(importViewProperty.getName(), dstPropertyValue); } }
protected Object getValueByProperty(T item, MetaPropertyPath property) { Preconditions.checkNotNullArgument(item); if (property.getMetaProperties().length == 1) { return item.getValue(property.getMetaProperty().getName()); } else { return item.getValueEx(property); } }
protected void checkCompositePrimaryKey(Entity entity) { if (metadata.getTools().hasCompositePrimaryKey(entity.getMetaClass()) && !(entity instanceof HasUuid)) { throw new UnsupportedOperationException(format("Entity %s has no persistent UUID attribute", entity)); } }
protected void setField(Entity bean, String field, Object value) throws IllegalAccessException, InvocationTargetException, IntrospectionException { bean.setValue(field, value); }
@Override public boolean hasChildren(E item) { return container.getItems().stream().anyMatch(it -> { E parentItem = it.getValue(hierarchyProperty); return parentItem != null && parentItem.equals(item); }); }
@Override public V getValue() { E item = datasource.getItem(); if (item != null) { return item.getValueEx(metaPropertyPath); } return null; }
private void loadOne(EntityCrossDataStoreProperty entityCrossDataStoreProperty) { Entity entity = entityCrossDataStoreProperty.entity; CrossDataStoreProperty aProp = entityCrossDataStoreProperty.crossProp; Object id = entity.getValue(aProp.relatedPropertyName); LoadContext<Entity> loadContext = new LoadContext<>(aProp.property.getRange().asClass()); loadContext.setId(id); if (aProp.viewProperty.getView() != null) loadContext.setView(aProp.viewProperty.getView()); loadContext.setJoinTransaction(joinTransaction); Entity relatedEntity = dataManager.load(loadContext); entity.setValue(aProp.property.getName(), relatedEntity); }