package com.ctrip.zeus.restful.resource;

import com.ctrip.zeus.exceptions.ValidationException;
import com.ctrip.zeus.restful.message.ResponseHandler;
import com.ctrip.zeus.tag.PropertyBox;
import com.ctrip.zeus.tag.PropertyService;
import com.ctrip.zeus.tag.entity.Property;
import com.ctrip.zeus.tag.entity.PropertyList;
import com.google.common.base.Joiner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 * Created by zhoumy on 2015/7/20.
 */
@Component
@Path("/")
public class PropertyResource {
    @Resource
    private PropertyBox propertyBox;
    @Resource
    private PropertyService propertyService;
    @Resource
    private ResponseHandler responseHandler;

    @GET
    @Path("/properties")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response listProperties(@Context HttpHeaders hh,
                                   @Context HttpServletRequest request,
                                   @QueryParam("type") String type,
                                   @QueryParam("targetId") Long targetId) throws Exception {
        List<Property> list;
        if (type != null && targetId != null) {
            list = propertyService.getProperties(type, targetId);
        } else {
            list = propertyService.getAllProperties();
        }
        PropertyList propertyList = new PropertyList().setTotal(list.size());
        for (Property property : list) {
            propertyList.addProperty(property);
        }
        return responseHandler.handle(propertyList, hh.getMediaType());
    }

    @GET
    @Path("/property/set")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response addPropertyItem(@Context HttpHeaders hh,
                                    @Context HttpServletRequest request,
                                    @QueryParam("pname") String pname,
                                    @QueryParam("pvalue") String pvalue,
                                    @QueryParam("type") String type,
                                    @QueryParam("targetId") Long targetId) throws Exception {
        if (pname == null || pvalue == null || type == null || targetId == null)
            throw new ValidationException("At least one parameter is missing.");
        propertyBox.set(pname, pvalue, type, targetId);
        return responseHandler.handle(targetId + " is added to property " + pname + "/" + pvalue + ".", hh.getMediaType());
    }

    @GET
    @Path("/property/clear")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response deletePropertyItem(@Context HttpHeaders hh,
                                       @Context HttpServletRequest request,
                                       @QueryParam("pname") String pname,
                                       @QueryParam("pvalue") String pvalue,
                                       @QueryParam("type") String type,
                                       @QueryParam("targetId") Long targetId) throws Exception {
        if ((pname == null && pvalue == null) || type == null)
            throw new ValidationException("At least one parameter is missing.");
        if (targetId != null) {
            propertyBox.clear(pname, pvalue, type, targetId);
            return responseHandler.handle(targetId + " is deleted from property " + pname + "/" + pvalue + ".", hh.getMediaType());
        }
        return responseHandler.handle("No action is performed.", hh.getMediaType());
    }

    @GET
    @Path("/property/delete")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response deleteProperty(@Context HttpHeaders hh,
                                   @Context HttpServletRequest request,
                                   @QueryParam("pname") String pname,
                                   @QueryParam("force") Boolean force) throws Exception {
        if (pname == null) throw new ValidationException("Parameter pname is required.");
        boolean forceEnabled = force != null && force;
        propertyBox.removeProperty(pname, forceEnabled);
        return responseHandler.handle("Successfully deleted property " + pname + ".", hh.getMediaType());
    }
}
@@ -2408,36 +2408,42 @@
 ram(%22targetId%22) 
+List%3C
 Long
+%3E
  targetId) throw
@@ -2427,32 +2427,33 @@
 t%3CLong%3E targetId
+s
 ) throws Excepti
@@ -2528,16 +2528,17 @@
 targetId
+s
  == null
@@ -2665,32 +2665,69 @@
 , type, targetId
+s.toArray(new Long%5BtargetIds.size()%5D)
 );%0A        retur
@@ -2747,32 +2747,52 @@
 dler.handle(
+Joiner.on(%22,%22).join(
 targetId
  + %22 is adde
@@ -2779,23 +2779,29 @@
 targetId
+s)
  + %22 is
+/are
  added t
@@ -3396,36 +3396,42 @@
 ram(%22targetId%22) 
+List%3C
 Long
+%3E
  targetId) throw
@@ -3415,32 +3415,33 @@
 t%3CLong%3E targetId
+s
 ) throws Excepti
@@ -3497,24 +3497,45 @@
 ) %7C%7C type ==
+ null %7C%7C targetIds ==
  null)%0A     
@@ -3614,44 +3614,8 @@
 %22);%0A
-        if (targetId != null) %7B%0A    
     
@@ -3661,31 +3661,64 @@
 pe, targetId
-);%0A    
+s.toArray(new Long%5BtargetIds.size()%5D));%0A
         retu
@@ -3743,24 +3743,44 @@
 .handle(
+Joiner.on(%22,%22).join(
 targetId
  + %22 is 
@@ -3771,23 +3771,29 @@
 targetId
+s)
  + %22 is
+/are
  deleted
@@ -3863,103 +3863,8 @@
 ));%0A
-        %7D%0A        return responseHandler.handle(%22No action is performed.%22, hh.getMediaType());%0A
     

support batch set/clear props