/* (non-Javadoc) * @see rezeptManager.entity.DataEntity#createNextChild(common.IEntity, java.lang.Class) */ @SuppressWarnings("rawtypes") @Override public IEntity createNextChild(IEntity parent, Class c) throws Exception { if (c == null) { return null; } int maxChildNr = 0; for (LinkedEntity child : getChildListForClass(c).values()) { if (child.getNr() > maxChildNr) { maxChildNr = child.getNr(); } } Integer newChildNr = maxChildNr; newChildNr++; if (this.getChildListForClass(c).size() >= getMaxChildListSize(c)) { return null; } else { Constructor[] ctors = c.getDeclaredConstructors(); Constructor ctor = null; for (int i = 0; i < ctors.length; i++) { ctor = ctors[i]; Type[] genericParameterTypes = ctor.getGenericParameterTypes(); if (genericParameterTypes.length == 2 && genericParameterTypes[0].equals(IEntity.class) && genericParameterTypes[1].equals(IEntity.class)) break; } IEntity child = (IEntity) (ctor != null ? ctor.newInstance(this, new DataEntity(newChildNr)) : null); return child; } }
/** * Gets the child. * * @param id the id * @return the child */ public LinkedEntity getChild(int id) { LinkedEntity child = childList.get(id); if (!child.inizialized()) { IEntity initializedChild = DataEntity.get(child.getId(), 0); childList.put(initializedChild.getId(), (LinkedEntity) initializedChild); } return child; }
/** * Gets the child. * * @param c the c * @param nr the nr * @return the child */ @SuppressWarnings("rawtypes") public LinkedEntity getChild(Class c, Integer nr) { for (Integer childKey : getChildListForClass(c).keySet()) { LinkedEntity child = getChild(childKey); if (child.getNr().equals(nr)) { return child; } } return null; }
/** * Adds the parent. * * @param linkedEntity the linked entity * @return the linked entity * @throws RuntimeException the runtime exception */ protected LinkedEntity addParent(LinkedEntity linkedEntity) throws RuntimeException { if (!getParentClassList().contains(linkedEntity.getClass())) { String msg = ""; if (!getParentClassList().isEmpty()) { msg = getParentClassList() + " wird als Parameter erwartet."; } throw new RuntimeException( getClass().getSimpleName() + "(" + getName() + ").addParent(" + linkedEntity.getClass().getSimpleName() + "(" + linkedEntity.getName() + ")) nicht moeglich. " + msg); } if (parentList.containsKey(linkedEntity.getId())) { throw new RuntimeException( getClass().getSimpleName() + "(" + getName() + ").addParent(" + linkedEntity.getClass().getSimpleName() + "(" + linkedEntity.getName() + ")) nicht moeglich. Id bereits vorhanden."); } else { Integer maxParentListSize = getMaxParentListSize(); if (parentList.size() < maxParentListSize) { this.parentList.put(linkedEntity.getId(), linkedEntity); modifiedDate = new Date(); } else { throw new RuntimeException( getClass().getSimpleName() + "(" + getName() + ").addParent(" + linkedEntity.getClass().getSimpleName() + "(" + linkedEntity.getName() + ")) nicht moeglich. Parentlist.size=" + parentList.size() + ", maxParentListSize=" + maxParentListSize); } } return this; }
/** * Instantiates a new linked entity. * * @param parent the parent * @param entity the entity */ public LinkedEntity(IEntity parent, IEntity entity) { super(parent, entity); if (parent != null) { ((LinkedEntity) parent).addChild(this); } }
/** * Adds the child. * * @param entity the entity * @return the linked entity * @throws RuntimeException the runtime exception */ public LinkedEntity addChild(LinkedEntity entity) throws RuntimeException { if (!getChildClassList().contains(entity.getClass())) { throw new RuntimeException( getClass().getSimpleName() + "(" + getName() + ").addChild(" + entity.getClass().getSimpleName() + "(" + entity.getName() + ")) nicht moeglich. " + getChildClassList() + " wird als Parameter erwartet."); } if (childList.containsKey(entity.getId())) { throw new RuntimeException( getClass().getSimpleName() + "(" + getName() + ").addChild(" + entity.getClass().getSimpleName() + "(" + entity.getName() + ")) nicht moeglich. Id bereits vorhanden."); } else { Integer maxChildListSize = this.getMaxChildListSize(entity.getClass()); if (getChildListForClass(entity.getClass()).size() < maxChildListSize) { this.childList.put(entity.getId(), entity); entity.addParent(this); modifiedDate = new Date(); } else { throw new RuntimeException( getClass().getSimpleName() + "(" + getName() + ").addChild(" + entity.getClass().getSimpleName() + "(" + entity.getName() + ")) nicht moeglich. Childlist.size=" + childList.size() + ", maxChildListSize=" + maxChildListSize); } } return this; }