<commit_before>package no.kantega.publishing.modules.forms.control;

import no.kantega.publishing.controls.AksessController;
import no.kantega.publishing.modules.forms.util.FormSubmissionBuilder;
import no.kantega.publishing.modules.forms.util.FilledFormBuilder;
import no.kantega.publishing.modules.forms.model.FormSubmission;
import no.kantega.publishing.modules.forms.model.AksessContentForm;
import no.kantega.publishing.modules.forms.formdelivery.FormDeliveryService;
import no.kantega.publishing.common.data.Content;
import no.kantega.publishing.security.SecuritySession;
import no.kantega.publishing.security.data.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import no.kantega.publishing.modules.forms.model.Form;
import no.kantega.publishing.modules.forms.validate.FormError;

/**
 *
 */
public class SaveFormSubmissionController implements AksessController {

    private String description;
    private FormSubmissionBuilder formSubmissionBuilder;
    private FilledFormBuilder filledFormBuilder;
    private List<FormDeliveryService> formDeliveryServices;

    public Map handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();

        Content content = (Content) request.getAttribute("aksess_this");
        Form form = null;

        if (content != null) {
            form = new AksessContentForm(content);
        }

        if (form != null) {
            if (request.getMethod().equalsIgnoreCase("POST") && request.getParameter("isAksessFormSubmit") != null) {
                Map<String, String[]> values = new HashMap<String, String[]>(request.getParameterMap());
                addValues(values, request);

                FormSubmission formSubmission = formSubmissionBuilder.buildFormSubmission(values, form);

                // Validate formsubmission
                List<FormError> errors = formSubmission.getErrors();
                if (errors != null && errors.size() > 0) {
                    // errrors
                    model.put("hasErrors", Boolean.TRUE);
                    model.put("formSubmission", formSubmission);
                    form = filledFormBuilder.buildFilledForm(formSubmission);
                } else {

                    addUserInformation(formSubmission, request);

                    for (FormDeliveryService service : formDeliveryServices) {
                        service.deliverForm(formSubmission);
                    }
                    model.put("formSubmission", formSubmission);
                    model.put("hasSubmitted", Boolean.TRUE);
                    model.put("hasErrors",Boolean.FALSE);

                }
            } else {
                // Form is entered for the first time
                Map<String, String[]> prefillValues = new HashMap<String, String[]>();
                prefill(prefillValues, request);
                if (prefillValues.size() > 0) {
                    FormSubmission formSubmission = formSubmissionBuilder.buildFormSubmission(prefillValues, form);
                    if (formSubmission != null) {
                        form = filledFormBuilder.buildFilledForm(formSubmission);
                    }
                }
            }

        }

        model.put("form", form);

        return model;
    }

    public String getDescription() {
        return description;
    }<commit_msg>Avoid NPE when prefill values are provided to SaveFormSubmissionController, but none of them matches any of the fields actually in the form.<commit_after>package no.kantega.publishing.modules.forms.control;

import no.kantega.publishing.controls.AksessController;
import no.kantega.publishing.modules.forms.util.FormSubmissionBuilder;
import no.kantega.publishing.modules.forms.util.FilledFormBuilder;
import no.kantega.publishing.modules.forms.model.FormSubmission;
import no.kantega.publishing.modules.forms.model.AksessContentForm;
import no.kantega.publishing.modules.forms.formdelivery.FormDeliveryService;
import no.kantega.publishing.common.data.Content;
import no.kantega.publishing.security.SecuritySession;
import no.kantega.publishing.security.data.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import no.kantega.publishing.modules.forms.model.Form;
import no.kantega.publishing.modules.forms.validate.FormError;

/**
 *
 */
public class SaveFormSubmissionController implements AksessController {

    private String description;
    private FormSubmissionBuilder formSubmissionBuilder;
    private FilledFormBuilder filledFormBuilder;
    private List<FormDeliveryService> formDeliveryServices;

    public Map handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();

        Content content = (Content) request.getAttribute("aksess_this");
        Form form = null;

        if (content != null) {
            form = new AksessContentForm(content);
        }

        if (form != null) {
            if (request.getMethod().equalsIgnoreCase("POST") && request.getParameter("isAksessFormSubmit") != null) {
                Map<String, String[]> values = new HashMap<String, String[]>(request.getParameterMap());
                addValues(values, request);

                FormSubmission formSubmission = formSubmissionBuilder.buildFormSubmission(values, form);

                // Validate formsubmission
                List<FormError> errors = formSubmission.getErrors();
                if (errors != null && errors.size() > 0) {
                    // errrors
                    model.put("hasErrors", Boolean.TRUE);
                    model.put("formSubmission", formSubmission);
                    form = filledFormBuilder.buildFilledForm(formSubmission);
                } else {

                    addUserInformation(formSubmission, request);

                    for (FormDeliveryService service : formDeliveryServices) {
                        service.deliverForm(formSubmission);
                    }
                    model.put("formSubmission", formSubmission);
                    model.put("hasSubmitted", Boolean.TRUE);
                    model.put("hasErrors",Boolean.FALSE);

                }
            } else {
                // Form is entered for the first time
                Map<String, String[]> prefillValues = new HashMap<String, String[]>();
                prefill(prefillValues, request);
                if (prefillValues.size() > 0) {
                    FormSubmission formSubmission = formSubmissionBuilder.buildFormSubmission(prefillValues, form);
                    if (formSubmission.getValues() != null) {
                        form = filledFormBuilder.buildFilledForm(formSubmission);
                    }
                }
            }

        }

        model.put("form", form);

        return model;
    }

    public String getDescription() {
        return description;
    }
{'fraction_non_alphanumeric': 0.09513917739925218, 'fraction_numerical': 0.000553939897521119, 'mean_word_length': 2.516066212268744, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '836002', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1800, 'n_words': 349}