001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     pierre
018 */
019package org.nuxeo.ecm.core.test;
020
021import java.util.Date;
022
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.PathRef;
026import org.nuxeo.ecm.core.api.security.ACE;
027import org.nuxeo.ecm.core.api.security.ACL;
028import org.nuxeo.ecm.core.api.security.ACP;
029import org.nuxeo.ecm.core.api.security.impl.ACLImpl;
030import org.nuxeo.ecm.core.api.security.impl.ACPImpl;
031
032/**
033 * @since 10.2
034 */
035public class DocumentSetRepositoryInit extends DefaultRepositoryInit {
036
037    public static final String USERNAME = "bob";
038
039    public static final String ROOT = "/default-domain/workspaces/test";
040
041    public static final int SIZE = 3;
042
043    public static final int DOC_BY_SIZE = 5;
044
045    public static final int DOC_BY_LEVEL = SIZE * DOC_BY_SIZE;
046
047    public static final int CREATED_NON_PROXY;
048
049    public static final int CREATED_PROXY;
050
051    public static final int CREATED_TOTAL;
052
053    static {
054        // with SIZE = 3 AND DOC_BY_SIZE = 5, 195 documents are created
055        int total = 0;
056        for (int i = 1; i <= SIZE; i++) {
057            total += DOC_BY_SIZE * Math.pow(SIZE, i);
058        }
059        CREATED_TOTAL = total;
060        CREATED_PROXY = total / DOC_BY_SIZE;
061        CREATED_NON_PROXY = total / DOC_BY_SIZE * (DOC_BY_SIZE - 1);
062    }
063
064    @Override
065    public void populate(CoreSession session) {
066        super.populate(session);
067        DocumentModel test = session.getDocument(new PathRef(ROOT));
068        ACP acp = new ACPImpl();
069        ACL acl = new ACLImpl();
070        acl.add(new ACE("Administrator", "Everything", true));
071        acl.add(new ACE(USERNAME, "WriteProperties", true));
072        acl.add(new ACE(USERNAME, "Read", true));
073        acp.addACL(acl);
074        test.setACP(acp, false);
075        createChildren(session, test, SIZE);
076    }
077
078    private void createChildren(CoreSession s, DocumentModel p, int depth) {
079        if (depth > 0) {
080            for (int i = 0; i < SIZE; i++) {
081                s.createDocument(s.createDocumentModel(p.getPathAsString(), p.getName() + "file" + i, "File"));
082                s.createDocument(s.createDocumentModel(p.getPathAsString(), p.getName() + "note" + i, "Note"));
083                DocumentModel child = s.createDocumentModel(p.getPathAsString(), p.getName() + "doc" + i, "ComplexDoc");
084                child.setPropertyValue("dc:modified", new Date());
085                child.setPropertyValue("dc:nature", "article");
086                child.setPropertyValue("dc:contributors", new String[] { "bob", "Administrator" });
087                child = s.createDocument(child);
088                s.createProxy(child.getRef(), p.getRef());
089                DocumentModel folder = s.createDocumentModel(p.getPathAsString(), p.getName() + "folder" + i, "Folder");
090                folder = s.createDocument(folder);
091                createChildren(s, folder, depth - 1);
092            }
093        }
094    }
095}