1 19 20 package org.netbeans.modules.j2ee.ejbcore.patterns; 21 22 23 import java.util.ArrayList ; 24 import javax.lang.model.element.Element; 25 import javax.lang.model.element.TypeElement; 26 import org.netbeans.api.java.source.CompilationController; 27 import org.netbeans.api.project.FileOwnerQuery; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.SourceGroup; 30 import org.netbeans.modules.j2ee.common.Util; 31 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField; 32 import org.netbeans.modules.j2ee.dd.api.ejb.CmrField; 33 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 34 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider; 35 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb; 36 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelation; 37 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelationshipRole; 38 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans; 39 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 40 import org.netbeans.modules.j2ee.dd.api.ejb.Relationships; 41 import org.openide.ErrorManager; 42 import org.openide.filesystems.FileObject; 43 44 48 public class DTOHelper { 49 private final Project project; 50 private EjbJar ejbJar; 51 private Entity entity; 52 private final TypeElement classElm; 53 private EnterpriseBeans beans; 54 private final FileObject fileObject; 55 56 59 public DTOHelper(CompilationController controller, Element feature) { 60 classElm = (TypeElement) feature.getEnclosingElement(); 61 fileObject = controller.getFileObject(); 62 project = FileOwnerQuery.getOwner(fileObject); 63 64 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(fileObject); 65 66 67 DDProvider provider = DDProvider.getDefault(); 68 try{ 69 ejbJar = provider.getMergedDDRoot(ejbModule.getMetadataUnit()); 70 beans = ejbJar.getEnterpriseBeans(); 71 72 entity = (Entity) beans.findBeanByName( 73 EnterpriseBeans.ENTITY, 74 Ejb.EJB_CLASS, classElm.getQualifiedName().toString()); 75 }catch(java.io.IOException ex) { 76 ErrorManager.getDefault().notify(ex); 78 } 79 } 80 81 84 public CmrField[] getCmrFields() { 85 ArrayList <CmrField> cmrFields = new ArrayList <CmrField>(); 86 EjbRelation[] ejbRelations = getRelation(); 87 if(ejbRelations != null){ 88 for (EjbRelation ejbRelation : ejbRelations) { 89 EjbRelationshipRole role = ejbRelation.getEjbRelationshipRole(); 90 if(isUseEjb(role)) { 91 cmrFields.add(role.getCmrField()); 92 } 93 role = ejbRelation.getEjbRelationshipRole2(); 94 if(isUseEjb(role)) { 95 cmrFields.add(role.getCmrField()); 96 } 97 } 98 } 99 return cmrFields.toArray(new CmrField[0]); 100 } 101 102 105 private EjbRelation[] getRelation() { 106 Relationships relation = ejbJar.getSingleRelationships(); 107 return (relation != null) ? relation.getEjbRelation() : null; 108 } 109 110 114 public boolean isMultiple(CmrField cmrField) { 115 boolean isMultiple = false; 116 EjbRelation[] ejbRelations = getRelation(); 117 if(ejbRelations != null){ 118 for (EjbRelation ejbRelation : ejbRelations) { 119 EjbRelationshipRole role = ejbRelation.getEjbRelationshipRole(); 120 if(cmrField == role.getCmrField() && 121 role.MULTIPLICITY_MANY.equals(ejbRelation.getEjbRelationshipRole2(). 122 getMultiplicity())) { 123 isMultiple = true; 124 } 125 role = ejbRelation.getEjbRelationshipRole2(); 126 if(cmrField == role.getCmrField() && 127 role.MULTIPLICITY_MANY.equals(ejbRelation.getEjbRelationshipRole(). 128 getMultiplicity())) { 129 isMultiple = true; 130 } 131 } 132 } 133 return isMultiple; 134 } 135 136 140 public String getOppositeFieldType(CmrField cmrField){ 141 String ejbName; 142 String cmrFieldType2 = ""; 143 EjbRelation[] ejbRelations = getRelation(); 144 if(ejbRelations != null) { 145 for (EjbRelation ejbRelation : ejbRelations) { 146 EjbRelationshipRole role = ejbRelation.getEjbRelationshipRole(); 147 if(cmrField == role.getCmrField()) { 148 ejbName = ejbRelation.getEjbRelationshipRole2().getRelationshipRoleSource().getEjbName(); 149 cmrFieldType2 = findLocalIntNameByEntityName(ejbName); 150 break; 151 } 152 role = ejbRelation.getEjbRelationshipRole2(); 153 if(cmrField == role.getCmrField()) { 154 ejbName = ejbRelation.getEjbRelationshipRole().getRelationshipRoleSource().getEjbName(); 155 cmrFieldType2 = findLocalIntNameByEntityName(ejbName); 156 break; 157 } 158 } 159 } 160 return cmrFieldType2; 161 } 162 163 167 public String findLocalIntNameByEntityName(String entityName) { 168 Entity[] ents = beans.getEntity(); 169 String localInterfaceName = ""; 170 for(int i = 0; i < ents.length; i++) { 171 if(entityName.equals(ents[i].getEjbName())) { 172 localInterfaceName = ents[i].getLocal(); 173 break; 174 } 175 } 176 return localInterfaceName; 177 } 178 179 183 public String findEntityNameByLocalInt(String interf) { 184 Entity[] ents = beans.getEntity(); 185 String ejbName = ""; 186 for(int i = 0; i < ents.length; i++) { 187 if(interf.equals(ents[i].getLocal())) { 188 ejbName = ents[i].getEjbName(); 189 break; 190 } 191 } 192 return ejbName; 193 } 194 195 199 private boolean isUseEjb(EjbRelationshipRole role) { 200 return role != null && 201 role.getRelationshipRoleSource() != null && 202 role.getRelationshipRoleSource().getEjbName().equals(getEntityName()); 203 } 204 205 208 public CmpField[] getCmpFields() { 209 return entity.getCmpField(); 210 } 211 212 216 public String getFieldType(String fieldName) { 217 String returnType = ""; 218 return returnType; 222 } 223 224 227 public String getEntityName() { 229 return entity.getEjbName(); 230 } 231 232 235 public String getLocalName(){ 236 return entity.getLocal(); 237 } 238 239 242 public String getFullName() { 243 return classElm.getQualifiedName().toString(); 244 } 245 246 249 public String getPackage(){ 250 return getFullName().substring(0, 251 getFullName().lastIndexOf('.')); 252 } 253 254 257 public Project getProject() { 258 return project; 259 } 260 261 264 public SourceGroup getSourceGroup(){ 265 SourceGroup entityFolder = null; 266 SourceGroup[] folders= Util.getJavaSourceGroups(project); 267 268 for(int i = 0; i < folders.length; i++){ 269 if(folders[i].contains(fileObject)){ 270 entityFolder = folders[i]; 271 } 272 } 273 return entityFolder; 274 } 275 } 276 | Popular Tags |