/**
  * Return the value for this property which we hold in the L2 cache entry.
  *
  * <p>This uses format() where possible to store the value as a string and this is done to make
  * any resulting Java object serialisation content smaller as strings get special treatment.
  */
 public Object getCacheDataValue(EntityBean bean) {
   Object value = getValue(bean);
   if (value == null || scalarType.isBinaryType()) {
     return value;
   } else {
     // convert to string as an optimisation for java object serialisation
     return scalarType.format(value);
   }
 }
 /**
  * Read the value for this property from L2 cache entry and set it to the bean.
  *
  * <p>This uses parse() as per the comment in getCacheDataValue().
  */
 public void setCacheDataValue(EntityBean bean, Object cacheData, PersistenceContext context) {
   if (cacheData instanceof String) {
     // parse back from string to support optimisation of java object serialisation
     cacheData = scalarType.parse((String) cacheData);
   }
   setValue(bean, cacheData);
 }
  public void jsonRead(ReadJson ctx, EntityBean bean) throws IOException {

    JsonToken event = ctx.nextToken();
    if (JsonToken.VALUE_NULL == event) {
      if (jsonDeserialize) {
        setValue(bean, null);
      }
    } else {
      // expect to read non-null json value
      Object objValue;
      if (scalarType != null) {
        objValue = scalarType.jsonRead(ctx.getParser());
      } else {
        try {
          objValue = ctx.readValueUsingObjectMapper(propertyType);
        } catch (IOException e) {
          // change in behavior for #318
          objValue = null;
          String msg =
              "Error trying to use Jackson ObjectMapper to read transient property "
                  + getFullBeanName()
                  + " - consider marking this property with @JsonIgnore";
          logger.error(msg, e);
        }
      }
      if (jsonDeserialize) {
        setValue(bean, objValue);
      }
    }
  }
  /**
   * Return the DB column length for character columns.
   *
   * <p>Note if there is no length explicitly defined then the scalarType is checked to see if that
   * has one (primarily to support putting a length on Enum types).
   */
  public int getDbLength() {
    if (dbLength == 0 && scalarType != null) {
      return scalarType.getLength();
    }

    return dbLength;
  }
  public Object readSet(DbReadContext ctx, EntityBean bean) throws SQLException {

    try {
      Object value = scalarType.read(ctx.getDataReader());
      if (bean != null) {
        setValue(bean, value);
      }
      return value;
    } catch (Exception e) {
      throw new PersistenceException("Error readSet on " + descriptor + "." + name, e);
    }
  }
  private void setEncryption(DeployBeanProperty prop, boolean dbEncString, int dbLen) {

    util.checkEncryptKeyManagerDefined(prop.getFullBeanName());

    ScalarType<?> st = prop.getScalarType();
    if (byte[].class.equals(st.getType())) {
      // Always using Java client encryption rather than DB for encryption
      // of binary data (partially as this is not supported on all db's etc)
      // This could be reviewed at a later stage.
      ScalarTypeBytesBase baseType = (ScalarTypeBytesBase) st;
      DataEncryptSupport support = createDataEncryptSupport(prop);
      ScalarTypeBytesEncrypted encryptedScalarType =
          new ScalarTypeBytesEncrypted(baseType, support);
      prop.setScalarType(encryptedScalarType);
      prop.setLocalEncrypted(true);
      return;
    }
    if (dbEncString) {

      DbEncrypt dbEncrypt = util.getDbPlatform().getDbEncrypt();

      if (dbEncrypt != null) {
        // check if we have a DB encryption function for this type
        int jdbcType = prop.getScalarType().getJdbcType();
        DbEncryptFunction dbEncryptFunction = dbEncrypt.getDbEncryptFunction(jdbcType);
        if (dbEncryptFunction != null) {
          // Use DB functions to encrypt and decrypt
          prop.setDbEncryptFunction(dbEncryptFunction, dbEncrypt, dbLen);
          return;
        }
      }
    }

    prop.setScalarType(createScalarType(prop, st));
    prop.setLocalEncrypted(true);
    if (dbLen > 0) {
      prop.setDbLength(dbLen);
    }
  }
  /** Add to the document mapping if this property is included for this index. */
  public void docStoreMapping(DocMappingBuilder mapping, String prefix) {

    if (mapping.includesProperty(prefix, name)) {

      DocPropertyType type = scalarType.getDocType();
      DocPropertyOptions options = docOptions.copy();
      if (DocPropertyType.UUID == type || DocPropertyType.ENUM == type || isStringId(type)) {
        options.setCode(true);
      }

      mapping.add(new DocPropertyMapping(name, type, options));
    }
  }
 @SuppressWarnings(value = "unchecked")
 public void jsonWrite(WriteJson writeJson, EntityBean bean) throws IOException {
   if (!jsonSerialize) {
     return;
   }
   Object value = getValueIntercept(bean);
   if (value == null) {
     writeJson.writeNullField(name);
   } else {
     if (scalarType != null) {
       writeJson.writeFieldName(name);
       scalarType.jsonWrite(writeJson.gen(), value);
     } else {
       writeJson.writeValueUsingObjectMapper(name, value);
     }
   }
 }
 /** Return true if the underlying type is mutable. */
 public boolean isMutableScalarType() {
   return scalarType != null && scalarType.isMutable();
 }
 public Object readData(DataInput dataInput) throws IOException {
   return scalarType.readData(dataInput);
 }
 public Object parseDateTime(long systemTimeMillis) {
   return scalarType.convertFromMillis(systemTimeMillis);
 }
 /** Perform DB to Logical type conversion (if necessary). */
 public Object convertToLogicalType(Object value) {
   if (scalarType != null) {
     return scalarType.toBeanType(value);
   }
   return value;
 }
 public boolean isDateTimeCapable() {
   return scalarType != null && scalarType.isDateTimeCapable();
 }
 public int getJdbcType() {
   return scalarType == null ? 0 : scalarType.getJdbcType();
 }
 /**
  * Return true if the mutable value is considered dirty. This is only used for 'mutable' scalar
  * types like hstore etc.
  */
 public boolean isDirtyValue(Object value) {
   return scalarType.isDirty(value);
 }
 public void loadIgnore(DbReadContext ctx) {
   scalarType.loadIgnore(ctx.getDataReader());
 }
 public Object read(DbReadContext ctx) throws SQLException {
   return scalarType.read(ctx.getDataReader());
 }
 @SuppressWarnings("unchecked")
 public void bind(DataBind b, Object value) throws SQLException {
   scalarType.bind(b, value);
 }
 @SuppressWarnings(value = "unchecked")
 public void writeData(DataOutput dataOutput, Object value) throws IOException {
   scalarType.writeData(dataOutput, value);
 }