class JobRequestHelper {

    public JobResult updateSpace(final String jobId,
                                 final String spaceName,
                                 final String spaceDescription,
                                 final String spaceOwner,
                                 final String defaultGroupId) {
        JobResult result = new JobResult();
        result.setJobId(jobId);

        if (spaceName == null) {
            result.setStatus(JobStatus.BAD_REQUEST);
            result.setResult("Space name must be provided");
            return result;
        }

        OrganizationalUnit organizationalUnit = organizationalUnitService.getOrganizationalUnit(spaceName);
        if (organizationalUnit == null) {
            result.setStatus(JobStatus.BAD_REQUEST);
            result.setResult("Space with name " + spaceName + " doesn't exists");
            return result;
        }

        String groupId = null;
        if (defaultGroupId == null || defaultGroupId.trim().isEmpty()) {
            groupId = organizationalUnit.getDefaultGroupId();
        } else {
            if (!organizationalUnitService.isValidGroupId(defaultGroupId)) {
                result.setStatus(JobStatus.BAD_REQUEST);
                result.setResult("Invalid default group id, only alphanumerical characters are admitted, " +
                                         "as well as '\"_\"', '\"-\"' or '\".\"'.");
                return result;
            } else {
                groupId = defaultGroupId;
            }
        }

        Collection<Contributor> contributors;

        if (spaceOwner == null) {
            contributors = organizationalUnit.getContributors();
        } else {
            contributors = Collections.singletonList(new Contributor(spaceOwner, ContributorType.OWNER));
        }

        organizationalUnit = organizationalUnitService.updateOrganizationalUnit(spaceName, groupId, contributors, spaceDescription);

        if (organizationalUnit != null) {
            result.setResult("Space " + organizationalUnit.getName() + " is updated successfully.");
            result.setStatus(JobStatus.SUCCESS);
        } else {
            result.setStatus(JobStatus.FAIL);
        }
        return result;
    }

}