/**
  * Convenience method to get the generic type of a field on a type in a project
  *
  * @param oracle The DMO representing a project
  * @param fullyQualifiedClassName The FQCN of the type
  * @param fieldName The field Name
  * @return
  */
 public static String getParametricFieldType(
     final ProjectDataModelOracle oracle,
     final String fullyQualifiedClassName,
     final String fieldName) {
   final String qualifiedFactFieldName = fullyQualifiedClassName + "#" + fieldName;
   return oracle.getProjectFieldParametersType().get(qualifiedFactFieldName);
 }
 /**
  * Convenience method to get a set of annotations on a type in a project
  *
  * @param oracle The DMO representing a project
  * @param fullyQualifiedClassName The FQCN of the type
  * @return
  */
 public static Set<Annotation> getTypeAnnotations(
     final ProjectDataModelOracle oracle, final String fullyQualifiedClassName) {
   final Map<String, Set<Annotation>> typeAnnotations = oracle.getProjectTypeAnnotations();
   if (!typeAnnotations.containsKey(fullyQualifiedClassName)) {
     return Collections.EMPTY_SET;
   }
   return typeAnnotations.get(fullyQualifiedClassName);
 }
 /**
  * Convenience method to get the fully qualified class name of the super type of another type in a
  * project
  *
  * @param oracle The DMO representing a project
  * @param fullyQualifiedClassName The FQCN of the type
  * @return
  */
 public static String getSuperType(
     final ProjectDataModelOracle oracle, final String fullyQualifiedClassName) {
   List<String> superTypes = oracle.getProjectSuperTypes().get(fullyQualifiedClassName);
   if (superTypes != null && superTypes.size() > 0) {
     return superTypes.get(0);
   } else {
     return null;
   }
 }
 private static String getFactNameFromType(
     final ProjectDataModelOracle oracle, final String fullyQualifiedClassName) {
   if (fullyQualifiedClassName == null) {
     return null;
   }
   if (oracle.getProjectModelFields().containsKey(fullyQualifiedClassName)) {
     return fullyQualifiedClassName;
   }
   for (Map.Entry<String, ModelField[]> entry : oracle.getProjectModelFields().entrySet()) {
     for (ModelField mf : entry.getValue()) {
       if (DataType.TYPE_THIS.equals(mf.getName())
           && fullyQualifiedClassName.equals(mf.getClassName())) {
         return entry.getKey();
       }
     }
   }
   return null;
 }
  /**
   * Convenience method to get an array of field names for a type in a project
   *
   * @param oracle The DMO representing a project
   * @param fullyQualifiedClassName The FQCN of the type
   * @return
   */
  public static String[] getFieldNames(
      final ProjectDataModelOracle oracle, final String fullyQualifiedClassName) {
    final ModelField[] modelFields = oracle.getProjectModelFields().get(fullyQualifiedClassName);
    if (modelFields == null) {
      return new String[0];
    }

    final String[] fieldNames = new String[modelFields.length];
    for (int i = 0; i < modelFields.length; i++) {
      fieldNames[i] = modelFields[i].getName();
    }
    return fieldNames;
  }
  /**
   * Convenience method to get an array of all fully qualified class names available in a project
   *
   * @param oracle The DMO representing a project
   * @return
   */
  public static String[] getFactTypes(final ProjectDataModelOracle oracle) {

    List<String> packageNames = oracle.getProjectPackageNames();

    final Map<String, ModelField[]> modelFields = oracle.getProjectModelFields();
    final List<String> types = new ArrayList<String>();
    for (String type : modelFields.keySet()) {
      int beginIndex = type.lastIndexOf('.');

      if (beginIndex < 0) {
        types.add(type);
      } else {
        String substring = type.substring(0, beginIndex);
        if (packageNames.contains(substring)) {
          types.add(type);
        }
      }
    }
    Collections.sort(types);

    return types.toArray(new String[types.size()]);
  }
 @Override
 protected ProjectDataModelOracle getProjectDataModelOracle(final Path path) {
   final ProjectDataModelOracle dmo = new ProjectDataModelOracleImpl();
   dmo.addProjectModelFields(
       new HashMap<String, ModelField[]>() {
         {
           put(
               "org.drools.workbench.screens.guided.dtree.backend.server.indexing.classes.Applicant",
               new ModelField[] {
                 new ModelField(
                     "age",
                     "java.lang.Integer",
                     ModelField.FIELD_CLASS_TYPE.REGULAR_CLASS,
                     ModelField.FIELD_ORIGIN.DECLARED,
                     FieldAccessorsAndMutators.ACCESSOR,
                     DataType.TYPE_NUMERIC_INTEGER)
               });
           put(
               "org.drools.workbench.screens.guided.dtree.backend.server.indexing.classes.Mortgage",
               new ModelField[] {
                 new ModelField(
                     "amount",
                     "java.lang.Integer",
                     ModelField.FIELD_CLASS_TYPE.REGULAR_CLASS,
                     ModelField.FIELD_ORIGIN.DECLARED,
                     FieldAccessorsAndMutators.ACCESSOR,
                     DataType.TYPE_NUMERIC_INTEGER),
                 new ModelField(
                     "applicant",
                     "org.drools.workbench.screens.guided.dtree.backend.server.indexing.classes.Applicant",
                     ModelField.FIELD_CLASS_TYPE.REGULAR_CLASS,
                     ModelField.FIELD_ORIGIN.DECLARED,
                     FieldAccessorsAndMutators.ACCESSOR,
                     "org.drools.workbench.screens.guided.dtree.backend.server.indexing.classes.Applicant")
               });
         }
       });
   return dmo;
 }
 private static ModelField getField(
     final ProjectDataModelOracle oracle,
     final String fullyQualifiedClassName,
     final String fieldName) {
   final String shortName = getFactNameFromType(oracle, fullyQualifiedClassName);
   final ModelField[] fields = oracle.getProjectModelFields().get(shortName);
   if (fields == null) {
     return null;
   }
   for (ModelField modelField : fields) {
     if (modelField.getName().equals(fieldName)) {
       return modelField;
     }
   }
   return null;
 }
 /**
  * Convenience method to get the source of a type in a project
  *
  * @param oracle The DMO representing a project
  * @param fullyQualifiedClassName The FQCN of the type
  * @return
  */
 public static TypeSource getTypeSource(
     final ProjectDataModelOracle oracle, final String fullyQualifiedClassName) {
   return oracle.getProjectTypeSources().get(fullyQualifiedClassName);
 }