package wang.yanjiong.metamate.core.ctl;

import one.kii.summer.io.context.ErestHeaders;
import one.kii.summer.io.context.ReadContext;
import one.kii.summer.io.receiver.ErestResponse;
import one.kii.summer.io.receiver.ReadController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import wang.yanjiong.metamate.core.api.VisitIntensionsApi;

/**
 * Created by WangYanJiong on 4/13/17.
 */

@RestController
@RequestMapping("/api/v1")
@CrossOrigin(origins = "*")
public class VisitIntensionCtl extends ReadController {


    private static final String NAME_ROOT = "root";

    private static final String TREE_MASTER = "master";

    @Autowired
    private VisitIntensionsApi api;

    @RequestMapping(value = "/{ownerId}/intension/{group}/{name}/{tree:.+}", method = RequestMethod.GET)
    public ResponseEntity<VisitIntensionsApi.Extension> readIntensionsByGroupNameVersion(
            @RequestHeader(value = ErestHeaders.REQUEST_ID, required = false) String requestId,
            @RequestHeader(ErestHeaders.VISITOR_ID) String visitorId,
            @PathVariable("ownerId") String ownerId,
            @PathVariable("group") String group,
            @PathVariable("name") String name,
            @PathVariable("tree") String tree) {

        ReadContext context = buildContext(requestId, ownerId, visitorId);

        VisitIntensionsApi.Form form = new VisitIntensionsApi.Form();

        form.setGroup(group);
        form.setName(name);
        form.setTree(tree);

        return ErestResponse.ok(requestId, api.readIntensionsByGroupNameVersion(context, form));
    }

    @RequestMapping(value = "/{ownerId}/intension/{group:.+}", method = RequestMethod.GET)
    public ResponseEntity<VisitIntensionsApi.Extension> readIntensionsByGroupNameVersion(
            @RequestHeader(value = ErestHeaders.REQUEST_ID, required = false) String requestId,
            @RequestHeader(ErestHeaders.VISITOR_ID) String visitorId,
            @PathVariable("ownerId") String ownerId,
            @PathVariable("group") String group) {

        ReadContext context = buildContext(requestId, ownerId, visitorId);

        VisitIntensionsApi.Form form = new VisitIntensionsApi.Form();

        form.setGroup(group);
        form.setName(NAME_ROOT);
        form.setTree(TREE_MASTER);

        return ErestResponse.ok(requestId, api.readIntensionsByGroupNameVersion(context, form));
    }
}

@@ -1342,33 +1342,32 @@
 ing tree) %7B%0A
-%0A
         
 ReadContext 
@@ -1358,328 +1358,90 @@
     
-ReadContext context = buildContext(requestId, ownerId, visitorId);%0A%0A        VisitIntensionsApi.Form form = new VisitIntensionsApi.Form();%0A%0A        form.setGroup(group);%0A        form.setName(name);%0A        form.setTree(tree);%0A%0A        return ErestResponse.ok(requestId, api.readIntensionsByGroupNameVersion(context, form)
+return getExtensionResponseEntity(requestId, ownerId, visitorId, group, name, tree
 );%0A 
@@ -1897,16 +1897,378 @@
 roup) %7B%0A
+        return getExtensionResponseEntity(requestId, ownerId, visitorId, group, NAME_ROOT, TREE_MASTER);%0A    %7D%0A%0A    private ResponseEntity%3CVisitIntensionsApi.Extension%3E getExtensionResponseEntity(%0A            String requestId,%0A            String ownerId,%0A            String visitorId,%0A            String group,%0A            String name,%0A            String tree) %7B
 %0A       
@@ -2406,17 +2406,16 @@
 Form();%0A
-%0A
         
@@ -2457,25 +2457,20 @@
 setName(
-NAME_ROOT
+name
 );%0A     
@@ -2489,23 +2489,15 @@
 ree(
-TREE_MASTER
+tree
 );%0A
-%0A
     

improve VisitIntensionCtl