📄 genericentity.java
字号:
set(name, Float.valueOf(value));
break;
case 8:
set(name, Double.valueOf(value));
break;
case 9:
set(name, Boolean.valueOf(value));
break;
case 10:
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".equals(value)) {
return Boolean.TRUE;
} else if ("N".equals(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) {
return (Double) get(name);
}
public byte[] getBytes(String name) {
ByteWrapper wrapper = (ByteWrapper) get(name);
if (wrapper == null) return null;
return wrapper.getBytes();
}
/** 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)) {
resource = this.getModelEntity().getDefaultResourceName();
// still empty? return the fieldValue
if (UtilValidate.isEmpty(resource)) {
//Debug.logWarning("Tried to getResource value for field named " + name + " but no resource name was passed to the method or specified in the default-resource-name attribute of the entity definition", module);
return fieldValue;
}
}
ResourceBundle bundle = UtilProperties.getResourceBundle(resource, locale);
if (bundle == null) {
//Debug.logWarning("Tried to getResource value for field named " + name + " but no resource was found with the name " + resource + " in the locale " + locale, module);
return fieldValue;
}
StringBuffer keyBuffer = new StringBuffer();
// start with the Entity Name
keyBuffer.append(this.getEntityName());
// next add the Field Name
keyBuffer.append('.');
keyBuffer.append(name);
// finish off by adding the values of all PK fields
Iterator iter = this.getModelEntity().getPksIterator();
while (iter != null && iter.hasNext()) {
ModelField curField = (ModelField) iter.next();
keyBuffer.append('.');
keyBuffer.append(this.get(curField.getName()));
}
String bundleKey = keyBuffer.toString();
Object resourceValue = null;
try {
resourceValue = bundle.getObject(bundleKey);
} catch (MissingResourceException e) {
return fieldValue;
}
if (resourceValue == null) {
return fieldValue;
} else {
return resourceValue;
}
}
public GenericPK getPrimaryKey() {
Collection pkNames = new LinkedList();
Iterator iter = this.getModelEntity().getPksIterator();
while (iter != null && iter.hasNext()) {
ModelField curField = (ModelField) iter.next();
pkNames.add(curField.getName());
}
GenericPK newPK = new GenericPK(getModelEntity(), this.getFields(pkNames));
newPK.setDelegator(this.getDelegator());
return newPK;
}
/** go through the pks and for each one see if there is an entry in fields to set */
public void setPKFields(Map fields) {
setAllFields(fields, true, null, Boolean.TRUE);
}
/** go through the pks and for each one see if there is an entry in fields to set */
public void setPKFields(Map fields, boolean setIfEmpty) {
setAllFields(fields, setIfEmpty, null, Boolean.TRUE);
}
/** go through the non-pks and for each one see if there is an entry in fields to set */
public void setNonPKFields(Map fields) {
setAllFields(fields, true, null, Boolean.FALSE);
}
/** go through the non-pks and for each one see if there is an entry in fields to set */
public void setNonPKFields(Map fields, boolean setIfEmpty) {
setAllFields(fields, setIfEmpty, null, Boolean.FALSE);
}
/** Intelligently sets fields on this entity from the Map of fields passed in
* @param fields The fields Map to get the values from
* @param setIfEmpty Used to specify whether empty/null values in the field Map should over-write non-empty values in this entity
* @param namePrefix If not null or empty will be pre-pended to each field name (upper-casing the first letter of the field name first), and that will be used as the fields Map lookup name instead of the field-name
* @param pks If null, get all values, if TRUE just get PKs, if FALSE just get non-PKs
*/
public void setAllFields(Map fields, boolean setIfEmpty, String namePrefix, Boolean pks) {
if (fields == null) {
return;
}
Iterator iter = null;
if (pks != null) {
if (pks.booleanValue()) {
iter = this.getModelEntity().getPksIterator();
} else {
iter = this.getModelEntity().getNopksIterator();
}
} else {
iter = this.getModelEntity().getFieldsIterator();
}
while (iter != null && iter.hasNext()) {
ModelField curField = (ModelField) iter.next();
String fieldName = curField.getName();
String sourceFieldName = null;
if (UtilValidate.isNotEmpty(namePrefix)) {
sourceFieldName = namePrefix + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
} else {
sourceFieldName = curField.getName();
}
if (fields.containsKey(sourceFieldName)) {
Object field = fields.get(sourceFieldName);
// if (Debug.verboseOn()) Debug.logVerbose("Setting field " + curField.getName() + ": " + field + ", setIfEmpty = " + setIfEmpty, module);
if (setIfEmpty) {
// if empty string, set to null
if (field != null && field instanceof String && ((String) field).length() == 0) {
this.set(curField.getName(), null);
} else {
this.set(curField.getName(), field);
}
} else {
// okay, only set if not empty...
if (field != null) {
// if it's a String then we need to check length, otherwise set it because it's not null
if (field instanceof String) {
String fieldStr = (String) field;
if (fieldStr.length() > 0) {
this.set(curField.getName(), field);
}
} else {
this.set(curField.getName(), field);
}
}
}
}
}
}
/** Returns keys of entity fields
* @return java.util.Collection
*/
public Collection getAllKeys() {
return fields.keySet();
}
/** Returns key/value pairs of entity fields
* @return java.util.Map
*/
public Map getAllFields() {
return new HashMap(fields);
}
/** Used by clients to specify exactly the fields they are interested in
* @param keysofFields the name of the fields the client is interested in
* @return java.util.Map
*/
public Map getFields(Collection keysofFields) {
if (keysofFields == null) return null;
Iterator keys = keysofFields.iterator();
Object aKey = null;
HashMap aMap = new HashMap();
while (keys.hasNext()) {
aKey = keys.next();
aMap.put(aKey, this.fields.get(aKey));
}
return aMap;
}
/** Used by clients to update particular fields in the entity
* @param keyValuePairs java.util.Map
*/
public synchronized void setFields(Map keyValuePairs) {
if (keyValuePairs == null) return;
Iterator entries = keyValuePairs.entrySet().iterator();
Map.Entry anEntry = null;
// this could be implement with Map.putAll, but we'll leave it like this for the extra features it has
while (entries.hasNext()) {
anEntry = (Map.Entry) entries.next();
this.set((String) anEntry.getKey(), anEntry.getValue(), true);
}
}
public boolean matchesFields(Map keyValuePairs) {
if (fields == null) return true;
if (keyValuePairs == null || keyValuePairs.size() == 0) return true;
Iterator entries = keyValuePairs.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry anEntry = (Map.Entry) entries.next();
if (!UtilValidate.areEqual(anEntry.getValue(), this.fields.get(anEntry.getKey()))) {
return false;
}
}
return true;
}
/** Used to indicate if locking is enabled for this entity
* @return True if locking is enabled
*/
public boolean lockEnabled() {
return modelEntity.lock();
}
// ======= XML Related Methods ========
public static Document makeXmlDocument(Collection values) {
Document document = UtilXml.makeEmptyXmlDocument("entity-engine-xml");
if (document == null) return null;
addToXmlDocument(values, document);
return document;
}
public static int addToXmlDocument(Collection values, Document document) {
if (values == null) return 0;
if (document == null) return 0;
Element rootElement = document.getDocumentElement();
Iterator iter = values.iterator();
int numberAdded = 0;
while (iter.hasNext()) {
GenericValue value = (GenericValue) iter.next();
Element valueElement = value.makeXmlElement(document);
rootElement.appendChild(valueElement);
numberAdded++;
}
return numberAdded;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -