<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.v1_0.resource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;

import org.openmrs.Concept;
import org.openmrs.ConceptAnswer;
import org.openmrs.ConceptName;
import org.openmrs.ConceptSearchResult;
import org.openmrs.annotation.Handler;
import org.openmrs.api.APIException;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.PropertySetter;
import org.openmrs.module.webservices.rest.web.annotation.RepHandler;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
import org.openmrs.module.webservices.rest.web.representation.RefRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.AlreadyPaged;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.response.ConversionException;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.util.LocaleUtility;

/**
 * {@link Resource} for {@link Concept}, supporting standard CRUD operations
 */
@Resource("concept")
@Handler(supports = Concept.class, order = 0)
public class ConceptResource extends DelegatingCrudResource<Concept> {
	
	@RepHandler(RefRepresentation.class)
	public SimpleObject asRef(Concept delegate) throws ConversionException {
		DelegatingResourceDescription description = new DelegatingResourceDescription();
		description.addProperty("uuid");
		description.addProperty("display", "displayString", Representation.DEFAULT);
		if (delegate.isRetired()) {
			description.addProperty("retired");
		}
		description.addSelfLink();
		return convertDelegateToRepresentation(delegate, description);
	}
	
	/**
	 * @see DelegatingCrudResource#getRepresentationDescription(Representation)
	 */
	@Override
	public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
		if (rep instanceof DefaultRepresentation) {
			DelegatingResourceDescription description = new DelegatingResourceDescription();
			description.addProperty("uuid");
			description.addProperty("display", findMethod("getDisplayName"));
			description.addProperty("name", Representation.DEFAULT);
			description.addProperty("datatype", Representation.REF);
			description.addProperty("conceptClass", Representation.REF);
			description.addProperty("set");
			description.addProperty("version");
			description.addProperty("retired");
			
			description.addProperty("names", Representation.REF);
			description.addProperty("descriptions", Representation.REF);
			
			description.addProperty("answers", Representation.REF);
			description.addProperty("setMembers", Representation.REF);
			//description.addProperty("conceptMappings", Representation.REF);  add as subresource
			
			description.addSelfLink();
			description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
			return description;
		} else if (rep instanceof FullRepresentation) {
			DelegatingResourceDescription description = new DelegatingResourceDescription();
			description.addProperty("uuid");
			description.addProperty("display", findMethod("getDisplayName"));
			description.addProperty("name", Representation.DEFAULT);
			description.addProperty("datatype", Representation.DEFAULT);
			description.addProperty("conceptClass", Representation.DEFAULT);
			description.addProperty("set");
			description.addProperty("version");
			description.addProperty("retired");
			
			description.addProperty("names", Representation.DEFAULT);
			description.addProperty("descriptions", Representation.DEFAULT);
			
			description.addProperty("answers", Representation.REF);
			description.addProperty("setMembers", Representation.REF);
			//description.addProperty("conceptMappings", Representation.DEFAULT);  add as subresource
			description.addProperty("auditInfo", findMethod("getAuditInfo"));
			description.addSelfLink();
			return description;
		}
		return null;
	}
	
	/**
	 * Sets the name property to be the fully specified name of the Concept in the current locale
	 * <commit_msg>RESTWS-190: Include a way to get set members and answers for concept<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.v1_0.resource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;

import org.openmrs.Concept;
import org.openmrs.ConceptAnswer;
import org.openmrs.ConceptName;
import org.openmrs.ConceptSearchResult;
import org.openmrs.annotation.Handler;
import org.openmrs.api.APIException;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.PropertySetter;
import org.openmrs.module.webservices.rest.web.annotation.RepHandler;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
import org.openmrs.module.webservices.rest.web.representation.RefRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.AlreadyPaged;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.response.ConversionException;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.util.LocaleUtility;

/**
 * {@link Resource} for {@link Concept}, supporting standard CRUD operations
 */
@Resource("concept")
@Handler(supports = Concept.class, order = 0)
public class ConceptResource extends DelegatingCrudResource<Concept> {
	
	@RepHandler(RefRepresentation.class)
	public SimpleObject asRef(Concept delegate) throws ConversionException {
		DelegatingResourceDescription description = new DelegatingResourceDescription();
		description.addProperty("uuid");
		description.addProperty("display", "displayString", Representation.DEFAULT);
		if (delegate.isRetired()) {
			description.addProperty("retired");
		}
		description.addSelfLink();
		return convertDelegateToRepresentation(delegate, description);
	}
	
	/**
	 * @see DelegatingCrudResource#getRepresentationDescription(Representation)
	 */
	@Override
	public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
		if (rep instanceof DefaultRepresentation) {
			DelegatingResourceDescription description = new DelegatingResourceDescription();
			description.addProperty("uuid");
			description.addProperty("display", findMethod("getDisplayName"));
			description.addProperty("name", Representation.DEFAULT);
			description.addProperty("datatype", Representation.REF);
			description.addProperty("conceptClass", Representation.REF);
			description.addProperty("set");
			description.addProperty("version");
			description.addProperty("retired");
			
			description.addProperty("names", Representation.REF);
			description.addProperty("descriptions", Representation.REF);
			
			description.addProperty("answers", Representation.REF);
			description.addProperty("setMembers", Representation.REF);
			//description.addProperty("conceptMappings", Representation.REF);  add as subresource
			
			description.addSelfLink();
			description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
			return description;
		} else if (rep instanceof FullRepresentation) {
			DelegatingResourceDescription description = new DelegatingResourceDescription();
			description.addProperty("uuid");
			description.addProperty("display", findMethod("getDisplayName"));
			description.addProperty("name", Representation.DEFAULT);
			description.addProperty("datatype", Representation.DEFAULT);
			description.addProperty("conceptClass", Representation.DEFAULT);
			description.addProperty("set");
			description.addProperty("version");
			description.addProperty("retired");
			
			description.addProperty("names", Representation.DEFAULT);
			description.addProperty("descriptions", Representation.DEFAULT);
			
			description.addProperty("answers", Representation.DEFAULT);
			description.addProperty("setMembers", Representation.DEFAULT);
			//description.addProperty("conceptMappings", Representation.DEFAULT);  add as subresource
			description.addProperty("auditInfo", findMethod("getAuditInfo"));
			description.addSelfLink();
			return description;
		}
		return null;
	}
	
	/**
	 * Sets the name property to be the fully specified name of the Concept in the current locale
	 * 
{'fraction_non_alphanumeric': 0.10714601965819534, 'fraction_numerical': 0.0011511555831045781, 'mean_word_length': 6.225847728726808, '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': '3961706', 'n_tokens_mistral': 3305, 'n_tokens_neox': 3050, 'n_words': 603}