<commit_before>package nl.b3p.kar.struts;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl.b3p.commons.services.FormUtils;
import nl.b3p.commons.struts.ExtendedMethodProperties;
import nl.b3p.kar.hibernate.Gebruiker;
import nl.b3p.kar.hibernate.Role;
import nl.b3p.transmodel.DataOwner;
import nl.b3p.transmodel.RoadsideEquipment;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.DynaValidatorForm;
import org.hibernate.HibernateException;

public final class RoadsideEquipmentAction extends TreeItemAction {

    protected Map getActionMethodPropertiesMap() {
        Map map = new HashMap();
        map.put("save", new ExtendedMethodProperties("save"));
        map.put("new", new ExtendedMethodProperties("create"));
        map.put("delete", new ExtendedMethodProperties("delete"));
        map.put("validate", new ExtendedMethodProperties("validate"));
        return map;
    }

    public ActionForward unspecified(ActionMapping mapping, DynaValidatorForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        RoadsideEquipment rseq = getRoadsideEquipment(form, request, true);
        if(rseq == null) {
            addMessage(request, "error.notfound");
            request.setAttribute(HIDE_FORM, Boolean.TRUE);
            return mapping.findForward(SUCCESS);
        }

        createLists(rseq, form, request);

        populateForm(rseq, form, request);

        return mapping.findForward(SUCCESS);
    }

    public ActionForward create(ActionMapping mapping, DynaValidatorForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        form.initialize(mapping);
        createLists(null, form, request);
        return mapping.findForward(SUCCESS);
    }

    public ActionForward save(ActionMapping mapping, DynaValidatorForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        EntityManager em = getEntityManager();
        RoadsideEquipment rseq = getRoadsideEquipment(form, request, true);
        if(rseq == null) {
            addMessage(request, "error.notfound");
            request.setAttribute(HIDE_FORM, Boolean.TRUE);
            return mapping.findForward(SUCCESS);
        }
        createLists(rseq, form, request);

        ActionErrors errors = form.validate(mapping, request);
        if(!errors.isEmpty()) {
            addMessages(request, errors);
            return getDefaultForward(mapping, request);
        }

        /* check hier en niet in populateObject(), omdat nodig is voor auth check */
        Integer daoId = FormUtils.StringToInteger(form.getString("dataOwner"));
        DataOwner dao = daoId == null ? null : em.find(DataOwner.class, daoId);
        if(dao == null) {
            addMessage(request, "errors.required", "Databeheerder");
            return mapping.findForward(SUCCESS);
        }

        if(!request.isUserInRole(Role.BEHEERDER)) {
            Gebruiker g = Gebruiker.getNonTransientPrincipal(request);
            if(!g.canEditDataOwner(dao)) {
                addMessage(request, "error.dataowner.uneditable", dao.getName());
                return mapping.findForward(SUCCESS);
            }
        }

        /* XXX Check constraints */

        rseq.setDataOwner(dao);
        populateObject(rseq, form, request, mapping);

        boolean newObject = rseq.getId() == null;
        if(newObject) {
            em.persist(rseq);
        }

        em.flush();

        populateForm(rseq, form, request);

        request.setAttribute(TREE_UPDATE, treeUpdateJson(newObject ? "insert" : "update", rseq, request));

        addDefaultMessage(mapping, request);
        return getDefaultForward(mapping, request);
    }

    protected RoadsideEquipment getRoadsideEquipment(DynaValidatorForm form, HttpServletRequest request, boolean createNew) throws Exception {
        EntityManager em = getEntityManager();
        RoadsideEquipment rseq = null;
        Integer id = FormUtils.StringToInteger(form.getString("id"));
        if(null == id && createNew) {
            rseq = new RoadsideEquipment();
        } else if (null != id) {
            rseq = (RoadsideEquipment) em.find(RoadsideEquipment.class, id);
        }
        return rseq;
    }

    protected void createLists(RoadsideEquipment rseq, DynaValidatorForm form, HttpServletRequest request) throws HibernateException, Exception {
        EntityManager em = getEntityManager();

        request.setAttribute("roadsideEquipment", rseq);

        if(em.contains(rseq)) {
            request.setAttribute("activationGroupCount",
                    em.createQuery("select count(*) from ActivationGroup ag where ag.roadsideEquipment = :this")
                        .setParameter("this", rseq)
                        .getSingleResult());
            rseq.getDataOwner().getName();
        }
        List dataOwners;
        if(request.isUserInRole(Role.BEHEERDER)) {
            dataOwners = em.createQuery("from DataOwner where type = :type order by id")
                .setParameter("type", DataOwner.TYPE_ROOW)
                .getResultList();
        } else {
            Gebruiker g = Gebruiker.getNonTransientPrincipal(request);
            Set dataOwnersUnsorted = g.getDataOwnerRights().keySet();
            dataOwners = new ArrayList();
            for (Iterator<DataOwner> it = dataOwnersUnsorted.iterator(); it.hasNext();) {
                DataOwner dao = it.next();<commit_msg>fixed lijstje met roadside equipment dataowners niet alle dataowners waar rechten op zitten maar alleen waarop editable rechten<commit_after>package nl.b3p.kar.struts;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl.b3p.commons.services.FormUtils;
import nl.b3p.commons.struts.ExtendedMethodProperties;
import nl.b3p.kar.hibernate.Gebruiker;
import nl.b3p.kar.hibernate.Role;
import nl.b3p.transmodel.DataOwner;
import nl.b3p.transmodel.RoadsideEquipment;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.DynaValidatorForm;
import org.hibernate.HibernateException;

public final class RoadsideEquipmentAction extends TreeItemAction {

    protected Map getActionMethodPropertiesMap() {
        Map map = new HashMap();
        map.put("save", new ExtendedMethodProperties("save"));
        map.put("new", new ExtendedMethodProperties("create"));
        map.put("delete", new ExtendedMethodProperties("delete"));
        map.put("validate", new ExtendedMethodProperties("validate"));
        return map;
    }

    public ActionForward unspecified(ActionMapping mapping, DynaValidatorForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        RoadsideEquipment rseq = getRoadsideEquipment(form, request, true);
        if(rseq == null) {
            addMessage(request, "error.notfound");
            request.setAttribute(HIDE_FORM, Boolean.TRUE);
            return mapping.findForward(SUCCESS);
        }

        createLists(rseq, form, request);

        populateForm(rseq, form, request);

        return mapping.findForward(SUCCESS);
    }

    public ActionForward create(ActionMapping mapping, DynaValidatorForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        form.initialize(mapping);
        createLists(null, form, request);
        return mapping.findForward(SUCCESS);
    }

    public ActionForward save(ActionMapping mapping, DynaValidatorForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        EntityManager em = getEntityManager();
        RoadsideEquipment rseq = getRoadsideEquipment(form, request, true);
        if(rseq == null) {
            addMessage(request, "error.notfound");
            request.setAttribute(HIDE_FORM, Boolean.TRUE);
            return mapping.findForward(SUCCESS);
        }
        createLists(rseq, form, request);

        ActionErrors errors = form.validate(mapping, request);
        if(!errors.isEmpty()) {
            addMessages(request, errors);
            return getDefaultForward(mapping, request);
        }

        /* check hier en niet in populateObject(), omdat nodig is voor auth check */
        Integer daoId = FormUtils.StringToInteger(form.getString("dataOwner"));
        DataOwner dao = daoId == null ? null : em.find(DataOwner.class, daoId);
        if(dao == null) {
            addMessage(request, "errors.required", "Databeheerder");
            return mapping.findForward(SUCCESS);
        }

        if(!request.isUserInRole(Role.BEHEERDER)) {
            Gebruiker g = Gebruiker.getNonTransientPrincipal(request);
            if(!g.canEditDataOwner(dao)) {
                addMessage(request, "error.dataowner.uneditable", dao.getName());
                return mapping.findForward(SUCCESS);
            }
        }

        /* XXX Check constraints */

        rseq.setDataOwner(dao);
        populateObject(rseq, form, request, mapping);

        boolean newObject = rseq.getId() == null;
        if(newObject) {
            em.persist(rseq);
        }

        em.flush();

        populateForm(rseq, form, request);

        request.setAttribute(TREE_UPDATE, treeUpdateJson(newObject ? "insert" : "update", rseq, request));

        addDefaultMessage(mapping, request);
        return getDefaultForward(mapping, request);
    }

    protected RoadsideEquipment getRoadsideEquipment(DynaValidatorForm form, HttpServletRequest request, boolean createNew) throws Exception {
        EntityManager em = getEntityManager();
        RoadsideEquipment rseq = null;
        Integer id = FormUtils.StringToInteger(form.getString("id"));
        if(null == id && createNew) {
            rseq = new RoadsideEquipment();
        } else if (null != id) {
            rseq = (RoadsideEquipment) em.find(RoadsideEquipment.class, id);
        }
        return rseq;
    }

    protected void createLists(RoadsideEquipment rseq, DynaValidatorForm form, HttpServletRequest request) throws HibernateException, Exception {
        EntityManager em = getEntityManager();

        request.setAttribute("roadsideEquipment", rseq);

        if(em.contains(rseq)) {
            request.setAttribute("activationGroupCount",
                    em.createQuery("select count(*) from ActivationGroup ag where ag.roadsideEquipment = :this")
                        .setParameter("this", rseq)
                        .getSingleResult());
            rseq.getDataOwner().getName();
        }
        List dataOwners;
        if(request.isUserInRole(Role.BEHEERDER)) {
            dataOwners = em.createQuery("from DataOwner where type = :type order by id")
                .setParameter("type", DataOwner.TYPE_ROOW)
                .getResultList();
        } else {
            Gebruiker g = Gebruiker.getNonTransientPrincipal(request);
            Set dataOwnersUnsorted = g.getEditableDataOwners();
            dataOwners = new ArrayList();
            for (Iterator<DataOwner> it = dataOwnersUnsorted.iterator(); it.hasNext();) {
                DataOwner dao = it.next();
{'fraction_non_alphanumeric': 0.10203913829962177, 'fraction_numerical': 0.001151126459463904, 'mean_word_length': 3.058391725058392, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4437699', 'n_tokens_mistral': 3349, 'n_tokens_neox': 3251, 'n_words': 694}