📄 genericentity.java
字号:
Iterator pkIter = getModelEntity().getPksIterator(); while (pkIter.hasNext()) { ModelField curPk = (ModelField) pkIter.next(); String fieldName = curPk.getName(); if (requireValue) { if (this.fields.get(fieldName) == null) return false; } else { if (!this.fields.containsKey(fieldName)) return false; } } return true; } /** Sets the named field to the passed value, even if the value is null * @param name The field name to set * @param value The value to set */ public void set(String name, Object value) { set(name, value, true); } /** Sets the named field to the passed value. If value is null, it is only * set if the setIfNull parameter is true. This is useful because an update * will only set values that are included in the HashMap and will store null * values in the HashMap to the datastore. If a value is not in the HashMap, * it will be left unmodified in the datastore. * @param name The field name to set * @param value The value to set * @param setIfNull Specifies whether or not to set the value if it is null */ public synchronized Object set(String name, Object value, boolean setIfNull) { if (!this.mutable) { // comment this out to disable the mutable check throw new IllegalStateException("This object has been flagged as immutable (unchangeable), probably because it came from an Entity Engine cache. Cannot set a value in an immutable entity object."); } ModelField modelField = getModelEntity().getField(name); if (modelField == null) { throw new IllegalArgumentException("[GenericEntity.set] \"" + name + "\" is not a field of " + entityName + ", must be one of: " + getModelEntity().fieldNameString()); } if (value != null || setIfNull) { ModelFieldType type = null; try { type = getDelegator().getEntityFieldType(getModelEntity(), modelField.getType()); } catch (GenericEntityException e) { Debug.logWarning(e, module); } if (type == null) { throw new IllegalArgumentException("Type " + modelField.getType() + " not found"); } if (value instanceof Boolean) { // if this is a Boolean check to see if we should convert from an indicator or just leave as is try { int fieldType = SqlJdbcUtil.getType(type.getJavaType()); if (fieldType != 9) { value = ((Boolean) value).booleanValue() ? "Y" : "N"; } } catch (GenericNotImplementedException e) { throw new IllegalArgumentException(e.getMessage()); } } else if (value != null) { // make sure the type matches the field Java type if (!ObjectType.instanceOf(value, type.getJavaType())) { String errMsg = "In entity field set the value passed in [" + value.getClass().getName() + "] is not compatible with the Java type of the field [" + type.getJavaType() + "]"; // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg); Debug.logWarning(errMsg, module); } } Object old = fields.put(name, value); generateHashCode = true; modified = true; this.setChanged(); this.notifyObservers(name); return old; } else { return fields.get(name); } } public void dangerousSetNoCheckButFast(ModelField modelField, Object value) { if (modelField == null) throw new IllegalArgumentException("Cannot set field with a null modelField"); generateHashCode = true; this.fields.put(modelField.getName(), value); } public Object dangerousGetNoCheckButFast(ModelField modelField) { if (modelField == null) throw new IllegalArgumentException("Cannot get field with a null modelField"); return this.fields.get(modelField.getName()); } /** Sets the named field to the passed value, converting the value from a String to the corrent type using <code>Type.valueOf()</code> * @param name The field name to set * @param value The String value to convert and set */ public void setString(String name, String value) { if (value == null) { set(name, null); return; } boolean isNullString = false; if ("null".equals(value)) { // count this as a null too, but only for numbers and stuff, not for Strings isNullString = true; } ModelField field = getModelEntity().getField(name); if (field == null) set(name, value); // this will get an error in the set() method... ModelFieldType type = null; try { type = getDelegator().getEntityFieldType(getModelEntity(), field.getType()); } catch (GenericEntityException e) { Debug.logWarning(e, module); } if (type == null) throw new IllegalArgumentException("Type " + field.getType() + " not found"); String fieldType = type.getJavaType(); try { switch (SqlJdbcUtil.getType(fieldType)) { case 1: set(name, value); break; case 2: set(name, isNullString ? null : java.sql.Timestamp.valueOf(value)); break; case 3: set(name, isNullString ? null : java.sql.Time.valueOf(value)); break; case 4: set(name, isNullString ? null : java.sql.Date.valueOf(value)); break; case 5: set(name, isNullString ? null : Integer.valueOf(value)); break; case 6: set(name, isNullString ? null : Long.valueOf(value)); break; case 7: set(name, isNullString ? null : Float.valueOf(value)); break; case 8: set(name, isNullString ? null : Double.valueOf(value)); break; case 9: set(name, isNullString ? null : Boolean.valueOf(value)); break; case 10: // BigDecimal set(name, isNullString ? null : new BigDecimal(value)); break; case 11: // Object set(name, value); break; case 12: // java.sql.Blob // TODO: any better way to handle Blob from String? set(name, value); break; case 13: // java.sql.Clob // TODO: any better way to handle Clob from String? set(name, value); break; case 14: // java.util.Date set(name, UtilDateTime.toDate(fieldType)); break; case 15: // java.util.Collection // TODO: how to convert from String to Collection? ie what should the default behavior be? set(name, value); break; } } catch (GenericNotImplementedException ex) { throw new IllegalArgumentException(ex.getMessage()); } } /** Sets a field with an array of bytes, wrapping them automatically for easy use. * @param name The field name to set * @param bytes The byte array to be wrapped and set */ public void setBytes(String name, byte[] bytes) { this.set(name, new ByteWrapper(bytes)); } public Boolean getBoolean(String name) { Object obj = get(name); if (obj == null) { return null; } if (obj instanceof Boolean) { return (Boolean) obj; } else if (obj instanceof String) { String value = (String) obj; if ("Y".equalsIgnoreCase(value) || "T".equalsIgnoreCase(value)) { return Boolean.TRUE; } else if ("N".equalsIgnoreCase(value) || "F".equalsIgnoreCase(value)) { return Boolean.FALSE; } else { throw new IllegalArgumentException("getBoolean could not map the String '" + value + "' to Boolean type"); } } else { throw new IllegalArgumentException("getBoolean could not map the object '" + obj.toString() + "' to Boolean type, unknown object type: " + obj.getClass().getName()); } } public String getString(String name) { // might be nice to add some ClassCastException handling... and auto conversion? hmmm... Object object = get(name); if (object == null) return null; if (object instanceof java.lang.String) { return (String) object; } else { return object.toString(); } } public java.sql.Timestamp getTimestamp(String name) { return (java.sql.Timestamp) get(name); } public java.sql.Time getTime(String name) { return (java.sql.Time) get(name); } public java.sql.Date getDate(String name) { return (java.sql.Date) get(name); } public Integer getInteger(String name) { return (Integer) get(name); } public Long getLong(String name) { return (Long) get(name); } public Float getFloat(String name) { return (Float) get(name); } public Double getDouble(String name) { // this "hack" is needed for now until the Double/BigDecimal issues are all resolved Object value = get(name); if (value instanceof BigDecimal) { return new Double(((BigDecimal) value).doubleValue()); } else { return (Double) get(name); } } public BigDecimal getBigDecimal(String name) { // this "hack" is needed for now until the Double/BigDecimal issues are all resolved // NOTE: for this to be used properly it should really be used as the java-type in the field type def XML files Object value = get(name); if (value instanceof Double) { return new BigDecimal(((Double) value).doubleValue()); } else { return (BigDecimal) get(name); } } public byte[] getBytes(String name) { Object value = get(name); if (value == null) { return null; } if (value instanceof ByteWrapper) { ByteWrapper wrapper = (ByteWrapper) value; return wrapper.getBytes(); } if (value instanceof byte[]) { return (byte[]) value; } // uh-oh, this shouldn't happen... throw new IllegalArgumentException("In call to getBytes the value is not a supported type, should be byte[] or ByteWrapper, is: " + value.getClass().getName()); } /** Checks a resource bundle for a value for this field using the entity name, the field name * and a composite of the Primary Key field values as a key. If no value is found in the * resource then the field value is returned. Uses the default-resource-name from the entity * definition as the resource name. To specify a resource name manually, use the other getResource method. * * So, the key in the resource bundle (properties file) should be as follows: * <entity-name>.<field-name>.<pk-field-value-1>.<pk-field-value-2>...<pk-field-value-n> * For example: * ProductType.description.FINISHED_GOOD * * @param name The name of the field on the entity * @param locale The locale to use when finding the ResourceBundle, if null uses the default * locale for the current instance of Java * @return If the corresponding resource is found and contains a key as described above, then that * property value is returned; otherwise returns the field value */ public Object get(String name, Locale locale) { return get(name, null, locale); } /** Same as the getResource method that does not take resource name, but instead allows manually * specifying the resource name. In general you should use the other method for more consistent * naming and use of the corresponding properties files. * @param name The name of the field on the entity * @param resource The name of the resource to get the value from; if null defaults to the * default-resource-name on the entity definition, if specified there * @param locale The locale to use when finding the ResourceBundle, if null uses the default * locale for the current instance of Java * @return If the specified resource is found and contains a key as described above, then that * property value is returned; otherwise returns the field value */ public Object get(String name, String resource, Locale locale) { Object fieldValue = get(name); if (UtilValidate.isEmpty(resource)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -