@Override
  public List<String> getFieldListTypeAllPossibleValues(ShellContext shellContext) {
    // Get current value of class
    String currentText = shellContext.getParameters().get("type");

    List<String> allPossibleValues = new ArrayList<String>();

    // Getting all existing entities
    Set<ClassOrInterfaceTypeDetails> entitiesInProject =
        typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY);
    for (ClassOrInterfaceTypeDetails entity : entitiesInProject) {
      String name = replaceTopLevelPackageString(entity, currentText);
      if (!allPossibleValues.contains(name)) {
        allPossibleValues.add(name);
      }
    }

    // Getting all existing dtos
    Set<ClassOrInterfaceTypeDetails> dtosInProject =
        typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_DTO);
    for (ClassOrInterfaceTypeDetails dto : dtosInProject) {
      String name = replaceTopLevelPackageString(dto, currentText);
      if (!allPossibleValues.contains(name)) {
        allPossibleValues.add(name);
      }
    }

    return allPossibleValues;
  }
 @Override
 public boolean isFieldManagementAvailable() {
   Set<ClassOrInterfaceTypeDetails> dtoClasses =
       typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_DTO);
   if (!dtoClasses.isEmpty()) {
     return true;
   }
   return false;
 }
  private Set<ClassOrInterfaceTypeDetails> getManagedIdentifiers() {
    final Set<ClassOrInterfaceTypeDetails> managedIdentifierTypes =
        new LinkedHashSet<ClassOrInterfaceTypeDetails>();

    final Set<ClassOrInterfaceTypeDetails> identifierTypes =
        typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(ROO_IDENTIFIER);
    for (final ClassOrInterfaceTypeDetails managedIdentifierType : identifierTypes) {
      final AnnotationMetadata identifierAnnotation =
          managedIdentifierType.getTypeAnnotation(ROO_IDENTIFIER);
      final AnnotationAttributeValue<?> attrValue = identifierAnnotation.getAttribute(DB_MANAGED);
      if (attrValue != null && (Boolean) attrValue.getValue()) {
        managedIdentifierTypes.add(managedIdentifierType);
      }
    }
    return managedIdentifierTypes;
  }
  private TemplateDataDictionary buildDictionary(final GwtType type, final String moduleName) {
    final Set<ClassOrInterfaceTypeDetails> proxies =
        typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_GWT_PROXY);
    final TemplateDataDictionary dataDictionary = buildStandardDataDictionary(type, moduleName);
    switch (type) {
      case APP_ENTITY_TYPES_PROCESSOR:
        for (final ClassOrInterfaceTypeDetails proxy : proxies) {
          if (!GwtUtils.scaffoldProxy(proxy)) {
            continue;
          }
          final String proxySimpleName = proxy.getName().getSimpleTypeName();
          final ClassOrInterfaceTypeDetails entity = gwtTypeService.lookupEntityFromProxy(proxy);
          if (entity != null) {
            final String entitySimpleName = entity.getName().getSimpleTypeName();

            dataDictionary.addSection("proxys").setVariable("proxy", proxySimpleName);

            final String entity1 =
                new StringBuilder("\t\tif (")
                    .append(proxySimpleName)
                    .append(".class.equals(clazz)) {\n\t\t\tprocessor.handle")
                    .append(entitySimpleName)
                    .append("((")
                    .append(proxySimpleName)
                    .append(") null);\n\t\t\treturn;\n\t\t}")
                    .toString();
            dataDictionary.addSection("entities1").setVariable("entity", entity1);

            final String entity2 =
                new StringBuilder("\t\tif (proxy instanceof ")
                    .append(proxySimpleName)
                    .append(") {\n\t\t\tprocessor.handle")
                    .append(entitySimpleName)
                    .append("((")
                    .append(proxySimpleName)
                    .append(") proxy);\n\t\t\treturn;\n\t\t}")
                    .toString();
            dataDictionary.addSection("entities2").setVariable("entity", entity2);

            final String entity3 =
                new StringBuilder("\tpublic abstract void handle")
                    .append(entitySimpleName)
                    .append("(")
                    .append(proxySimpleName)
                    .append(" proxy);")
                    .toString();
            dataDictionary.addSection("entities3").setVariable("entity", entity3);
            addImport(dataDictionary, proxy.getName().getFullyQualifiedTypeName());
          }
        }
        break;
      case MASTER_ACTIVITIES:
        for (final ClassOrInterfaceTypeDetails proxy : proxies) {
          if (!GwtUtils.scaffoldProxy(proxy)) {
            continue;
          }
          final String proxySimpleName = proxy.getName().getSimpleTypeName();
          final ClassOrInterfaceTypeDetails entity = gwtTypeService.lookupEntityFromProxy(proxy);
          if (entity != null && !Modifier.isAbstract(entity.getModifier())) {
            final String entitySimpleName = entity.getName().getSimpleTypeName();
            final TemplateDataDictionary section = dataDictionary.addSection("entities");
            section.setVariable("entitySimpleName", entitySimpleName);
            section.setVariable("entityFullPath", proxySimpleName);
            addImport(dataDictionary, entitySimpleName, GwtType.LIST_ACTIVITY, moduleName);
            addImport(dataDictionary, proxy.getName().getFullyQualifiedTypeName());
            addImport(dataDictionary, entitySimpleName, GwtType.LIST_VIEW, moduleName);
            addImport(dataDictionary, entitySimpleName, GwtType.MOBILE_LIST_VIEW, moduleName);
          }
        }
        break;
      case APP_REQUEST_FACTORY:
        for (final ClassOrInterfaceTypeDetails proxy : proxies) {
          if (!GwtUtils.scaffoldProxy(proxy)) {
            continue;
          }
          final ClassOrInterfaceTypeDetails entity = gwtTypeService.lookupEntityFromProxy(proxy);
          if (entity != null && !Modifier.isAbstract(entity.getModifier())) {
            final String entitySimpleName = entity.getName().getSimpleTypeName();
            final ClassOrInterfaceTypeDetails request =
                gwtTypeService.lookupRequestFromProxy(proxy);
            if (request != null) {
              final String requestExpression =
                  new StringBuilder("\t")
                      .append(request.getName().getSimpleTypeName())
                      .append(" ")
                      .append(StringUtils.uncapitalize(entitySimpleName))
                      .append("Request();")
                      .toString();
              dataDictionary.addSection("entities").setVariable("entity", requestExpression);
              addImport(dataDictionary, request.getName().getFullyQualifiedTypeName());
            }
          }
          dataDictionary.setVariable(
              "sharedScaffoldPackage",
              GwtPath.SHARED_SCAFFOLD.packageName(
                  projectOperations.getTopLevelPackage(moduleName)));
        }

        if (projectOperations.isFeatureInstalledInFocusedModule(FeatureNames.GAE)) {
          dataDictionary.showSection("gae");
        }
        break;
      case LIST_PLACE_RENDERER:
        for (final ClassOrInterfaceTypeDetails proxy : proxies) {
          if (!GwtUtils.scaffoldProxy(proxy)) {
            continue;
          }
          final ClassOrInterfaceTypeDetails entity = gwtTypeService.lookupEntityFromProxy(proxy);
          if (entity != null) {
            final String entitySimpleName = entity.getName().getSimpleTypeName();
            final String proxySimpleName = proxy.getName().getSimpleTypeName();
            final TemplateDataDictionary section = dataDictionary.addSection("entities");
            section.setVariable("entitySimpleName", entitySimpleName);
            section.setVariable("entityFullPath", proxySimpleName);
            addImport(dataDictionary, proxy.getName().getFullyQualifiedTypeName());
          }
        }
        break;
      case DETAILS_ACTIVITIES:
        for (final ClassOrInterfaceTypeDetails proxy : proxies) {
          if (!GwtUtils.scaffoldProxy(proxy)) {
            continue;
          }
          final ClassOrInterfaceTypeDetails entity = gwtTypeService.lookupEntityFromProxy(proxy);
          if (entity != null) {
            final String proxySimpleName = proxy.getName().getSimpleTypeName();
            final String entitySimpleName = entity.getName().getSimpleTypeName();
            final String entityExpression =
                new StringBuilder("\t\t\tpublic void handle")
                    .append(entitySimpleName)
                    .append("(")
                    .append(proxySimpleName)
                    .append(" proxy) {\n")
                    .append("\t\t\t\tsetResult(new ")
                    .append(entitySimpleName)
                    .append(
                        "ActivitiesMapper(requests, placeController).getActivity(proxyPlace));\n\t\t\t}")
                    .toString();
            dataDictionary.addSection("entities").setVariable("entity", entityExpression);
            addImport(dataDictionary, proxy.getName().getFullyQualifiedTypeName());
            addImport(
                dataDictionary,
                GwtType.ACTIVITIES_MAPPER
                        .getPath()
                        .packageName(projectOperations.getTopLevelPackage(moduleName))
                    + "."
                    + entitySimpleName
                    + GwtType.ACTIVITIES_MAPPER.getSuffix());
          }
        }
        break;
      case MOBILE_ACTIVITIES:
        // Do nothing
        break;
    }

    return dataDictionary;
  }
 @Override
 public boolean isViewAvailable() {
   return projectOperations.isFocusedProjectAvailable()
       && typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(ROO_ACTIVITY).size() > 0;
 }
  public void notify(String upstreamDependency, String downstreamDependency) {
    ProjectMetadata projectMetadata = projectOperations.getProjectMetadata();
    if (projectMetadata == null) {
      return;
    }

    if (MetadataIdentificationUtils.isIdentifyingClass(downstreamDependency)) {
      Assert.isTrue(
          MetadataIdentificationUtils.getMetadataClass(upstreamDependency)
              .equals(
                  MetadataIdentificationUtils.getMetadataClass(
                      PhysicalTypeIdentifier.getMetadataIdentiferType())),
          "Expected class-level notifications only for PhysicalTypeIdentifier (not '"
              + upstreamDependency
              + "')");

      ClassOrInterfaceTypeDetails cid =
          typeLocationService.getTypeForIdentifier(upstreamDependency);
      boolean processed = false;
      if (MemberFindingUtils.getAnnotationOfType(cid.getAnnotations(), RooJavaType.ROO_GWT_REQUEST)
          != null) {
        ClassOrInterfaceTypeDetails proxy = gwtTypeService.lookupProxyFromRequest(cid);
        if (proxy != null) {
          JavaType typeName = PhysicalTypeIdentifier.getJavaType(proxy.getDeclaredByMetadataId());
          Path typePath = PhysicalTypeIdentifier.getPath(proxy.getDeclaredByMetadataId());
          downstreamDependency = GwtLocatorMetadata.createIdentifier(typeName, typePath);
          processed = true;
        }
      }
      if (!processed
          && MemberFindingUtils.getAnnotationOfType(cid.getAnnotations(), RooJavaType.ROO_GWT_PROXY)
              == null) {
        boolean found = false;
        for (ClassOrInterfaceTypeDetails classOrInterfaceTypeDetails :
            typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(
                RooJavaType.ROO_GWT_PROXY)) {
          AnnotationMetadata annotationMetadata =
              GwtUtils.getFirstAnnotation(
                  classOrInterfaceTypeDetails, GwtUtils.ROO_PROXY_REQUEST_ANNOTATIONS);
          if (annotationMetadata != null) {
            AnnotationAttributeValue<?> attributeValue = annotationMetadata.getAttribute("value");
            if (attributeValue != null) {
              String mirrorName = GwtUtils.getStringValue(attributeValue);
              if (mirrorName != null
                  && cid.getName().getFullyQualifiedTypeName().equals(attributeValue.getValue())) {
                found = true;
                JavaType typeName =
                    PhysicalTypeIdentifier.getJavaType(
                        classOrInterfaceTypeDetails.getDeclaredByMetadataId());
                Path typePath =
                    PhysicalTypeIdentifier.getPath(
                        classOrInterfaceTypeDetails.getDeclaredByMetadataId());
                downstreamDependency = GwtLocatorMetadata.createIdentifier(typeName, typePath);
                break;
              }
            }
          }
        }
        if (!found) {
          return;
        }
      } else if (!processed) {
        // A physical Java type has changed, and determine what the corresponding local metadata
        // identification string would have been
        JavaType typeName = PhysicalTypeIdentifier.getJavaType(upstreamDependency);
        Path typePath = PhysicalTypeIdentifier.getPath(upstreamDependency);
        downstreamDependency = GwtLocatorMetadata.createIdentifier(typeName, typePath);
      }

      // We only need to proceed if the downstream dependency relationship is not already registered
      // (if it's already registered, the event will be delivered directly later on)
      if (metadataDependencyRegistry
          .getDownstream(upstreamDependency)
          .contains(downstreamDependency)) {
        return;
      }
    }

    // We should now have an instance-specific "downstream dependency" that can be processed by this
    // class
    Assert.isTrue(
        MetadataIdentificationUtils.getMetadataClass(downstreamDependency)
            .equals(MetadataIdentificationUtils.getMetadataClass(getProvidesType())),
        "Unexpected downstream notification for '"
            + downstreamDependency
            + "' to this provider (which uses '"
            + getProvidesType()
            + "'");

    metadataService.get(downstreamDependency, true);
  }
  private void reverseEngineer(final Database database) {
    final Set<ClassOrInterfaceTypeDetails> managedEntities =
        typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(ROO_DB_MANAGED);
    // Determine whether to create "active record" CRUD methods
    database.setActiveRecord(isActiveRecord(database, managedEntities));

    // Lookup the relevant destination package if not explicitly given
    final JavaPackage destinationPackage = getDestinationPackage(database, managedEntities);

    // Set the destination package in the database
    database.setDestinationPackage(destinationPackage);

    // Get tables from database
    final Set<Table> tables = new LinkedHashSet<Table>(database.getTables());

    // Manage existing entities with @RooDbManaged annotation
    for (final ClassOrInterfaceTypeDetails managedEntity : managedEntities) {
      // Remove table from set as each managed entity is processed.
      // The tables that remain in the set will be used for creation of
      // new entities later
      final Table table = updateOrDeleteManagedEntity(managedEntity, database);
      if (table != null) {
        tables.remove(table);
      }
    }

    // Create new entities from tables
    final List<ClassOrInterfaceTypeDetails> newEntities =
        new ArrayList<ClassOrInterfaceTypeDetails>();
    for (final Table table : tables) {
      // Don't create types from join tables in many-to-many associations
      if (!table.isJoinTable()) {
        JavaPackage schemaPackage = destinationPackage;
        if (database.hasMultipleSchemas()) {
          schemaPackage =
              new JavaPackage(
                  destinationPackage.getFullyQualifiedPackageName()
                      + "."
                      + DbreTypeUtils.suggestPackageName(table.getSchema().getName()));
        }
        final JavaType javaType =
            DbreTypeUtils.suggestTypeNameForNewTable(table.getName(), schemaPackage);
        final boolean activeRecord = database.isActiveRecord() && !database.isRepository();
        if (typeLocationService.getTypeDetails(javaType) == null) {
          table.setIncludeNonPortableAttributes(database.isIncludeNonPortableAttributes());
          table.setDisableVersionFields(database.isDisableVersionFields());
          table.setDisableGeneratedIdentifiers(database.isDisableGeneratedIdentifiers());
          newEntities.add(createNewManagedEntityFromTable(javaType, table, activeRecord));
        }
      }
    }

    // Create repositories if required
    if (database.isRepository()) {
      for (final ClassOrInterfaceTypeDetails entity : newEntities) {
        final JavaType type = entity.getType();
        repositoryJpaOperations.setupRepository(
            new JavaType(type.getFullyQualifiedTypeName() + "Repository"), type);
      }
    }

    // Create services if required
    if (database.isService()) {
      for (final ClassOrInterfaceTypeDetails entity : newEntities) {
        final JavaType type = entity.getType();
        final String typeName = type.getFullyQualifiedTypeName();
        serviceOperations.setupService(
            new JavaType(typeName + "Service"),
            new JavaType(typeName + "ServiceImpl"),
            type,
            false,
            "",
            false,
            false);
      }
    }

    // Create integration tests if required
    if (database.isTestAutomatically()) {
      for (final ClassOrInterfaceTypeDetails entity : newEntities) {
        integrationTestOperations.newIntegrationTest(entity.getType());
      }
    }

    // Notify
    final List<ClassOrInterfaceTypeDetails> allEntities =
        new ArrayList<ClassOrInterfaceTypeDetails>();
    allEntities.addAll(newEntities);
    allEntities.addAll(managedEntities);
    notify(allEntities);
  }