private List<ResourceDto> createInputData(List<Content> contents) {
    List<ResourceDto> list = new ArrayList<>(contents.size());

    for (final Content content : contents) {
      ResourceDto dto =
          (ResourceDto)
              CollectionUtils.find(
                  list,
                  new Predicate() {
                    @Override
                    public boolean evaluate(Object object) {
                      return (object instanceof ResourceDto)
                          && equalsContent(
                              (ResourceDto) object, content.getName(), getContentType(content));
                    }
                  });

      if (dto == null) {
        list.add(new ResourceDto(content));
      } else {
        dto.addLanguage(content.getLanguage());
      }
    }

    return list;
  }
Ejemplo n.º 2
0
 private void appendChildProjects(
     long projectId, ResourceMapper mapper, List<ResourceDto> resources) {
   List<ResourceDto> subProjects = mapper.selectDescendantProjects(projectId);
   for (ResourceDto subProject : subProjects) {
     resources.add(subProject);
     appendChildProjects(subProject.getId(), mapper, resources);
   }
 }
Ejemplo n.º 3
0
 public static ComponentDto toComponent(ResourceDto resourceDto) {
   return new ComponentDto()
       .setId(resourceDto.getId())
       .setKey(resourceDto.getKey())
       .setLongName(resourceDto.getLongName())
       .setName(resourceDto.getName())
       .setQualifier(resourceDto.getQualifier());
 }
Ejemplo n.º 4
0
  @Test
  public void get_resource_path_and_module_key() {
    setupData("fixture");

    ResourceDto dir = dao.getResource(3L);
    assertThat(dir.getPath()).isEqualTo("src/org/struts");

    ResourceDto file = dao.getResource(4L);
    assertThat(file.getPath()).isEqualTo("src/org/struts/RequestContext.java");
  }
Ejemplo n.º 5
0
  @Test
  public void get_uuid() {
    setupData("fixture");

    ResourceDto file = dao.getResource(4L);
    assertThat(file.getUuid()).isEqualTo("DEFG");
    assertThat(file.getProjectUuid()).isEqualTo("ABCD");
    assertThat(file.getModuleUuid()).isEqualTo("BCDE");
    assertThat(file.getModuleUuidPath()).isEqualTo(".ABCD.BCDE.");
  }
Ejemplo n.º 6
0
  @Test
  public void insert_add_uuids_on_project_if_missing() {
    setupData("insert");

    ResourceDto project =
        new ResourceDto()
            .setKey("org.struts:struts:struts")
            .setScope(Scopes.PROJECT)
            .setQualifier(Qualifiers.PROJECT);

    ResourceDto file =
        new ResourceDto()
            .setKey("org.struts:struts:/src/main/java/org/struts/Action.java")
            .setScope(Scopes.FILE)
            .setQualifier(Qualifiers.FILE);

    dao.insertOrUpdate(project, file);

    assertThat(project.getUuid()).isNotNull();
    assertThat(project.getProjectUuid()).isEqualTo(project.getUuid());
    assertThat(project.getModuleUuidPath()).isEmpty();

    assertThat(file.getUuid()).isNull();
    assertThat(file.getProjectUuid()).isNull();
    assertThat(file.getModuleUuidPath()).isNull();
  }
Ejemplo n.º 7
0
 private static Set<ResourceDto> collectAllModules(
     long projectId, String stringToReplace, ResourceKeyUpdaterMapper mapper) {
   ResourceDto project = mapper.selectProject(projectId);
   Set<ResourceDto> modules = Sets.newHashSet();
   if (project.getKey().contains(stringToReplace)) {
     modules.add(project);
   }
   for (ResourceDto submodule : mapper.selectDescendantProjects(projectId)) {
     modules.addAll(collectAllModules(submodule.getId(), stringToReplace, mapper));
   }
   return modules;
 }
Ejemplo n.º 8
0
 private static void runBatchUpdateForAllResources(
     Collection<ResourceDto> resources,
     String oldKey,
     String newKey,
     ResourceKeyUpdaterMapper mapper) {
   for (ResourceDto resource : resources) {
     String resourceKey = resource.getKey();
     resource.setKey(newKey + resourceKey.substring(oldKey.length(), resourceKey.length()));
     String resourceDeprecatedKey = resource.getDeprecatedKey();
     if (StringUtils.isNotBlank(resourceDeprecatedKey)) {
       resource.setDeprecatedKey(
           newKey
               + resourceDeprecatedKey.substring(oldKey.length(), resourceDeprecatedKey.length()));
     }
     mapper.update(resource);
   }
 }
Ejemplo n.º 9
0
 public ResourceDao insertOrUpdate(ResourceDto... resources) {
   SqlSession session = mybatis.openSession(false);
   ResourceMapper mapper = session.getMapper(ResourceMapper.class);
   try {
     for (ResourceDto resource : resources) {
       if (resource.getId() == null) {
         resource.setCreatedAt(new Date());
         mapper.insert(resource);
       } else {
         mapper.update(resource);
       }
     }
     session.commit();
   } finally {
     MyBatis.closeQuietly(session);
   }
   return this;
 }
Ejemplo n.º 10
0
  public void bulkUpdateKey(
      DbSession session, long projectId, String stringToReplace, String replacementString) {
    ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
    // must SELECT first everything
    Set<ResourceDto> modules = collectAllModules(projectId, stringToReplace, mapper);
    checkNewNameOfAllModules(modules, stringToReplace, replacementString, mapper);
    Map<ResourceDto, List<ResourceDto>> allResourcesByModuleMap = Maps.newHashMap();
    for (ResourceDto module : modules) {
      allResourcesByModuleMap.put(module, mapper.selectProjectResources(module.getId()));
    }

    // and then proceed with the batch UPDATE at once
    for (ResourceDto module : modules) {
      String oldModuleKey = module.getKey();
      String newModuleKey = computeNewKey(module, stringToReplace, replacementString);
      Collection<ResourceDto> resources = Lists.newArrayList(module);
      resources.addAll(allResourcesByModuleMap.get(module));
      runBatchUpdateForAllResources(resources, oldModuleKey, newModuleKey, mapper);
    }
  }
Ejemplo n.º 11
0
 public Map<String, String> checkModuleKeysBeforeRenaming(
     long projectId, String stringToReplace, String replacementString) {
   SqlSession session = mybatis.openSession(false);
   ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
   Map<String, String> result = Maps.newHashMap();
   try {
     Set<ResourceDto> modules = collectAllModules(projectId, stringToReplace, mapper);
     for (ResourceDto module : modules) {
       String newKey = computeNewKey(module, stringToReplace, replacementString);
       if (mapper.countResourceByKey(newKey) > 0) {
         result.put(module.getKey(), "#duplicate_key#");
       } else {
         result.put(module.getKey(), newKey);
       }
     }
   } finally {
     MyBatis.closeQuietly(session);
   }
   return result;
 }
Ejemplo n.º 12
0
  @Test
  public void should_update() {
    setupData("update");

    ResourceDto project =
        new ResourceDto()
            .setKey("org.struts:struts")
            .setDeprecatedKey("deprecated key")
            .setScope(Scopes.PROJECT)
            .setQualifier(Qualifiers.PROJECT)
            .setName("Struts")
            .setLongName("Apache Struts")
            .setLanguage("java")
            .setDescription("MVC Framework")
            .setPath("/foo/bar")
            .setId(1L);

    dao.insertOrUpdate(project);

    assertThat(project.getId()).isNotNull();
    checkTables("update", "projects");
  }
Ejemplo n.º 13
0
  public void updateKey(long projectId, String newKey) {
    DbSession session = mybatis.openSession(true);
    ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
    try {
      if (mapper.countResourceByKey(newKey) > 0) {
        throw new IllegalStateException(
            "Impossible to update key: a resource with \"" + newKey + "\" key already exists.");
      }

      // must SELECT first everything
      ResourceDto project = mapper.selectProject(projectId);
      String projectOldKey = project.getKey();
      List<ResourceDto> resources = mapper.selectProjectResources(projectId);
      resources.add(project);

      // and then proceed with the batch UPDATE at once
      runBatchUpdateForAllResources(resources, projectOldKey, newKey, mapper);

      session.commit();
    } finally {
      MyBatis.closeQuietly(session);
    }
  }
Ejemplo n.º 14
0
  @Test
  public void should_insert() {
    setupData("insert");

    ResourceDto file1 =
        new ResourceDto()
            .setUuid("ABCD")
            .setProjectUuid("EFGH")
            .setModuleUuid("EFGH")
            .setModuleUuidPath(".EFGH.")
            .setKey("org.struts:struts:/src/main/java/org/struts/Action.java")
            .setDeprecatedKey("org.struts:struts:org.struts.Action")
            .setScope(Scopes.FILE)
            .setQualifier(Qualifiers.FILE)
            .setLanguage("java")
            .setName("Action")
            .setLongName("org.struts.Action")
            .setPath("/foo/bar");
    ResourceDto file2 =
        new ResourceDto()
            .setUuid("BCDE")
            .setProjectUuid("FGHI")
            .setModuleUuid("FGHI")
            .setModuleUuidPath(".FGHI.")
            .setKey("org.struts:struts:/src/main/java/org/struts/Filter.java")
            .setDeprecatedKey("org.struts:struts:org.struts.Filter")
            .setScope(Scopes.FILE)
            .setQualifier(Qualifiers.FILE)
            .setLanguage("java")
            .setName("Filter")
            .setLongName("org.struts.Filter");

    dao.insertOrUpdate(file1, file2);

    assertThat(file1.getId()).isNotNull();
    assertThat(file2.getId()).isNotNull();
    checkTables("insert", new String[] {"authorization_updated_at"}, "projects");

    // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table
    ResourceDto fileLoadedFromDB = dao.getResource(file1.getId());
    assertThat(fileLoadedFromDB.getCreatedAt()).isNotNull();
    assertThat(fileLoadedFromDB.getAuthorizationUpdatedAt()).isNotNull();
  }
Ejemplo n.º 15
0
  @Test
  public void get_resource_by_uuid() {
    dbTester.prepareDbUnit(getClass(), "fixture.xml");

    ResourceDto resource = underTest.selectResource("ABCD");

    assertThat(resource.getUuid()).isEqualTo("ABCD");
    assertThat(resource.getProjectUuid()).isEqualTo("ABCD");
    assertThat(resource.getPath()).isNull();
    assertThat(resource.getName()).isEqualTo("Struts");
    assertThat(resource.getLongName()).isEqualTo("Apache Struts");
    assertThat(resource.getScope()).isEqualTo("PRJ");
    assertThat(resource.getDescription()).isEqualTo("the description");
    assertThat(resource.getLanguage()).isEqualTo("java");
    assertThat(resource.isEnabled()).isTrue();
    assertThat(resource.getAuthorizationUpdatedAt()).isNotNull();
    assertThat(resource.getCreatedAt()).isNotNull();
  }
Ejemplo n.º 16
0
 private static String computeNewKey(
     ResourceDto resource, String stringToReplace, String replacementString) {
   return resource.getKey().replaceAll(stringToReplace, replacementString);
 }
Ejemplo n.º 17
0
  @Test
  public void get_resource_by_id() {
    setupData("fixture");

    ResourceDto resource = dao.getResource(1L);

    assertThat(resource.getUuid()).isEqualTo("ABCD");
    assertThat(resource.getProjectUuid()).isEqualTo("ABCD");
    assertThat(resource.getPath()).isNull();
    assertThat(resource.getName()).isEqualTo("Struts");
    assertThat(resource.getLongName()).isEqualTo("Apache Struts");
    assertThat(resource.getScope()).isEqualTo("PRJ");
    assertThat(resource.getDescription()).isEqualTo("the description");
    assertThat(resource.getLanguage()).isEqualTo("java");
    assertThat(resource.isEnabled()).isTrue();
    assertThat(resource.getAuthorizationUpdatedAt()).isNotNull();
    assertThat(resource.getCreatedAt()).isNotNull();
  }