/*
 * 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) OpenMRS, LLC.  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.annotation.Handler;
import org.openmrs.module.openhmis.commons.api.entity.IMetadataDataService;
import org.openmrs.module.openhmis.inventory.api.ICategoryDataService;
import org.openmrs.module.openhmis.inventory.api.model.Category;
import org.openmrs.module.openhmis.inventory.web.ModuleRestConstants;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.response.ResponseException;

@Resource(name = ModuleRestConstants.CATEGORY_RESOURCE, supportedClass=Category.class, supportedOpenmrsVersions={"1.9"})
@Handler(supports = { Category.class }, order = 0)
public class CategoryResource extends BaseRestMetadataResource<Category> {

    private static final Log LOG = LogFactory.getLog(CategoryResource.class);

    @Override
    public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
        DelegatingResourceDescription description = super.getRepresentationDescription(rep);
        description.addProperty("description", Representation.REF);

        return description;
    }

	@Override
	public Category newDelegate() {
		return new Category();
	}

	@Override
	public Class<? extends IMetadataDataService<Category>> getServiceClass() {
		return ICategoryDataService.class;
	}

	@Override
	public Category getByUniqueId(String uniqueId) {
		return StringUtils.isEmpty(uniqueId) ? null : super.getByUniqueId(uniqueId);
	}

    @Override
    public void purge(Category category, RequestContext context) {
        try {
            super.purge(category, context);
        } catch(Exception e) {
            LOG.error("Exception occured when trying to purge category <" + category.getName() + ">", e);
            throw new ResponseException("Can't purge category with name <" +  category.getName() + "> as it is still in use") {
                private static final long serialVersionUID = 1L;
            };
        }
    }
}


@@ -762,16 +762,53 @@
 andler;%0A
+import org.openmrs.api.APIException;%0A
 import o
@@ -867,32 +867,109 @@
 ataDataService;%0A
+import org.openmrs.module.openhmis.commons.api.exception.PrivilegeException;%0A
 import org.openm
@@ -2664,17 +2664,306 @@
  %7D catch
-(
+ (PrivilegeException ce) %7B%0A        %09LOG.error(%22Exception occured when trying to purge category %3C%22 + category.getName() + %22%3E%22, ce);%0A        %09throw new PrivilegeException(%22Can't purge category with name %3C%22 +  category.getName() + %22%3E as required privilege is missing%22);    %0A        %7D catch(API
 Exceptio

fix purge for CategoryResoure