/**
 * 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.Map;

import org.openmrs.annotation.Handler;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
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.DataDelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
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.module.webservices.rest.web.util.IncomingHl7Message;

/**
 * {@link Resource} for {@link IncomingHl7Message}, supporting standard CRUD operations
 */
@Resource("hl7")
@Handler(supports = IncomingHl7Message.class, order = 0)
public class HL7MessageResource extends DataDelegatingCrudResource<IncomingHl7Message> {
	
	/**
	 * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#delete(java.lang.Object,
	 *      java.lang.String, org.openmrs.module.webservices.rest.web.RequestContext)
	 */
	@Override
	protected void delete(IncomingHl7Message delegate, String reason, RequestContext context) throws ResponseException {
		throw new ResourceDoesNotSupportOperationException();
	}
	
	/**
	 * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getByUniqueId(java.lang.String)
	 */
	@Override
	public IncomingHl7Message getByUniqueId(String uniqueId) {
		// currently it's returning null because we don't have methods within HL7 service, which are returning hl7 message
		// by its uuid. It will be fixed when such methods will be implemented 
		return null;
	}
	
	/**
	 * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getRepresentationDescription(org.openmrs.module.webservices.rest.web.representation.Representation)
	 */
	@Override
	public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
		if (rep instanceof DefaultRepresentation) {
			DelegatingResourceDescription description = new DelegatingResourceDescription();
			description.addProperty("uuid");
			description.addProperty("display", findMethod("getDisplayString"));
			description.addProperty("messageState");
			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("getDisplayString"));
			description.addProperty("source", Representation.DEFAULT);
			description.addProperty("sourceKey");
			description.addProperty("data");
			description.addProperty("messageState");
			description.addSelfLink();
			return description;
		}
		return null;
	}
	
	/**
	 * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties()
	 */
	@Override
	public DelegatingResourceDescription getCreatableProperties() throws ResponseException {
		DelegatingResourceDescription description = new DelegatingResourceDescription();
		description.addRequiredProperty("hl7");
		return description;
	}
	
	/**
	 * It needs to be overwritten to allow for hidden properties.
	 * 
	 * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#setConvertedProperties(java.lang.Object,
	 *      java.util.Map,
	 *      org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription,
	 *      boolean)
	 */
	@Override
	protected void setConvertedProperties(IncomingHl7Message delegate, Map<String, Object> propertyMap,
	        DelegatingResourceDescription description, boolean mustIncludeRequiredProperties) throws ConversionException {
		for (Map.Entry<String, Object> prop : propertyMap.entrySet()) {
			setProperty(delegate, prop.getKey(), prop.getValue());
		}
	}
	
	/**
	 * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#newDelegate()
	 */
	@Override
	protected IncomingHl7Message newDelegate() {
		return new IncomingHl7Message();
	}
	
	/**
	 * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#purge(java.lang.Object,
	 *      org.openmrs.module.webservices.rest.web.RequestContext)
	 */
	@Override
	public void purge(IncomingHl7Message delegate, RequestContext context) throws ResponseException {
		throw new ResourceDoesNotSupportOperationException();
	}
	
	/**
	 * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#save(java.lang.Object)
	 */
	@Override
	protected IncomingHl7Message save(IncomingHl7Message delegate) {
		return new IncomingHl7Message(Context.getHL7Service().saveHL7InQueue(delegate.toHL7InQueue()));
	}
	
	/**
	 * Gets the display string for an incoming hl7 message resource.
	 * 
	 * @param message the hl7 message.
	 * @return the display string.
	 */
	public String getDisplayString(IncomingHl7Message message) {
		return message.getSourceKey();
	}
}

@@ -1676,16 +1676,102 @@
 essage;%0D
+%0Aimport org.openmrs.module.webservices.rest.web.v1_0.controller.HL7MessageController;%0D
 %0A%0D%0A/**%0D%0A
@@ -4541,16 +4541,295 @@
 operties
+: source, sourceKey and data. They%0D%0A%09 * are automatically extracted from the hl7 property and populated in%0D%0A%09 * %7B@link HL7MessageController#create(String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)%7D%0D%0A%09 * . They should not be POSTed by the user
 .%0D%0A%09 * %0D
@@ -5225,16 +5225,46 @@
         
+                              
 Delegati
@@ -5336,16 +5336,22 @@
 perties)
+%0D%0A%09   
  throws 

Define creatable/updatable properties on HL7Message resource