@Override @SuppressWarnings("unchecked") public T createBean() { return (T) createEntityBean(true); }
/** * Create an EntityBean for this type. */ public EntityBean createEntityBean() { return descriptor.createEntityBean(); }
/** * Creates a new entity bean without invoking {@link BeanPostConstructListener#postCreate(Object)} */ @Override public EntityBean createEntityBean() { return createEntityBean(false); }
/** * Create an entity bean for JSON marshalling (which differs for the element collection case). */ public EntityBean createEntityBeanForJson() { return createEntityBean(); }
/** * Load the embedded bean given this is the bean type. */ EntityBean embeddedBeanLoadDirect(CachedBeanData data, PersistenceContext context) { EntityBean bean = desc.createEntityBean(); CachedBeanDataToBean.load(desc, bean, data, context); return bean; }
/** * Create a bean of the target type to be used as an embeddedId * value. */ public Object createEmbeddedId() { return getTargetDescriptor().createEntityBean(); }
/** * Create a new EntityBean bean. * <p> * This will generally return a subclass of the parameter 'type' which * additionally implements the EntityBean interface. That is, the returned * bean is typically an instance of a dynamically generated class. * </p> */ @Override @SuppressWarnings("unchecked") public <T> T createEntityBean(Class<T> type) { BeanDescriptor<T> desc = getBeanDescriptor(type); return (T) desc.createEntityBean(true); }
@SuppressWarnings("unchecked") protected T buildBeanFromLineContent(int row, String[] line) { try { EntityBean entityBean = descriptor.createEntityBean(); T bean = (T) entityBean; for (int columnPos = 0; columnPos < line.length; columnPos++) { convertAndSetColumn(columnPos, line[columnPos], entityBean); } return bean; } catch (RuntimeException e) { String msg = "Error at line: " + row + " line[" + Arrays.toString(line) + "]"; throw new RuntimeException(msg, e); } }
@Override public Object read(DbReadContext ctx) throws SQLException { EntityBean embId = idDesc.createEntityBean(); boolean notNull = true; for (BeanProperty prop : props) { Object value = prop.readSet(ctx, embId); if (value == null) { notNull = false; } } if (notNull) { return embId; } else { return null; } }
@Override Object read(DataReader reader) throws SQLException { EntityBean embeddedBean = property.targetDescriptor.createEntityBean(); boolean notNull = false; for (BeanProperty property : property.embeddedProps) { Object value = property.readSet(reader, embeddedBean); if (value != null) { notNull = true; } } if (notNull) { return embeddedBean; } else { return null; } }
/** * Convert back from a Map to embedded bean. */ @Override @SuppressWarnings("unchecked") public Object convertIdFromJson(Object value) { Map<String, Object> map = (Map<String, Object>) value; EntityBean idValue = idDesc.createEntityBean(); for (BeanProperty prop : props) { Object val = map.get(prop.getName()); prop.setValue(idValue, val); } return idValue; }
@Override public Object pathGetNested(Object bean) { Object value = getValueIntercept((EntityBean) bean); if (value == null) { value = targetDescriptor.createEntityBean(); setValueIntercept((EntityBean) bean, value); } return value; }
@Override public Object readData(DataInput dataInput) throws IOException { EntityBean embId = idDesc.createEntityBean(); boolean notNull = true; for (BeanProperty prop : props) { Object value = prop.readData(dataInput); prop.setValue(embId, value); if (value == null) { notNull = false; } } if (notNull) { return embId; } else { return null; } }
/** * Convert delete by Ids into delete many beans. */ private int deleteAsBeans(Collection<?> ids, Transaction transaction, DeleteMode deleteMode, BeanDescriptor<?> descriptor) { // convert to a delete by bean int total = 0; for (Object id : ids) { EntityBean bean = descriptor.createEntityBean(); descriptor.convertSetId(id, bean); int rowCount = deleteRecurse(bean, transaction, deleteMode); if (rowCount == -1) { total = -1; } else if (total != -1) { total += rowCount; } } return total; }
@Override Object read(DbReadContext ctx) throws SQLException { EntityBean embeddedBean = property.targetDescriptor.createEntityBean(); boolean notNull = false; for (BeanProperty property : property.embeddedProps) { Object value = property.readSet(ctx, embeddedBean); if (value != null) { notNull = true; } } if (notNull) { ctx.propagateState(embeddedBean); return embeddedBean; } else { return null; } }
/** * Delete by Id. */ @Override public int delete(Class<?> beanType, Object id, Transaction transaction, boolean permanent) { BeanDescriptor<?> descriptor = beanDescriptorManager.getBeanDescriptor(beanType); if (descriptor.isMultiTenant()) { // convert to a delete by bean EntityBean bean = descriptor.createEntityBean(); descriptor.convertSetId(id, bean); return delete(bean, transaction, permanent); } id = descriptor.convertId(id); DeleteMode deleteMode = (permanent || !descriptor.isSoftDelete()) ? DeleteMode.HARD : DeleteMode.SOFT; return delete(descriptor, id, null, transaction, deleteMode); }
/** * Load the entity bean from cache data given this is the root bean type. */ EntityBean loadBeanDirect(Object id, Boolean readOnly, CachedBeanData data, PersistenceContext context) { if (context == null) { context = new DefaultPersistenceContext(); } EntityBean bean = desc.createEntityBean(); id = desc.convertSetId(id, bean); CachedBeanDataToBean.load(desc, bean, data, context); EntityBeanIntercept ebi = bean._ebean_getIntercept(); // Not using a loadContext for beans coming out of L2 cache // so that means no batch lazy loading for these beans ebi.setBeanLoader(desc.getEbeanServer()); if (Boolean.TRUE.equals(readOnly)) { ebi.setReadOnly(true); } ebi.setPersistenceContext(context); desc.contextPut(context, id, bean); if (desc.isReadAuditing()) { desc.readAuditBean("l2", "", bean); } return bean; }
/** * Transfer the values from the draftBean to the liveBean. * <p> * This will recursive transfer values to all @DraftableElement properties. * </p> */ @SuppressWarnings("unchecked") public T publish(T draftBean, T liveBean) { if (liveBean == null) { liveBean = (T) desc.createEntityBean(); } EntityBean draft = (EntityBean) draftBean; EntityBean live = (EntityBean) liveBean; BeanProperty idProperty = desc.getIdProperty(); if (idProperty != null) { idProperty.publish(draft, live); } BeanProperty[] props = desc.propertiesNonMany(); for (BeanProperty prop : props) { prop.publish(draft, live); } BeanPropertyAssocMany<?>[] many = desc.propertiesMany(); for (BeanPropertyAssocMany<?> aMany : many) { if (aMany.getTargetDescriptor().isDraftable()) { aMany.publishMany(draft, live); } } return liveBean; }
/** * Create a non read only reference bean without checking cacheSharableBeans. */ @SuppressWarnings("unchecked") public T createReference(Object id, PersistenceContext pc) { try { if (inheritInfo != null && !inheritInfo.isConcrete()) { return findReferenceBean(id, pc); } EntityBean eb = createEntityBean(); id = convertSetId(id, eb); EntityBeanIntercept ebi = eb._ebean_getIntercept(); ebi.setBeanLoader(ebeanServer); ebi.setReference(idPropertyIndex); if (pc != null) { contextPut(pc, id, eb); ebi.setPersistenceContext(pc); } return (T) eb; } catch (Exception ex) { throw new PersistenceException(ex); } }
private static EntityBean createSharableBean(BeanDescriptor<?> desc, EntityBean bean, EntityBeanIntercept beanEbi) { if (!desc.isCacheSharableBeans() || !beanEbi.isFullyLoadedBean()) { return null; } if (beanEbi.isReadOnly()) { return bean; } // create a readOnly sharable instance by copying the data EntityBean sharableBean = desc.createEntityBean(); BeanProperty idProp = desc.getIdProperty(); if (idProp != null) { Object v = idProp.getValue(bean); idProp.setValue(sharableBean, v); } BeanProperty[] propertiesNonTransient = desc.propertiesNonTransient(); for (BeanProperty aPropertiesNonTransient : propertiesNonTransient) { Object v = aPropertiesNonTransient.getValue(bean); aPropertiesNonTransient.setValue(sharableBean, v); } EntityBeanIntercept intercept = sharableBean._ebean_getIntercept(); intercept.setReadOnly(true); intercept.setLoaded(); return sharableBean; }