📄 modelviewentity.java
字号:
Map.Entry entry = (Map.Entry) meIter.next(); ModelMemberEntity modelMemberEntity = (ModelMemberEntity) entry.getValue(); String aliasedEntityName = modelMemberEntity.getEntityName(); ModelEntity aliasedEntity = modelReader.getModelEntityNoCheck(aliasedEntityName); if (aliasedEntity == null) { continue; } memberModelEntities.put(entry.getKey(), aliasedEntity); Iterator aliasedFieldIterator = aliasedEntity.getFieldsIterator(); while (aliasedFieldIterator.hasNext()) { ModelField aliasedModelField = (ModelField) aliasedFieldIterator.next(); ModelField newModelField = new ModelField(); for (int i = 0; i < aliasedModelField.getValidatorsSize(); i++) { newModelField.addValidator(aliasedModelField.getValidator(i)); } newModelField.setColName(modelMemberEntity.getEntityAlias() + "." + aliasedModelField.getColName()); newModelField.setName(modelMemberEntity.getEntityAlias() + "." + aliasedModelField.getName()); newModelField.setType(aliasedModelField.getType()); newModelField.setIsPk(false); aliasedModelEntity.addField(newModelField); } } expandAllAliasAlls(modelReader); for (int i = 0; i < aliases.size(); i++) { ModelAlias alias = (ModelAlias) aliases.get(i); ModelField field = new ModelField(); field.setModelEntity(this); field.name = alias.name; // if this is a groupBy field, add it to the groupBys list if (alias.groupBy) { this.groupBys.add(field); } // show a warning if function is specified and groupBy is true if (UtilValidate.isNotEmpty(alias.function) && alias.groupBy) { Debug.logWarning("The view-entity alias with name=" + alias.name + " has a function value and is specified as a group-by field; this may be an error, but is not necessarily.", module); } if (alias.isComplexAlias()) { // if this is a complex alias, make a complex column name... StringBuffer colNameBuffer = new StringBuffer(); StringBuffer fieldTypeBuffer = new StringBuffer(); alias.makeAliasColName(colNameBuffer, fieldTypeBuffer, this, modelReader); field.colName = colNameBuffer.toString(); field.type = fieldTypeBuffer.toString(); field.isPk = false; } else { ModelEntity aliasedEntity = getAliasedEntity(alias.entityAlias, modelReader); ModelField aliasedField = getAliasedField(aliasedEntity, alias.field, modelReader); if (aliasedField == null) { Debug.logError("[ModelViewEntity.populateFields (" + this.getEntityName() + ")] ERROR: could not find ModelField for field name \"" + alias.field + "\" on entity with name: " + aliasedEntity.getEntityName(), module); continue; } if (alias.isPk != null) { field.isPk = alias.isPk.booleanValue(); } else { field.isPk = aliasedField.isPk; } field.type = aliasedField.type; field.validators = aliasedField.validators; field.colName = alias.entityAlias + "." + SqlJdbcUtil.filterColName(aliasedField.colName); } this.fields.add(field); if (field.isPk) { this.pks.add(field); } else { this.nopks.add(field); } if ("count".equals(alias.function) || "count-distinct".equals(alias.function)) { // if we have a "count" function we have to change the type field.type = "numeric"; } if (UtilValidate.isNotEmpty(alias.function)) { String prefix = (String) functionPrefixMap.get(alias.function); if (prefix == null) { Debug.logWarning("Specified alias function [" + alias.function + "] not valid; must be: min, max, sum, avg, count or count-distinct; using a column name with no function function", module); } else { field.colName = prefix + field.colName + ")"; } } } } protected ModelConversion getOrCreateModelConversion(String aliasName) { ModelEntity member = getMemberModelEntity(aliasName); if (member == null) { Debug.logWarning("No member found for aliasName - " + aliasName, module); throw new RuntimeException("Cannot create View Entity"); } Map aliasConversions = (HashMap) conversions.get(member.getEntityName()); if (aliasConversions == null) { aliasConversions = new HashMap(); conversions.put(member.getEntityName(), aliasConversions); } ModelConversion conversion = (ModelConversion) aliasConversions.get(aliasName); if (conversion == null) { conversion = new ModelConversion(aliasName, member); aliasConversions.put(aliasName, conversion); } return conversion; } public void populateReverseLinks() { Map containedModelFields = new HashMap(); Iterator it = getAliasesIterator(); while (it.hasNext()) { ModelViewEntity.ModelAlias alias = (ModelViewEntity.ModelAlias) it.next(); ModelEntity member = getMemberModelEntity(alias.getEntityAlias()); ModelConversion conversion = getOrCreateModelConversion(alias.getEntityAlias()); conversion.addConversion(alias.getField(), alias.getName()); ModelField field = (ModelField) member.getField(alias.getField()); List aliases = (List)containedModelFields.get(alias.getField()); if (aliases == null) { aliases = new ArrayList(); containedModelFields.put(alias.getField(), aliases); } aliases.add(alias.getName()); } it = getViewLinksIterator(); while (it.hasNext()) { ModelViewEntity.ModelViewLink link = (ModelViewEntity.ModelViewLink) it.next(); String leftAlias = link.getEntityAlias(); String rightAlias = link.getRelEntityAlias(); ModelConversion leftConversion = getOrCreateModelConversion(leftAlias); ModelConversion rightConversion = getOrCreateModelConversion(rightAlias); Iterator it2 = link.getKeyMapsIterator(); Debug.logVerbose(leftAlias + "<->" + rightAlias, module); while (it2.hasNext()) { ModelKeyMap mkm = (ModelKeyMap) it2.next(); String leftFieldName = mkm.getFieldName(); String rightFieldName = mkm.getRelFieldName(); rightConversion.addAllAliasConversions((List) containedModelFields.get(leftFieldName), rightFieldName); leftConversion.addAllAliasConversions((List) containedModelFields.get(rightFieldName), leftFieldName); } } it = conversions.entrySet().iterator(); int[] currentIndex = new int[conversions.size()]; int[] maxIndex = new int[conversions.size()]; ModelConversion[][] allConversions = new ModelConversion[conversions.size()][]; int i = 0; while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Map aliasConversions = (Map) entry.getValue(); currentIndex[i] = 0; maxIndex[i] = aliasConversions.size(); allConversions[i] = new ModelConversion[aliasConversions.size()]; Iterator it2 = aliasConversions.values().iterator(); for (int j = 0; it2.hasNext() && j < aliasConversions.size(); j++) { allConversions[i][j] = (ModelConversion) it2.next(); } i++; } int ptr = 0; ModelConversion[] currentConversions = new ModelConversion[conversions.size()]; for (int j = 0, k; j < currentIndex.length; j++) { for (int l = 0; l < maxIndex[ j ]; l++ ) { while (true) { for (i = 0, k = 0; i < currentIndex.length; i++) { if (i == j && currentIndex[i] == l) continue; currentConversions[k++] = allConversions[i][currentIndex[i]]; } Debug.logVerbose(j + "," + l + ":" + Arrays.asList(currentConversions), module); while (ptr < currentIndex.length && ++currentIndex[ptr] == maxIndex[ptr]) { currentIndex[ptr] = 0; ptr++; } if (ptr == currentIndex.length) break; ptr = 0; } } } Debug.logVerbose(this + ":" + conversions, module); } public List convert(String fromEntityName, Map data) { Map foo = (Map) conversions.get(fromEntityName); if (foo == null) return null; Iterator it = foo.values().iterator(); List values = new ArrayList(foo.size()); while (it.hasNext()) { ModelConversion conversion = (ModelConversion) it.next(); values.add(conversion.convert(data)); } return values; } /** * Go through all aliasAlls and create an alias for each field of each member entity */ private void expandAllAliasAlls(ModelReader modelReader) { Iterator aliasAllIter = aliasAlls.iterator(); while (aliasAllIter.hasNext()) { ModelAliasAll aliasAll = (ModelAliasAll) aliasAllIter.next(); String prefix = aliasAll.getPrefix(); ModelMemberEntity modelMemberEntity = (ModelMemberEntity) memberModelMemberEntities.get(aliasAll.getEntityAlias()); if (modelMemberEntity == null) { Debug.logError("Member entity referred to in alias-all not found, ignoring: " + aliasAll.getEntityAlias(), module); continue; } String aliasedEntityName = modelMemberEntity.getEntityName(); ModelEntity aliasedEntity = modelReader.getModelEntityNoCheck(aliasedEntityName); if (aliasedEntity == null) { Debug.logError("Entity referred to in member-entity " + aliasAll.getEntityAlias() + " not found, ignoring: " + aliasedEntityName, module); continue; } List entFieldList = aliasedEntity.getAllFieldNames(); if (entFieldList == null) { Debug.logError("Entity referred to in member-entity " + aliasAll.getEntityAlias() + " has no fields, ignoring: " + aliasedEntityName, module); continue; } Iterator fieldnamesIterator = entFieldList.iterator(); while (fieldnamesIterator.hasNext()) { // now merge the lists, leaving out any that duplicate an existing alias name String fieldName = (String) fieldnamesIterator.next(); String aliasName = fieldName; ModelField modelField = aliasedEntity.getField(fieldName); if (modelField.getIsAutoCreatedInternal()) { // never auto-alias these continue; } if (aliasAll.shouldExclude(fieldName)) { // if specified as excluded, leave it out continue; } if (UtilValidate.isNotEmpty(prefix)) { StringBuffer newAliasBuffer = new StringBuffer(prefix); //make sure the first letter is uppercase to delineate the field name newAliasBuffer.append(Character.toUpperCase(aliasName.charAt(0))); newAliasBuffer.append(aliasName.substring(1)); aliasName = newAliasBuffer.toString(); } ModelAlias existingAlias = this.getAlias(aliasName); if (existingAlias != null) { //log differently if this is part of a view-link key-map because that is a common case when a field will be auto-expanded multiple times boolean isInViewLink = false; Iterator viewLinkIter = this.getViewLinksIterator(); while (viewLinkIter.hasNext() && !isInViewLink) { ModelViewLink modelViewLink = (ModelViewLink) viewLinkIter.next(); boolean isRel = false; if (modelViewLink.getRelEntityAlias().equals(aliasAll.getEntityAlias())) { isRel = true; } else if (!modelViewLink.getEntityAlias().equals(aliasAll.getEntityAlias())) { // not the rel-entity-alias or the entity-alias, so move along continue; } Iterator keyMapIter = modelViewLink.getKeyMapsIterator(); while (keyMapIter.hasNext() && !isInViewLink) { ModelKeyMap modelKeyMap = (ModelKeyMap) keyMapIter.next(); if (!isRel && modelKeyMap.getFieldName().equals(fieldName)) { isInViewLink = true; } else if (isRel && modelKeyMap.getRelFieldName().equals(fieldName)) { isInViewLink = true; } } } //already exists, oh well... probably an override, but log just in case String warnMsg = "Throwing out field alias in view entity " + this.getEntityName() + " because one already exists with the alias name [" + aliasName + "] and field name [" + modelMemberEntity.getEntityAlias() + "(" + aliasedEntity.getEntityName() + ")." + fieldName + "], existing field name is [" + existingAlias.getEntityAlias() + "." + existingAlias.getField() + "]"; if (isInViewLink) { Debug.logVerbose(warnMsg, module); } else { Debug.logInfo(warnMsg, module); } continue; } ModelAlias expandedAlias = new ModelAlias(); expandedAlias.name = aliasName; expandedAlias.field = fieldName; expandedAlias.entityAlias = aliasAll.getEntityAlias(); expandedAlias.isFromAliasAll = true; expandedAlias.colAlias = ModelUtil.javaNameToDbName(UtilXml.checkEmpty(expandedAlias.name)); aliases.add(expandedAlias); } } } public String toString() { return "ModelViewEntity[" + getEntityName() + "]"; } public static class ModelMemberEntity implements Serializable { protected String entityAlias = ""; protected String entityName = ""; public ModelMemberEntity(String entityAlias, String entityName) { this.entityAlias = entityAlias; this.entityName = entityName; } public String getEntityAlias() { return this.entityAlias; } public String getEntityName() { return this.entityName; } } public static class ModelAliasAll implements Serializable { protected String entityAlias = ""; protected String prefix = ""; protected Set fieldsToExclude = null; protected ModelAliasAll() {} public ModelAliasAll(String entityAlias, String prefix) { this.entityAlias = entityAlias; this.prefix = prefix; } public ModelAliasAll(Element aliasAllElement) { this.entityAlias = UtilXml.checkEmpty(aliasAllElement.getAttribute("entity-alias")); this.prefix = UtilXml.checkEmpty(aliasAllElement.getAttribute("prefix")); List excludes = UtilXml.childElementList(aliasAllElement, "exclude"); if (excludes != null && excludes.size() > 0) { this.fieldsToExclude = new HashSet(); Iterator excludeIter = excludes.iterator(); while (excludeIter.hasNext()) { Element excludeElement = (Element) excludeIter.next(); this.fieldsToExclude.add(excludeElement.getAttribute("field"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -