File size: 15,168 Bytes
9373c61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cfe5c4
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package com.dalab.policyengine.mapper;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Component;

import com.dalab.policyengine.dto.PolicyDraftInputDTO;
import com.dalab.policyengine.dto.PolicyDraftOutputDTO;
import com.dalab.policyengine.model.PolicyDraft;
import com.dalab.policyengine.model.PolicyDraftStatus;

/**

 * Mapper for converting between PolicyDraft entities and DTOs.

 * Handles workflow status calculation and available actions based on current status and user permissions.

 */
@Component
public class PolicyDraftMapper {

    /**

     * Convert PolicyDraft entity to output DTO with workflow information.

     */
    public PolicyDraftOutputDTO toOutputDTO(PolicyDraft draft) {
        if (draft == null) {
            return null;
        }

        PolicyDraftOutputDTO dto = new PolicyDraftOutputDTO();
        
        // Basic information
        dto.setId(draft.getId());
        dto.setName(draft.getName());
        dto.setDescription(draft.getDescription());
        dto.setStatus(draft.getStatus().name());
        dto.setVersion(draft.getVersion());
        dto.setBasePolicyId(draft.getBasePolicyId());
        dto.setConditionLogic(draft.getConditionLogic());
        dto.setRulesDefinition(draft.getRulesDefinition());
        dto.setActions(draft.getActions());
        dto.setChangeSummary(draft.getChangeSummary());
        dto.setJustification(draft.getJustification());
        dto.setExpectedImpact(draft.getExpectedImpact());
        dto.setTargetImplementationDate(draft.getTargetImplementationDate());
        dto.setPriority(draft.getPriority());
        dto.setCategory(draft.getCategory());
        dto.setTags(draft.getTags());
        dto.setStakeholders(draft.getStakeholders());
        dto.setApprovalMetadata(draft.getApprovalMetadata());
        dto.setReviewComments(draft.getReviewComments());

        // Audit trail
        dto.setCreatedAt(draft.getCreatedAt());
        dto.setUpdatedAt(draft.getUpdatedAt());
        dto.setCreatedByUserId(draft.getCreatedByUserId());
        dto.setUpdatedByUserId(draft.getUpdatedByUserId());
        dto.setSubmittedAt(draft.getSubmittedAt());
        dto.setSubmittedByUserId(draft.getSubmittedByUserId());
        dto.setApprovedAt(draft.getApprovedAt());
        dto.setApprovedByUserId(draft.getApprovedByUserId());
        dto.setRejectedAt(draft.getRejectedAt());
        dto.setRejectedByUserId(draft.getRejectedByUserId());
        dto.setPublishedAt(draft.getPublishedAt());
        dto.setPublishedByUserId(draft.getPublishedByUserId());

        // Enhanced workflow information
        dto.setWorkflowStatus(createWorkflowStatus(draft));
        dto.setAvailableActions(createAvailableActions(draft));

        return dto;
    }

    /**

     * Convert input DTO to PolicyDraft entity.

     */
    public PolicyDraft toEntity(PolicyDraftInputDTO inputDTO) {
        if (inputDTO == null) {
            return null;
        }

        PolicyDraft draft = new PolicyDraft();
        
        draft.setName(inputDTO.getName());
        draft.setDescription(inputDTO.getDescription());
        draft.setBasePolicyId(inputDTO.getBasePolicyId());
        draft.setConditionLogic(inputDTO.getConditionLogic());
        draft.setRulesDefinition(inputDTO.getRulesDefinition());
        draft.setActions(inputDTO.getActions());
        draft.setChangeSummary(inputDTO.getChangeSummary());
        draft.setJustification(inputDTO.getJustification());
        draft.setExpectedImpact(inputDTO.getExpectedImpact());
        draft.setTargetImplementationDate(inputDTO.getTargetImplementationDate());
        draft.setPriority(inputDTO.getPriority());
        draft.setCategory(inputDTO.getCategory());
        
        if (inputDTO.getTags() != null) {
            draft.setTags(new ArrayList<>(inputDTO.getTags()));
        }
        if (inputDTO.getStakeholders() != null) {
            draft.setStakeholders(new ArrayList<>(inputDTO.getStakeholders()));
        }

        return draft;
    }

    /**

     * Update entity from input DTO (for updates).

     */
    public void updateEntity(PolicyDraft draft, PolicyDraftInputDTO inputDTO) {
        if (draft == null || inputDTO == null) {
            return;
        }

        draft.setName(inputDTO.getName());
        draft.setDescription(inputDTO.getDescription());
        draft.setBasePolicyId(inputDTO.getBasePolicyId());
        draft.setConditionLogic(inputDTO.getConditionLogic());
        draft.setRulesDefinition(inputDTO.getRulesDefinition());
        draft.setActions(inputDTO.getActions());
        draft.setChangeSummary(inputDTO.getChangeSummary());
        draft.setJustification(inputDTO.getJustification());
        draft.setExpectedImpact(inputDTO.getExpectedImpact());
        draft.setTargetImplementationDate(inputDTO.getTargetImplementationDate());
        draft.setPriority(inputDTO.getPriority());
        draft.setCategory(inputDTO.getCategory());
        
        if (inputDTO.getTags() != null) {
            draft.getTags().clear();
            draft.getTags().addAll(inputDTO.getTags());
        }
        if (inputDTO.getStakeholders() != null) {
            draft.getStakeholders().clear();
            draft.getStakeholders().addAll(inputDTO.getStakeholders());
        }
    }

    /**

     * Create workflow status information for the draft.

     */
    private PolicyDraftOutputDTO.WorkflowStatusDTO createWorkflowStatus(PolicyDraft draft) {
        PolicyDraftOutputDTO.WorkflowStatusDTO status = new PolicyDraftOutputDTO.WorkflowStatusDTO();
        
        PolicyDraftStatus currentStatus = draft.getStatus();
        status.setCurrentStage(currentStatus.name());
        status.setStatusDescription(getStatusDescription(currentStatus));
        status.setNextPossibleStates(getNextPossibleStates(currentStatus));
        status.setStageColor(getStageColor(currentStatus));
        
        // Calculate days in current status
        Instant statusChangeTime = getLastStatusChangeTime(draft, currentStatus);
        if (statusChangeTime != null) {
            long days = ChronoUnit.DAYS.between(statusChangeTime, Instant.now());
            status.setDaysInCurrentStatus((int) days);
        }

        // Set permission flags (simplified - would integrate with security context in real implementation)
        status.setCanEdit(canEdit(currentStatus));
        status.setCanSubmit(canSubmit(currentStatus));
        status.setCanApprove(canApprove(currentStatus));
        status.setCanReject(canReject(currentStatus));
        status.setCanPublish(canPublish(currentStatus));

        return status;
    }

    /**

     * Create available workflow actions for the draft.

     */
    private List<PolicyDraftOutputDTO.WorkflowActionDTO> createAvailableActions(PolicyDraft draft) {
        List<PolicyDraftOutputDTO.WorkflowActionDTO> actions = new ArrayList<>();
        PolicyDraftStatus status = draft.getStatus();

        switch (status) {
            case CREATED:
            case REQUIRES_CHANGES:
                actions.add(new PolicyDraftOutputDTO.WorkflowActionDTO("SUBMIT", "Submit for Review"));
                actions.add(new PolicyDraftOutputDTO.WorkflowActionDTO("ARCHIVE", "Archive Draft"));
                break;
            case SUBMITTED:
            case UNDER_REVIEW:
                actions.add(new PolicyDraftOutputDTO.WorkflowActionDTO("APPROVE", "Approve Draft"));
                actions.add(new PolicyDraftOutputDTO.WorkflowActionDTO("REJECT", "Reject Draft"));
                actions.add(new PolicyDraftOutputDTO.WorkflowActionDTO("REQUEST_CHANGES", "Request Changes"));
                break;
            case APPROVED:
                actions.add(new PolicyDraftOutputDTO.WorkflowActionDTO("PUBLISH", "Publish Policy"));
                actions.add(new PolicyDraftOutputDTO.WorkflowActionDTO("REJECT", "Reject Draft"));
                break;
            default:
                // No actions available for REJECTED, PUBLISHED, ARCHIVED
                break;
        }

        // Set action properties
        for (PolicyDraftOutputDTO.WorkflowActionDTO action : actions) {
            setActionProperties(action);
        }

        return actions;
    }

    /**

     * Get human-readable description for status.

     */
    private String getStatusDescription(PolicyDraftStatus status) {
        switch (status) {
            case CREATED:
                return "Draft has been created and is ready for editing";
            case SUBMITTED:
                return "Draft has been submitted and is awaiting review";
            case UNDER_REVIEW:
                return "Draft is currently being reviewed";
            case REQUIRES_CHANGES:
                return "Draft requires changes based on reviewer feedback";
            case APPROVED:
                return "Draft has been approved and is ready for publication";
            case REJECTED:
                return "Draft has been rejected and will not be published";
            case PUBLISHED:
                return "Draft has been published as an active policy";
            case ARCHIVED:
                return "Draft has been archived and is no longer active";
            default:
                return "Unknown status";
        }
    }

    /**

     * Get next possible states for workflow transitions.

     */
    private List<String> getNextPossibleStates(PolicyDraftStatus status) {
        switch (status) {
            case CREATED:
            case REQUIRES_CHANGES:
                return Arrays.asList("SUBMITTED", "ARCHIVED");
            case SUBMITTED:
                return Arrays.asList("UNDER_REVIEW", "APPROVED", "REJECTED", "REQUIRES_CHANGES");
            case UNDER_REVIEW:
                return Arrays.asList("APPROVED", "REJECTED", "REQUIRES_CHANGES");
            case APPROVED:
                return Arrays.asList("PUBLISHED", "REJECTED");
            default:
                return new ArrayList<>(); // Terminal states
        }
    }

    /**

     * Get color for UI status indicators.

     */
    private String getStageColor(PolicyDraftStatus status) {
        switch (status) {
            case CREATED:
                return "blue";
            case SUBMITTED:
            case UNDER_REVIEW:
                return "orange";
            case REQUIRES_CHANGES:
                return "yellow";
            case APPROVED:
                return "green";
            case REJECTED:
                return "red";
            case PUBLISHED:
                return "purple";
            case ARCHIVED:
                return "gray";
            default:
                return "gray";
        }
    }

    /**

     * Get the timestamp of last status change.

     */
    private Instant getLastStatusChangeTime(PolicyDraft draft, PolicyDraftStatus currentStatus) {
        switch (currentStatus) {
            case SUBMITTED:
                return draft.getSubmittedAt();
            case APPROVED:
                return draft.getApprovedAt();
            case REJECTED:
                return draft.getRejectedAt();
            case PUBLISHED:
                return draft.getPublishedAt();
            default:
                return draft.getUpdatedAt();
        }
    }

    /**

     * Check if draft can be edited in current status.

     */
    private boolean canEdit(PolicyDraftStatus status) {
        return status == PolicyDraftStatus.CREATED || status == PolicyDraftStatus.REQUIRES_CHANGES;
    }

    /**

     * Check if draft can be submitted in current status.

     */
    private boolean canSubmit(PolicyDraftStatus status) {
        return status == PolicyDraftStatus.CREATED || status == PolicyDraftStatus.REQUIRES_CHANGES;
    }

    /**

     * Check if draft can be approved in current status.

     */
    private boolean canApprove(PolicyDraftStatus status) {
        return status == PolicyDraftStatus.SUBMITTED || status == PolicyDraftStatus.UNDER_REVIEW;
    }

    /**

     * Check if draft can be rejected in current status.

     */
    private boolean canReject(PolicyDraftStatus status) {
        return status == PolicyDraftStatus.SUBMITTED || 
               status == PolicyDraftStatus.UNDER_REVIEW || 
               status == PolicyDraftStatus.APPROVED;
    }

    /**

     * Check if draft can be published in current status.

     */
    private boolean canPublish(PolicyDraftStatus status) {
        return status == PolicyDraftStatus.APPROVED;
    }

    /**

     * Set properties for workflow actions.

     */
    private void setActionProperties(PolicyDraftOutputDTO.WorkflowActionDTO action) {
        switch (action.getActionType()) {
            case "SUBMIT":
                action.setActionDescription("Submit the draft for review by approvers");
                action.setRequiresComment(false);
                action.setConfirmationMessage("Are you sure you want to submit this draft for review?");
                action.setButtonStyle("primary");
                break;
            case "APPROVE":
                action.setActionDescription("Approve the draft for publication");
                action.setRequiresComment(false);
                action.setConfirmationMessage("Are you sure you want to approve this draft?");
                action.setButtonStyle("success");
                break;
            case "REJECT":
                action.setActionDescription("Reject the draft and prevent publication");
                action.setRequiresComment(true);
                action.setConfirmationMessage("Are you sure you want to reject this draft? This action cannot be undone.");
                action.setButtonStyle("danger");
                break;
            case "REQUEST_CHANGES":
                action.setActionDescription("Request changes from the draft author");
                action.setRequiresComment(true);
                action.setConfirmationMessage("Please provide details about the required changes");
                action.setButtonStyle("warning");
                break;
            case "PUBLISH":
                action.setActionDescription("Publish the draft as an active policy");
                action.setRequiresComment(false);
                action.setConfirmationMessage("Are you sure you want to publish this draft as an active policy?");
                action.setButtonStyle("success");
                break;
            case "ARCHIVE":
                action.setActionDescription("Archive the draft");
                action.setRequiresComment(false);
                action.setConfirmationMessage("Are you sure you want to archive this draft?");
                action.setButtonStyle("secondary");
                break;
        }
    }
}