📄 genericentity.java
字号:
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 = null; try { bundle = UtilProperties.getResourceBundle(resource, locale); } catch (IllegalArgumentException e) { bundle = null; } 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 = FastList.newInstance(); Iterator iter = this.getModelEntity().getPksIterator(); while (iter != null && iter.hasNext()) { ModelField curField = (ModelField) iter.next(); pkNames.add(curField.getName()); } GenericPK newPK = GenericPK.create(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() { Map newMap = FastMap.newInstance(); newMap.putAll(this.fields); return newMap; } /** 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; Map aMap = FastMap.newInstance(); 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 getModelEntity().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) { return addToXmlElement(values, document, document.getDocumentElement()); } public static int addToXmlElement(Collection values, Document document, Element element) { if (values == null) return 0; if (document == null) return 0; Iterator iter = values.iterator(); int numberAdded = 0; while (iter.hasNext()) { GenericValue value = (GenericValue) iter.next(); Element valueElement = value.makeXmlElement(document); element.appendChild(valueElement); numberAdded++; } return numberAdded; } /** Makes an XML Element object with an attribute for each field of the entity *@param document The XML Document that the new Element will be part of *@return org.w3c.dom.Element object representing this generic entity */ public Element makeXmlElement(Document document) { return makeXmlElement(document, null); } /** Makes an XML Element object with an attribute for each field of the entity *@param document The XML Document that the new Element will be part of *@param prefix A prefix to put in front of the entity name in the tag name *@return org.w3c.dom.Element object representing this generic entity */ public Element makeXmlElement(Document document, String prefix) { Element element = null; if (prefix == null) prefix = ""; if (document != null) element = document.createElement(prefix + this.getEntityName()); // else element = new ElementImpl(null, this.getEntityName()); if (element == null) return null; Iterator modelFields = this.getModelEntity().getFieldsIterator(); while (modelFields.hasNext()) { ModelField modelField = (ModelField) modelFields.next(); String name = modelField.getName(); String value = this.getString(name); if (value != null) { if (value.indexOf('\n') >= 0 || value.indexOf('\r') >= 0) { UtilXml.addChildElementCDATAValue(element, name, value, document); } else { element.setAttribute(name, value); } } } return element; } /** Writes XML text with an attribute or CDATA element for each field of the entity *@param writer A PrintWriter to write to *@param prefix A prefix to put in front of the entity name in the tag name */ public void writeXmlText(PrintWriter writer, String prefix) { final int indent = 4; if (prefix == null) prefix = ""; for (int i = 0; i < indent; i++) writer.print(' '); writer.print('<'); writer.print(prefix); writer.print(this.getEntityName()); // write attributes immediately and if a CDATA element is needed, put those in a Map for now Map cdataMap = FastMap.newInstance(); Iterator modelFields = this.getModelEntity().getFieldsIterator(); while (modelFields.hasNext()) { ModelField modelField = (ModelField) modelFields.next(); String name = modelField.getName(); String type = modelField.getType(); if (type != null && type.equals("blob")) { Object obj = get(name); boolean b1 = obj instanceof byte []; if (b1) { byte [] binData = (byte [])obj; String strData = new String(Base64.base64Encode(binData)); cdataMap.put(name, strData); } else { Debug.logWarning("Field:" + name + " is not of type 'byte[]'. obj: " + obj, module); } } else { String valueStr = this.getString(name); if (valueStr != null) { StringBuffer value = new StringBuffer(valueStr); boolean needsCdata = false; // check each character, if line-feed or carriage-return is found set needsCdata to true; also look for invalid characters for (int i = 0; i < value.length(); i++) { char curChar = value.charAt(i); /* Some common character for these invalid values, have seen these are mostly from MS Word, but may be part of some standard: 5 = ... 18 = apostrophe
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -