<commit_before>/**
 * Copyright 2007 The JA-SIG Collaborative.  All rights reserved.
 * See license distributed with this file and
 * available online at http://www.uportal.org/license.html
 */
package org.jasig.portlet.test.mvc.tests;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.commons.collections.Factory;
import org.apache.commons.collections.list.LazyList;
import org.jasig.portlet.test.om.prefs.Preference;
import org.jasig.portlet.test.om.prefs.Preferences;
import org.springframework.validation.BindException;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.PortletRequestUtils;
import org.springframework.web.portlet.mvc.AbstractFormController;

/**
 * @author <NAME>
 * @version $Revision$
 */
public class PortletPrefrencesTest extends AbstractFormController {
    private static final PreferenceFactory PREFERENCE_FACTORY = new PreferenceFactory();
    private static final StringFactory STRING_FACTORY = new StringFactory();
    
    private static final class PreferenceFactory implements Factory {
        public Preference create() {
            final Preference preference = new Preference();
            
            List<String> values = new LinkedList<String>();
            values = LazyList.decorate(values, STRING_FACTORY);
            preference.setValues(values);
            
            return preference;
        }
    }
    
    private static final class StringFactory implements Factory {
        public String create() {
            return new String();
        }
    }

    /* (non-Javadoc)
     * @see org.springframework.web.portlet.mvc.AbstractFormController#processFormSubmission(javax.portlet.ActionRequest, javax.portlet.ActionResponse, java.lang.Object, org.springframework.validation.BindException)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected void processFormSubmission(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception {
        final Preferences preferences = (Preferences)command;
        final List<Preference> preferencesList = preferences.getPreferences();
        
        //Remove any preferences marked for removal
        final int delPrefIndex = PortletRequestUtils.getIntParameter(request, "delPrefIndex", -1);
        if (delPrefIndex >= 0) {
            final int delValueIndex = PortletRequestUtils.getIntParameter(request, "delValueIndex", -1);
            if (delValueIndex >= 0) {
                final Preference preference = preferencesList.get(delPrefIndex);
                final List<String> values = preference.getValues();
                values.remove(delValueIndex);
            }
            else {
                preferencesList.remove(delPrefIndex);
            }
        }
        
        final PortletPreferences portletPreferences = request.getPreferences();
        
        //Clear out all preferences before adding new ones
        for (final Enumeration<String> names = portletPreferences.getNames(); names.hasMoreElements(); ) {
            final String name = names.nextElement();
            portletPreferences.reset(name);
        }
        
        //Add all the submitted preferences to the PortletPreferences
        for (final Preference preference : preferencesList) {
            final String name = preference.getName();<commit_msg>Add bounds checking<commit_after>/**
 * Copyright 2007 The JA-SIG Collaborative.  All rights reserved.
 * See license distributed with this file and
 * available online at http://www.uportal.org/license.html
 */
package org.jasig.portlet.test.mvc.tests;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.commons.collections.Factory;
import org.apache.commons.collections.list.LazyList;
import org.jasig.portlet.test.om.prefs.Preference;
import org.jasig.portlet.test.om.prefs.Preferences;
import org.springframework.validation.BindException;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.PortletRequestUtils;
import org.springframework.web.portlet.mvc.AbstractFormController;

/**
 * @author <NAME>
 * @version $Revision$
 */
public class PortletPrefrencesTest extends AbstractFormController {
    private static final PreferenceFactory PREFERENCE_FACTORY = new PreferenceFactory();
    private static final StringFactory STRING_FACTORY = new StringFactory();
    
    private static final class PreferenceFactory implements Factory {
        public Preference create() {
            final Preference preference = new Preference();
            
            List<String> values = new LinkedList<String>();
            values = LazyList.decorate(values, STRING_FACTORY);
            preference.setValues(values);
            
            return preference;
        }
    }
    
    private static final class StringFactory implements Factory {
        public String create() {
            return new String();
        }
    }

    /* (non-Javadoc)
     * @see org.springframework.web.portlet.mvc.AbstractFormController#processFormSubmission(javax.portlet.ActionRequest, javax.portlet.ActionResponse, java.lang.Object, org.springframework.validation.BindException)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected void processFormSubmission(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception {
        final Preferences preferences = (Preferences)command;
        final List<Preference> preferencesList = preferences.getPreferences();
        
        //Remove any preferences marked for removal
        final int delPrefIndex = PortletRequestUtils.getIntParameter(request, "delPrefIndex", -1);
        if (delPrefIndex >= 0) {
            final int delValueIndex = PortletRequestUtils.getIntParameter(request, "delValueIndex", -1);
            if (delValueIndex >= 0) {
                final Preference preference = preferencesList.get(delPrefIndex);
                final List<String> values = preference.getValues();
                values.remove(delValueIndex);
            }
            else if (0 <= delPrefIndex && delPrefIndex < preferencesList.size()) {
                preferencesList.remove(delPrefIndex);
            }
        }
        
        final PortletPreferences portletPreferences = request.getPreferences();
        
        //Clear out all preferences before adding new ones
        for (final Enumeration<String> names = portletPreferences.getNames(); names.hasMoreElements(); ) {
            final String name = names.nextElement();
            portletPreferences.reset(name);
        }
        
        //Add all the submitted preferences to the PortletPreferences
        for (final Preference preference : preferencesList) {
            final String name = preference.getName();
{'fraction_non_alphanumeric': 0.08651535380507343, 'fraction_numerical': 0.0022696929238985313, 'mean_word_length': 3.1849162011173187, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5529516', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1871, 'n_words': 458}