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 {
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())) {
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;
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());
}
}
}
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;
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);
}
}
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 {
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;
* @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;
* @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();