001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Guillaume Renard
016 */
017package org.nuxeo.ecm.platform.forms.layout.demo.factories;
018
019import org.nuxeo.ecm.core.api.DocumentModel;
020import org.nuxeo.ecm.platform.content.template.factories.SimpleTemplateBasedFactory;
021import org.nuxeo.ecm.platform.content.template.service.TemplateItemDescriptor;
022
023/**
024 * Create a Folder with a couple of documents with titles starting with 'aaa'.
025 *
026 * @since 5.7.2
027 */
028public class DemoFolderTemplateFactory extends SimpleTemplateBasedFactory {
029
030    private final static String[] TOKENS = { "aaaabb", "aaaccc", "aaaddd", "aaaaeee", "aaaafff", "aaaggg", "aaahhh",
031            "aaaiii", "aaaajjj", "aaakkk", };
032
033    @Override
034    public void createContentStructure(DocumentModel eventDoc) {
035        initSession(eventDoc);
036
037        if (eventDoc.isVersion() || !isTargetEmpty(eventDoc)) {
038            return;
039        }
040
041        setAcl(acl, eventDoc.getRef());
042
043        DocumentModel newChild = null;
044
045        char a = 'a';
046        for (TemplateItemDescriptor item : template) {
047            String itemPath = eventDoc.getPathAsString();
048            if (item.getPath() != null) {
049                itemPath += "/" + item.getPath();
050            }
051            newChild = session.createDocumentModel(itemPath, item.getId(), item.getTypeName());
052            newChild.setProperty("dublincore", "title", item.getTitle());
053            newChild.setProperty("dublincore", "description", item.getDescription());
054            setProperties(item.getProperties(), newChild);
055            newChild = session.createDocument(newChild);
056            setAcl(item.getAcl(), newChild.getRef());
057
058            if (newChild.isFolder()) {
059                DocumentModel newGrantChild = session.createDocumentModel(newChild.getPathAsString() + "/",
060                        "defaultId", "File");
061                newGrantChild.setProperty("dublincore", "title", "Some sample text");
062                newGrantChild = session.createDocument(newGrantChild);
063                for (String token : TOKENS) {
064                    final String id = token + a;
065                    newGrantChild = session.createDocumentModel(newChild.getPathAsString() + "/", id, "File");
066                    newGrantChild.setProperty("dublincore", "title", id);
067                    newGrantChild = session.createDocument(newGrantChild);
068                }
069            }
070            a++;
071        }
072    }
073
074}