/*
  * If this method is overridden it should be called as well by super.preparePersist(). Note that
  * it recurses down though all to-many relationships.
  */
 public void preparePersist(String modId, EntityManager manager, GateKeeper gateKeeper)
     throws IcatException {
   this.id = null;
   this.modId = modId;
   Class<? extends EntityBaseBean> klass = this.getClass();
   Set<Relationship> rs = eiHandler.getRelatedEntities(klass);
   Map<Field, Method> getters = eiHandler.getGetters(klass);
   for (Relationship r : rs) {
     if (r.isCollection()) {
       Method m = getters.get(r.getField());
       try {
         @SuppressWarnings("unchecked")
         List<EntityBaseBean> collection = (List<EntityBaseBean>) m.invoke(this);
         if (!collection.isEmpty()) {
           Method rev = r.getInverseSetter();
           for (EntityBaseBean bean : collection) {
             bean.preparePersist(modId, manager, gateKeeper);
             rev.invoke(bean, this);
           }
         }
       } catch (Exception e) {
         throw new IcatException(IcatExceptionType.INTERNAL, e.getClass() + " " + e.getMessage());
       }
     }
   }
 }
 public void addToLucene(LuceneSingleton lucene) throws IcatException {
   lucene.addDocument(this);
   Class<? extends EntityBaseBean> klass = this.getClass();
   Set<Relationship> rs = eiHandler.getRelatedEntities(klass);
   Map<Field, Method> getters = eiHandler.getGetters(klass);
   for (Relationship r : rs) {
     if (r.isCollection()) {
       Method m = getters.get(r.getField());
       try {
         @SuppressWarnings("unchecked")
         List<EntityBaseBean> collection = (List<EntityBaseBean>) m.invoke(this);
         if (!collection.isEmpty()) {
           for (EntityBaseBean bean : collection) {
             bean.addToLucene(lucene);
           }
         }
       } catch (Exception e) {
         throw new IcatException(IcatExceptionType.INTERNAL, e.getMessage());
       }
     }
   }
 }
 /**
  * Clone a pruned copy of tree
  *
  * @param one true if INCLUDE 1 specified
  * @param hereVarNum current idVarNum. If steps is null this value is irrelevant.
  * @param steps list of steps to consider. This may be null. Steps starting with hereVarNum will
  *     be followed. Steps hold the starting idVarNum, the field to navigate and the destination
  *     idVarNum.
  * @param manager
  * @param userId
  * @return
  * @throws IcatException
  */
 public EntityBaseBean pruned(
     boolean one,
     int hereVarNum,
     List<Step> steps,
     GateKeeper gateKeeper,
     String userId,
     EntityManager manager)
     throws IcatException {
   Class<? extends EntityBaseBean> klass = this.getClass();
   if (logger.isDebugEnabled()) {
     if (one) {
       logger.debug("Pruning " + klass.getSimpleName() + " INCLUDE 1");
     } else if (steps != null) {
       logger.debug("Pruning " + klass.getSimpleName() + " INCLUDE from " + hereVarNum);
     } else {
       logger.debug("Pruning " + klass.getSimpleName());
     }
   }
   try {
     Constructor<? extends EntityBaseBean> con = eiHandler.getConstructor(klass);
     EntityBaseBean clone = con.newInstance();
     clone.id = this.id;
     clone.createTime = this.createTime;
     clone.createId = this.createId;
     clone.modTime = this.modTime;
     clone.modId = this.modId;
     Set<Field> atts = eiHandler.getAttributes(klass);
     Map<Field, Method> getters = eiHandler.getGetters(klass);
     Map<Field, Method> setters = eiHandler.getSettersForUpdate(klass);
     for (Field att : atts) {
       Object value = getters.get(att).invoke(this);
       if (value != null) {
         setters.get(att).invoke(clone, new Object[] {value});
       }
     }
     if (one) {
       for (Relationship r : eiHandler.getOnes(klass)) {
         Field att = r.getField();
         EntityBaseBean value = allowedOne(r, getters.get(att), gateKeeper, userId, manager);
         if (value != null) {
           value = value.pruned(false, 0, null, gateKeeper, userId, manager);
           setters.get(att).invoke(clone, new Object[] {value});
         }
       }
     } else if (steps != null) {
       for (Step step : steps) {
         if (step.getHereVarNum() == hereVarNum) {
           Relationship r = step.getRelationship();
           Field field = r.getField();
           if (r.isCollection()) {
             List<EntityBaseBean> values = allowedMany(step, getters, gateKeeper, userId, manager);
             @SuppressWarnings("unchecked")
             List<EntityBaseBean> cloneList =
                 (List<EntityBaseBean>) getters.get(field).invoke(clone);
             for (EntityBaseBean value : values) {
               value =
                   value.pruned(false, step.getThereVarNum(), steps, gateKeeper, userId, manager);
               cloneList.add(value);
             }
           } else {
             EntityBaseBean value = allowedOne(r, getters.get(field), gateKeeper, userId, manager);
             if (value != null) {
               value =
                   value.pruned(false, step.getThereVarNum(), steps, gateKeeper, userId, manager);
               setters.get(field).invoke(clone, new Object[] {value});
             }
           }
         }
       }
     }
     return clone;
   } catch (Exception e) {
     reportUnexpected(e);
     throw new IcatException(IcatException.IcatExceptionType.INTERNAL, "" + e);
   }
 }