Search in sources :

Example 16 with EntityInfo

use of org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo in project jbosstools-hibernate by jbosstools.

the class AllEntitiesInfoCollector method resolveRelations.

/**
 * process all entities pairs iteratively:
 * firstly process pairs with more information about and
 * pairs with small information about - process in last order
 */
public void resolveRelations() {
    Iterator<Map.Entry<String, EntityInfo>> it = null;
    // resolve parent/child relations
    // in the case if a child has a @MappedSuperclass parent with version property
    // we shouldn't add version property to the child
    it = mapCUs_Info.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, EntityInfo> entry = it.next();
        EntityInfo entityInfo = entry.getValue();
        String javaProjectName = entityInfo.getJavaProjectName();
        String parentName = entityInfo.getFullyQualifiedParentName();
        if (hasMappedSuperclassVersion(javaProjectName, parentName)) {
            entityInfo.setAddVersionFlag(false);
        } else {
            entityInfo.setAddVersionFlag(true);
        }
        entityInfo.updateVersionImport(entityInfo.isAddVersionFlag());
    }
    // 
    // resolve connection relations
    int fromVariableCounter = 0;
    int fromMethodCounter = 0;
    // generate RefFieldInfoMap (for simple process)
    it = mapCUs_Info.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, EntityInfo> entry = it.next();
        EntityInfo entryInfo = entry.getValue();
        entryInfo.generateRefFieldInfoMap();
        fromVariableCounter += entryInfo.getFromVariableCounter();
        fromMethodCounter += entryInfo.getFromMethodCounter();
    }
    if (fromVariableCounter >= fromMethodCounter) {
        annotationStylePreference = AnnotStyle.FIELDS;
    } else {
        annotationStylePreference = AnnotStyle.GETTERS;
    }
    // update relations
    EntityProcessor ep = new EntityProcessor();
    // 0)
    // process the user prompts
    IConnector promptsConnector = new IConnector() {

        public boolean updateRelation() {
            if (pi == null) {
                return false;
            }
            if (pi.refEntityInfo == null || pi.refEntityInfo2 == null) {
                return false;
            }
            // no user prompt
            int hasPrompt = 0;
            // - use it as prompting from the user
            if ((pi.fieldId != null && pi.fieldId.equals(pi.refEntityInfo2.mappedBy))) {
                hasPrompt |= 1;
            }
            if ((pi.fieldId2 != null && pi.fieldId2.equals(pi.refEntityInfo.mappedBy))) {
                hasPrompt |= 2;
            }
            if (hasPrompt != 0) {
                // remember: in case of incorrect user prompts - we proceed his suggestions
                if (hasPrompt == 1) {
                    if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.ONE2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
                        pi.refEntityInfo.refType = RefType.MANY2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.ONE2MANY;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo2.refType == RefType.MANY2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2MANY;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.MANY2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo2.refType == RefType.MANY2MANY) {
                        pi.refEntityInfo.refType = RefType.MANY2MANY;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.MANY2MANY;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    }
                } else if (hasPrompt == 2) {
                    if (pi.refEntityInfo.refType == RefType.ONE2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId;
                        pi.refEntityInfo2.refType = RefType.ONE2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId2;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo.refType == RefType.ONE2MANY) {
                        pi.refEntityInfo.refType = RefType.ONE2MANY;
                        pi.refEntityInfo.mappedBy = pi.fieldId;
                        pi.refEntityInfo2.refType = RefType.MANY2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId2;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo.refType == RefType.MANY2ONE) {
                        pi.refEntityInfo.refType = RefType.MANY2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId;
                        pi.refEntityInfo2.refType = RefType.ONE2MANY;
                        pi.refEntityInfo2.mappedBy = pi.fieldId2;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo.refType == RefType.MANY2MANY) {
                        pi.refEntityInfo.refType = RefType.MANY2MANY;
                        pi.refEntityInfo.mappedBy = pi.fieldId;
                        pi.refEntityInfo2.refType = RefType.MANY2MANY;
                        pi.refEntityInfo2.mappedBy = pi.fieldId2;
                        updateOwner(pi);
                    }
                }
                // and it is possible this is not correct info... so try to adjust it.
                if (pi.refEntityInfo.refType == RefType.ONE2ONE) {
                    if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.ONE2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
                        pi.refEntityInfo.refType = RefType.MANY2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.ONE2MANY;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    }
                } else if (pi.refEntityInfo.refType == RefType.ONE2MANY) {
                    if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2MANY;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.MANY2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
                        pi.refEntityInfo.refType = RefType.MANY2MANY;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.MANY2MANY;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    }
                }
            }
            return true;
        }
    };
    ep.setConnector(promptsConnector);
    ep.enumEntityPairs();
    // prefer other mapping type to ManyToMany, so
    // 1)
    // first - try to assign other relations
    IConnector simpleRelConnector = new IConnector() {

        public boolean updateRelation() {
            if (pi == null) {
                return false;
            }
            if (pi.refEntityInfo == null || pi.refEntityInfo2 == null) {
                return false;
            }
            if (pi.refEntityInfo.mappedBy == null && pi.refEntityInfo2.mappedBy == null) {
                if (pi.refEntityInfo.refType == RefType.ONE2ONE) {
                    if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.ONE2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
                        pi.refEntityInfo.refType = RefType.MANY2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.ONE2MANY;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    }
                } else if (pi.refEntityInfo.refType == RefType.ONE2MANY) {
                    if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2MANY;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.MANY2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo2.refType == RefType.MANY2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2MANY;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.MANY2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    }
                } else if (pi.refEntityInfo.refType == RefType.MANY2ONE) {
                    if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
                        pi.refEntityInfo.refType = RefType.MANY2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.ONE2MANY;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    } else if (pi.refEntityInfo2.refType == RefType.MANY2ONE) {
                        pi.refEntityInfo.refType = RefType.ONE2ONE;
                        pi.refEntityInfo.mappedBy = pi.fieldId2;
                        pi.refEntityInfo2.refType = RefType.ONE2ONE;
                        pi.refEntityInfo2.mappedBy = pi.fieldId;
                        updateOwner(pi);
                    }
                }
            }
            return true;
        }
    };
    ep.setConnector(simpleRelConnector);
    ep.enumEntityPairs();
    // 2)
    // second - try to assign - ManyToMany
    // remember - here prefer other mapping type to ManyToMany
    IConnector m2mRelConnector = new IConnector() {

        public boolean updateRelation() {
            if (pi == null) {
                return false;
            }
            if (pi.refEntityInfo == null || pi.refEntityInfo2 == null) {
                return false;
            }
            if (pi.refEntityInfo.mappedBy == null && pi.refEntityInfo2.mappedBy == null) {
                if (pi.refEntityInfo.refType == RefType.ONE2MANY) {
                    if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
                        if (pi.refEntityInfo2.mappedBy == null) {
                            pi.refEntityInfo.refType = RefType.MANY2MANY;
                            pi.refEntityInfo.mappedBy = pi.fieldId2;
                            pi.refEntityInfo2.refType = RefType.MANY2MANY;
                            pi.refEntityInfo2.mappedBy = pi.fieldId;
                            updateOwner(pi);
                        }
                    }
                }
            }
            return true;
        }
    };
    ep.setConnector(m2mRelConnector);
    ep.enumEntityPairs();
    // 3)
    // third - try to assign - "single candidates"
    EntitySingleCandidateResolver escr = new EntitySingleCandidateResolver();
    escr.enumEntityPairs();
    // update import flags
    it = mapCUs_Info.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, EntityInfo> entry = it.next();
        EntityInfo entryInfo = entry.getValue();
        Iterator<Map.Entry<String, RefEntityInfo>> referencesIt = entryInfo.getReferences().entrySet().iterator();
        boolean isOne2One = false;
        boolean isOne2Many = false;
        boolean isMany2One = false;
        boolean isMany2Many = false;
        boolean isEnumerated = false;
        for (; referencesIt.hasNext(); ) {
            Map.Entry<String, RefEntityInfo> entry2 = referencesIt.next();
            RefEntityInfo refEntityInfo = entry2.getValue();
            if (refEntityInfo != null) {
                if (refEntityInfo.refType == RefType.ONE2ONE && !refEntityInfo.resolvedAnnotationName) {
                    isOne2One = true;
                } else if (refEntityInfo.refType == RefType.ONE2MANY && !refEntityInfo.resolvedAnnotationName) {
                    isOne2Many = true;
                } else if (refEntityInfo.refType == RefType.MANY2ONE && !refEntityInfo.resolvedAnnotationName) {
                    isMany2One = true;
                } else if (refEntityInfo.refType == RefType.MANY2MANY && !refEntityInfo.resolvedAnnotationName) {
                    isMany2Many = true;
                } else if (refEntityInfo.refType == RefType.ENUMERATED && !refEntityInfo.resolvedAnnotationName) {
                    isEnumerated = true;
                }
            }
        }
        if (isOne2One) {
            entryInfo.addRequiredImport(JPAConst.IMPORT_ONE2ONE);
        }
        if (isOne2Many) {
            entryInfo.addRequiredImport(JPAConst.IMPORT_ONE2MANY);
        }
        if (isMany2One) {
            entryInfo.addRequiredImport(JPAConst.IMPORT_MANY2ONE);
        }
        if (isMany2Many) {
            entryInfo.addRequiredImport(JPAConst.IMPORT_MANY2MANY);
        }
        if (isEnumerated) {
            entryInfo.addRequiredImport(JPAConst.IMPORT_ENUMERATED);
        }
    }
    // re-generate RefFieldInfoMap (for simple process)
    it = mapCUs_Info.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, EntityInfo> entry = it.next();
        EntityInfo entryInfo = entry.getValue();
        entryInfo.generateRefFieldInfoMap();
    }
    // if the parent has primary id - child should not generate it
    it = mapCUs_Info.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, EntityInfo> entry = it.next();
        EntityInfo entryInfo = entry.getValue();
        adjustParentId(entryInfo);
    }
}
Also used : RefEntityInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefEntityInfo) EntityInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo) RefEntityInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefEntityInfo) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

EntityInfo (org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo)16 RefEntityInfo (org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefEntityInfo)10 Map (java.util.Map)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 HashMap (java.util.HashMap)4 TreeMap (java.util.TreeMap)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 ArrayList (java.util.ArrayList)2 Entry (java.util.Map.Entry)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 AllEntitiesInfoCollector (org.hibernate.eclipse.jdt.ui.internal.jpa.collect.AllEntitiesInfoCollector)2 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)2 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)1