13
13
import jakarta .inject .Inject ;
14
14
15
15
import java .util .Collections ;
16
+ import java .util .HashMap ;
17
+ import java .util .HashSet ;
16
18
import java .util .List ;
17
19
import java .util .Map ;
18
- import java .util .stream . Collectors ;
20
+ import java .util .Set ;
19
21
20
22
/**
21
23
* Implements the {@link RulesService} interface.
@@ -45,9 +47,9 @@ public void applyRules(String groupId, String artifactId, String artifactType, T
45
47
RuleApplicationType ruleApplicationType , List <ArtifactReference > references ,
46
48
Map <String , TypedContent > resolvedReferences ) throws RuleViolationException {
47
49
@ SuppressWarnings ("unchecked" )
48
- List <RuleType > rules = Collections .EMPTY_LIST ;
50
+ Set <RuleType > artifactRules = Collections .EMPTY_SET ;
49
51
if (ruleApplicationType == RuleApplicationType .UPDATE ) {
50
- rules = storage .getArtifactRules (groupId , artifactId );
52
+ artifactRules = new HashSet <>( storage .getArtifactRules (groupId , artifactId ) );
51
53
}
52
54
LazyContentList currentContent = null ;
53
55
if (ruleApplicationType == RuleApplicationType .UPDATE ) {
@@ -57,39 +59,44 @@ public void applyRules(String groupId, String artifactId, String artifactType, T
57
59
currentContent = new LazyContentList (storage , Collections .emptyList ());
58
60
}
59
61
60
- applyGlobalAndArtifactRules (groupId , artifactId , artifactType , currentContent , content , rules ,
61
- references , resolvedReferences );
62
+ applyAllRules (groupId , artifactId , artifactType , currentContent , content , artifactRules , references ,
63
+ resolvedReferences );
62
64
}
63
65
64
- private void applyGlobalAndArtifactRules (String groupId , String artifactId , String artifactType ,
65
- List <TypedContent > currentContent , TypedContent updatedContent , List <RuleType > artifactRules ,
66
+ private void applyAllRules (String groupId , String artifactId , String artifactType ,
67
+ List <TypedContent > currentContent , TypedContent updatedContent , Set <RuleType > artifactRules ,
66
68
List <ArtifactReference > references , Map <String , TypedContent > resolvedReferences ) {
67
69
68
- Map <RuleType , RuleConfigurationDto > globalOrArtifactRulesMap = artifactRules .stream ()
69
- .collect (Collectors .toMap (ruleType -> ruleType ,
70
- ruleType -> storage .getArtifactRule (groupId , artifactId , ruleType )));
71
-
72
- if (globalOrArtifactRulesMap .isEmpty ()) {
73
- List <RuleType > globalRules = storage .getGlobalRules ();
74
- globalOrArtifactRulesMap = globalRules .stream ()
75
- .collect (Collectors .toMap (ruleType -> ruleType , storage ::getGlobalRule ));
76
-
77
- // Add any default global rules to the map (after filtering out any global rules from
78
- // artifactStore)
79
- Map <RuleType , RuleConfigurationDto > filteredDefaultGlobalRulesMap = rulesProperties
80
- .getFilteredDefaultGlobalRules (globalRules ).stream ().collect (Collectors
81
- .toMap (ruleType -> ruleType , rulesProperties ::getDefaultGlobalRuleConfiguration ));
82
- globalOrArtifactRulesMap .putAll (filteredDefaultGlobalRulesMap );
83
- }
84
-
85
- if (globalOrArtifactRulesMap .isEmpty ()) {
86
- return ;
87
- }
88
-
89
- for (RuleType ruleType : globalOrArtifactRulesMap .keySet ()) {
70
+ // TODO Getting the list of rules to apply results in several (admittedly fast) DB calls.
71
+ // Can we perhaps do a single DB call to get the map of rules to apply?
72
+
73
+ Map <RuleType , RuleConfigurationDto > allRules = new HashMap <>();
74
+
75
+ // Get the group rules (we already have the artifact rules)
76
+ Set <RuleType > groupRules = storage .isGroupExists (groupId )
77
+ ? new HashSet <>(storage .getGroupRules (groupId )) : Set .of ();
78
+ // Get the global rules
79
+ Set <RuleType > globalRules = new HashSet <>(storage .getGlobalRules ());
80
+ // Get the configured default global rules
81
+ Set <RuleType > defaultGlobalRules = rulesProperties .getDefaultGlobalRules ();
82
+
83
+ // Build the map of rules to apply (may be empty)
84
+ List .of (RuleType .values ()).forEach (rt -> {
85
+ if (artifactRules .contains (rt )) {
86
+ allRules .put (rt , storage .getArtifactRule (groupId , artifactId , rt ));
87
+ } else if (groupRules .contains (rt )) {
88
+ allRules .put (rt , storage .getGroupRule (groupId , rt ));
89
+ } else if (globalRules .contains (rt )) {
90
+ allRules .put (rt , storage .getGlobalRule (rt ));
91
+ } else if (defaultGlobalRules .contains (rt )) {
92
+ allRules .put (rt , rulesProperties .getDefaultGlobalRuleConfiguration (rt ));
93
+ }
94
+ });
95
+
96
+ // Apply rules
97
+ for (RuleType ruleType : allRules .keySet ()) {
90
98
applyRule (groupId , artifactId , artifactType , currentContent , updatedContent , ruleType ,
91
- globalOrArtifactRulesMap .get (ruleType ).getConfiguration (), references ,
92
- resolvedReferences );
99
+ allRules .get (ruleType ).getConfiguration (), references , resolvedReferences );
93
100
}
94
101
}
95
102
@@ -138,8 +145,8 @@ public void applyRules(String groupId, String artifactId, String artifactVersion
138
145
artifactVersion );
139
146
TypedContent typedVersionContent = TypedContent .create (versionContent .getContent (),
140
147
versionContent .getContentType ());
141
- applyGlobalAndArtifactRules ( groupId , artifactId , artifactType ,
142
- Collections .singletonList (typedVersionContent ), updatedContent ,
143
- storage . getArtifactRules ( groupId , artifactId ) , references , resolvedReferences );
148
+ Set < RuleType > artifactRules = new HashSet <>( storage . getArtifactRules ( groupId , artifactId ));
149
+ applyAllRules ( groupId , artifactId , artifactType , Collections .singletonList (typedVersionContent ),
150
+ updatedContent , artifactRules , references , resolvedReferences );
144
151
}
145
152
}
0 commit comments