<commit_before>package org.motechproject.server.omod.web.controller;

import org.motechproject.server.model.Community;
import org.motechproject.server.model.Facility;
import org.motechproject.server.omod.ContextService;
import org.motechproject.server.omod.web.model.WebCommunity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import java.util.List;

@Controller
public class CommunityController {

    @Autowired
    ContextService contextService;
    
    @RequestMapping(value = "/module/motechmodule/community", method = RequestMethod.GET)
    public String list(ModelMap modelMap){
        return "/module/motechmodule/communities";
    }

    @RequestMapping(value = "/module/motechmodule/community/add.form", method = RequestMethod.GET)
    public String viewAddForm(ModelMap modelMap){
        modelMap.addAttribute("community", new WebCommunity());
        return "/module/motechmodule/addcommunity";
    }

    @RequestMapping(value = "/module/motechmodule/community/submit.form", method = RequestMethod.POST)
    public String submitForm(WebCommunity webCommunity, Errors errors, ModelMap modelMap, SessionStatus status){
        if(webCommunity.getName().equals("")){
            errors.reject("name", "Name cannot be blank");
        }

        if(!errors.hasErrors()){
            Community community;
            if(webCommunity.getCommunityId() == null){
                community = new Community();
                community.setName(webCommunity.getName());
                community.setFacility(contextService.getMotechService().getFacilityById(webCommunity.getFacilityId()));
            }else{
                community = contextService.getMotechService().getCommunityById(webCommunity.getCommunityId());
                community.setFacility(contextService.getMotechService().getFacilityById(webCommunity.getFacilityId()));
                community.setName(webCommunity.getName());
            }
            contextService.getRegistrarBean().saveCommunity(community);
            modelMap.addAttribute("successMsg", "Community added");
            status.setComplete();
            return "/module/motechmodule/communities";
        }
        return "/module/motechmodule/addcommunity";
    }

    @RequestMapping(value = "/module/motechmodule/community/editcommunity.form", method = RequestMethod.GET)
    public String viewEditForm(@RequestParam(required = true) Integer communityId, ModelMap modelMap){
        Community community = contextService.getMotechService().getCommunityById(communityId);
        modelMap.addAttribute("community", new WebCommunity(community));
        return "/module/motechmodule/editcommunity";<commit_msg>#764, #763 - Editing communities, not letting community names with blank space<commit_after>package org.motechproject.server.omod.web.controller;

import org.motechproject.server.model.Community;
import org.motechproject.server.model.Facility;
import org.motechproject.server.omod.ContextService;
import org.motechproject.server.omod.web.model.WebCommunity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import java.util.List;

@Controller
public class CommunityController {

    @Autowired
    ContextService contextService;
    
    @RequestMapping(value = "/module/motechmodule/community", method = RequestMethod.GET)
    public String list(ModelMap modelMap){
        return "/module/motechmodule/communities";
    }

    @RequestMapping(value = "/module/motechmodule/community/add.form", method = RequestMethod.GET)
    public String viewAddForm(ModelMap modelMap){
        modelMap.addAttribute("community", new WebCommunity());
        return "/module/motechmodule/addcommunity";
    }

    @RequestMapping(value = "/module/motechmodule/community/submit.form", method = RequestMethod.POST)
    public String submitForm(WebCommunity webCommunity, Errors errors, ModelMap modelMap, SessionStatus status){
        if(webCommunity.getName().trim().isEmpty()){
            errors.reject("name", "Name cannot be blank");
        }

        if(!errors.hasErrors()){
            Community community;
            if(webCommunity.getCommunityId() == null){
                community = new Community();
                community.setName(webCommunity.getName().trim());
                community.setFacility(contextService.getMotechService().getFacilityById(webCommunity.getFacilityId()));
            }else{
                community = contextService.getMotechService().getCommunityById(webCommunity.getCommunityId());
                community.setFacility(contextService.getMotechService().getFacilityById(webCommunity.getFacilityId()));
                community.setName(webCommunity.getName().trim());
            }
            contextService.getRegistrarBean().saveCommunity(community);
            modelMap.addAttribute("successMsg", "Community added");
            status.setComplete();
            return "/module/motechmodule/communities";
        }
        modelMap.addAttribute("community", webCommunity);
        return "/module/motechmodule/addcommunity";
    }

    @RequestMapping(value = "/module/motechmodule/community/editcommunity.form", method = RequestMethod.GET)
    public String viewEditForm(@RequestParam(required = true) Integer communityId, ModelMap modelMap){
        Community community = contextService.getMotechService().getCommunityById(communityId);
        modelMap.addAttribute("community", new WebCommunity(community));
        return "/module/motechmodule/editcommunity";
{'fraction_non_alphanumeric': 0.10787537184906842, 'fraction_numerical': 0.0009394081728511038, 'mean_word_length': 3.971206225680934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2149761', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1577, 'n_words': 261}