@Override @Transactional public OpenHubStackEntity process(OpenHubStackDTO dto) throws Exception { OpenHubStackEntity entity = super.process(dto); if (entity != null && entity.getStackEntries() != null) { Iterator<OpenHubStackEntryEntity> iterator = entity.getStackEntries().iterator(); while (iterator.hasNext()) { OpenHubStackEntryEntity stackEntry = iterator.next(); if (stackEntry.getProject() != null) { projectService.reloadTagsFromDatabase(stackEntry.getProject()); projectService.reloadLicensesFromDatabase(stackEntry.getProject()); projectService.reloadAnalysisFromDatabase(stackEntry.getProject()); OpenHubProjectEntity project = projectService.save(stackEntry.getProject()); if (project != null) { stackEntry.setProject(project); stackEntry.setProjectId(project.getId()); stackEntry.setStack(entity); stackEntry.setStackId(entity.getId()); } else { iterator.remove(); logger.log( Level.SEVERE, String.format( "stack entry %d project %d nao persistiu corretamente", stackEntry.getId(), stackEntry.getProjectId())); } } else if (stackEntry.getProject() == null || stackEntry.getStack() == null) { iterator.remove(); logger.log( Level.SEVERE, String.format( "stack entry %d project %d nao persistiu corretamente", stackEntry.getId(), stackEntry.getProjectId())); } } } getRepository().save(entity); return entity; }
@Override public void validateEntity(OpenHubStackEntity entity) throws Exception { super.validateEntity(entity); OpenHubBaseRequest request = new OpenHubBaseRequest(); if (entity.getStackEntries() != null) { Map<String, OpenHubTagEntity> tagMap = new HashMap<>(); Map<String, OpenHubLicenseEntity> licenseMap = new HashMap<>(); for (OpenHubStackEntryEntity stackEntry : entity.getStackEntries()) { if (stackEntry.getProjectId() != null) { OpenHubProjectEntity project = projectService.findById(stackEntry.getProjectId()); if (project == null) { OpenHubProjectDTO projectDTO = restfulClient.getProject(stackEntry.getProjectId().toString(), request); if (projectDTO != null) { project = projectService.buildEntity(projectDTO); if (project != null) { projectService.validateEntity(project); } } } if (project != null) { if (project.getTags() != null) { for (OpenHubTagEntity tag : project.getTags()) { tagMap.put(tag.getName(), tag); } } if (project.getLicenses() != null) { for (OpenHubLicenseEntity license : project.getLicenses()) { licenseMap.put(license.getName(), license); } } } stackEntry.setProject(project); } } for (OpenHubStackEntryEntity stackEntry : entity.getStackEntries()) { if (stackEntry.getProject() != null && stackEntry.getProject().getTags() != null) { List<OpenHubTagEntity> projectTagList = new ArrayList<>(); List<OpenHubLicenseEntity> projectLicenseList = new ArrayList<>(); for (OpenHubTagEntity tag : stackEntry.getProject().getTags()) { projectTagList.add(tagMap.get(tag.getName())); } for (OpenHubLicenseEntity license : stackEntry.getProject().getLicenses()) { projectLicenseList.add(licenseMap.get(license.getName())); } stackEntry.getProject().getTags().clear(); stackEntry.getProject().getTags().addAll(projectTagList); stackEntry.getProject().getLicenses().clear(); stackEntry.getProject().getLicenses().addAll(projectLicenseList); } } } }