rain
2024-03-23 35a8fd809547c3e5c0293c03a0a50096e658de79
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.dji.sample.map.controller;
 
import com.dji.sample.common.model.CustomClaim;
import com.dji.sample.common.model.ResponseResult;
import com.dji.sample.component.websocket.model.BizCodeEnum;
import com.dji.sample.component.websocket.service.ISendMessageService;
import com.dji.sample.map.model.dto.*;
import com.dji.sample.map.service.IWorkspaceElementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
 
import static com.dji.sample.component.AuthInterceptor.TOKEN_CLAIM;
 
/**
 * @author sean
 * @version 0.2
 * @date 2021/11/29
 */
@RestController
@RequestMapping("${url.map.prefix}${url.map.version}/workspaces")
public class WorkspaceElementController {
 
    @Autowired
    private IWorkspaceElementService elementService;
 
    @Autowired
    private ISendMessageService sendMessageService;
 
    /**
     * In the first connection, pilot will send out this http request to obtain the group element list.
     * Also, if pilot receives a group refresh instruction from WebSocket,
     * it needs the same interface to request the group element list.
     * @param workspaceId
     * @param groupId
     * @param isDistributed
     * @return
     */
    @GetMapping("/{workspace_id}/element-groups")
    public ResponseResult<List<GroupDTO>> getAllElements(@PathVariable(name = "workspace_id") String workspaceId,
                               @RequestParam(name = "group_id", required = false) String groupId,
                               @RequestParam(name = "is_distributed", required = false) Boolean isDistributed) {
        List<GroupDTO> groupsList = elementService.getAllGroupsByWorkspaceId(workspaceId, groupId, isDistributed);
        return ResponseResult.success(groupsList);
    }
 
    /**
     * When user draws a point, line or polygon on the PILOT/Web side.
     * Save the element information to the database.
     * @param request
     * @param workspaceId
     * @param groupId
     * @param elementCreate
     * @return
     */
    @PostMapping("/{workspace_id}/element-groups/{group_id}/elements")
    public ResponseResult saveElement(HttpServletRequest request,
                            @PathVariable(name = "workspace_id") String workspaceId,
                            @PathVariable(name = "group_id") String groupId,
                            @RequestBody ElementCreateDTO elementCreate) {
        CustomClaim claims = (CustomClaim) request.getAttribute(TOKEN_CLAIM);
        // Set the creator of the element
        elementCreate.getResource().setUsername(claims.getUsername());
 
        ResponseResult response = elementService.saveElement(groupId, elementCreate);
        if (response.getCode() != ResponseResult.CODE_SUCCESS) {
            return response;
        }
 
        // Notify all WebSocket connections in this workspace to be updated when an element is created.
        elementService.getElementByElementId(elementCreate.getId())
                .ifPresent(groupElement -> sendMessageService.sendBatch(
                        workspaceId, BizCodeEnum.MAP_ELEMENT_CREATE.getCode(), groupElement));
 
        return ResponseResult.success(new ConcurrentHashMap<>(Map.of("id", elementCreate.getId())));
    }
 
    /**
     * When user edits a point, line or polygon on the PILOT/Web side.
     * Update the element information to the database.
     * @param request
     * @param workspaceId
     * @param elementId
     * @param elementUpdate
     * @return
     */
    @PutMapping("/{workspace_id}/elements/{element_id}")
    public ResponseResult updateElement(HttpServletRequest request,
                              @PathVariable(name = "workspace_id") String workspaceId,
                              @PathVariable(name = "element_id") String elementId,
                              @RequestBody ElementUpdateDTO elementUpdate) {
 
        CustomClaim claims = (CustomClaim) request.getAttribute(TOKEN_CLAIM);
 
        ResponseResult response = elementService.updateElement(elementId, elementUpdate, claims.getUsername());
        if (response.getCode() != ResponseResult.CODE_SUCCESS) {
            return response;
        }
 
        // Notify all WebSocket connections in this workspace to update when there is an element update.
        elementService.getElementByElementId(elementId)
                .ifPresent(groupElement -> sendMessageService.sendBatch(
                        workspaceId, BizCodeEnum.MAP_ELEMENT_UPDATE.getCode(), groupElement));
        return response;
    }
 
    /**
     * When user delete a point, line or polygon on the PILOT/Web side,
     * Delete the element information in the database.
     * @param workspaceId
     * @param elementId
     * @return
     */
    @DeleteMapping("/{workspace_id}/elements/{element_id}")
    public ResponseResult deleteElement(@PathVariable(name = "workspace_id") String workspaceId,
                              @PathVariable(name = "element_id") String elementId) {
 
        Optional<GroupElementDTO> elementOpt = elementService.getElementByElementId(elementId);
 
        ResponseResult response = elementService.deleteElement(elementId);
 
        // Notify all WebSocket connections in this workspace to update when an element is deleted.
        if (ResponseResult.CODE_SUCCESS == response.getCode()) {
            elementOpt.ifPresent(element ->
                    sendMessageService.sendBatch(workspaceId, BizCodeEnum.MAP_ELEMENT_DELETE.getCode(),
                                    WebSocketElementDelDTO.builder()
                                            .elementId(elementId)
                                            .groupId(element.getGroupId())
                                            .build()));
        }
        return response;
    }
 
    /**
     * Delete all the element information in this group based on the group id.
     * @param workspaceId
     * @param groupId
     * @return
     */
    @DeleteMapping("/{workspace_id}/element-groups/{group_id}/elements")
    public ResponseResult deleteAllElementByGroupId(@PathVariable(name = "workspace_id") String workspaceId,
                                          @PathVariable(name = "group_id") String groupId) {
 
        ResponseResult response = elementService.deleteAllElementByGroupId(groupId);
 
        // Notify all WebSocket connections in this workspace to update when elements are deleted.
        if (ResponseResult.CODE_SUCCESS == response.getCode()) {
 
            sendMessageService.sendBatch(workspaceId, BizCodeEnum.MAP_GROUP_REFRESH.getCode(),
                            // Group ids that need to re-request data
                            new ConcurrentHashMap<String, String[]>(Map.of("ids", new String[]{groupId})));
        }
        return response;
    }
}