<commit_before>package jpaoletti.jpm2.controller;

import jpaoletti.jpm2.core.PMException;
import jpaoletti.jpm2.core.model.Entity;
import jpaoletti.jpm2.core.model.EntityInstance;
import jpaoletti.jpm2.core.model.Operation;
import jpaoletti.jpm2.core.model.ValidationException;
import jpaoletti.jpm2.core.service.JPMService;
import jpaoletti.jpm2.util.JPMUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author jpaoletti
 */
@Controller
public class AddController extends BaseController {

    @Autowired
    private JPMService service;

    /**
     * GET method prepares form.
     *
     * @param entity
     * @return model and view
     * @throws PMException
     */
    @RequestMapping(value = "/jpm/{entity}/add", method = RequestMethod.GET)
    public ModelAndView addPrepare(@PathVariable Entity entity) throws PMException {
        getContext().setEntity(entity);
        getContext().setOperation(entity.getOperation("add"));
        getContext().setObject(JPMUtils.newInstance(entity.getClazz()));
        getContext().setEntityInstance(newEntityInstance(null, entity));
        return new ModelAndView("jpm-edit");
    }

    /**
     * GET method prepares form.
     *
     * @param entity
     * @return model and view
     * @throws PMException
     */
    @RequestMapping(value = "/jpm/{owner}/{ownerId}/{entity}/add", method = RequestMethod.GET)
    public ModelAndView addWeakPrepare(@PathVariable Entity entity, @PathVariable String ownerId) throws PMException {
        final ModelAndView mav = addPrepare(entity);
        if (entity.isWeak()) {
            mav.addObject("ownerId", ownerId);
        }
        return mav;
    }

    /**
     * POST method finalizes the operation
     *
     * @param entity
     * @return redirect to show
     * @throws PMException
     */
    @RequestMapping(value = "/jpm/{entity}/add", method = RequestMethod.POST)
    public ModelAndView addCommit(@PathVariable Entity entity) throws PMException {
        final Operation operation = entity.getOperation("add");
        getContext().setEntity(entity);
        getContext().setOperation(operation);
        try {
            final String instanceId = getService().save(entity, operation,
                    new EntityInstance(null, entity, operation, null),
                    getRequest().getParameterMap());
            return new ModelAndView("redirect:/jpm/" + entity.getId() + "/" + instanceId + "/show");
        } catch (ValidationException e) {
            if (e.getMsg() != null) {
                getContext().getEntityMessages().add(e.getMsg());
            }
            return addPrepare(entity);
        }
    }

    /**<commit_msg>setting entity instance owner id<commit_after>package jpaoletti.jpm2.controller;

import jpaoletti.jpm2.core.PMException;
import jpaoletti.jpm2.core.model.Entity;
import jpaoletti.jpm2.core.model.EntityInstance;
import jpaoletti.jpm2.core.model.Operation;
import jpaoletti.jpm2.core.model.ValidationException;
import jpaoletti.jpm2.core.service.JPMService;
import jpaoletti.jpm2.util.JPMUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author jpaoletti
 */
@Controller
public class AddController extends BaseController {

    @Autowired
    private JPMService service;

    /**
     * GET method prepares form.
     *
     * @param entity
     * @return model and view
     * @throws PMException
     */
    @RequestMapping(value = "/jpm/{entity}/add", method = RequestMethod.GET)
    public ModelAndView addPrepare(@PathVariable Entity entity) throws PMException {
        getContext().setEntity(entity);
        getContext().setOperation(entity.getOperation("add"));
        getContext().setObject(JPMUtils.newInstance(entity.getClazz()));
        getContext().setEntityInstance(newEntityInstance(null, entity));
        return new ModelAndView("jpm-edit");
    }

    /**
     * GET method prepares form.
     *
     * @param entity
     * @return model and view
     * @throws PMException
     */
    @RequestMapping(value = "/jpm/{owner}/{ownerId}/{entity}/add", method = RequestMethod.GET)
    public ModelAndView addWeakPrepare(@PathVariable Entity entity, @PathVariable String ownerId) throws PMException {
        final ModelAndView mav = addPrepare(entity);
        if (entity.isWeak()) {
            getContext().getEntityInstance().setOwnerId(ownerId);
        }
        return mav;
    }

    /**
     * POST method finalizes the operation
     *
     * @param entity
     * @return redirect to show
     * @throws PMException
     */
    @RequestMapping(value = "/jpm/{entity}/add", method = RequestMethod.POST)
    public ModelAndView addCommit(@PathVariable Entity entity) throws PMException {
        final Operation operation = entity.getOperation("add");
        getContext().setEntity(entity);
        getContext().setOperation(operation);
        try {
            final String instanceId = getService().save(entity, operation,
                    new EntityInstance(null, entity, operation, null),
                    getRequest().getParameterMap());
            return new ModelAndView("redirect:/jpm/" + entity.getId() + "/" + instanceId + "/show");
        } catch (ValidationException e) {
            if (e.getMsg() != null) {
                getContext().getEntityMessages().add(e.getMsg());
            }
            return addPrepare(entity);
        }
    }

    /**
{'fraction_non_alphanumeric': 0.11566666666666667, 'fraction_numerical': 0.0026666666666666666, 'mean_word_length': 3.0906612133605997, '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': '3022902', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1668, 'n_words': 331}