📄 modelreader.java
字号:
me.addViewEntity(curViewEntity); } } // auto-create relationships TreeSet orderedMessages = new TreeSet(); Iterator entityNamesIter = new TreeSet(this.getEntityNames()).iterator(); while (entityNamesIter.hasNext()) { String curEntityName = (String) entityNamesIter.next(); ModelEntity curModelEntity = this.getModelEntity(curEntityName); if (curModelEntity instanceof ModelViewEntity) { // for view-entities auto-create relationships for all member-entity relationships that have all corresponding fields in the view-entity } else { // for entities auto-create many relationships for all type one relationships // just in case we add a new relation to the same entity, keep in a separate list and add them at the end List newSameEntityRelations = FastList.newInstance(); Iterator relationsIter = curModelEntity.getRelationsIterator(); while (relationsIter.hasNext()) { ModelRelation modelRelation = (ModelRelation) relationsIter.next(); if (("one".equals(modelRelation.getType()) || "one-nofk".equals(modelRelation.getType())) && !modelRelation.isAutoRelation()) { ModelEntity relatedEnt = null; try { relatedEnt = this.getModelEntity(modelRelation.getRelEntityName()); } catch (GenericModelException e) { throw new GenericModelException("Error getting related entity [" + modelRelation.getRelEntityName() + "] definition from entity [" + curEntityName + "]", e); } if (relatedEnt != null) { // don't do relationship to the same entity, unless title is "Parent", then do a "Child" automatically String targetTitle = modelRelation.getTitle(); if (curModelEntity.getEntityName().equals(relatedEnt.getEntityName()) && "Parent".equals(targetTitle)) { targetTitle = "Child"; } // create the new relationship even if one exists so we can show what we are looking for in the info message ModelRelation newRel = new ModelRelation(); newRel.setModelEntity(relatedEnt); newRel.setRelEntityName(curModelEntity.getEntityName()); newRel.setTitle(targetTitle); newRel.setAutoRelation(true); Set curEntityKeyFields = FastSet.newInstance(); for (int kmn = 0; kmn < modelRelation.getKeyMapsSize(); kmn++) { ModelKeyMap curkm = modelRelation.getKeyMap(kmn); ModelKeyMap newkm = new ModelKeyMap(); newRel.addKeyMap(newkm); newkm.setFieldName(curkm.getRelFieldName()); newkm.setRelFieldName(curkm.getFieldName()); curEntityKeyFields.add(curkm.getFieldName()); } // decide whether it should be one or many by seeing if the key map represents the complete pk of the relEntity if (curModelEntity.containsAllPkFieldNames(curEntityKeyFields)) { // always use one-nofk, we don't want auto-fks getting in for these automatic ones newRel.setType("one-nofk"); // to keep it clean, remove any additional keys that aren't part of the PK List curPkFieldNames = curModelEntity.getPkFieldNames(); Iterator nrkmIter = newRel.getKeyMapsIterator(); while (nrkmIter.hasNext()) { ModelKeyMap nrkm = (ModelKeyMap) nrkmIter.next(); String checkField = nrkm.getRelFieldName(); if (!curPkFieldNames.contains(checkField)) { nrkmIter.remove(); } } } else { newRel.setType("many"); } ModelRelation existingRelation = relatedEnt.getRelation(targetTitle + curModelEntity.getEntityName()); if (existingRelation == null) { numAutoRelations++; if (curModelEntity.getEntityName().equals(relatedEnt.getEntityName())) { newSameEntityRelations.add(newRel); } else { relatedEnt.addRelation(newRel); } } else { if (newRel.equals(existingRelation)) { // don't warn if the target title+entity = current title+entity if (!(targetTitle + curModelEntity.getEntityName()).equals(modelRelation.getTitle() + modelRelation.getRelEntityName())) { //String errorMsg = "Relation already exists to entity [] with title [" + targetTitle + "],from entity []"; String message = "Entity [" + relatedEnt.getPackageName() + ":" + relatedEnt.getEntityName() + "] already has identical relationship to entity [" + curModelEntity.getEntityName() + "] title [" + targetTitle + "]; would auto-create: type [" + newRel.getType() + "] and fields [" + newRel.keyMapString(",", "") + "]"; orderedMessages.add(message); } } else { String message = "Existing relationship with the same name, but different specs found from what would be auto-created for Entity [" + relatedEnt.getEntityName() + "] ant relationship to entity [" + curModelEntity.getEntityName() + "] title [" + targetTitle + "]; would auto-create: type [" + newRel.getType() + "] and fields [" + newRel.keyMapString(",", "") + "]"; //Debug.logInfo(message, module); } } } else { String errorMsg = "Could not find related entity [" + modelRelation.getRelEntityName() + "], no reverse relation added."; Debug.logWarning(errorMsg, module); } } } if (newSameEntityRelations.size() > 0) { Iterator newRelsIter = newSameEntityRelations.iterator(); while (newRelsIter.hasNext()) { ModelRelation newRel = (ModelRelation) newRelsIter.next(); curModelEntity.addRelation(newRel); } } } } Iterator omIter = orderedMessages.iterator(); while (omIter.hasNext()) { Debug.logInfo((String) omIter.next(), module); } Debug.log("FINISHED LOADING ENTITIES - ALL FILES; #Entities=" + numEntities + " #ViewEntities=" + numViewEntities + " #Fields=" + numFields + " #Relationships=" + numRelations + " #AutoRelationships=" + numAutoRelations, module); } } } return entityCache; } /** rebuilds the resourceHandlerEntities Map of Collections based on the current * entityResourceHandlerMap Map, must be done whenever a manual change is made to the * entityResourceHandlerMap Map after the initial load to make them consistent again. */ public void rebuildResourceHandlerEntities() { resourceHandlerEntities = FastMap.newInstance(); Iterator entityResourceIter = entityResourceHandlerMap.entrySet().iterator(); while (entityResourceIter.hasNext()) { Map.Entry entry = (Map.Entry) entityResourceIter.next(); // add entityName to appropriate resourceHandlerEntities collection Collection resourceHandlerEntityNames = (Collection) resourceHandlerEntities.get(entry.getValue()); if (resourceHandlerEntityNames == null) { resourceHandlerEntityNames = FastList.newInstance(); resourceHandlerEntities.put(entry.getValue(), resourceHandlerEntityNames); } resourceHandlerEntityNames.add(entry.getKey()); } } public Iterator getResourceHandlerEntitiesKeyIterator() { if (resourceHandlerEntities == null) return null; return resourceHandlerEntities.keySet().iterator(); } public Collection getResourceHandlerEntities(ResourceHandler resourceHandler) { if (resourceHandlerEntities == null) return null; return (Collection) resourceHandlerEntities.get(resourceHandler); } public void addEntityToResourceHandler(String entityName, String loaderName, String location) { entityResourceHandlerMap.put(entityName, new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, loaderName, location)); } public ResourceHandler getEntityResourceHandler(String entityName) { return (ResourceHandler) entityResourceHandlerMap.get(entityName); } /** Gets an Entity object based on a definition from the specified XML Entity descriptor file. * @param entityName The entityName of the Entity definition to use. * @return An Entity object describing the specified entity of the specified descriptor file. */ public ModelEntity getModelEntity(String entityName) throws GenericEntityException { if (entityName == null) { throw new IllegalArgumentException("Tried to find entity definition for a null entityName"); } Map ec = getEntityCache(); if (ec == null) { throw new GenericEntityConfException("ERROR: Unable to load Entity Cache"); } ModelEntity modelEntity = (ModelEntity) ec.get(entityName); if (modelEntity == null) { throw new GenericModelException("Could not find definition for entity name " + entityName); } return modelEntity; } public ModelEntity getModelEntityNoCheck(String entityName) { Map ec = null; try { ec = getEntityCache(); } catch (GenericEntityException e) { Debug.logError(e, "Error getting entity cache", module); } if (ec == null) { return null; } ModelEntity modelEntity = (ModelEntity) ec.get(entityName); return modelEntity; } /** Creates a Iterator with the entityName of each Entity defined in the specified XML Entity Descriptor file. * @return A Iterator of entityName Strings */ public Iterator getEntityNamesIterator() throws GenericEntityException { Collection collection = getEntityNames(); if (collection != null) { return collection.iterator(); } else { return null; } } /** Creates a Collection with the entityName of each Entity defined in the specified XML Entity Descriptor file. * @return A Collection of entityName Strings */ public Collection getEntityNames() throws GenericEntityException { Map ec = getEntityCache(); if (ec == null) { throw new GenericEntityConfException("ERROR: Unable to load Entity Cache"); } return ec.keySet(); } ModelEntity createModelEntity(Element entityElement, UtilTimer utilTimer, ModelInfo def) { if (entityElement == null) return null; this.numEntities++; ModelEntity entity = new ModelEntity(this, entityElement, utilTimer, def); return entity; } ModelEntity createModelViewEntity(Element entityElement, UtilTimer utilTimer, ModelInfo def) { if (entityElement == null) return null; this.numViewEntities++; ModelViewEntity entity = new ModelViewEntity(this, entityElement, utilTimer, def); return entity; } public ModelRelation createRelation(ModelEntity entity, Element relationElement) { this.numRelations++; ModelRelation relation = new ModelRelation(entity, relationElement); return relation; } public ModelField findModelField(ModelEntity entity, String fieldName) { for (int i = 0; i < entity.fields.size(); i++) { ModelField field = (ModelField) entity.fields.get(i); if (field.name.compareTo(fieldName) == 0) { return field; } } return null; } public ModelField createModelField(String name, String type, String colName, boolean isPk) { this.numFields++; ModelField field = new ModelField(name, type, colName, isPk); return field; } public ModelField createModelField(Element fieldElement) { if (fieldElement == null) { return null; } this.numFields++; ModelField field = new ModelField(fieldElement); return field; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -