class BuildCapabilityProcess {

    public String[] updateStatusAndReturnBuildsToUpdate(Project project) throws ProjectNotFoundException,
            BuildNotFoundException {
        try {
            String lastBuildId = project.getBuildConnection().getLastBuildId(project.getBuildProjectId());
            boolean lastBuilding = project.getBuildConnection().isBuilding(project.getBuildProjectId(), lastBuildId);

            String previousLastBuildId = project.getLastBuildId();
            boolean previousBuilding = false;
            try {
                previousBuilding = project.getLastBuild().isBuilding();
            } catch (BuildNotFoundException e) {
                LOG.debug("No lastBuild found to say the project was building before refresh " + project);
            }

            Build lastBuild = project.findCreatedBuild(lastBuildId);

            try {
                if (previousBuilding == false && lastBuilding == false && !lastBuildId.equals(previousLastBuildId)) {
                    LOG.info("there is an already finished new build {}  {}", lastBuildId, project);
                    return new String[] { lastBuildId };
                }
                if (previousBuilding == false && lastBuilding == true) {
                    LOG.info("Build {} is now running {}", lastBuild.getBuildId(), project);
                    Runnable finishTimeRunner = getEstimatedFinishTimeRunner(project, lastBuild);
                    scheduler.schedule(finishTimeRunner, new Date());
                }
                if (previousBuilding == true && lastBuilding == true) {
                    if (!lastBuildId.equals(previousLastBuildId)) {
                        LOG.info("Previous build {} is over and a new build {} is already running {}", new Object[] {
                                previousLastBuildId, lastBuildId, project });
                        project.getBuilds().get(previousLastBuildId).setEstimatedFinishTime(null);
                        project.getBuilds().get(previousLastBuildId).setBuilding(false);
                        Runnable finishTimeRunner = getEstimatedFinishTimeRunner(project, lastBuild);
                        scheduler.schedule(finishTimeRunner, new Date());
                        return new String[] { previousLastBuildId };
                    }
                    // building is still running
                }

                if (previousBuilding == true && lastBuilding == false) {
                    // build is over
                    project.getBuilds().get(previousLastBuildId).setEstimatedFinishTime(null);
                    project.getBuilds().get(previousLastBuildId).setBuilding(false);
                    if (!lastBuildId.equals(previousLastBuildId)) {
                        LOG.info("previous build {} is over and a new build {} is also over {}", new Object[] {
                                previousLastBuildId, lastBuildId, project });
                        return new String[] { previousLastBuildId, lastBuildId };
                    }
                    LOG.info("Previous build {} is over and no new build ", previousLastBuildId, project);
                    return new String[] { previousLastBuildId };
                }
            } finally {
                lastBuild.setBuilding(lastBuilding);
                project.setLastBuildId(lastBuildId);
            }
        } catch (BuildIdNotFoundException e) {
            LOG.debug("No last build id found to update project " + project);
        }
        return new String[] {};
    }

}