FileNET P8 5.2.1 FP2 - edit permissions on document creation

时间:2017-06-15 10:15:52

标签: java filenet-p8 filenet-content-engine

We're asked to set up permissions when a document is created.

Basically the code part written is reproduced below :

public void onEvent(ObjectChangeEvent event, Id eventId) {
    if (event instanceof CreationEvent) {
        Document doc = (Document) event.get_SourceObject();
        AccessPermissionList permissions = doc.get_Permissions();
        String creatorGranteeName = getCreatorGranteeName(doc);
        Iterator<AccessPermission> iter = permissions.iterator();
        boolean found = false;
        while (iter.hasNext()) {
            AccessPermission ace = (AccessPermission) iter.next();
            if (ace.get_GranteeName().equals(creatorGranteeName)) {
                permissions.remove(ace);
                // relevant ? is "permission" duplicated ?
                doc.set_Permissions(permissions);
                break;
            }
        }
        if (!found) return ; // no need to save
        doc.save(RefreshMode.REFRESH); // --> triggers CreationEvent -> loop
        System.out.println("Saved."); // never reached
    }
}

I tried two ways : a preprocessor or a subscription.

Preprocessor doesn't work since the document doesn't seems to be fully built, especially about permissions (only administrators are set). Fetching doesn't seem to work (which is understandable, since the document isn't yet stored).

Susbcription crashes if it's synchronously processed at doc.save() line, regardless if the refresh mode is RefreshMode.REFRESH or RefreshMode.NO_REFRESH. If it's asynchrounsly done, it seems to loop, as if doc.save retriggers a CreationEvent.

So I'm looking for a help if I did something wrong, or for a third way if it exists.

EDIT : added block code which skips saving if no permissions to remove was found.

2 个答案:

答案 0 :(得分:1)

由于声誉不能发表评论,我必须回答 你试过吗

doc.checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);

之前doc.save()

更新回答

您可以尝试使用

而不是event.get_SourceObject();
`changeEvent.get_SourceObjectId(); 
Document doc= Factory.Document.fetchInstance(os, documentId, propertyFilter);`

答案 1 :(得分:1)

正如@Manjunatha Muniyappa所说,我通过从Object存储中获取文档而不是从CreationEvent对象获取文档来解决我的问题。 It seems to be recommended by the editor“作为最佳做法,获取事件的持久源对象”)。同样通过这种方式不会引发CreationEvent(我不知道为什么)。

所以解决方法是在Creation Event上创建一个异步订阅,与下面的处理程序类相关联:

// Only relevant lines are kept.
public class CustomEventAction implements EventActionHandler {
    // [...]
    public void onEvent(ObjectChangeEvent event, Id eventId) {
        if (event instanceof CreationEvent) {
            ObjectStore os = event.getObjectStore();
            Id id = event.get_SourceObjectId();
            FilterElement fe = 
                    new FilterElement(null, null, null, "permissions creator", null);
            PropertyFilter pf = new PropertyFilter();
            pf.addIncludeProperty(fe);
            Document doc = Factory.Document.fetchInstance(os, id, pf);

            AccessPermissionList permissions;

            String creatorGranteeName = getCreatorGranteeName(doc);
            permissions = doc.get_Permissions();
            Iterator<AccessPermission> iter = permissions.iterator();

            boolean found = false;
            while (iter.hasNext()) {
                AccessPermission ace = (AccessPermission) iter.next();
                if (ace.get_GranteeName().equals(creatorGranteeName)) {
                    permissions.remove(ace);
                    found = true;
                    break;
                }
            }

            if (!found) {
                return;
            }

            doc.save(RefreshMode.REFRESH);
        }
    }
}