]> source.dussan.org Git - sonarqube.git/commitdiff
Improve WsUtils checkFound methods
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 6 Oct 2015 06:29:17 +0000 (08:29 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 6 Oct 2015 06:29:17 +0000 (08:29 +0200)
server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataLoader.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/RemoveUserAction.java
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/UserGroupFinder.java
server/sonar-server/src/main/java/org/sonar/server/ws/WsUtils.java

index 5b990a5f06da717d8b8abad0c3e48ce5394d9292..d6e2236665d008753a663fc9f9ddb66ff3dbf7fc 100644 (file)
@@ -43,8 +43,7 @@ import org.sonar.server.user.UserSession;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Maps.newHashMap;
-import static java.lang.String.format;
-import static org.sonar.server.ws.WsUtils.checkFound;
+import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
 
 @ServerSide
 public class ProjectDataLoader {
@@ -64,8 +63,8 @@ public class ProjectDataLoader {
     DbSession session = dbClient.openSession(false);
     try {
       ProjectRepositories data = new ProjectRepositories();
-      ComponentDto module = checkFound(dbClient.componentDao().selectByKey(session, query.getModuleKey()),
-        format("Project or module with key '%s' is not found", query.getModuleKey()));
+      ComponentDto module = checkFoundWithOptional(dbClient.componentDao().selectByKey(session, query.getModuleKey()),
+        "Project or module with key '%s' is not found", query.getModuleKey());
 
       // Scan permission is enough to analyze all projects but preview permission is limited to projects user can access
       if (query.isIssuesMode() && !userSession.hasProjectPermissionByUuid(UserRole.USER, module.projectUuid())) {
index 187ba67d49a6e1b3e586848273519eb4edf26f0f..1bd97f2dad5c882950010a645af5f7b88ed78374 100644 (file)
@@ -33,7 +33,6 @@ import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.usergroups.ws.UserGroupFinder;
 import org.sonar.server.usergroups.ws.WsGroupRef;
 
-import static java.lang.String.format;
 import static org.sonar.api.security.DefaultGroups.ANYONE;
 import static org.sonar.api.security.DefaultGroups.isAnyone;
 import static org.sonar.server.ws.WsUtils.checkFound;
@@ -88,16 +87,18 @@ public class PermissionDependenciesFinder {
 
   public UserDto getUser(DbSession dbSession, String userLogin) {
     return checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, userLogin),
-      format("User with login '%s' is not found'", userLogin));
+      "User with login '%s' is not found'", userLogin);
   }
 
   public PermissionTemplateDto getTemplate(DbSession dbSession, WsTemplateRef template) {
     if (template.uuid() != null) {
-      return checkFound(dbClient.permissionTemplateDao().selectByUuid(dbSession, template.uuid()),
-        format("Permission template with id '%s' is not found", template.uuid()));
+      return checkFound(
+        dbClient.permissionTemplateDao().selectByUuid(dbSession, template.uuid()),
+        "Permission template with id '%s' is not found", template.uuid());
     } else {
-      return checkFound(dbClient.permissionTemplateDao().selectByName(dbSession, template.name()),
-        format("Permission template with name '%s' is not found (case insensitive)", template.name()));
+      return checkFound(
+        dbClient.permissionTemplateDao().selectByName(dbSession, template.name()),
+        "Permission template with name '%s' is not found (case insensitive)", template.name());
     }
   }
 }
index 07e36fa6c5dae58dc47029e609d9575a65e584e4..401135556a31bd26085a92f8225d969a1919bca2 100644 (file)
@@ -106,12 +106,12 @@ public class RemoveUserAction implements UserGroupsWsAction {
 
     if (groupId != null) {
       groupDto = checkFound(dbClient.groupDao().selectById(dbSession, groupId),
-        format("Group with id '%d' is not found", groupId));
+        "Group with id '%d' is not found", groupId);
     }
 
     if (groupName != null) {
       groupDto = checkFound(dbClient.groupDao().selectByName(dbSession, groupName),
-        format("Group with name '%s' is not found", groupName));
+        "Group with name '%s' is not found", groupName);
     }
 
     return groupDto;
@@ -119,6 +119,6 @@ public class RemoveUserAction implements UserGroupsWsAction {
 
   private UserDto getUser(DbSession dbSession, String userLogin) {
     return checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, userLogin),
-      format("User with login '%s' is not found'", userLogin));
+      "User with login '%s' is not found'", userLogin);
   }
 }
index 470aa8aeee7e66590350b04ffe79e0b268a21e64..fc082cc7aa402b8364d4ba8156ed3277e86ff350 100644 (file)
@@ -24,7 +24,6 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.user.GroupDto;
 
-import static java.lang.String.format;
 import static org.sonar.server.ws.WsUtils.checkFound;
 
 public class UserGroupFinder {
@@ -42,12 +41,12 @@ public class UserGroupFinder {
 
     if (groupId != null) {
       groupDto = checkFound(dbClient.groupDao().selectById(dbSession, groupId),
-        format("Group with id '%d' is not found", groupId));
+        "Group with id '%d' is not found", groupId);
     }
 
     if (groupName != null) {
       groupDto = checkFound(dbClient.groupDao().selectByName(dbSession, groupName),
-        format("Group with name '%s' is not found", groupName));
+        "Group with name '%s' is not found", groupName);
     }
 
     return groupDto;
index 2e010e6e85b5fc4e5999acd4a590db2a8e3cd3eb..3aefb651505117986a7a861dd6c7634280ba29f7 100644 (file)
@@ -70,9 +70,9 @@ public class WsUtils {
    * @throws NotFoundException if the value if null
    * @return the value
    */
-  public static <T> T checkFound(T value, String message) {
+  public static <T> T checkFound(T value, String message, Object... messageArguments) {
     if (value == null) {
-      throw new NotFoundException(message);
+      throw new NotFoundException(format(message, messageArguments));
     }
 
     return value;
@@ -82,9 +82,9 @@ public class WsUtils {
    * @throws NotFoundException if the value is not present
    * @return the value
    */
-  public static <T> T checkFound(Optional<T> value, String message) {
+  public static <T> T checkFoundWithOptional(Optional<T> value, String message, Object... messageArguments) {
     if (!value.isPresent()) {
-      throw new NotFoundException(message);
+      throw new NotFoundException(format(message, messageArguments));
     }
 
     return value.get();