Skip to content

Commit

Permalink
Merge pull request sw360#395 from sw360/dev/projectReleaseRelationEnu…
Browse files Browse the repository at this point in the history
…m#257

Convert Project To Relase Relations to Enums

reviewed-by: [email protected]
tested-by: [email protected]
  • Loading branch information
alexbrdn authored May 19, 2017
2 parents 144dd82 + ba6e346 commit 03faaf7
Show file tree
Hide file tree
Showing 41 changed files with 454 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.eclipse.sw360.components.summary.SummaryType;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.common.SW360Utils;
Expand All @@ -31,8 +33,6 @@
import org.eclipse.sw360.datahandler.thrift.users.RequestedAction;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.ektorp.DocumentOperationResult;
import org.ektorp.http.HttpClient;
import org.jetbrains.annotations.NotNull;
Expand All @@ -53,9 +53,7 @@
import static org.eclipse.sw360.datahandler.permissions.PermissionUtils.makePermission;
import static org.eclipse.sw360.datahandler.thrift.ThriftUtils.copyFields;
import static org.eclipse.sw360.datahandler.thrift.ThriftUtils.immutableOfComponent;
import static org.eclipse.sw360.datahandler.thrift.ThriftValidate.ensureEccInformationIsSet;
import static org.eclipse.sw360.datahandler.thrift.ThriftValidate.prepareComponents;
import static org.eclipse.sw360.datahandler.thrift.ThriftValidate.prepareReleases;
import static org.eclipse.sw360.datahandler.thrift.ThriftValidate.*;

/**
* Class for accessing Component information from the database
Expand Down Expand Up @@ -724,12 +722,14 @@ private Optional<ReleaseLink> createReleaseLink(String id, Object relation, Stri


private void fillValueFieldInReleaseLink(ReleaseLink releaseLink, Object relation) {
if (relation instanceof String) {
releaseLink.setComment((String) relation);
if (relation instanceof ProjectReleaseRelationship) {
ProjectReleaseRelationship rel = (ProjectReleaseRelationship) relation;
releaseLink.setReleaseRelationship(rel.getReleaseRelation());
releaseLink.setMainlineState(rel.getMainlineState());
} else if (relation instanceof ReleaseRelationship) {
releaseLink.setReleaseRelationship((ReleaseRelationship) relation);
} else {
throw new IllegalArgumentException("Only String or ReleaseRelationship is allowed as ReleaseLink's relation value");
throw new IllegalArgumentException("Only ProjectReleaseRelationship or ReleaseRelationship is allowed as ReleaseLink's relation value");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Sets;
import org.apache.log4j.Logger;
import org.eclipse.sw360.components.summary.SummaryType;
import org.eclipse.sw360.datahandler.businessrules.ReleaseClearingStateSummaryComputer;
import org.eclipse.sw360.datahandler.common.CommonUtils;
Expand All @@ -32,7 +33,6 @@
import org.eclipse.sw360.datahandler.thrift.users.RequestedAction;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.vulnerabilities.ProjectVulnerabilityRating;
import org.apache.log4j.Logger;
import org.ektorp.http.HttpClient;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -273,6 +273,7 @@ private Optional<ProjectLink> createProjectLink(String id, ProjectRelationship r
projectLink = new ProjectLink(id, project.name);
if (project.isSetReleaseIdToUsage()){
List<ReleaseLink> linkedReleases = componentDatabaseHandler.getLinkedReleases(project.getReleaseIdToUsage(), releaseMap, visitedIds);
fillMainlineStates(linkedReleases, project.getReleaseIdToUsage());
projectLink.setLinkedReleases(nullToEmptyList(linkedReleases));
}

Expand All @@ -296,6 +297,12 @@ private Optional<ProjectLink> createProjectLink(String id, ProjectRelationship r
return Optional.ofNullable(projectLink);
}

private void fillMainlineStates(List<ReleaseLink> linkedReleases, Map<String, ProjectReleaseRelationship> releaseIdToUsage) {
for (ReleaseLink releaseLink : linkedReleases) {
releaseLink.setMainlineState(releaseIdToUsage.get(releaseLink.getId()).getMainlineState());
}
}

private String generateNodeId(String id, CountingStack<String> visitedIds) {
return id == null ? null : id + "_" + visitedIds.getCount(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.eclipse.sw360.datahandler.common.Moderator;
import org.eclipse.sw360.datahandler.common.SW360Utils;
import org.eclipse.sw360.datahandler.thrift.ReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.ThriftClients;
import org.eclipse.sw360.datahandler.thrift.components.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2015. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2017. Part of the SW360 Portal Project.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -8,14 +8,17 @@
*/
package org.eclipse.sw360.components;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.common.DatabaseSettings;
import org.eclipse.sw360.datahandler.db.ComponentDatabaseHandler;
import org.eclipse.sw360.datahandler.db.ComponentSearchHandler;
import org.eclipse.sw360.datahandler.thrift.*;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
import org.eclipse.sw360.datahandler.thrift.components.*;
import org.eclipse.sw360.datahandler.thrift.components.Component;
import org.eclipse.sw360.datahandler.thrift.components.ComponentService;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.components.ReleaseLink;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.apache.thrift.TException;
import org.ektorp.http.HttpClient;

import java.io.IOException;
Expand Down Expand Up @@ -378,7 +381,7 @@ public Set<Attachment> getSourceAttachments(String releaseId) throws TException
}

@Override
public List<ReleaseLink> getLinkedReleases(Map<String, String> relations) throws TException {
public List<ReleaseLink> getLinkedReleases(Map<String, ProjectReleaseRelationship> relations) throws TException {
assertNotNull(relations);

return handler.getLinkedReleases(relations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import org.eclipse.sw360.datahandler.db.ComponentDatabaseHandler;
import org.eclipse.sw360.datahandler.entitlement.ComponentModerator;
import org.eclipse.sw360.datahandler.entitlement.ReleaseModerator;
import org.eclipse.sw360.datahandler.thrift.ReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.ThriftUtils;
import org.eclipse.sw360.datahandler.thrift.components.*;
import org.eclipse.sw360.datahandler.thrift.users.RequestedAction;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.jetbrains.annotations.NotNull;
import org.junit.*;
Expand Down Expand Up @@ -320,9 +320,9 @@ public void testGetSubscribedReleases() throws Exception {
@Test
public void testGetLinkedReleases() throws Exception {

final Map<String, String> relations = new HashMap<>();
relations.put("R1A", "Important linked release");
relations.put("---", "Dummy not existing release");
final Map<String, ReleaseRelationship> relations = new HashMap<>();
relations.put("R1A", ReleaseRelationship.REFERRED);
relations.put("---", ReleaseRelationship.UNKNOWN);

final Release r1A = handler.getRelease("R1A", user1);
r1A.setReleaseIdToRelationship(ImmutableMap.of("R1B", ReleaseRelationship.CONTAINED,
Expand Down Expand Up @@ -386,7 +386,7 @@ public void testGetLinkedReleases() throws Exception {
.setParentNodeId("R1A_1")
.setSubreleases(Arrays.asList(releaseLinkR2A_R1B));
ReleaseLink releaseLinkR1A = createReleaseLinkTo(r1A)
.setComment("Important linked release")
.setReleaseRelationship(ReleaseRelationship.REFERRED)
.setNodeId("R1A_1")
.setSubreleases(Arrays.asList(releaseLinkR1B_R1A, releaseLinkR2A_R1A));

Expand All @@ -406,8 +406,8 @@ private ReleaseLink createReleaseLinkTo(Release release) {
@Test
public void testGetLinkedReleases2() throws Exception {

final Map<String, String> relations = new HashMap<>();
relations.put("R1A", "Important linked release");
final Map<String, ReleaseRelationship> relations = new HashMap<>();
relations.put("R1A", ReleaseRelationship.REFERRED);

final Release r1A = handler.getRelease("R1A", user1);
r1A.setReleaseIdToRelationship(ImmutableMap.of("R1B", ReleaseRelationship.CONTAINED,
Expand Down Expand Up @@ -447,7 +447,7 @@ public void testGetLinkedReleases2() throws Exception {
.setParentNodeId("R1A_1")
.setSubreleases(Arrays.asList(releaseLinkR2A_R1B));
ReleaseLink releaseLinkR1A = createReleaseLinkTo(r1A)
.setComment("Important linked release")
.setReleaseRelationship(ReleaseRelationship.REFERRED)
.setNodeId("R1A_1")
.setSubreleases(Arrays.asList(releaseLinkR1B, releaseLinkR2A_R1A));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import org.eclipse.sw360.datahandler.db.ComponentDatabaseHandler;
import org.eclipse.sw360.datahandler.db.ProjectDatabaseHandler;
import org.eclipse.sw360.datahandler.entitlement.ProjectModerator;
import org.eclipse.sw360.datahandler.thrift.MainlineState;
import org.eclipse.sw360.datahandler.thrift.ProjectReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.ReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.components.Component;
import org.eclipse.sw360.datahandler.thrift.components.ComponentType;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.components.ReleaseLink;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectLink;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectRelationship;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectType;
import org.eclipse.sw360.datahandler.thrift.projects.*;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.junit.After;
import org.junit.Before;
Expand All @@ -32,6 +32,7 @@
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import javax.xml.crypto.Data;
Expand All @@ -44,6 +45,7 @@
import static org.eclipse.sw360.datahandler.TestUtils.assertTestString;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

@RunWith(MockitoJUnitRunner.class)
public class ProjectDatabaseHandlerTest {
Expand Down Expand Up @@ -88,9 +90,9 @@ public void setUp() throws Exception {
projects = new ArrayList<>();
Project project1 = new Project().setId("P1").setName("project1").setLinkedProjects(ImmutableMap.of("P2", ProjectRelationship.CONTAINED));
projects.add(project1);
Project project2 = new Project().setId("P2").setName("project2").setLinkedProjects(ImmutableMap.of("P3", ProjectRelationship.REFERRED, "P4", ProjectRelationship.CONTAINED)).setReleaseIdToUsage(ImmutableMap.of("R1A", "used", "R1B", "abused"));
Project project2 = new Project().setId("P2").setName("project2").setLinkedProjects(ImmutableMap.of("P3", ProjectRelationship.REFERRED, "P4", ProjectRelationship.CONTAINED)).setReleaseIdToUsage(ImmutableMap.of("R1A", newDefaultProjectReleaseRelationship(), "R1B", newDefaultProjectReleaseRelationship()));
projects.add(project2);
Project project3 = new Project().setId("P3").setName("project3").setLinkedProjects(ImmutableMap.of("P2", ProjectRelationship.UNKNOWN)).setReleaseIdToUsage(ImmutableMap.of("R2A", "used", "R2B", "considered for use"));
Project project3 = new Project().setId("P3").setName("project3").setLinkedProjects(ImmutableMap.of("P2", ProjectRelationship.UNKNOWN)).setReleaseIdToUsage(ImmutableMap.of("R2A", newDefaultProjectReleaseRelationship(), "R2B", newDefaultProjectReleaseRelationship()));
projects.add(project3);
Project project4 = new Project().setId("P4").setName("project4").setLinkedProjects(ImmutableMap.of("P1", ProjectRelationship.UNKNOWN));
projects.add(project4);
Expand Down Expand Up @@ -124,6 +126,10 @@ public void setUp() throws Exception {
handler = new ProjectDatabaseHandler(DatabaseSettings.getConfiguredHttpClient(), dbName, attachmentsDbName, moderator, componentHandler);
}

private ProjectReleaseRelationship newDefaultProjectReleaseRelationship() {
return new ProjectReleaseRelationship(ReleaseRelationship.REFERRED, MainlineState.MAINLINE);
}

@After
public void tearDown() throws Exception {
TestUtils.deleteDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName);
Expand All @@ -142,10 +148,10 @@ public void testGetLinkedProjects() throws Exception {

final List<ProjectLink> linkedProjects = completionFuture.get();

ReleaseLink releaseLinkR1A = new ReleaseLink("R1A", "vendor", "component1", "releaseA", "vendor component1 releaseA").setComment("used").setNodeId("R1A_1").setComponentType(ComponentType.OSS);
ReleaseLink releaseLinkR1B = new ReleaseLink("R1B", "vendor", "component1", "releaseB", "vendor component1 releaseB").setComment("abused").setNodeId("R1B_1").setComponentType(ComponentType.OSS);
ReleaseLink releaseLinkR2A = new ReleaseLink("R2A", "vendor", "component2", "releaseA", "vendor component2 releaseA").setComment("used").setNodeId("R2A_1");
ReleaseLink releaseLinkR2B = new ReleaseLink("R2B", "vendor", "component2", "releaseB", "vendor component2 releaseB").setComment("considered for use").setNodeId("R2B_1");
ReleaseLink releaseLinkR1A = new ReleaseLink("R1A", "vendor", "component1", "releaseA", "vendor component1 releaseA").setReleaseRelationship(ReleaseRelationship.REFERRED).setMainlineState(MainlineState.MAINLINE).setNodeId("R1A_1").setComponentType(ComponentType.OSS);
ReleaseLink releaseLinkR1B = new ReleaseLink("R1B", "vendor", "component1", "releaseB", "vendor component1 releaseB").setReleaseRelationship(ReleaseRelationship.REFERRED).setMainlineState(MainlineState.MAINLINE).setNodeId("R1B_1").setComponentType(ComponentType.OSS);
ReleaseLink releaseLinkR2A = new ReleaseLink("R2A", "vendor", "component2", "releaseA", "vendor component2 releaseA").setReleaseRelationship(ReleaseRelationship.REFERRED).setMainlineState(MainlineState.MAINLINE).setNodeId("R2A_1");
ReleaseLink releaseLinkR2B = new ReleaseLink("R2B", "vendor", "component2", "releaseB", "vendor component2 releaseB").setReleaseRelationship(ReleaseRelationship.REFERRED).setMainlineState(MainlineState.MAINLINE).setNodeId("R2B_1");

ProjectLink link3 = new ProjectLink("P3", "project3")
.setRelation(ProjectRelationship.REFERRED)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2016. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2017. Part of the SW360 Portal Project.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -26,13 +26,16 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.eclipse.sw360.datahandler.common.CommonUtils.add;
import static org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyMap;
import static org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet;

/**
* Class for comparing a document with its counterpart in the database
* Writes the difference (= additions and deletions) to the moderation request
*
* @author [email protected]
* @author [email protected]
*/
public abstract class ModerationRequestGenerator<U extends TFieldIdEnum, T extends TBase<T, U>> {
protected T documentAdditions;
Expand Down Expand Up @@ -113,30 +116,28 @@ protected <S> void dealWithEnumMap(U field, Class<? extends TEnum> S) {
}
}

protected void dealWithStringMap(U field) {
Map<String,String> addedMap = (Map<String, String>) updateDocument.getFieldValue(field);
if(addedMap == null) {
addedMap = new HashMap<>();
}
Map<String,String> actualMap = (Map<String, String>) actualDocument.getFieldValue(field);
for(Map.Entry<String, String> entry : actualMap.entrySet()){
addedMap.remove(entry);
}
protected void dealWithStringKeyedMap(U field) {
Map<String, Object> addedMap = new HashMap<>();
addedMap.putAll(nullToEmptyMap((Map<String, Object>) updateDocument.getFieldValue(field)));

Map<String,String> deletedMap = (Map<String, String>) actualDocument.getFieldValue(field);
if (deletedMap == null) {
deletedMap = new HashMap<>();
Map<String, Object> actualMap = (Map<String, Object>) actualDocument.getFieldValue(field);
for(String key: actualMap.keySet()){
addedMap.remove(key);
}
Map<String,String> updateMap = (Map<String, String>) updateDocument.getFieldValue(field);
for(Map.Entry<String, String> entry : updateMap.entrySet()){
deletedMap.remove(entry);

Map<String, Object> deletedMap = new HashMap<>();
deletedMap.putAll(nullToEmptyMap((Map<String, Object>) actualDocument.getFieldValue(field)));

Map<String, Object> updateMap = (Map<String, Object>) updateDocument.getFieldValue(field);
for(String key : updateMap.keySet()){
deletedMap.remove(key);
}

//determine changes in common linkedProjects
Set<String> commonKeys = Sets.intersection(updateMap.keySet(), actualMap.keySet());
for(String id : commonKeys) {
String actual = actualMap.get(id);
String update = updateMap.get(id);
Object actual = actualMap.get(id);
Object update = updateMap.get(id);
if(! actual.equals(update)) {
addedMap.put(id, update);
deletedMap.put(id, actual);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2016. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2017. Part of the SW360 Portal Project.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down Expand Up @@ -57,7 +57,7 @@ public ModerationRequest setAdditionsAndDeletions(ModerationRequest request, Pro
dealWithEnumMap(Project._Fields.LINKED_PROJECTS, ProjectRelationship.class);
break;
case RELEASE_ID_TO_USAGE:
dealWithStringMap(Project._Fields.RELEASE_ID_TO_USAGE);
dealWithStringKeyedMap(Project._Fields.RELEASE_ID_TO_USAGE);
break;
case ROLES:
dealWithCustomMap(Project._Fields.ROLES);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2016. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2017. Part of the SW360 Portal Project.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -9,6 +9,7 @@

package org.eclipse.sw360.moderation.db;

import org.eclipse.sw360.datahandler.thrift.ReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.components.*;
import org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest;

Expand Down
Loading

0 comments on commit 03faaf7

Please sign in to comment.