class TeamCityConnection implements BuildCapability, TestCapability, ViewCapability {

    @Override
    public BuildState getBuildState(SoftwareProjectId softwareProjectId, String buildId)
            throws ProjectNotFoundException, BuildNotFoundException {
        checkConnected();
        checkSoftwareProjectId(softwareProjectId);
        checkBuildId(buildId);
        try {
            String projectId = softwareProjectId.getProjectId();
            TeamCityBuild build = teamCity.findBuild(projectId, buildId);
            String status = build.getStatus();
            return States.asVisuwallState(status);
        } catch (TeamCityBuildTypeNotFoundException e) {
            throw new ProjectNotFoundException("Cannot find build type for software project id:" + softwareProjectId, e);
        } catch (TeamCityBuildNotFoundException e) {
            try {
                TeamCityBuild runningBuild = teamCity.findRunningBuild();
                if (buildId.equals(runningBuild.getId())) {
                    return BuildState.UNKNOWN;
                }
            } catch (TeamCityBuildNotFoundException e1) {
            }
            throw new BuildNotFoundException("Cannot find build #" + buildId + " for software project id:"
                    + softwareProjectId, e);
        }
    }

}