@Override
  public Package resolveParentPackage(final Package pkg) {
    final Set<String> packageNames = new HashSet<String>();

    final org.uberfire.java.nio.file.Path nioProjectRootPath =
        Paths.convert(pkg.getProjectRootPath());
    packageNames.addAll(
        getPackageNames(
            nioProjectRootPath,
            Paths.convert(pkg.getPackageMainSrcPath()).getParent(),
            true,
            false,
            false));

    // Construct Package objects for each package name
    for (String packagePathSuffix : packageNames) {
      for (String src : sourcePaths) {
        if (packagePathSuffix == null) {
          return null;
        }
        final org.uberfire.java.nio.file.Path nioPackagePath =
            nioProjectRootPath.resolve(src).resolve(packagePathSuffix);
        if (Files.exists(nioPackagePath)) {
          return resolvePackage(Paths.convert(nioPackagePath));
        }
      }
    }

    return null;
  }
 private String getFullyQualifiedClassName(final Path path) {
   final Package pkg = projectService.resolvePackage(Paths.convert(path));
   final String packageName = pkg.getPackageName();
   if (packageName == null) {
     return null;
   }
   final String className = path.getFileName().toString().replace(".java", "");
   return (packageName.equals("") ? className : packageName + "." + className);
 }
  @Before
  public void setup() {
    service =
        new GuidedDecisionTableGraphEditorServiceImpl(
            ioService,
            copyService,
            deleteService,
            renameService,
            projectService,
            versionRecordService,
            dtableService,
            dtableLinkManager,
            resourceOpenedEvent,
            commentedOptionFactory,
            dtResourceType,
            sessionInfo) {
          {
            this.metadataService = mockMetaDataService;
          }
        };

    when(projectService.resolvePackage(any(Path.class))).thenReturn(pkg);
    when(pkg.getPackageMainResourcesPath())
        .thenReturn(PathFactory.newPath("project", "default://project/src/main/resources"));

    resolvedPaths.clear();
    when(ioService.newDirectoryStream(any(org.uberfire.java.nio.file.Path.class)))
        .thenReturn(new MockDirectoryStream(resolvedPaths));
  }
  @Override
  public void create(
      final Package pkg, final String baseFileName, final NewResourcePresenter presenter) {
    busyIndicatorView.showBusyIndicator(DecisionTableXLSEditorConstants.INSTANCE.Uploading());

    final Path path = pkg.getPackageMainResourcesPath();
    final String fileName = buildFileName(baseFileName, fileExtensionSelector.getResourceType());
    // Package Path is already encoded, fileName needs to be encoded
    final Path newPath =
        PathFactory.newPathBasedOn(fileName, path.toURI() + "/" + encode(fileName), path);

    uploadWidget.submit(
        path,
        fileName,
        URLHelper.getServletUrl(),
        new Command() {

          @Override
          public void execute() {
            busyIndicatorView.hideBusyIndicator();
            presenter.complete();
            notifySuccess();
            placeManager.goTo(newPath);
          }
        },
        new Command() {

          @Override
          public void execute() {
            busyIndicatorView.hideBusyIndicator();
          }
        });
  }
  @Override
  public void create(
      final Package pkg, final String baseFileName, final NewResourcePresenter presenter) {
    busyIndicatorView.showBusyIndicator(ScoreCardXLSEditorConstants.INSTANCE.Uploading());

    final Path path = pkg.getPackageMainResourcesPath();
    final String fileName = buildFileName(resourceType, baseFileName);
    final Path newPath =
        PathFactory.newPath(
            path.getFileSystem(), fileName, URL.encode(path.toURI() + "/" + fileName));

    uploadWidget.submit(
        path,
        fileName,
        URLHelper.getServletUrl(),
        new Command() {

          @Override
          public void execute() {
            busyIndicatorView.hideBusyIndicator();
            presenter.complete();
            notifySuccess();
            final PlaceRequest place = new PathPlaceRequest(newPath);
            placeManager.goTo(place);
          }
        },
        new Command() {

          @Override
          public void execute() {
            busyIndicatorView.hideBusyIndicator();
          }
        });
  }
  private Package doNewPackage(final Package parentPackage, final String packageName) {
    // If the package name contains separators, create sub-folders
    String newPackageName = packageName.toLowerCase();
    if (newPackageName.contains(".")) {
      newPackageName = newPackageName.replace(".", "/");
    }

    // Return new package
    final Path mainSrcPath = parentPackage.getPackageMainSrcPath();
    final Path testSrcPath = parentPackage.getPackageTestSrcPath();
    final Path mainResourcesPath = parentPackage.getPackageMainResourcesPath();
    final Path testResourcesPath = parentPackage.getPackageTestResourcesPath();

    Path pkgPath = null;

    final org.uberfire.java.nio.file.Path nioMainSrcPackagePath =
        Paths.convert(mainSrcPath).resolve(newPackageName);
    if (!Files.exists(nioMainSrcPackagePath)) {
      pkgPath = Paths.convert(ioService.createDirectory(nioMainSrcPackagePath));
    }
    final org.uberfire.java.nio.file.Path nioTestSrcPackagePath =
        Paths.convert(testSrcPath).resolve(newPackageName);
    if (!Files.exists(nioTestSrcPackagePath)) {
      pkgPath = Paths.convert(ioService.createDirectory(nioTestSrcPackagePath));
    }
    final org.uberfire.java.nio.file.Path nioMainResourcesPackagePath =
        Paths.convert(mainResourcesPath).resolve(newPackageName);
    if (!Files.exists(nioMainResourcesPackagePath)) {
      pkgPath = Paths.convert(ioService.createDirectory(nioMainResourcesPackagePath));
    }
    final org.uberfire.java.nio.file.Path nioTestResourcesPackagePath =
        Paths.convert(testResourcesPath).resolve(newPackageName);
    if (!Files.exists(nioTestResourcesPackagePath)) {
      pkgPath = Paths.convert(ioService.createDirectory(nioTestResourcesPackagePath));
    }

    // If pkgPath is null the package already existed in src/main/java, scr/main/resources,
    // src/test/java and src/test/resources
    if (pkgPath == null) {
      throw new PackageAlreadyExistsException(packageName);
    }

    // Return new package
    final Package newPackage = resolvePackage(pkgPath);
    return newPackage;
  }
  @Test
  public void testResolvePackageDefaultResources() throws Exception {

    final Bean projectServiceBean =
        (Bean) beanManager.getBeans(ProjectService.class).iterator().next();
    final CreationalContext cc = beanManager.createCreationalContext(projectServiceBean);
    final ProjectService projectService =
        (ProjectService) beanManager.getReference(projectServiceBean, ProjectService.class, cc);

    final URL testUrl =
        this.getClass().getResource("/ProjectBackendTestProjectStructureValid/src/main/resources");
    final org.kie.commons.java.nio.file.Path nioTestPath = fs.getPath(testUrl.toURI());
    final Path testPath = paths.convert(nioTestPath);

    // Test /src/main/resources resolves as the default package
    final Package result = projectService.resolvePackage(testPath);
    assertEquals("", result.getPackageName());
  }
 @Override
 public void create(
     final Package pkg, final String baseFileName, final NewResourcePresenter presenter) {
   busyIndicatorView.showBusyIndicator(CommonConstants.INSTANCE.Saving());
   solverService
       .call(
           getSuccessCallback(presenter),
           new HasBusyIndicatorDefaultErrorCallback(busyIndicatorView))
       .create(
           pkg.getPackageMainResourcesPath(), buildFileName(baseFileName, resourceType), "", "");
 }
  @Override
  public Set<Package> resolvePackages(final Package pkg) {
    final Set<Package> packages = new HashSet<Package>();
    final Set<String> packageNames = new HashSet<String>();
    if (pkg == null) {
      return packages;
    }

    // Build a set of all package names across /src/main/java, /src/main/resources, /src/test/java
    // and /src/test/resources paths
    // It is possible (if the project was not created within the workbench that some packages only
    // exist in certain paths)

    final Path projectRoot = pkg.getProjectRootPath();
    final org.uberfire.java.nio.file.Path nioProjectRootPath = Paths.convert(projectRoot);

    for (String src : sourcePaths) {
      final org.uberfire.java.nio.file.Path nioPackageRootSrcPath =
          nioProjectRootPath.resolve(src).resolve(resolvePkgName(pkg.getCaption()));
      packageNames.addAll(
          getPackageNames(nioProjectRootPath, nioPackageRootSrcPath, false, true, false));
    }

    // Construct Package objects for each package name
    final java.util.Set<String> resolvedPackages = new java.util.HashSet<String>();
    for (String packagePathSuffix : packageNames) {
      for (String src : sourcePaths) {
        final org.uberfire.java.nio.file.Path nioPackagePath =
            nioProjectRootPath.resolve(src).resolve(packagePathSuffix);
        if (Files.exists(nioPackagePath) && !resolvedPackages.contains(packagePathSuffix)) {
          packages.add(resolvePackage(Paths.convert(nioPackagePath)));
          resolvedPackages.add(packagePathSuffix);
        }
      }
    }

    return packages;
  }