Migrate ApprovalTypes away from ApprovalCategory(Value)

Many callers of ApprovalTypes depend directly on ApprovalCategory and
ApprovalCategoryValue, which are the types stored in the database. We
want to move away from storing approval categories in the database in
favor of arbitrary labels that can be defined on a per-project level,
including implicitly in submit rules. Leaking the database types
around the code makes this harder.

Replace ApprovalCategoryValue everywhere except the database code with
a similar and more concise LabelValue type. Use Strings instead of the
database Id types when referring to labels in general. In the short
term this may create some confusion in the code (alleviated,
hopefully, by documentation and variable names), since some places use
legacy category IDs as unique keys and others use new style label
names as unique keys. In the long term we will stop using category IDs
except to map them to unique label names.

Unlike ApprovalCategory, the new and improved ApprovalType does not
distinguish between the label name and the human-readable name.
Generally these do not vary significantly other than the label name
having dashes instead of spaces, which is minor enough in the UI. The
other affected functionality here is searching by label; since label
names can no longer contain spaces "label:CodeReview" no longer works,
only "label:Code-Review".

Change-Id: I6854259d60af88b6d9df8d7553120a9af64c74ad
This commit is contained in:
Dave Borowitz
2013-02-05 15:34:18 -08:00
parent 1c830c11e7
commit 41f909fa98
31 changed files with 404 additions and 294 deletions

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gerrit.common.data.ApprovalType;
@@ -90,9 +91,9 @@ public class ApprovalsUtil {
for (PatchSetApproval a : patchSetApprovals) {
// ApprovalCategory.SUBMIT is still in db but not relevant in git-store
if (!ApprovalCategory.SUBMIT.equals(a.getCategoryId())) {
final ApprovalType type = approvalTypes.byId(a.getCategoryId());
final ApprovalType type = approvalTypes.byId(a.getCategoryId().get());
if (a.getPatchSetId().equals(source) &&
type.getCategory().isCopyMinScore() &&
type.isCopyMinScore() &&
type.isMaxNegative(a)) {
db.patchSetApprovals().insert(
Collections.singleton(new PatchSetApproval(dest, a)));
@@ -128,7 +129,8 @@ public class ApprovalsUtil {
need.removeAll(existingReviewers);
List<PatchSetApproval> cells = Lists.newArrayListWithCapacity(need.size());
ApprovalCategory.Id catId = allTypes.get(allTypes.size() - 1).getCategory().getId();
ApprovalCategory.Id catId =
Iterables.getLast(allTypes).getApprovalCategoryId();
for (Account.Id account : need) {
PatchSetApproval psa = new PatchSetApproval(
new PatchSetApproval.Key(ps.getId(), account, catId),

View File

@@ -37,12 +37,12 @@ import com.google.common.collect.Sets;
import com.google.gerrit.common.changes.ListChangesOption;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.LabelValue;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.common.data.SubmitRecord;
import com.google.gerrit.extensions.restapi.Url;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Change.Status;
import com.google.gerrit.reviewdb.client.ChangeMessage;
@@ -408,7 +408,7 @@ public class ChangeJson {
for (PatchSetApproval psa : cd.currentApprovals(db)) {
short val = psa.getValue();
if (val != 0 && min < val && val < max
&& psa.getCategoryId().equals(type.getCategory().getId())) {
&& psa.getCategoryId().get().equals(type.getId())) {
if (0 < val) {
label.recommended = accountLoader.get(psa.getAccountId());
label.value = val != 1 ? val : null;
@@ -429,25 +429,24 @@ public class ChangeJson {
FunctionState fs =
functionState.create(ctl, cd.change(db).currentPatchSetId(), approvals);
for (ApprovalType at : approvalTypes.getApprovalTypes()) {
CategoryFunction.forCategory(at.getCategory()).run(at, fs);
CategoryFunction.forType(at).run(at, fs);
}
Multimap<Account.Id, String> existing =
HashMultimap.create(approvals.size(), labels.size());
for (PatchSetApproval psa : approvals) {
ApprovalType at = approvalTypes.byId(psa.getCategoryId());
ApprovalType at = approvalTypes.byId(psa.getCategoryId().get());
if (at == null) {
continue;
}
String name = at.getCategory().getLabelName();
LabelInfo p = labels.get(name);
LabelInfo p = labels.get(at.getName());
if (p == null) {
continue; // TODO: support arbitrary labels.
}
if (!getRange(ctl, psa.getAccountId(), name).isEmpty()) {
if (!getRange(ctl, psa.getAccountId(), at.getName()).isEmpty()) {
p.addApproval(approvalInfo(psa.getAccountId(), psa.getValue()));
}
existing.put(psa.getAccountId(), at.getCategory().getLabelName());
existing.put(psa.getAccountId(), at.getName());
}
// Add dummy approvals for all permitted labels for each user even if they
@@ -478,11 +477,11 @@ public class ChangeJson {
Map<String, LabelInfo> labels =
new TreeMap<String, LabelInfo>(LabelOrdering.create(approvalTypes));
for (PatchSetApproval psa : cd.currentApprovals(db)) {
ApprovalType type = approvalTypes.byId(psa.getCategoryId());
ApprovalType type = approvalTypes.byId(psa.getCategoryId().get());
if (type == null) {
continue;
}
String label = type.getCategory().getLabelName();
String label = type.getName();
LabelInfo li = labels.get(label);
if (li == null) {
li = new LabelInfo();
@@ -540,8 +539,8 @@ public class ChangeJson {
private void setLabelValues(ApprovalType type, LabelInfo label) {
label.values = Maps.newLinkedHashMap();
for (ApprovalCategoryValue acv : type.getValues()) {
label.values.put(acv.formatValue(), acv.getName());
for (LabelValue v : type.getValues()) {
label.values.put(v.formatValue(), v.getText());
}
if (isOnlyZero(label.values.keySet())) {
label.values = null;
@@ -562,9 +561,9 @@ public class ChangeJson {
continue; // TODO: Support arbitrary labels.
}
PermissionRange range = ctl.getRange(Permission.forLabel(r.label));
for (ApprovalCategoryValue acv : type.getValues()) {
if (range.contains(acv.getValue())) {
permitted.put(r.label, acv.formatValue());
for (LabelValue v : type.getValues()) {
if (range.contains(v.getValue())) {
permitted.put(r.label, v.formatValue());
}
}
}

View File

@@ -26,7 +26,7 @@ class LabelOrdering {
@Override
public Short apply(String n) {
ApprovalType at = approvalTypes.byLabel(n);
return at != null ? at.getCategory().getPosition() : null;
return at != null ? at.getPosition() : null;
}
}).compound(Ordering.natural());
}

View File

@@ -209,7 +209,7 @@ public class PostReview implements RestModifyView<RevisionResource, Input> {
}
}
String name = at.getCategory().getLabelName();
String name = at.getName();
PermissionRange range = ctl.getRange(Permission.forLabel(name));
if (range == null || !range.contains(ent.getValue())) {
if (strict) {
@@ -346,7 +346,7 @@ public class PostReview implements RestModifyView<RevisionResource, Input> {
for (Map.Entry<String, Short> ent : labels.entrySet()) {
// TODO Support arbitrary label names.
ApprovalType at = approvalTypes.byLabel(ent.getKey());
String name = at.getCategory().getLabelName();
String name = at.getName();
if (change.getStatus().isClosed()) {
// TODO Allow updating some labels even when closed.
continue;
@@ -368,23 +368,23 @@ public class PostReview implements RestModifyView<RevisionResource, Input> {
upd.add(c);
labelDelta.add(format(name, c.getValue()));
categories.put(
at.getCategory().getId(),
at.getValue(c.getValue()).getId());
at.getApprovalCategoryId(),
at.getApprovalCategoryValueId(c.getValue()));
} else if (c != null && c.getValue() == ent.getValue()) {
current.put(name, c);
} else if (c == null) {
c = new PatchSetApproval(new PatchSetApproval.Key(
rsrc.getPatchSet().getId(),
rsrc.getAccountId(),
at.getCategory().getId()),
at.getApprovalCategoryId()),
ent.getValue());
c.setGranted(timestamp);
c.cache(change);
ins.add(c);
labelDelta.add(format(name, c.getValue()));
categories.put(
at.getCategory().getId(),
at.getValue(c.getValue()).getId());
at.getApprovalCategoryId(),
at.getApprovalCategoryValueId(c.getValue()));
}
}
@@ -406,7 +406,7 @@ public class PostReview implements RestModifyView<RevisionResource, Input> {
PatchSetApproval c = new PatchSetApproval(new PatchSetApproval.Key(
rsrc.getPatchSet().getId(),
rsrc.getAccountId(),
approvalTypes.getApprovalTypes().get(0).getCategory().getId()),
approvalTypes.getApprovalTypes().get(0).getApprovalCategoryId()),
(short) 0);
c.setGranted(timestamp);
c.cache(change);
@@ -433,9 +433,9 @@ public class PostReview implements RestModifyView<RevisionResource, Input> {
continue;
}
ApprovalType at = approvalTypes.byId(a.getCategoryId());
ApprovalType at = approvalTypes.byId(a.getCategoryId().get());
if (at != null) {
current.put(at.getCategory().getLabelName(), a);
current.put(at.getName(), a);
} else {
del.add(a);
}

View File

@@ -117,8 +117,8 @@ public class PostReviewers implements RestModifyView<ChangeResource, Input> {
this.accountCache = accountCache;
this.json = json;
this.addReviewerCategoryId = Iterables.getLast(
approvalTypes.getApprovalTypes()).getCategory().getId();
this.addReviewerCategoryId = Iterables.getLast(approvalTypes.getApprovalTypes())
.getApprovalCategoryId();
}
@Override

View File

@@ -14,7 +14,7 @@
package com.google.gerrit.server.change;
import static com.google.gerrit.reviewdb.client.ApprovalCategoryValue.formatValue;
import static com.google.gerrit.common.data.LabelValue.formatValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
@@ -85,7 +85,7 @@ public class ReviewerJson {
FunctionState fs = functionState.create(ctl, psId, approvals);
for (ApprovalType at : approvalTypes.getApprovalTypes()) {
CategoryFunction.forCategory(at.getCategory()).run(at, fs);
CategoryFunction.forType(at).run(at, fs);
}
// Don't use Maps.newTreeMap(Comparator) due to OpenJDK bug 100167.
@@ -95,10 +95,9 @@ public class ReviewerJson {
for (PermissionRange pr : ctl.getLabelRanges()) {
if (!pr.isEmpty()) {
// TODO: Support arbitrary labels.
ApprovalType at = approvalTypes.byId(ca.getCategoryId());
ApprovalType at = approvalTypes.byId(ca.getCategoryId().get());
if (at != null) {
out.approvals.put(at.getCategory().getLabelName(),
formatValue(ca.getValue())); }
out.approvals.put(at.getName(), formatValue(ca.getValue())); }
}
}
}

View File

@@ -47,7 +47,7 @@ public class ApprovalTypesProvider implements Provider<ApprovalTypes> {
for (final ApprovalCategory c : db.approvalCategories().all()) {
final List<ApprovalCategoryValue> values =
db.approvalCategoryValues().byCategory(c.getId()).toList();
types.add(new ApprovalType(c, values));
types.add(ApprovalType.fromApprovalCategory(c, values));
}
} finally {
db.close();

View File

@@ -460,9 +460,9 @@ public class EventFactory {
a.by = asAccountAttribute(approval.getAccountId());
a.grantedOn = approval.getGranted().getTime() / 1000L;
ApprovalType at = approvalTypes.byId(approval.getCategoryId());
ApprovalType at = approvalTypes.byId(approval.getCategoryId().get());
if (at != null) {
a.description = at.getCategory().getName();
a.description = at.getName();
}
return a;
}

View File

@@ -937,7 +937,7 @@ public class MergeOp {
identifiedUserFactory.create(c.getOwner())),
merged, approvals);
for (ApprovalType at : approvalTypes.getApprovalTypes()) {
CategoryFunction.forCategory(at.getCategory()).run(at, fs);
CategoryFunction.forType(at).run(at, fs);
}
for (PatchSetApproval a : approvals) {
if (a.getValue() > 0

View File

@@ -254,12 +254,12 @@ public class MergeUtil {
} else if (VRIF.equals(a.getCategoryId())) {
tag = "Tested-by";
} else {
final ApprovalType at = approvalTypes.byId(a.getCategoryId());
final ApprovalType at = approvalTypes.byId(a.getCategoryId().get());
if (at == null) {
// A deprecated/deleted approval type, ignore it.
// TODO: Support arbitrary labels.
continue;
}
tag = at.getCategory().getName().replace(' ', '-');
tag = at.getName();
}
if (!contains(footers, new FooterKey(tag), identbuf.toString())) {

View File

@@ -14,8 +14,8 @@
package com.google.gerrit.server.git;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.ApprovalCategory;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
@@ -51,9 +51,8 @@ class ReviewNoteHeaderFormatter {
sb.append("Change-Id: ").append(changeKey.get()).append("\n");
}
void appendApproval(ApprovalCategory category,
short value, Account user) {
sb.append(category.getLabelName());
void appendApproval(ApprovalType type, short value, Account user) {
sb.append(type.getName());
sb.append(value < 0 ? "-" : "+").append(Math.abs(value)).append(": ");
appendUserData(user);
sb.append("\n");

View File

@@ -14,22 +14,20 @@
package com.google.gerrit.server.mail;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.LabelValue;
import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
import com.google.gerrit.reviewdb.client.ApprovalCategory;
import com.google.gerrit.reviewdb.client.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import java.util.HashMap;
import java.util.Map;
/** Send notice about a change successfully merged. */
public class MergedSender extends ReplyToChangeSender {
public static interface Factory {
@@ -61,18 +59,18 @@ public class MergedSender extends ReplyToChangeSender {
public String getApprovals() {
try {
final Map<Account.Id, Map<ApprovalCategory.Id, PatchSetApproval>> pos =
new HashMap<Account.Id, Map<ApprovalCategory.Id, PatchSetApproval>>();
final Map<Account.Id, Map<ApprovalCategory.Id, PatchSetApproval>> neg =
new HashMap<Account.Id, Map<ApprovalCategory.Id, PatchSetApproval>>();
Table<Account.Id, String, PatchSetApproval> pos = HashBasedTable.create();
Table<Account.Id, String, PatchSetApproval> neg = HashBasedTable.create();
for (PatchSetApproval ca : args.db.get().patchSetApprovals()
.byPatchSet(patchSet.getId())) {
ApprovalType lt = approvalTypes.byId(ca.getCategoryId().get());
if (lt == null) {
continue;
}
if (ca.getValue() > 0) {
insert(pos, ca);
pos.put(ca.getAccountId(), lt.getName(), ca);
} else if (ca.getValue() < 0) {
insert(neg, ca);
neg.put(ca.getAccountId(), lt.getName(), ca);
}
}
@@ -83,22 +81,20 @@ public class MergedSender extends ReplyToChangeSender {
return "";
}
private String format(final String type,
final Map<Account.Id, Map<ApprovalCategory.Id, PatchSetApproval>> list) {
private String format(String type,
Table<Account.Id, String, PatchSetApproval> approvals) {
StringBuilder txt = new StringBuilder();
if (list.isEmpty()) {
if (approvals.isEmpty()) {
return "";
}
txt.append(type + ":\n");
for (final Map.Entry<Account.Id, Map<ApprovalCategory.Id, PatchSetApproval>> ent : list
.entrySet()) {
final Map<ApprovalCategory.Id, PatchSetApproval> l = ent.getValue();
txt.append(type).append(":\n");
for (Account.Id id : approvals.rowKeySet()) {
txt.append(" ");
txt.append(getNameFor(ent.getKey()));
txt.append(getNameFor(id));
txt.append(": ");
boolean first = true;
for (ApprovalType at : approvalTypes.getApprovalTypes()) {
final PatchSetApproval ca = l.get(at.getCategory().getId());
for (ApprovalType lt : approvalTypes.getApprovalTypes()) {
PatchSetApproval ca = approvals.get(id, lt.getName());
if (ca == null) {
continue;
}
@@ -109,32 +105,18 @@ public class MergedSender extends ReplyToChangeSender {
txt.append("; ");
}
final ApprovalCategoryValue v = at.getValue(ca);
LabelValue v = lt.getValue(ca);
if (v != null) {
txt.append(v.getName());
txt.append(v.getText());
} else {
txt.append(at.getCategory().getName());
txt.append("=");
if (ca.getValue() > 0) {
txt.append("+");
}
txt.append("" + ca.getValue());
txt.append(lt.getName());
txt.append('=');
txt.append(LabelValue.formatValue(ca.getValue()));
}
}
txt.append("\n");
txt.append('\n');
}
txt.append("\n");
txt.append('\n');
return txt.toString();
}
private void insert(
final Map<Account.Id, Map<ApprovalCategory.Id, PatchSetApproval>> list,
final PatchSetApproval ca) {
Map<ApprovalCategory.Id, PatchSetApproval> m = list.get(ca.getAccountId());
if (m == null) {
m = new HashMap<ApprovalCategory.Id, PatchSetApproval>();
list.put(ca.getAccountId(), m);
}
m.put(ca.getCategoryId(), ca);
}
}

View File

@@ -18,7 +18,6 @@ import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.ApprovalCategory;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.server.ReviewDb;
@@ -58,34 +57,28 @@ class LabelPredicate extends OperatorPredicate<ChangeData> {
abstract boolean match(int psValue, int expValue);
}
private static ApprovalCategory category(ApprovalTypes types, String toFind) {
private static ApprovalType type(ApprovalTypes types, String toFind) {
if (types.byLabel(toFind) != null) {
return types.byLabel(toFind).getCategory();
return types.byLabel(toFind);
}
if (types.byId(new ApprovalCategory.Id(toFind)) != null) {
return types.byId(new ApprovalCategory.Id(toFind)).getCategory();
if (types.byId(toFind) != null) {
return types.byId(toFind);
}
for (ApprovalType at : types.getApprovalTypes()) {
ApprovalCategory category = at.getCategory();
if (toFind.equalsIgnoreCase(category.getName())) {
return category;
} else if (toFind.equalsIgnoreCase(category.getName().replace(" ", ""))) {
return category;
if (toFind.equalsIgnoreCase(at.getName())) {
return at;
}
}
for (ApprovalType at : types.getApprovalTypes()) {
ApprovalCategory category = at.getCategory();
if (toFind.equalsIgnoreCase(category.getAbbreviatedName())) {
return category;
if (toFind.equalsIgnoreCase(at.getAbbreviatedName())) {
return at;
}
}
return new ApprovalCategory(new ApprovalCategory.Id(toFind), toFind);
return ApprovalType.withDefaultValues(toFind.substring(0, 4), toFind);
}
private static Test op(String op) {
@@ -114,7 +107,7 @@ class LabelPredicate extends OperatorPredicate<ChangeData> {
private final IdentifiedUser.GenericFactory userFactory;
private final Provider<ReviewDb> dbProvider;
private final Test test;
private final ApprovalCategory category;
private final ApprovalType type;
private final String permissionName;
private final int expVal;
@@ -129,22 +122,22 @@ class LabelPredicate extends OperatorPredicate<ChangeData> {
Matcher m1 = Pattern.compile("(=|>=|<=)([+-]?\\d+)$").matcher(value);
Matcher m2 = Pattern.compile("([+-]\\d+)$").matcher(value);
if (m1.find()) {
category = category(types, value.substring(0, m1.start()));
type = type(types, value.substring(0, m1.start()));
test = op(m1.group(1));
expVal = value(m1.group(2));
} else if (m2.find()) {
category = category(types, value.substring(0, m2.start()));
type = type(types, value.substring(0, m2.start()));
test = Test.EQ;
expVal = value(m2.group(1));
} else {
category = category(types, value);
type = type(types, value);
test = Test.EQ;
expVal = 1;
}
this.permissionName = Permission.forLabel(category.getLabelName());
this.permissionName = Permission.forLabel(type.getName());
}
@Override
@@ -153,7 +146,7 @@ class LabelPredicate extends OperatorPredicate<ChangeData> {
final Set<Account.Id> approversThatVotedInCategory = new HashSet<Account.Id>();
for (PatchSetApproval p : object.currentApprovals(dbProvider)) {
allApprovers.add(p.getAccountId());
if (p.getCategoryId().equals(category.getId())) {
if (p.getCategoryId().get().equals(type.getId())) {
approversThatVotedInCategory.add(p.getAccountId());
if (match(object.change(dbProvider), p.getValue(), p.getAccountId())) {
return true;

View File

@@ -32,6 +32,22 @@ public abstract class CategoryFunction {
all.put(NoBlock.NAME, new NoBlock());
}
public static CategoryFunction forName(String name) {
return all.get(name);
}
/**
* Locate a function by type.
*
* @param type the type the function is for.
* @return the function implementation; {@link NoOpFunction} if the function
* is not known to Gerrit and thus cannot be executed.
*/
public static CategoryFunction forType(ApprovalType type) {
CategoryFunction r = all.get(type.getFunctionName());
return r != null ? r : new NoOpFunction();
}
/**
* Locate a function by category.
*

View File

@@ -16,14 +16,12 @@ package com.google.gerrit.server.workflow;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.LabelValue;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.reviewdb.client.ApprovalCategory;
import com.google.gerrit.reviewdb.client.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.client.ApprovalCategory.Id;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.project.ChangeControl;
@@ -47,10 +45,9 @@ public class FunctionState {
private final ApprovalTypes approvalTypes;
private final IdentifiedUser.GenericFactory userFactory;
private final Map<ApprovalCategory.Id, Collection<PatchSetApproval>> approvals =
new HashMap<ApprovalCategory.Id, Collection<PatchSetApproval>>();
private final Map<ApprovalCategory.Id, Boolean> valid =
new HashMap<ApprovalCategory.Id, Boolean>();
private final Map<String, Collection<PatchSetApproval>> approvals =
new HashMap<String, Collection<PatchSetApproval>>();
private final Map<String, Boolean> valid = new HashMap<String, Boolean>();
private final ChangeControl callerChangeControl;
private final Change change;
@@ -67,10 +64,15 @@ public class FunctionState {
for (final PatchSetApproval ca : all) {
if (psId.equals(ca.getPatchSetId())) {
Collection<PatchSetApproval> l = approvals.get(ca.getCategoryId());
Collection<PatchSetApproval> l =
approvals.get(ca.getCategoryId().get());
if (l == null) {
l = new ArrayList<PatchSetApproval>();
approvals.put(ca.getCategoryId(), l);
ApprovalType at = approvalTypes.byId(ca.getCategoryId().get());
if (at != null) {
// TODO: Support arbitrary labels
approvals.put(at.getName(), l);
}
}
l.add(ca);
}
@@ -90,20 +92,20 @@ public class FunctionState {
}
public boolean isValid(final ApprovalType at) {
return isValid(id(at));
return isValid(at.getName());
}
public boolean isValid(final ApprovalCategory.Id id) {
final Boolean b = valid.get(id);
public boolean isValid(final String labelName) {
final Boolean b = valid.get(labelName);
return b != null && b;
}
public Collection<PatchSetApproval> getApprovals(final ApprovalType at) {
return getApprovals(id(at));
return getApprovals(at.getName());
}
public Collection<PatchSetApproval> getApprovals(final ApprovalCategory.Id id) {
final Collection<PatchSetApproval> l = approvals.get(id);
public Collection<PatchSetApproval> getApprovals(final String labelName) {
final Collection<PatchSetApproval> l = approvals.get(labelName);
return l != null ? l : Collections.<PatchSetApproval> emptySet();
}
@@ -113,13 +115,13 @@ public class FunctionState {
* <p>
*/
private void applyTypeFloor(final ApprovalType at, final PatchSetApproval a) {
final ApprovalCategoryValue atMin = at.getMin();
final LabelValue atMin = at.getMin();
if (atMin != null && a.getValue() < atMin.getValue()) {
a.setValue(atMin.getValue());
}
final ApprovalCategoryValue atMax = at.getMax();
final LabelValue atMax = at.getMax();
if (atMax != null && a.getValue() > atMax.getValue()) {
a.setValue(atMax.getValue());
}
@@ -135,8 +137,7 @@ public class FunctionState {
* <p>
*/
private void applyRightFloor(final ApprovalType at, final PatchSetApproval a) {
final ApprovalCategory category = at.getCategory();
final String permission = Permission.forLabel(category.getLabelName());
final String permission = Permission.forLabel(at.getName());
final IdentifiedUser user = userFactory.create(a.getAccountId());
final PermissionRange range = controlFor(user).getRange(permission);
a.setValue((short) range.squash(a.getValue()));
@@ -152,7 +153,7 @@ public class FunctionState {
applyRightFloor(at, ca);
}
private static Id id(final ApprovalType at) {
return at.getCategory().getId();
private static String id(final ApprovalType at) {
return at.getId();
}
}