刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
package jnpf.flowable.util.filepermission;
 
import jnpf.flowable.model.filepermission.FilePermissionPolicy.Action;
import java.util.*;
 
/** 原生保存前的附件引用差异;调用者仍负责从可信旧值提取主子行及验证新附件来源。 */
public final class FileAttachmentChanges {
    private FileAttachmentChanges() { }
 
    public static Set<Action> classify(Collection<String> before, Collection<String> after) {
        Set<String> oldIds = ids(before);
        Set<String> newIds = ids(after);
        Set<String> removed = new HashSet<>(oldIds);
        removed.removeAll(newIds);
        Set<String> added = new HashSet<>(newIds);
        added.removeAll(oldIds);
        Set<Action> changes = EnumSet.noneOf(Action.class);
        if (!removed.isEmpty() && !added.isEmpty()) changes.add(Action.REPLACE);
        if (added.size() > removed.size()) changes.add(Action.ADD);
        if (removed.size() > added.size()) changes.add(Action.REMOVE);
        return Collections.unmodifiableSet(changes);
    }
 
    /** 编辑另存的可信采用需先由工作稿服务验证,不能把客户端 fileId 替换认作 EDIT。 */
    public static void requireAllowed(Collection<String> before, Collection<String> after, Map<Action, Boolean> actions) {
        Objects.requireNonNull(actions, "actions");
        for (Action action : classify(before, after)) {
            if (!Boolean.TRUE.equals(actions.get(action))) throw new IllegalArgumentException("没有附件操作权限: " + action);
        }
    }
 
    private static Set<String> ids(Collection<String> files) {
        Objects.requireNonNull(files, "files");
        Set<String> result = new HashSet<>();
        for (String file : files) {
            if (file == null || file.trim().isEmpty() || !result.add(file)) {
                throw new IllegalArgumentException("附件标识为空或重复");
            }
        }
        return result;
    }
}