/**
  * returns all non-system, non-hidden fields for this entity
  *
  * @param db
  * @return
  * @throws DatabaseException
  * @throws MolgenisModelException
  */
 private Vector<Field> getAllFields(Database db) throws DatabaseException, MolgenisModelException {
   String simpleName = this.getEntityClass().getSimpleName();
   org.molgenis.model.elements.Entity entity = db.getMetaData().getEntity(simpleName);
   Vector<Field> allFields = entity.getAllFields();
   for (Iterator<Field> it = entity.getAllFields().iterator(); it.hasNext(); ) {
     Field field = it.next();
     if (field.isSystem() || field.isHidden()) it.remove();
   }
   return allFields;
 }
  QueryRule createFilterRule(Database db, Operator operator, String value)
      throws DatabaseException, MolgenisModelException {
    if (operator == null || StringUtils.isEmpty(value)) return null;

    List<QueryRule> queryRules = new ArrayList<QueryRule>();
    for (Field field : getAllFields(db)) {
      String fieldName = getSearchField(field.getName());
      if (!queryRules.isEmpty()) queryRules.add(new QueryRule(Operator.OR));
      queryRules.add(new QueryRule(fieldName, operator, value));
    }
    return !queryRules.isEmpty() ? new QueryRule(queryRules) : null;
  }
  QueryRule createFilterRule(Database db, String fieldName, Operator operator, String value)
      throws DatabaseException, MolgenisModelException {
    if (StringUtils.isEmpty(fieldName) || operator == null || StringUtils.isEmpty(value))
      return null;

    QueryRule queryRule = null;
    for (Field field : getAllFields(db)) {
      if (field.getName().equals(fieldName)) {
        fieldName = getSearchField(fieldName);
        queryRule = new QueryRule(fieldName, operator, value);
        break;
      }
    }
    return queryRule;
  }
  public void generate(Model model, MolgenisOptions options, boolean wait) throws Exception {
    Template template = createTemplate("/" + getClass().getSimpleName() + ".java.ftl");
    Map<String, Object> templateArgs = createTemplateArguments(options);

    File target = new File(this.getDocumentationPath(options) + "/module-dependency-diagram.dot");
    boolean created = target.getParentFile().mkdirs();
    if (!created && !target.getParentFile().exists()) {
      throw new IOException("could not create " + target.getParentFile());
    }

    // count the relationships
    Map<String, Integer> mapOfRelations = new LinkedHashMap<String, Integer>();
    for (Entity e : model.getEntities()) {
      if (e.getModel() != null) {
        // interface
        if (e.hasImplements()) {
          for (Entity i : e.getImplements()) {
            if (i.getModule() != null) {
              addRule(mapOfRelations, e, i);
            }
          }
        }
        // superclass
        if (e.hasAncestor()) {
          addRule(mapOfRelations, e, e.getAncestor());
        }
        // xrefs/mrefs
        for (Field f : e.getFields()) {
          if (f.getType() instanceof XrefField || f.getType() instanceof MrefField) {
            addRule(mapOfRelations, e, f.getXrefEntity());
          }
        }
      }
    }

    templateArgs.put("rules", mapOfRelations);

    apply(templateArgs, template, target);
    logger.info("generated " + target);
    executeDot(target, "png", wait);
  }