<commit_before>/*
 * Copyright 2011 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl1.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kns.uif.util;

import java.security.GeneralSecurityException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.core.api.encryption.EncryptionService;
import org.kuali.rice.core.api.services.CoreApiServiceLocator;
import org.kuali.rice.core.web.format.Formatter;
import org.kuali.rice.kns.datadictionary.AttributeDefinition;
import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
import org.kuali.rice.kns.uif.control.CheckboxControl;
import org.kuali.rice.kns.uif.control.Control;
import org.kuali.rice.kns.uif.control.RadioGroupControl;
import org.kuali.rice.kns.uif.control.TextAreaControl;
import org.kuali.rice.kns.uif.control.TextControl;
import org.kuali.rice.kns.uif.field.AttributeField;
import org.kuali.rice.kns.util.KNSConstants;
import org.kuali.rice.kns.web.spring.form.UifFormBase;

/**
 * Class for utility methods that pertain to UIF Lookup processing
 * 
 * @author K<NAME> (<EMAIL>)
 */
public class LookupInquiryUtils {
	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupInquiryUtils.class);

	public static void initializeAttributeFieldFromAttributeDefinition(AttributeField attributeField,
			AttributeDefinition attributeDefinition) {
		// label
		if (StringUtils.isEmpty(attributeField.getLabel())) {
			attributeField.setLabel(attributeDefinition.getLabel());
		}

		// short label
		if (StringUtils.isEmpty(attributeField.getShortLabel())) {
			attributeField.setShortLabel(attributeDefinition.getShortLabel());
		}

		// control
		setupControlFromAttributeDefinition(attributeField, attributeDefinition);

		// do not inherit requiredness
		// if (attributeField.getRequired() == null) {
		// attributeField.setRequired(attributeDefinition.isRequired());
		// }

		// do not inherit summary
		// if (StringUtils.isEmpty(attributeField.getSummary())) {
		// attributeField.setSummary(attributeDefinition.getSummary());
		// }

		// do not inherit description
		// if (StringUtils.isEmpty(attributeField.getDescription())) {
		// attributeField.setDescription(attributeDefinition.getDescription());
		// }

		// do not inherit constraint
		// if (StringUtils.isEmpty(attributeField.getConstraint())) {
		// attributeField.setConstraint(attributeDefinition.getConstraint());
		// }

		// max length
		// if (attributeField.getMaxLength() == null) {
		// attributeField.setMaxLength(attributeDefinition.getMaxLength());
		// }

		// min length
		// if (attributeField.getMinLength() == null) {
		// attributeField.setMinLength(attributeDefinition.getMinLength());
		// }

	}

	protected static void setupControlFromAttributeDefinition(AttributeField attributeField,
			AttributeDefinition attributeDefinition) {
		if (attributeField.getControl() == null) {
			attributeField.setControl(convertControlToLookupControl(attributeDefinition));
		}
	}

	protected static Control convertControlToLookupControl(AttributeDefinition attributeDefinition) {
		Control newControl = null;
		if (attributeDefinition.getControlField() == null) {
		    return null;
		}
		
		if (CheckboxControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
			newControl = generateCustomLookupControlFromExisting(RadioGroupControl.class,
					attributeDefinition.getControlField());
		}
		else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
			newControl = generateCustomLookupControlFromExisting(TextControl.class,
					attributeDefinition.getControlField());
		}
		else {
			newControl = ComponentUtils.copy(attributeDefinition.getControlField(), "");
		}
		return newControl;
	}

	public static Control generateCustomLookupControlFromExisting(Class<? extends Control> controlClass,
			Control existingControl) {
		try {
			Control newControl = controlClass.newInstance();
			for (String propertyPath : ComponentUtils.getComponentPropertyNames(existingControl.getClass())) {
				Object propValue = ObjectPropertyUtils.getPropertyValue(existingControl, propertyPath);
				if (propValue != null) {
					ObjectPropertyUtils.setPropertyValue(newControl, propertyPath, propValue, true);
				}
			}
			return newControl;<commit_msg>Changed lookup control conversion to used component factory<commit_after>/*
 * Copyright 2011 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl1.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kns.uif.util;

import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.hibernate.mapping.Component;
import org.kuali.rice.core.api.encryption.EncryptionService;
import org.kuali.rice.core.api.services.CoreApiServiceLocator;
import org.kuali.rice.core.util.ConcreteKeyValue;
import org.kuali.rice.core.util.KeyValue;
import org.kuali.rice.core.web.format.Formatter;
import org.kuali.rice.kns.datadictionary.AttributeDefinition;
import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
import org.kuali.rice.kns.uif.control.CheckboxControl;
import org.kuali.rice.kns.uif.control.Control;
import org.kuali.rice.kns.uif.control.RadioGroupControl;
import org.kuali.rice.kns.uif.control.TextAreaControl;
import org.kuali.rice.kns.uif.control.TextControl;
import org.kuali.rice.kns.uif.field.AttributeField;
import org.kuali.rice.kns.util.KNSConstants;
import org.kuali.rice.kns.web.spring.form.UifFormBase;

/**
 * Class for utility methods that pertain to UIF Lookup processing
 * 
 * @author Kuali Rice Team (<EMAIL>)
 */
public class LookupInquiryUtils {
	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupInquiryUtils.class);

	public static void initializeAttributeFieldFromAttributeDefinition(AttributeField attributeField,
			AttributeDefinition attributeDefinition) {
		// label
		if (StringUtils.isEmpty(attributeField.getLabel())) {
			attributeField.setLabel(attributeDefinition.getLabel());
		}

		// short label
		if (StringUtils.isEmpty(attributeField.getShortLabel())) {
			attributeField.setShortLabel(attributeDefinition.getShortLabel());
		}

		// control
		setupControlFromAttributeDefinition(attributeField, attributeDefinition);

		// do not inherit requiredness
		// if (attributeField.getRequired() == null) {
		// attributeField.setRequired(attributeDefinition.isRequired());
		// }

		// do not inherit summary
		// if (StringUtils.isEmpty(attributeField.getSummary())) {
		// attributeField.setSummary(attributeDefinition.getSummary());
		// }

		// do not inherit description
		// if (StringUtils.isEmpty(attributeField.getDescription())) {
		// attributeField.setDescription(attributeDefinition.getDescription());
		// }

		// do not inherit constraint
		// if (StringUtils.isEmpty(attributeField.getConstraint())) {
		// attributeField.setConstraint(attributeDefinition.getConstraint());
		// }

		// max length
		// if (attributeField.getMaxLength() == null) {
		// attributeField.setMaxLength(attributeDefinition.getMaxLength());
		// }

		// min length
		// if (attributeField.getMinLength() == null) {
		// attributeField.setMinLength(attributeDefinition.getMinLength());
		// }

	}

	protected static void setupControlFromAttributeDefinition(AttributeField attributeField,
			AttributeDefinition attributeDefinition) {
		if (attributeField.getControl() == null) {
			attributeField.setControl(convertControlToLookupControl(attributeDefinition));
		}
	}

	protected static Control convertControlToLookupControl(AttributeDefinition attributeDefinition) {
		Control newControl = null;
		if (attributeDefinition.getControlField() == null) {
		    return null;
		}
		
		if (CheckboxControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
			newControl = ComponentFactory.getRadioGroupControl();
            List<KeyValue> options = new ArrayList<KeyValue>();
            options.add(new ConcreteKeyValue("Y", "Yes"));
            options.add(new ConcreteKeyValue("N", "No"));
            options.add(new ConcreteKeyValue("", "Both"));

            ((RadioGroupControl) newControl).setOptions(options);
		}
		else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
			newControl = ComponentFactory.getTextControl();
		}
		else {
			newControl = ComponentUtils.copy(attributeDefinition.getControlField(), "");
		}

		return newControl;
	}

	public static Control generateCustomLookupControlFromExisting(Class<? extends Control> controlClass,
			Control existingControl) {
		try {
			Control newControl = controlClass.newInstance();
			for (String propertyPath : ComponentUtils.getComponentPropertyNames(existingControl.getClass())) {
				Object propValue = ObjectPropertyUtils.getPropertyValue(existingControl, propertyPath);
				if (propValue != null) {
					ObjectPropertyUtils.setPropertyValue(newControl, propertyPath, propValue, true);
				}
			}
			return newControl;
{'fraction_non_alphanumeric': 0.10774212540476891, 'fraction_numerical': 0.001766264350897851, 'mean_word_length': 4.956750438340152, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '6276878', 'n_tokens_mistral': 3023, 'n_tokens_neox': 2797, 'n_words': 579}