@Override
public PatchSetPublishDetail call() throws OrmException,
PatchSetInfoNotAvailableException, NoSuchChangeException {
final Change.Id changeId = patchSetId.getParentKey();
final ChangeControl control = changeControlFactory.validateFor(changeId);
change = control.getChange();
PatchSet patchSet = db.patchSets().get(patchSetId);
patchSetInfo = infoFactory.get(change, patchSet);
drafts = db.patchComments().draftByPatchSetAuthor(patchSetId, user.getAccountId()).toList();
aic.want(change.getOwner());
PatchSetPublishDetail detail = new PatchSetPublishDetail();
detail.setPatchSetInfo(patchSetInfo);
detail.setChange(change);
detail.setDrafts(drafts);
List<PermissionRange> allowed = Collections.emptyList();
List<PatchSetApproval> given = Collections.emptyList();
if (change.getStatus().isOpen()
&& patchSetId.equals(change.currentPatchSetId())) {
// TODO Push this selection of labels down into the Prolog interpreter.
// Ideally we discover the labels the user can apply here based on doing
// a findall() over the space of labels they can apply combined against
// the submit rule, thereby skipping any mutually exclusive cases. However
// those are not common, so it might just be reasonable to take this
// simple approach.
Map<String, PermissionRange> rangeByName =
new HashMap<String, PermissionRange>();
for (PermissionRange r : control.getLabelRanges()) {
if (r.isLabel()) {
rangeByName.put(r.getLabel(), r);
}
}
allowed = new ArrayList<PermissionRange>();
given = db.patchSetApprovals() //
.byPatchSetUser(patchSetId, user.getAccountId()) //
.toList();
boolean couldSubmit = false;
List<SubmitRecord> submitRecords = control.canSubmit(db, patchSet);
for (SubmitRecord rec : submitRecords) {
if (rec.status == SubmitRecord.Status.OK) {
couldSubmit = true;
}
if (rec.labels != null) {
int ok = 0;
for (SubmitRecord.Label lbl : rec.labels) {
aic.want(lbl.appliedBy);
boolean canMakeOk = false;
PermissionRange range = rangeByName.get(lbl.label);
if (range != null) {
if (!allowed.contains(range)) {
allowed.add(range);
}
ApprovalType at = approvalTypes.byLabel(lbl.label);
if (at == null || at.getMax().getValue() == range.getMax()) {
canMakeOk = true;
}
}
switch (lbl.status) {
case OK:
case MAY:
ok++;
break;
case NEED:
if (canMakeOk) {
ok++;
}
break;
}
}
if (rec.status == SubmitRecord.Status.NOT_READY
&& ok == rec.labels.size()) {
couldSubmit = true;
}
}
}
if (couldSubmit && control.getRefControl().canSubmit()) {
detail.setCanSubmit(true);
}
detail.setSubmitRecords(submitRecords);
}