1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.issue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.util.RubyUtils;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
/**
* @since 3.7
*/
public class IssueBulkChangeQuery {
private List<String> issues;
private List<String> actions;
private boolean hasComment;
private boolean sendNotifications;
Map<String, Map<String, Object>> propertiesByActions = new HashMap<String, Map<String, Object>>();
public IssueBulkChangeQuery(Map<String, Object> props, String comment, boolean sendNotifications) {
this.sendNotifications = sendNotifications;
parse(props, comment);
}
@VisibleForTesting
IssueBulkChangeQuery(Map<String, Object> props, boolean sendNotifications) {
this.sendNotifications = sendNotifications;
parse(props, null);
}
private void parse(Map<String, Object> props, @Nullable String comment) {
this.issues = sanitizeList(RubyUtils.toStrings(props.get("issues")));
if (issues == null || issues.isEmpty()) {
throw new BadRequestException("issue_bulk_change.error.empty_issues");
}
actions = sanitizeList(RubyUtils.toStrings(props.get("actions")));
if (actions == null || actions.isEmpty()) {
throw new BadRequestException("issue_bulk_change.error.need_one_action");
}
for (String action : actions) {
Map<String, Object> actionProperties = getActionProps(action, props);
propertiesByActions.put(action, actionProperties);
}
if (!Strings.isNullOrEmpty(comment)) {
hasComment = true;
Map<String, Object> commentMap = newHashMap();
commentMap.put(CommentAction.COMMENT_PROPERTY, comment);
propertiesByActions.put(CommentAction.KEY, commentMap);
}
}
private List<String> sanitizeList(@Nullable List<String> list) {
if (list == null || list.isEmpty()) {
return Collections.emptyList();
}
return newArrayList(Iterables.filter(list, new Predicate<String>() {
@Override
public boolean apply(@Nullable String input) {
return !Strings.isNullOrEmpty(input);
}
}));
}
public List<String> issues() {
return issues;
}
/**
* The list of actions to apply
* Note that even if a comment has been added, this list will NOT contains the comment action
*/
public List<String> actions() {
return actions;
}
public boolean hasComment() {
return hasComment;
}
public boolean sendNotifications() {
return sendNotifications;
}
public Map<String, Object> properties(String action) {
return propertiesByActions.get(action);
}
private static Map<String, Object> getActionProps(String action, Map<String, Object> props) {
Map<String, Object> actionProps = newHashMap();
for (Map.Entry<String, Object> propsEntry : props.entrySet()) {
String key = propsEntry.getKey();
String actionPrefix = action + ".";
String property = StringUtils.substringAfter(key, actionPrefix);
if (!property.isEmpty()) {
actionProps.put(property, propsEntry.getValue());
}
}
props.get(action);
return actionProps;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
|