Robert Massaioli avatar Robert Massaioli committed 7a5d6ad

I18n'd the REST resource.

Comments (0)

Files changed (3)

src/main/java/com/atlassian/jira/extension/rest/PingMeRestResource.java

 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import com.atlassian.crowd.embedded.api.User;
+import com.atlassian.jira.extension.rest.entities.*;
+import com.atlassian.sal.api.message.I18nResolver;
 import org.joda.time.DateTime;
 import org.joda.time.format.DateTimeFormat;
 import org.joda.time.format.ISODateTimeFormat;
 
 import com.atlassian.jira.extension.PingService;
 import com.atlassian.jira.extension.persistence.PingEntity;
-import com.atlassian.jira.extension.rest.entities.IssueResult;
-import com.atlassian.jira.extension.rest.entities.PingAdd;
-import com.atlassian.jira.extension.rest.entities.PingDelete;
-import com.atlassian.jira.extension.rest.entities.SearchResult;
 import com.atlassian.jira.issue.IssueManager;
 import com.atlassian.jira.issue.MutableIssue;
 import com.atlassian.jira.security.JiraAuthenticationContext;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 
+import java.util.List;
+
 /**
  * A resource of message.
  */
     private final PingService pingService;
     private final JiraAuthenticationContext jiraAuthenticationContext;
     private final IssueManager issueManager;
+    private final I18nResolver i18nResolver;
 
-    public PingMeRestResource(PingService pingService, JiraAuthenticationContext jiraAuthenticationContext, IssueManager issueManager) {
+    public PingMeRestResource(PingService pingService, JiraAuthenticationContext jiraAuthenticationContext, IssueManager issueManager, I18nResolver i18nResolver) {
         this.pingService = pingService;
         this.jiraAuthenticationContext = jiraAuthenticationContext;
         this.issueManager = issueManager;
+        this.i18nResolver = i18nResolver;
     }
 
-    // Fri Nov 30 2012 00:00:00 GMT+1100 (EST)
-    private static final String DATE_FORMAT = "EEE MMM d yyyy HH:mm:ss z";
-
     @POST
     public Response addPing(PingAdd pingAdd)
     {
         final DateTime dateTime = ISODateTimeFormat.dateTimeParser().parseDateTime(pingAdd.getPingDate());
-        final boolean pingSucceeded = pingService.addPing(jiraAuthenticationContext.getLoggedInUser(), pingAdd.getIssueId(), dateTime.toDate());
+        final User loggedInUser = jiraAuthenticationContext.getLoggedInUser();
+        final boolean pingSucceeded = pingService.addPing(loggedInUser, pingAdd.getIssueId(), dateTime.toDate());
         if(pingSucceeded)
         {
             return Response.ok().build();
         }
         else
         {
-            // TODO this error message sucks
-            return Response.serverError().build();
+            final ErrorResult errorResult = new ErrorResult(i18nResolver.getText("pingme.rest.add.error", loggedInUser.getDisplayName()));
+            return Response.serverError().entity(errorResult).build();
         }
     }
 
         if(pingRemoved) {
             return Response.ok().build();
         }
-        return Response.serverError().build();
+        return Response.serverError().entity(new ErrorResult(i18nResolver.getText("pingme.rest.remove.error", pingDelete.getPingId()))).build();
     }
 
     @GET
     @Path("/search")
     public Response findMyPings()
     {
-        final Iterable<SearchResult> searchResults = Iterables.filter(Lists.transform(pingService.getPingsForUser(jiraAuthenticationContext.getLoggedInUser()), new Function<PingEntity, SearchResult>() {
+        return Response.ok(convertPingsToSearchResults(pingService.getPingsForUser(jiraAuthenticationContext.getLoggedInUser()))).build();
+    }
+
+    @GET
+    @Path("/search/issue/{issueid}")
+    public Response findMyPingsByIssue(@PathParam("issueid") int issueId)
+    {
+        return Response.ok(convertPingsToSearchResults(pingService.getPingsForUserByIssue(jiraAuthenticationContext.getLoggedInUser(), issueId))).build();
+    }
+
+    @GET
+    @Path("/search/project/{projectkey}")
+    public Response findMyPingsByProject(@PathParam("projectkey") String projectKey)
+    {
+        // TODO not implemented yet
+        return Response.serverError().entity(new ErrorResult(i18nResolver.getText("pingme.generic.not-implemented"))).build();
+    }
+
+    private Iterable<SearchResult> convertPingsToSearchResults(List<PingEntity> pings) {
+        return Iterables.filter(Lists.transform(pings, new Function<PingEntity, SearchResult>() {
             @Override
             public SearchResult apply(PingEntity pingEntity) {
                 final MutableIssue currentIssue = issueManager.getIssueObject(Long.valueOf(pingEntity.getIssueId()));
                     final String pingDate = new DateTime(pingEntity.getPingDate()).toString(DateTimeFormat.fullDate());
                     return new SearchResult(pingEntity.getID(), issue, pingDate);
                 } else {
-                	// TODO you should warn and then just delete the ping entry here. Clearly they are no longer required.
-                	// This is valid because the issue could have been deleted in the meantime.
+                    // TODO you should warn and then just delete the ping entry here. Clearly they are no longer required.
+                    // This is valid because the issue could have been deleted in the meantime.
                     return null;
                 }
             }
                 return searchResult != null;
             }
         });
-
-        return Response.ok(searchResults).build();
-    }
-
-    @GET
-    @Path("/search/issue/{issueid}")
-    public Response findMyPingsByIssue(@PathParam("issueid") int issueId)
-    {
-        return Response.ok(pingService.getPingsForUserByIssue(jiraAuthenticationContext.getLoggedInUser(), issueId)).build();
-    }
-
-    @GET
-    @Path("/search/project/{projectkey}")
-    public Response findMyPingsByProject(@PathParam("projectkey") String projectKey)
-    {
-        // TODO not implemented yet
-        return Response.serverError().build();
     }
 
     @DELETE
     @Path("/all")
     public Response clearMyPings() {
-        final boolean clearedAll = pingService.clearPingsForUser(jiraAuthenticationContext.getLoggedInUser());
+        final User loggedInUser = jiraAuthenticationContext.getLoggedInUser();
+        final boolean clearedAll = pingService.clearPingsForUser(loggedInUser);
 
         if(clearedAll) {
             return Response.ok().build();
         }
 
-        // TODO what server error?
-        return Response.serverError().build();
+        return Response.serverError().entity(new ErrorResult(i18nResolver.getText("pingme.rest.clear-user-all.error", loggedInUser.getDisplayName()))).build();
     }
 }

src/main/java/com/atlassian/jira/extension/rest/entities/ErrorResult.java

+package com.atlassian.jira.extension.rest.entities;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "error")
+public class ErrorResult {
+    @XmlElement(name = "message")
+    private String message;
+
+    public ErrorResult(String message) {
+        this.message = message;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+}

src/main/resources/com/atlassian/jira/extension/pingme.properties

+# Ping Me Plugin Generic
+pingme.generic.not-implemented=This feature is not implemented yet.
+
+# Ping Me Plugin Actions
 ping-me-action.label=Ping Me
 ping-me-action.name=Ping Me
 ping-me-action.description=The PingMe Button
 
 # Scheduler
 ping-scheduler.name=Ping Notification Scheduler
-ping-scheduler.description=This service will send Ping's to users that have them scheduled.
+ping-scheduler.description=This service will send Ping's to users that have them scheduled.
+
+# REST Error Messages
+pingme.rest.add.error=We were not able to add a ping for user: {0}
+pingme.rest.remove.error=We were not able to delete a ping with ID {0} it may have already been sent or deleted.
+pingme.rest.clear-user-all.error=Unable to clear the pings for user: {0}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.