public IssueLabel getIssueLabel(Project project, IssueLabelCategory category) { IssueLabel label = new IssueLabel(); label.project = project; label.name = labelName; label.color = labelColor; label.category = category; return label; }
/** * Responds to a request to delete the specified issue label. * * <p>This method is used when a user click a button to delete a issue label in issue list, * editing issue or new issue page. * * <p>Deletes an issue label corresponding to the given {@code id}. * * <p>- Returns {@code 200 OK} if the issue label is deleted succesfully. - Returns {@code 403 * Forbidden} if the user has no permission to delete the issue label. - Returns {@code 404 Not * Found} if no issue label is found. * * <p>The request must have a {@code _method} parameter and the parameter value must be * case-insensitive "delete"; otherwise, returns 400 Bad Request. We use this trick because an * HTML Form does not support the DELETE method. * * @param ownerName Don't use. * @param projectName Don't use. * @param id the id of the label to be deleted * @return the response to the request to delete an issue label */ @Transactional @IsAllowed(value = Operation.DELETE, resourceType = ResourceType.ISSUE_LABEL) public static Result delete(String ownerName, String projectName, Long id) { // _method must be 'delete' DynamicForm bindedForm = form().bindFromRequest(); if (!bindedForm.get("_method").toLowerCase().equals("delete")) { return badRequest(ErrorViews.BadRequest.render("_method must be 'delete'.")); } IssueLabel label = IssueLabel.finder.byId(id); label.delete(); return ok(); }
@IsAllowed(value = Operation.UPDATE, resourceType = ResourceType.ISSUE_LABEL) public static Result update(String ownerName, String projectName, Long id) { Form<IssueLabel> form = new Form<>(IssueLabel.class).bindFromRequest(); if (form.hasErrors()) { return badRequest(form.errorsAsJson()); } IssueLabel label = form.get(); label.id = id; label.project = Project.findByOwnerAndProjectName(ownerName, projectName); label.update(); return ok(); }
@IsAllowed(Operation.UPDATE) public static Result labelsForm(String ownerName, String projectName) { Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<IssueLabel> labels = IssueLabel.findByProject(project); return ok(views.html.project.issuelabels.render(project, labels)); }
private static Result labelsAsPjax(String ownerName, String projectName) { response().setHeader("Cache-Control", "no-cache, no-store"); Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<IssueLabel> labels = IssueLabel.findByProject(project); return ok(views.html.project.partial_issuelabels_list.render(project, labels)); }
/** * Responds to a request to add an issue label for the specified project. * * <p>This method is used when a user tries to add a issue label in issue list, editing issue or * new issue page. * * <p>Adds an issue label created with values taken from {@link * Form#bindFromRequest(java.util.Map, String...)} in the project specified by the {@code * ownerName} and {@code projectName}. But if there has already been the same issue label in * category, name and color, then this method returns an empty 204 No Content response. * * <p>When a new label is added, this method encodes the label's fields: {@link IssueLabel#id}, * {@link IssueLabel#name}, {@link IssueLabel#color}, and {@link IssueLabel#category} into {@code * application/json}, and includes them in the body of the 201 Created response. But if the client * cannot accept {@code application/json}, it returns the 201 Created with no response body. * * <p>Returns 403 Forbidden, if the user has no permission to add a new label in the project. * * @param ownerName the name of a project owner * @param projectName the name of a project * @return the response to the request to add a new issue label */ @Transactional @IsCreatable(ResourceType.ISSUE_LABEL) public static Result newLabel(String ownerName, String projectName) { Project project = Project.findByOwnerAndProjectName(ownerName, projectName); Form<NewLabel> newLabelForm = form(NewLabel.class).bindFromRequest(); if (newLabelForm.hasErrors()) { return badRequest(newLabelForm.errorsAsJson()); } IssueLabelCategory category = newLabelForm.get().getIssueLabelCategory(project); if (category.exists()) { category = IssueLabelCategory.findBy(category); } else { category.save(); } IssueLabel label = newLabelForm.get().getIssueLabel(project, category); if (label.exists()) { return noContent(); } else { label.save(); if (!request().accepts("application/json")) { return created(); } response().setHeader("Content-Type", "application/json"); Map<String, String> labelPropertyMap = new HashMap<>(); labelPropertyMap.put("id", "" + label.id); labelPropertyMap.put("name", label.name); labelPropertyMap.put("color", label.color); labelPropertyMap.put("category", label.category.name); labelPropertyMap.put("categoryId", "" + label.category.id); return created(toJson(labelPropertyMap)); } }
/** * Responds to a request for the CSS styles of all issue labels in the specified project. * * <p>This method is used when CSS styles of issue labels are required in any page which uses * issue label. * * @param ownerName the name of a project owner * @param projectName the name of a project * @return the response to the request for the css styles in text/css. */ @IsAllowed(Operation.READ) public static Result labelStyles(String ownerName, String projectName) { Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<IssueLabel> labels = IssueLabel.findByProject(project); String eTag = "\"" + labels.hashCode() + "\""; String ifNoneMatchValue = request().getHeader("If-None-Match"); if (ifNoneMatchValue != null && ifNoneMatchValue.equals(eTag)) { response().setHeader("ETag", eTag); return status(NOT_MODIFIED); } response().setHeader("ETag", eTag); return ok(views.html.common.issueLabelColor.render(labels)).as("text/css"); }
/** * Retrieves a project corresponding to {@code ownerName} and {@code projectName}, and returns its * list of all issue labels in {@code application/json}. Each label has four fields: {@link * IssueLabel#id}, {@link IssueLabel#category}, {@link IssueLabel#color}, and {@link * IssueLabel#name}. * * <p>Returns 406 Not Acceptable if the client cannot accept {@code application/json}. Success * response can only be returned when the content type of the body is {@code application/json}. * * @param ownerName * @param projectName * @return the response to the request for issue labels */ private static Result labelsAsJSON(String ownerName, String projectName) { if (!request().accepts("application/json")) { return status(Http.Status.NOT_ACCEPTABLE); } Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<Map<String, String>> labels = new ArrayList<>(); for (IssueLabel label : IssueLabel.findByProject(project)) { Map<String, String> labelPropertyMap = new HashMap<>(); labelPropertyMap.put("id", "" + label.id); labelPropertyMap.put("category", label.category.name); labelPropertyMap.put("categoryId", "" + label.category.id); labelPropertyMap.put("color", label.color); labelPropertyMap.put("name", label.name); labels.add(labelPropertyMap); } response().setHeader("Content-Type", "application/json"); return ok(toJson(labels)); }