/**
   * Build a map of logical to physical names for use in Orm Updates for a given descriptor.
   *
   * <p>This includes the dbWrite scalar properties and imported foreign key properties.
   */
  public static Map<String, String> build(BeanDescriptor<?> descriptor) {

    Map<String, String> deployMap = new HashMap<String, String>();

    String shortName = descriptor.getName();
    String beanName = shortName.toLowerCase();
    deployMap.put(beanName, descriptor.getBaseTable());

    BeanProperty[] baseScalar = descriptor.propertiesBaseScalar();
    for (BeanProperty baseProp : baseScalar) {
      // excluding formula, secondary table properties
      if (baseProp.isDbInsertable() || baseProp.isDbUpdatable()) {
        deployMap.put(baseProp.getName().toLowerCase(), baseProp.getDbColumn());
      }
    }

    BeanPropertyAssocOne<?>[] oneImported = descriptor.propertiesOneImported();
    for (BeanPropertyAssocOne<?> assocOne : oneImported) {

      ImportedId importedId = assocOne.getImportedId();
      if (importedId == null) {
        String m =
            descriptor.getFullName()
                + " importedId is null for associated: "
                + assocOne.getFullBeanName();
        logger.log(Level.SEVERE, m);

      } else if (importedId.isScalar()) {
        deployMap.put(importedId.getLogicalName(), importedId.getDbColumn());
      }
    }

    return deployMap;
  }
  public Object convertSetId(Object idValue, Object bean) {

    // allow Map or String for concatenated id
    Map<?, ?> mapVal = null;
    if (idValue instanceof Map<?, ?>) {
      mapVal = (Map<?, ?>) idValue;
    } else {
      mapVal = MapFromString.parse(idValue.toString());
    }

    // Use a new LinkedHashMap to control the order
    LinkedHashMap<String, Object> newMap = new LinkedHashMap<String, Object>();

    for (int i = 0; i < props.length; i++) {
      BeanProperty prop = props[i];

      Object value = mapVal.get(prop.getName());

      // Convert the property type if required
      value = props[i].getScalarType().toBeanType(value);
      newMap.put(prop.getName(), value);
      if (bean != null) {
        // support PropertyChangeSupport
        prop.setValueIntercept(bean, value);
      }
    }

    return newMap;
  }
  public BeanProperty getSoftDeleteProperty() {

    for (BeanProperty prop : nonManys) {
      if (prop.isSoftDelete()) {
        return prop;
      }
    }
    return null;
  }
  /** Allocate the property to a list. */
  private void allocateToList(BeanProperty prop) {
    if (prop.isTransient()) {
      transients.add(prop);
      if (prop.isDraft()) {
        draft = prop;
      }
      return;
    }
    if (prop.isId()) {
      ids.add(prop);
      return;
    } else {
      nonTransients.add(prop);
    }

    if (prop.isMutableScalarType()) {
      mutable.add(prop);
    }

    if (desc.getInheritInfo() != null && prop.isLocal()) {
      local.add(prop);
    }

    if (prop instanceof BeanPropertyAssocMany<?>) {
      manys.add((BeanPropertyAssocMany<?>) prop);

    } else {
      nonManys.add(prop);
      if (prop instanceof BeanPropertyAssocOne<?>) {
        BeanPropertyAssocOne<?> assocOne = (BeanPropertyAssocOne<?>) prop;
        if (prop.isEmbedded()) {
          embedded.add(assocOne);
        } else {
          ones.add(assocOne);
          if (!assocOne.isOneToOneExported()) {
            onesImported.add(assocOne);
          }
        }
      } else {
        // its a "base" property...
        if (prop.isVersion()) {
          if (versionProperty == null) {
            versionProperty = prop;
          } else {
            logger.warn(
                "Multiple @Version properties - property "
                    + prop.getFullBeanName()
                    + " not treated as a version property");
          }
        } else if (prop.isDraftDirty()) {
          draftDirty = prop;
        }
        if (prop instanceof BeanPropertyCompound) {
          baseCompound.add((BeanPropertyCompound) prop);
        } else {
          baseScalar.add(prop);
        }
      }
    }
  }
 /**
  * Determine the concurrency mode depending on fully/partially populated bean.
  *
  * <p>Specifically with version concurrency we want to check that the version property was one of
  * the loaded properties.
  */
 public ConcurrencyMode determineConcurrencyMode() {
   if (loadedProps != null) {
     // 'partial bean' update/delete...
     if (concurrencyMode.equals(ConcurrencyMode.VERSION)) {
       // check the version property was loaded
       BeanProperty prop = beanDescriptor.firstVersionProperty();
       if (prop != null && loadedProps.contains(prop.getName())) {
         // OK to use version property
       } else {
         concurrencyMode = ConcurrencyMode.NONE;
       }
     }
   }
   return concurrencyMode;
 }
Beispiel #6
0
 public void add(ElPropertyDeploy elProp) {
   String join = elProp.getElPrefix();
   BeanProperty p = elProp.getBeanProperty();
   if ((p instanceof BeanPropertyAssocMany)) {
     join = addManyToJoin(join, p.getName());
   }
   if (join != null) {
     this.joins.add(join);
     String secondaryTableJoinPrefix = p.getSecondaryTableJoinPrefix();
     if (secondaryTableJoinPrefix != null) {
       this.joins.add(join + "." + secondaryTableJoinPrefix);
     }
     addParentJoins(join);
   }
 }
 /** Return true if this property is loaded (full bean or included in partial bean). */
 public boolean isLoadedProperty(BeanProperty prop) {
   if (loadedProps == null) {
     return true;
   } else {
     return loadedProps.contains(prop.getName());
   }
 }
  protected void executeDerivedRelationships() {
    List<DerivedRelationshipData> derivedRelationships = persistRequest.getDerivedRelationships();
    if (derivedRelationships != null) {

      SpiEbeanServer ebeanServer = (SpiEbeanServer) persistRequest.getEbeanServer();

      for (int i = 0; i < derivedRelationships.size(); i++) {
        DerivedRelationshipData derivedRelationshipData = derivedRelationships.get(i);

        BeanDescriptor<?> beanDescriptor =
            ebeanServer.getBeanDescriptor(derivedRelationshipData.getBean().getClass());

        BeanProperty prop =
            beanDescriptor.getBeanProperty(derivedRelationshipData.getLogicalName());
        EntityBean entityBean = (EntityBean) derivedRelationshipData.getBean();
        entityBean._ebean_getIntercept().markPropertyAsChanged(prop.getPropertyIndex());

        ebeanServer.update(entityBean, transaction);
      }
    }
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public DeployBeanPropertyLists(
      BeanDescriptorMap owner, BeanDescriptor<?> desc, DeployBeanDescriptor<?> deploy) {
    this.desc = desc;

    setImportedPrimaryKeys(deploy);

    DeployBeanPropertyAssocOne<?> deployUnidirectional = deploy.getUnidirectional();
    if (deployUnidirectional == null) {
      unidirectional = null;
    } else {
      unidirectional = new BeanPropertyAssocOne(owner, desc, deployUnidirectional);
    }

    this.propertyMap = new LinkedHashMap<String, BeanProperty>();

    // see if there is a discriminator property we should add
    String discriminatorColumn = null;
    BeanProperty discProperty = null;

    InheritInfo inheritInfo = deploy.getInheritInfo();
    if (inheritInfo != null) {
      // Create a BeanProperty for the discriminator column to support
      // using RawSql queries with inheritance
      discriminatorColumn = inheritInfo.getDiscriminatorColumn();
      DeployBeanProperty discDeployProp =
          new DeployBeanProperty(deploy, String.class, new ScalarTypeString(), null);
      discDeployProp.setDiscriminator();
      discDeployProp.setName(discriminatorColumn);
      discDeployProp.setDbColumn(discriminatorColumn);

      // only register it in the propertyMap. This might not be used if
      // an explicit property is mapped to the discriminator on the bean
      discProperty = new BeanProperty(desc, discDeployProp);
    }

    for (DeployBeanProperty prop : deploy.propertiesAll()) {
      if (discriminatorColumn != null && discriminatorColumn.equals(prop.getDbColumn())) {
        // we have an explicit property mapped to the discriminator column
        prop.setDiscriminator();
        discProperty = null;
      }
      BeanProperty beanProp = createBeanProperty(owner, prop);
      propertyMap.put(beanProp.getName(), beanProp);
    }

    int order = 0;
    for (BeanProperty prop : propertyMap.values()) {
      prop.setDeployOrder(order++);
      allocateToList(prop);
    }

    if (discProperty != null) {
      // put the discriminator property into the property map only
      // (after the real properties have been organised into their lists)
      propertyMap.put(discProperty.getName(), discProperty);
    }

    List<DeployTableJoin> deployTableJoins = deploy.getTableJoins();
    tableJoins = new TableJoin[deployTableJoins.size()];
    for (int i = 0; i < deployTableJoins.size(); i++) {
      tableJoins[i] = new TableJoin(deployTableJoins.get(i));
    }
  }
 /** Return true if this property should be included in the where clause. */
 public boolean isIncludedWhere(BeanProperty prop) {
   return (includeWhereProps == null || includeWhereProps.contains(prop.getName()));
 }
  /** Test if the property value has changed and if so include it in the update. */
  public boolean hasChanged(BeanProperty prop) {

    return changedProps.contains(prop.getName());
  }
  /**
   * Create a Matching BeanProperty with some attributes overridden.
   *
   * <p>Primarily for supporting Embedded beans with overridden dbColumn mappings.
   */
  public BeanProperty(BeanProperty source, BeanPropertyOverride override) {

    this.descriptor = source.descriptor;
    this.name = InternString.intern(source.getName());
    this.propertyIndex = source.propertyIndex;
    this.dbColumn = InternString.intern(override.getDbColumn());
    // override with sqlFormula not currently supported
    this.sqlFormulaJoin = null;
    this.sqlFormulaSelect = null;
    this.formula = false;

    this.excludedFromHistory = source.excludedFromHistory;
    this.draft = source.draft;
    this.draftDirty = source.draftDirty;
    this.draftOnly = source.draftOnly;
    this.draftReset = source.draftReset;
    this.softDelete = source.softDelete;
    this.softDeleteDbSet = source.softDeleteDbSet;
    this.softDeleteDbPredicate = source.softDeleteDbPredicate;
    this.fetchEager = source.fetchEager;
    this.unidirectionalShadow = source.unidirectionalShadow;
    this.discriminator = source.discriminator;
    this.localEncrypted = source.isLocalEncrypted();
    this.isTransient = source.isTransient();
    this.secondaryTable = source.isSecondaryTable();
    this.secondaryTableJoin = source.secondaryTableJoin;
    this.secondaryTableJoinPrefix = source.secondaryTableJoinPrefix;

    this.dbComment = source.dbComment;
    this.dbBind = source.getDbBind();
    this.dbEncrypted = source.isDbEncrypted();
    this.dbEncryptedType = source.getDbEncryptedType();
    this.dbEncryptFunction = source.dbEncryptFunction;
    this.dbRead = source.isDbRead();
    this.dbInsertable = source.isDbInsertable();
    this.dbUpdatable = source.isDbUpdatable();
    this.nullable = source.isNullable();
    this.unique = source.isUnique();
    this.naturalKey = source.isNaturalKey();
    this.dbLength = source.getDbLength();
    this.dbScale = source.getDbScale();
    this.dbColumnDefn = InternString.intern(source.getDbColumnDefn());
    this.dbColumnDefault = source.dbColumnDefault;

    this.inherited = source.isInherited();
    this.owningType = source.owningType;
    this.local = owningType.equals(descriptor.getBeanType());

    this.version = source.isVersion();
    this.embedded = source.isEmbedded();
    this.id = source.isId();
    this.generatedProperty = source.getGeneratedProperty();
    this.getter = source.getter;
    this.setter = source.setter;
    this.dbType = source.getDbType(true);
    this.scalarType = source.scalarType;
    this.lob = isLobType(dbType);
    this.propertyType = source.getPropertyType();
    this.field = source.getField();
    this.docOptions = source.docOptions;

    this.elPlaceHolder = override.replace(source.elPlaceHolder, source.dbColumn);
    this.elPlaceHolderEncrypted = override.replace(source.elPlaceHolderEncrypted, source.dbColumn);

    this.jsonSerialize = source.jsonSerialize;
    this.jsonDeserialize = source.jsonDeserialize;
  }