<commit_before>/*
 * The contents of this file are subject to the OpenMRS Public License
 * Version 2.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) OpenHMIS.  All Rights Reserved.
 */
package org.openmrs.module.webservices.rest.resource;

import org.apache.commons.lang3.StringUtils;
import org.openmrs.api.context.Context;
import org.openmrs.module.openhmis.cashier.api.model.BillLineItem;
import org.openmrs.module.openhmis.commons.api.entity.IEntityDataService;
import org.openmrs.module.openhmis.inventory.api.IItemDataService;
import org.openmrs.module.openhmis.inventory.api.model.ItemPrice;
import org.openmrs.module.webservices.rest.helper.Converter;
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.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.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;

@Resource(name = RestConstants.VERSION_2 + "/cashier/billLineItem", supportedClass = BillLineItem.class,
        supportedOpenmrsVersions = { "1.9.*", "1.10.*" })
public class BillLineItemResource extends BaseRestDataResource<BillLineItem> {
	@Override
	public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
		DelegatingResourceDescription description = super.getRepresentationDescription(rep);
		if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
			description.addProperty("item");
			description.addProperty("quantity");
			description.addProperty("price");
			description.addProperty("priceName");
			description.addProperty("priceUuid");
			description.addProperty("lineItemOrder");
		}
		return description;
	}
	
	@PropertySetter(value = "price")
	public void setPriceValue(BillLineItem instance, Object price) {
		instance.setPrice(Converter.objectToBigDecimal(price));
	}
	
	@PropertySetter(value = "priceName")
	public void setPriceName(BillLineItem instance, String name) {
		//name is set in setItemPriceMethod as not set in js
	}
	
	@PropertyGetter(value = "priceName")
	public String getPriceName(BillLineItem instance) {
		String itemName = instance.getPriceName();
		return StringUtils.isNotBlank(itemName)  ? itemName : ""; 
	}
	
	@PropertySetter(value = "priceUuid")
	public void setItemPrice(BillLineItem instance, String uuid) {
		IItemDataService itemDataService = Context.getService(IItemDataService.class);
		ItemPrice itemPrice = itemDataService.getItemPriceByUuid(uuid);
		instance.setItemPrice(itemPrice);
		instance.setPriceName(itemPrice.getName());
	}
	
	@PropertyGetter(value = "priceUuid")
	public String getItemPriceUuid(BillLineItem instance) {
		ItemPrice itemPrice = instance.getItemPrice();
		return itemPrice != null ? itemPrice.getUuid() : ""; 
	}
	
	@Override
	public BillLineItem newDelegate() {
		return new BillLineItem();
	}
	
	@Override<commit_msg>catch exceptions etc if price is deleted but used in bill<commit_after>/*
 * The contents of this file are subject to the OpenMRS Public License
 * Version 2.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) OpenHMIS.  All Rights Reserved.
 */
package org.openmrs.module.webservices.rest.resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.context.Context;
import org.openmrs.module.openhmis.cashier.api.model.BillLineItem;
import org.openmrs.module.openhmis.commons.api.entity.IEntityDataService;
import org.openmrs.module.openhmis.inventory.api.IItemDataService;
import org.openmrs.module.openhmis.inventory.api.model.ItemPrice;
import org.openmrs.module.webservices.rest.helper.Converter;
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.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.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;

@Resource(name = RestConstants.VERSION_2 + "/cashier/billLineItem", supportedClass = BillLineItem.class,
        supportedOpenmrsVersions = { "1.9.*", "1.10.*" })
public class BillLineItemResource extends BaseRestDataResource<BillLineItem> {
	
	private final static Log LOG = LogFactory.getLog(BillLineItemResource.class);
	
	@Override
	public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
		DelegatingResourceDescription description = super.getRepresentationDescription(rep);
		if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
			description.addProperty("item");
			description.addProperty("quantity");
			description.addProperty("price");
			description.addProperty("priceName");
			description.addProperty("priceUuid");
			description.addProperty("lineItemOrder");
		}
		return description;
	}
	
	@PropertySetter(value = "price")
	public void setPriceValue(BillLineItem instance, Object price) {
		instance.setPrice(Converter.objectToBigDecimal(price));
	}
	
	@PropertySetter(value = "priceName")
	public void setPriceName(BillLineItem instance, String name) {
		//name is set in setItemPriceMethod as not set in js
	}
	
	@PropertyGetter(value = "priceName")
	public String getPriceName(BillLineItem instance) {
		String itemName = instance.getPriceName();
		return StringUtils.isNotBlank(itemName)  ? itemName : ""; 
	}
	
	@PropertySetter(value = "priceUuid")
	public void setItemPrice(BillLineItem instance, String uuid) {
		IItemDataService itemDataService = Context.getService(IItemDataService.class);
		ItemPrice itemPrice = itemDataService.getItemPriceByUuid(uuid);
		if (itemPrice != null) {
			instance.setItemPrice(itemPrice);
			instance.setPriceName(itemPrice.getName());
		}
	}
	
	@PropertyGetter(value = "priceUuid")
	public String getItemPriceUuid(BillLineItem instance) {
		try {
			ItemPrice itemPrice = instance.getItemPrice();
			return itemPrice != null ? itemPrice.getUuid() : ""; 
		} catch (Exception e) {
			LOG.warn("Price probably was deleted", e);
			return "";
		}
	}
	
	@Override
	public BillLineItem newDelegate() {
		return new BillLineItem();
	}
	
	@Override
{'fraction_non_alphanumeric': 0.10119205298013245, 'fraction_numerical': 0.0023841059602649007, 'mean_word_length': 5.635325131810193, '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': '3961260', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2169, 'n_words': 480}