@Override
  public Map getRangesMap(String namespace) {
    TreeMap treeMap = new TreeMap<String, String>();

    FormEditorContext context = formEditorContextManager.getRootEditorContext(namespace);

    if (context == null) return treeMap;

    Path currentForm = null;
    try {
      currentForm = paths.convert(ioService.get(new URI(context.getPath())));
    } catch (Exception e) {
      log.warn("Unable to load asset on '" + context.getPath() + "': ", e);
      return treeMap;
    }
    String currentFormDirUri = getFormDirUri(currentForm);
    String currentFormName = currentForm.getFileName();

    Project project = projectService.resolveProject(currentForm);

    FileUtils utils = FileUtils.getInstance();

    List<org.kie.commons.java.nio.file.Path> nioPaths =
        new ArrayList<org.kie.commons.java.nio.file.Path>();
    nioPaths.add(paths.convert(project.getRootPath()));

    Collection<FileUtils.ScanResult> forms = utils.scan(ioService, nioPaths, "form", true);

    String resourcesPath =
        paths
            .convert(projectService.resolveProject(currentForm).getRootPath())
            .resolve(SubformFinderService.MAIN_RESOURCES_PATH)
            .toUri()
            .getPath();

    Path formPath;
    String formDirUri;
    String formName;
    for (FileUtils.ScanResult form : forms) {
      formPath = paths.convert(form.getFile());
      formDirUri = getFormDirUri(formPath);
      formName = formPath.getFileName();

      if (currentFormDirUri.equals(formDirUri)
          && !formName.startsWith(".")
          && !currentFormName.equals(formName)) {
        treeMap.put(formPath.getFileName(), formPath.getFileName());
      }
    }
    return treeMap;
  }
Ejemplo n.º 2
0
  public List<ValidationMessage> validate(
      final Path resourcePath,
      final InputStream resource,
      final DirectoryStream.Filter<org.uberfire.java.nio.file.Path>... supportingFileFilters) {

    final Project project = projectService.resolveProject(resourcePath);
    if (project == null) {
      return Collections.emptyList();
    }

    final KieServices kieServices = KieServices.Factory.get();
    final KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    final String projectPrefix = project.getRootPath().toURI();

    // Add Java Model files
    final org.uberfire.java.nio.file.Path nioProjectRoot = paths.convert(project.getRootPath());
    final DirectoryStream<org.uberfire.java.nio.file.Path> directoryStream =
        Files.newDirectoryStream(nioProjectRoot);
    visitPaths(projectPrefix, kieFileSystem, directoryStream, supportingFileFilters);

    // Add resource to be validated
    final String destinationPath = resourcePath.toURI().substring(projectPrefix.length() + 1);
    final BufferedInputStream bis = new BufferedInputStream(resource);

    kieFileSystem.write(
        destinationPath, KieServices.Factory.get().getResources().newInputStreamResource(bis));

    // Validate
    final KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    final Results kieResults = kieBuilder.buildAll().getResults();
    final List<ValidationMessage> results = convertMessages(kieResults);

    return results;
  }
  @Test
  public void testResolvePackageWithRootPath() 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 rootUrl = this.getClass().getResource("/ProjectBackendTestProjectStructureValid");
    final org.kie.commons.java.nio.file.Path nioRootPath = fs.getPath(rootUrl.toURI());
    final Path rootPath = paths.convert(nioRootPath);

    // Test a root resolves to null
    final Package result = projectService.resolvePackage(rootPath);
    assertNull(result);
  }
  @Test
  public void testResolvePackageNameWithNonProjectPath() 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("/");
    final org.kie.commons.java.nio.file.Path testNioPath = fs.getPath(testUrl.toURI());
    final Path testPath = paths.convert(testNioPath);

    // Test a non-Project Path resolves to null
    final Package result = projectService.resolvePackage(testPath);
    assertNull(result);
  }
  @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());
  }
  public MockIncrementalDataModelServiceCaller(final PackageDataModelOracle packageLoader) {
    final Project project = mock(Project.class);
    final Package pkg =
        new Package(
            mock(Path.class),
            mock(Path.class),
            mock(Path.class),
            mock(Path.class),
            mock(Path.class),
            packageLoader.getPackageName(),
            packageLoader.getPackageName(),
            packageLoader.getPackageName());
    final LRUDataModelOracleCache cachePackages = mock(LRUDataModelOracleCache.class);
    when(cachePackages.assertPackageDataModelOracle(project, pkg)).thenReturn(packageLoader);

    final ProjectService projectService = mock(ProjectService.class);
    when(projectService.resolveProject(any(Path.class))).thenReturn(project);
    when(projectService.resolvePackage(any(Path.class))).thenReturn(pkg);

    this.service = new IncrementalDataModelServiceImplWrapper(cachePackages, projectService);
  }
 private Set<Project> getProjects(
     final Repository repository, final IOService ioService, final ProjectService projectService) {
   final Set<Project> authorizedProjects = new HashSet<Project>();
   if (repository == null) {
     return authorizedProjects;
   }
   final Path repositoryRoot = Paths.convert(repository.getRoot());
   final DirectoryStream<Path> nioRepositoryPaths = ioService.newDirectoryStream(repositoryRoot);
   for (Path nioRepositoryPath : nioRepositoryPaths) {
     if (Files.isDirectory(nioRepositoryPath)) {
       final org.uberfire.backend.vfs.Path projectPath = Paths.convert(nioRepositoryPath);
       final Project project = projectService.resolveProject(projectPath);
       if (project != null) {
         authorizedProjects.add(project);
       }
     }
   }
   return authorizedProjects;
 }