Skip to content

Commit

Permalink
internal: error handling in rest
Browse files Browse the repository at this point in the history
  • Loading branch information
grundic committed Jun 10, 2012
1 parent b0bca0b commit 7c4859f
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 59 deletions.
Expand Up @@ -10,6 +10,10 @@
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.parser.JqlParseException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

/**
* Manager for disposition custom field
Expand All @@ -19,21 +23,24 @@ public interface DispositionManager {

/**
* Reset disposition for all issues in configured Jql query for selected user
*
* @param currentUser - user, for whom reindex will be executed
* @param step - value between each issue
* @param errors - container for errors
* @throws JqlParseException
* @throws SearchException
*/
public void resetDisposition(User currentUser, Double step) throws JqlParseException, SearchException;
public void resetDisposition(@NotNull User currentUser, @NotNull Double step, @NotNull Collection<String> errors) throws JqlParseException, SearchException;


/**
* Set order for issue
*
* @param issue - issue to be ordered
* @param value - value for order
* @param errors - container for errors
*/
public void setDisposition(Issue issue, Double value) throws JqlParseException, SearchException;
public void setDisposition(@NotNull Issue issue, @NotNull Double value, @NotNull Collection<String> errors) throws JqlParseException, SearchException;


/**
Expand All @@ -42,6 +49,7 @@ public interface DispositionManager {
* @param above - issue above current (should have higher order)
* @param dragged - current issue
* @param below - issue below current (should have lower order)
* @param errors - container for errors
*/
public void setDisposition(Issue above, Issue dragged, Issue below) throws SearchException, JqlParseException;
public void setDisposition(@Nullable Issue above, @NotNull Issue dragged, @Nullable Issue below, @NotNull Collection<String> errors) throws SearchException, JqlParseException;
}
Expand Up @@ -65,15 +65,15 @@ public DispositionManagerImpl(@NotNull JqlQueryParser jqlQueryParser, @NotNull S
}

@Override
public void resetDisposition(User userToBeReset, Double step) throws JqlParseException, SearchException {
public void resetDisposition(@NotNull User userToBeReset, @NotNull Double step, @NotNull Collection<String> errors) throws JqlParseException, SearchException {

User user = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();

String jql = replaceCurrentUser(JQL_QUERY, userToBeReset.getName());

CustomField field = getCustomFieldByIssueAndType(IssueDispositionCF.class, null);
if (null == field) {
log.error("Can't find custom field object - is should be configured first!");
errors.add("Can't find custom field object - is should be configured first!");
return;
}

Expand All @@ -82,6 +82,7 @@ public void resetDisposition(User userToBeReset, Double step) throws JqlParseExc
SearchResults searchResults = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter());

if (null == searchResults) {
errors.add("Failed to search for issues - searchResults are null!");
return;
}

Expand All @@ -95,26 +96,26 @@ public void resetDisposition(User userToBeReset, Double step) throws JqlParseExc
}

@Override
public void setDisposition(Issue issue, Double value) throws JqlParseException, SearchException {
public void setDisposition(@NotNull Issue issue, @NotNull Double value, @NotNull Collection<String> errors) throws JqlParseException, SearchException {
User user = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();

CustomField field = getCustomFieldByIssueAndType(IssueDispositionCF.class, issue);
if (null == field) {
log.error(String.format("Can't find custom field object for issue %s!", issue.getKey()));
errors.add(String.format("Can't find custom field object for issue %s!", issue.getKey()));
return;
}

Double prevValue = (Double) issue.getCustomFieldValue(field);
// don't waste time for setting same value
if (null != prevValue && prevValue.equals(value)) {
log.warn("Values are equal!");
errors.add("Old and new disposition values are equal!");
return;
}


// if issue in not in configured Jql - return
if (!isIssueInJQL(JQL_QUERY, issue, user)) {
log.error(String.format("Issue %s in not in Jql '%s'!", issue.getKey(), JQL_QUERY));
errors.add(String.format("Issue %s in not in Jql '%s'!", issue.getKey(), JQL_QUERY));
return;
}

Expand All @@ -128,26 +129,26 @@ public void setDisposition(Issue issue, Double value) throws JqlParseException,
}

@Override
public void setDisposition(@Nullable Issue above, @NotNull Issue dragged, @Nullable Issue below) throws SearchException, JqlParseException {
public void setDisposition(@Nullable Issue above, @NotNull Issue dragged, @Nullable Issue below, @NotNull Collection<String> errors) throws SearchException, JqlParseException {
User user = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();

if (null == above && null == below) {
log.error("High and low issues can't be null at the same time!");
errors.add("High and low issues can't be null at the same time!");
return;
}

if (null != above && !isIssueInJQL(JQL_QUERY, above, user)) {
log.warn("High issue must belong to configured jql query!");
errors.add("High issue must belong to configured jql query!");
return;
}

if (null != below && !isIssueInJQL(JQL_QUERY, below, user)) {
log.warn("Low issue must belong to configured jql query!");
errors.add("Low issue must belong to configured jql query!");
return;
}

if (!isIssueInJQL(JQL_QUERY, dragged, user)) {
log.warn("Dragged issue must belong to configured jql query!");
errors.add("Dragged issue must belong to configured jql query!");
return;
}

Expand All @@ -156,7 +157,7 @@ public void setDisposition(@Nullable Issue above, @NotNull Issue dragged, @Nulla
@Nullable
CustomField field = getCustomFieldByIssueAndType(IssueDispositionCF.class, dragged);
if (null == field) {
log.error(String.format("Can't find custom field object for issue %s!", dragged.getKey()));
errors.add(String.format("Can't find custom field object for issue %s!", dragged.getKey()));
return;
}

Expand All @@ -166,7 +167,7 @@ public void setDisposition(@Nullable Issue above, @NotNull Issue dragged, @Nulla
Double belowValue = (below != null) ? (Double) below.getCustomFieldValue(field) : null;

if (null == aboveValue && null == belowValue) {
log.warn("Both above and below issues should have initialized disposition values!");
errors.add("Both High and Low issues should have initialized disposition values!");
return;
}

Expand Down
Expand Up @@ -5,8 +5,6 @@
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.parser.JqlParseException;
import com.atlassian.jira.jql.parser.JqlQueryParser;
import com.atlassian.query.Query;
import com.atlassian.query.clause.Clause;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.mail.jira.plugins.disposition.manager.DispositionManager;
Expand All @@ -17,8 +15,10 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;

import static javax.ws.rs.core.Response.serverError;

/**
* User: g.chernyshev
Expand Down Expand Up @@ -55,8 +55,10 @@ public Response setDisposition(@Nullable @QueryParam("issue") final String issue

Issue issueObject = issueManager.getIssueObject(issue);

Collection<String> errors = new ArrayList<String>();

try {
dispositionManager.setDisposition(issueObject, value);
dispositionManager.setDisposition(issueObject, value, errors);
} catch (JqlParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
Expand All @@ -65,6 +67,11 @@ public Response setDisposition(@Nullable @QueryParam("issue") final String issue
throw new RuntimeException(e);
}

if (errors.size() > 0) {

return serverError().build();
}

return null;
}

Expand All @@ -81,7 +88,7 @@ public Response setDisposition(@Nullable @QueryParam("high") final String high,


try {
dispositionManager.setDisposition(highIssue, draggedIssue, lowIssue);
dispositionManager.setDisposition(highIssue, draggedIssue, lowIssue, null);
} catch (SearchException e) {
e.printStackTrace();
throw new RuntimeException(e);
Expand All @@ -93,45 +100,27 @@ public Response setDisposition(@Nullable @QueryParam("high") final String high,
return null;
}

private Response badRequest(Collection<String> errors) {
return Response.status(Response.Status.BAD_REQUEST).
type(MediaType.APPLICATION_JSON).
entity(new ErrorListEntity(Response.Status.BAD_REQUEST, errors)).
build();
}

@Path("/check")
@GET
public Response parseJqlQuery(@Nullable @QueryParam("q") final String q) {
if (null == q) {
return null;
}

Query query;
try {
query = jqlQueryParser.parseQuery(q);
} catch (JqlParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}

Collection<String> fields = clauseRecursion(query.getWhereClause().getClauses());

System.out.println("\n\n" + "-------------------------------------------------");
for (String field : fields) {
System.out.println(field);
}

return null;
}


private Collection<String> clauseRecursion(Collection<Clause> clauses) {

Collection<String> fields = new LinkedHashSet<String>();

for (Clause clause : clauses) {
if (clause.getClauses() != null && clause.getClauses().size() > 0) {
fields.addAll(clauseRecursion(clause.getClauses()));
} else {
fields.add(clause.getName());
}
}
Collection<String> errors = new ArrayList<String>();
errors.add("Error-1");
errors.add("Error-2");
errors.add("Error-3");
errors.add("Error-4");

return fields;
return Response.status(Response.Status.BAD_REQUEST).
type(MediaType.APPLICATION_JSON).
entity(new ErrorListEntity(Response.Status.BAD_REQUEST, errors)).
build();
}

}
@@ -0,0 +1,51 @@
package ru.mail.jira.plugins.disposition.rest;

import javax.ws.rs.core.Response.Status;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Arrays;
import java.util.Collection;

/**
* @author g.chernyshev
*/

@XmlRootElement(name = "error")
public class ErrorListEntity {
@XmlAttribute
private int status;

@XmlElement(name = "message")
private Collection<String> errors;

public ErrorListEntity() {
}

public ErrorListEntity(int status, String... errors) {
this(status, Arrays.asList(errors));
}

public ErrorListEntity(int status, Collection<String> errors) {
this.status = status;
this.errors = errors;
}

public ErrorListEntity(Status status, String... errors) {
this(status, Arrays.asList(errors));
}

public ErrorListEntity(Status status, Collection<String> errors) {
this.status = status.getStatusCode();
this.errors = errors;
}

public Collection<String> getErrors() {
return errors;
}

public int getStatus() {
return status;
}
}

Expand Up @@ -52,7 +52,7 @@ protected String doExecute() {
User remoteUser = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();
User assigneeUser = userManager.getUser(assignee);
try {
dispositionManager.resetDisposition(assigneeUser, getStep());
dispositionManager.resetDisposition(assigneeUser, getStep(), null);
} catch (JqlParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
Expand Down
Expand Up @@ -17,7 +17,6 @@ AJS.toInit(function() {
helper: helper,

update: function(event, ui) {
//AJS.$(this).sortable('cancel');

var high = AJS.$(ui.item).prev().data('issuekey') || '';
var dragged = AJS.$(ui.item).data('issuekey');
Expand All @@ -28,10 +27,15 @@ AJS.toInit(function() {
JIRA.SmartAjax.makeRequest({
url: contextPath + "/rest/issue-disposition/1.0/disposition?" + params,
complete: function (xhr, textStatus, smartAjaxResult) {
console.log("Complete!");
JIRA.IssueNavigator.reload();
if (smartAjaxResult.successful){
JIRA.IssueNavigator.reload();
}
else {
AJS.$(this).sortable('cancel');
}
}
});

}
}).disableSelection();
});

0 comments on commit 7c4859f

Please sign in to comment.