1 ///*2 // * The contents of this file are subject to the terms of the Common Development3 // * and Distribution License (the License). You may not use this file except in4 // * compliance with the License.5 // *6 // * You can obtain a copy of the License at http://www.netbeans.org/cddl.html7 // * or http://www.netbeans.org/cddl.txt.8 // *9 // * When distributing Covered Code, include this CDDL Header Notice in each file10 // * and include the License file at http://www.netbeans.org/cddl.txt.11 // * If applicable, add the following below the CDDL Header, with the fields12 // * enclosed by brackets [] replaced by your own identifying information:13 // * "Portions Copyrighted [year] [name of copyright owner]"14 // *15 // * The Original Software is NetBeans. The Initial Developer of the Original16 // * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun17 // * Microsystems, Inc. All Rights Reserved.18 // */19 //20 //package org.netbeans.modules.j2ee.persistence.wizard;21 //22 //import java.util.ArrayList;23 //import java.util.HashMap;24 //import java.util.HashSet;25 //import java.util.Iterator;26 //import java.util.List;27 //import java.util.Set;28 //import javax.swing.event.ChangeListener;29 //import org.netbeans.jmi.javamodel.Annotation;30 //import org.netbeans.jmi.javamodel.Feature;31 //import org.netbeans.jmi.javamodel.Field;32 //import org.netbeans.jmi.javamodel.JavaClass;33 //import org.netbeans.jmi.javamodel.Method;34 //import org.netbeans.jmi.javamodel.ParameterizedType;35 //import org.netbeans.jmi.javamodel.Type;36 //import org.netbeans.modules.j2ee.common.JMIUtils;37 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.Entity;38 //import org.netbeans.modules.j2ee.persistence.wizard.fromdb.ChangeSupport;39 //40 ///**41 // *42 // * @author Pavel Buzek43 // */44 // TODO: RETOUCHE45 //public class EntityClosure {46 // 47 // private final ChangeSupport changeSupport = new ChangeSupport(this);48 // 49 // private Set<Entity> availableEntities = new HashSet<Entity>();50 // private Set<Entity> wantedEntities = new HashSet<Entity>();51 // private Set<Entity> selectedEntities = new HashSet<Entity>();52 // private Set<Entity> referencedEntities = new HashSet<Entity>();53 // private HashMap<String, Entity> name2entity = new HashMap<String, Entity>();54 // 55 // private boolean closureEnabled = true;56 // 57 // public EntityClosure(Set<Entity> entities) {58 // addAvaliableEntities(entities);59 // }60 // 61 // public void addAvaliableEntities(Set<Entity> entities) {62 // availableEntities.addAll(entities);63 // for(Entity e : entities) {64 // name2entity.put(e.getClass2(), e);65 // }66 // changeSupport.fireChange();67 // }68 // 69 // public void addChangeListener(ChangeListener listener) {70 // changeSupport.addChangeListener(listener);71 // }72 // 73 // public Set<Entity> getAvailableEntities() {74 // return availableEntities;75 // }76 // 77 // public Set<Entity> getWantedEntities() {78 // return wantedEntities;79 // }80 // 81 // public Set<Entity> getSelectedEntities() {82 // return selectedEntities;83 // }84 // 85 // public void addEntities(Set<Entity> entities) {86 // if (isClosureEnabled()) {87 // if (wantedEntities.addAll(entities)) {88 // Set<Entity> refEntities = getReferencedEntitiesTransitively(entities);89 // Set<Entity> addedEntities = new HashSet<Entity>(entities);90 // addedEntities.addAll(refEntities);91 // 92 // selectedEntities.addAll(addedEntities);93 // referencedEntities.addAll(refEntities);94 // availableEntities.removeAll(addedEntities);95 // 96 // changeSupport.fireChange();97 // }98 // } else {99 // wantedEntities.addAll(entities);100 // selectedEntities.addAll(entities);101 // availableEntities.removeAll(entities);102 // 103 // changeSupport.fireChange();104 // }105 // }106 // 107 // public void removeEntities(Set<Entity> Entities) {108 // if (isClosureEnabled()) {109 // if (wantedEntities.removeAll(Entities)) {110 // redoClosure();111 // 112 // changeSupport.fireChange();113 // }114 // } else {115 // wantedEntities.removeAll(Entities);116 // selectedEntities.removeAll(Entities);117 // availableEntities.addAll(Entities);118 // 119 // changeSupport.fireChange();120 // }121 // }122 // 123 // public void addAllEntities() {124 // wantedEntities.addAll(availableEntities);125 // 126 // if (isClosureEnabled()) {127 // redoClosure();128 // 129 // changeSupport.fireChange();130 // } else {131 // selectedEntities.addAll(wantedEntities);132 // availableEntities.clear();133 // 134 // changeSupport.fireChange();135 // }136 // }137 // 138 // public void removeAllEntities() {139 // availableEntities.addAll(selectedEntities);140 // wantedEntities.clear();141 // selectedEntities.clear();142 // referencedEntities.clear();143 // 144 // changeSupport.fireChange();145 // }146 // 147 // /**148 // * Returns the tables transitively referenced by the contents of the tables parameter149 // * (not including tables passed in this parameter). If a table references itself,150 // * it is not added to the result.151 // */152 // private Set getReferencedEntitiesTransitively(Set<Entity> entities) {153 // Queue<Entity> entityQueue = new Queue<Entity>(entities);154 // Set<Entity> refEntities = new HashSet<Entity>();155 // 156 // while (!entityQueue.isEmpty()) {157 // Entity entity = entityQueue.poll();158 // 159 // Set<Entity> referenced = getReferencedEntities(entity);160 // for (Entity refEntity : referenced) {161 // 162 // if (!refEntity.equals(entity)) {163 // refEntities.add(refEntity);164 // }165 // entityQueue.offer(refEntity);166 // }167 // }168 // 169 // return refEntities;170 // }171 // 172 // private Set<Entity> getReferencedEntities(Entity entity) {173 // Set<Entity> referenced = new HashSet<Entity>();174 // JavaClass jc = JMIUtils.findClass(entity.getClass2());175 // List<Feature> features;176 // if (isFieldAccess(jc)) {177 // for (Field f : JMIUtils.getFields(jc)) {178 // Type type = f.getType();179 // addReferences(referenced, type);180 // }181 // } else {182 // for (Method m : JMIUtils.getMethods(jc)) {183 // if (m.getName().startsWith("get")) { //NOI18N184 // Type type = m.getType();185 // addReferences(referenced, type);186 // }187 // }188 // }189 // 190 // return referenced;191 // }192 //193 // private void addReferences(final Set<Entity> referenced, final Type type) {194 // String clsName = null;195 // if (type instanceof ParameterizedType) {196 // ParameterizedType pt = (ParameterizedType) type;197 // JavaClass referencedClass = null;198 // for (Object param : pt.getParameters()) {199 // if (param instanceof JavaClass) {200 // clsName = ((JavaClass) param).getName();201 // break;202 // }203 // }204 // }205 // if (clsName == null) {206 // clsName = type.getName();207 // }208 // Entity e = name2entity.get(clsName);209 // if (e != null) {210 // referenced.add(e);211 // }212 // }213 //214 // private void redoClosure() {215 // Set<Entity> allEntities = new HashSet<Entity>(availableEntities);216 // allEntities.addAll(selectedEntities);217 // 218 // referencedEntities.clear();219 // referencedEntities.addAll(getReferencedEntitiesTransitively(wantedEntities));220 //221 // selectedEntities.clear();222 // selectedEntities.addAll(wantedEntities);223 // selectedEntities.addAll(referencedEntities);224 //225 // availableEntities.clear();226 // availableEntities.addAll(allEntities);227 // availableEntities.removeAll(selectedEntities);228 // }229 // 230 // public boolean isClosureEnabled() {231 // return closureEnabled;232 // }233 //234 // public void setClosureEnabled(boolean closureEnabled) {235 // if (this.closureEnabled == closureEnabled) {236 // return;237 // }238 // this.closureEnabled = closureEnabled;239 // if (closureEnabled) {240 // redoClosure();241 // } else {242 // Set<Entity> allEntities = new HashSet<Entity>(availableEntities);243 // allEntities.addAll(selectedEntities);244 //245 // referencedEntities.clear();246 //247 // selectedEntities.clear();248 // selectedEntities.addAll(wantedEntities);249 //250 // availableEntities.clear();251 // availableEntities.addAll(allEntities);252 // availableEntities.removeAll(selectedEntities);253 // }254 // changeSupport.fireChange();255 // }256 // 257 // private static boolean isFieldAccess(JavaClass jc) {258 // //detect access type259 // List features = jc.getFeatures();260 // boolean fieldAccess = false;261 // boolean accessTypeDetected = false;262 // for (Iterator featuresIter = features.iterator(); featuresIter.hasNext() && !accessTypeDetected;) {263 // Feature feature = (Feature) featuresIter.next();264 // for (Iterator it = feature.getAnnotations().iterator(); it.hasNext() && !accessTypeDetected;) {265 // Annotation ann = (Annotation) it.next();266 // if (ann != null && ann.getType() != null && "javax.persistence.Id".equals(ann.getType().getName())) {267 // if (feature instanceof Field) {268 // fieldAccess = true;269 // }270 // accessTypeDetected = true;271 // }272 // }273 // }274 // return fieldAccess;275 // }276 // 277 // /**278 // * A simple queue. An object can only be added once, even279 // * if it has already been removed from the queue. This class could implement280 // * the {@link java.util.Queue} interface, but it doesn't because that281 // * interface has too many unneeded methods. Not private because of the tests.282 // */283 // static final class Queue<T> {284 // 285 // /**286 // * The queue. Implemented as ArrayList since will be iterated using get().287 // */288 // private final List<T> queue;289 // 290 // /**291 // * The contents of the queue, needed in order to quickly (ideally292 // * in a constant time) tell if a table has been already added.293 // */294 // private final Set<T> contents;295 // 296 // /**297 // * The position in the queue.298 // */299 // private int currentIndex;300 // 301 // /**302 // * Creates a queue with an initial contents.303 // */304 // public Queue(Set<T> initialContents) {305 // assert !initialContents.contains(null);306 // 307 // queue = new ArrayList(initialContents);308 // contents = new HashSet(initialContents);309 // }310 // 311 // /**312 // * Adds an elements to the queue if it hasn't been already added.313 // */314 // public void offer(T element) {315 // assert element != null;316 // 317 // if (!contents.contains(element)) {318 // contents.add(element);319 // queue.add(element);320 // }321 // }322 // 323 // /**324 // * Returns the element at the top of the queue without removing it or null325 // * if the queue is empty.326 // */327 // public boolean isEmpty() {328 // return currentIndex >= queue.size();329 // }330 // 331 // /**332 // * Returns and removes the elements at the top of the queue or null if333 // * the queue is empty.334 // */335 // public T poll() {336 // T result = null;337 // if (!isEmpty()) {338 // result = queue.get(currentIndex);339 // currentIndex++;340 // }341 // return result;342 // }343 // }344 //345 //}346