<commit_before>/**
 * The contents of this file are subject to the OpenMRS Public 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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.webservices.rest.web.resource.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.Hyperlink;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.RepresentationDescription;
import org.openmrs.module.webservices.rest.web.response.ConversionException;

/**
 * Used by implementations of {@link DelegatingCrudResource} to indicate what delegate properties,
 * and what methods they want to include in a particular representation
 */
public class DelegatingResourceDescription implements RepresentationDescription {
	
	Map<String, Property> properties = new LinkedHashMap<String, Property>();
	
	List<Hyperlink> links = new ArrayList<Hyperlink>();
	
	public void addProperty(String propertyName) {
		addProperty(propertyName, propertyName, null, false);
	}
	
	public void addRequiredProperty(String propertyName) {
		addProperty(propertyName, propertyName, null, true);
	}
	
	public void addProperty(String propertyName, Representation rep) {
		addProperty(propertyName, propertyName, rep, false);
	}
	
	public void addRequiredProperty(String propertyName, Representation rep) {
		addProperty(propertyName, propertyName, rep, true);
	}
	
	public void addProperty(String propertyName, Method method) {
		addProperty(propertyName, method, null, false);
	}
	
	public void addProperty(String propertyName, String delegatePropertyName) {
		addProperty(propertyName, delegatePropertyName, null, false);
	}
	
	public void addRequiredProperty(String propertyName, String delegatePropertyName) {
		addProperty(propertyName, delegatePropertyName, null, true);
	}
	
	public void addProperty(String propertyName, String delegatePropertyName, Representation rep) {
		addProperty(propertyName, delegatePropertyName, rep, false);
	}
	
	public void addRequiredProperty(String propertyName, String delegatePropertyName, Representation rep) {
		addProperty(propertyName, delegatePropertyName, rep, true);
	}
	
	public void addProperty(String propertyName, Method method, Representation rep) {
		addProperty(propertyName, method, rep, false);
	}
	
	protected void addProperty(String propertyName, String delegatePropertyName, Representation rep, boolean required) {
		if (rep == null)
			rep = Representation.DEFAULT;
		properties.put(propertyName, new Property(delegatePropertyName, rep, required));
	}
	
	protected void addProperty(String propertyName, Method method, Representation rep, boolean required) {
		if (rep == null)
			rep = Representation.DEFAULT;
		properties.put(propertyName, new Property(method, rep, required));
	}
	
	public DelegatingResourceDescription addSelfLink() {
		return addLink("self", ".");
	}
	
	public DelegatingResourceDescription addLink(String rel, String uri) {
		links.add(new Hyperlink(rel, uri));
		return this;
	}
	
	/**
	 * @return the properties
	 */
	public Map<String, Property> getProperties() {
		return properties;
	}
	
	/**<commit_msg>RESTWS-226: Resources need to specify what fields are editable, not blindly expose all delegate properties<commit_after>/**
 * The contents of this file are subject to the OpenMRS Public 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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.webservices.rest.web.resource.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.Hyperlink;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.RepresentationDescription;
import org.openmrs.module.webservices.rest.web.response.ConversionException;

/**
 * Used by implementations of {@link DelegatingCrudResource} to indicate what delegate properties,
 * and what methods they want to include in a particular representation
 */
public class DelegatingResourceDescription implements RepresentationDescription {
	
	Map<String, Property> properties = new LinkedHashMap<String, Property>();
	
	List<Hyperlink> links = new ArrayList<Hyperlink>();
	
	public void addProperty(String propertyName) {
		addProperty(propertyName, propertyName, null, false);
	}
	
	public void addRequiredProperty(String propertyName) {
		addProperty(propertyName, propertyName, null, true);
	}
	
	public void addProperty(String propertyName, Representation rep) {
		addProperty(propertyName, propertyName, rep, false);
	}
	
	public void addRequiredProperty(String propertyName, Representation rep) {
		addProperty(propertyName, propertyName, rep, true);
	}
	
	public void addProperty(String propertyName, Method method) {
		addProperty(propertyName, method, null, false);
	}
	
	public void addProperty(String propertyName, String delegatePropertyName) {
		addProperty(propertyName, delegatePropertyName, null, false);
	}
	
	public void addRequiredProperty(String propertyName, String delegatePropertyName) {
		addProperty(propertyName, delegatePropertyName, null, true);
	}
	
	public void addProperty(String propertyName, String delegatePropertyName, Representation rep) {
		addProperty(propertyName, delegatePropertyName, rep, false);
	}
	
	public void addRequiredProperty(String propertyName, String delegatePropertyName, Representation rep) {
		addProperty(propertyName, delegatePropertyName, rep, true);
	}
	
	public void addProperty(String propertyName, Method method, Representation rep) {
		addProperty(propertyName, method, rep, false);
	}
	
	public void addProperty(String propertyName, String delegatePropertyName, Representation rep, boolean required) {
		if (rep == null)
			rep = Representation.DEFAULT;
		properties.put(propertyName, new Property(delegatePropertyName, rep, required));
	}
	
	public void addProperty(String propertyName, Method method, Representation rep, boolean required) {
		if (rep == null)
			rep = Representation.DEFAULT;
		properties.put(propertyName, new Property(method, rep, required));
	}
	
	public DelegatingResourceDescription addSelfLink() {
		return addLink("self", ".");
	}
	
	public DelegatingResourceDescription addLink(String rel, String uri) {
		links.add(new Hyperlink(rel, uri));
		return this;
	}
	
	/**
	 * @return the properties
	 */
	public Map<String, Property> getProperties() {
		return properties;
	}
	
	/**
{'fraction_non_alphanumeric': 0.08938180863630397, 'fraction_numerical': 0.0009187557422233889, 'mean_word_length': 5.501706484641638, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2021832', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2005, 'n_words': 632}