001/*
002 * (C) Copyright 2013 Nuxeo SA (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 *     Guillaume Renard
018 */
019package org.nuxeo.ecm.platform.forms.layout.demo.factories;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.platform.content.template.factories.SimpleTemplateBasedFactory;
023import org.nuxeo.ecm.platform.content.template.service.TemplateItemDescriptor;
024
025/**
026 * Create a Folder with a couple of documents with titles starting with 'aaa'.
027 *
028 * @since 5.7.2
029 */
030public class DemoFolderTemplateFactory extends SimpleTemplateBasedFactory {
031
032    private final static String[] TOKENS = { "aaaabb", "aaaccc", "aaaddd", "aaaaeee", "aaaafff", "aaaggg", "aaahhh",
033            "aaaiii", "aaaajjj", "aaakkk", };
034
035    @Override
036    public void createContentStructure(DocumentModel eventDoc) {
037        initSession(eventDoc);
038
039        if (eventDoc.isVersion() || !isTargetEmpty(eventDoc)) {
040            return;
041        }
042
043        setAcl(acl, eventDoc.getRef());
044
045        DocumentModel newChild = null;
046
047        char a = 'a';
048        for (TemplateItemDescriptor item : template) {
049            String itemPath = eventDoc.getPathAsString();
050            if (item.getPath() != null) {
051                itemPath += "/" + item.getPath();
052            }
053            newChild = session.createDocumentModel(itemPath, item.getId(), item.getTypeName());
054            newChild.setProperty("dublincore", "title", item.getTitle());
055            newChild.setProperty("dublincore", "description", item.getDescription());
056            setProperties(item.getProperties(), newChild);
057            newChild = session.createDocument(newChild);
058            setAcl(item.getAcl(), newChild.getRef());
059
060            if (newChild.isFolder()) {
061                DocumentModel newGrantChild = session.createDocumentModel(newChild.getPathAsString() + "/",
062                        "defaultId", "File");
063                newGrantChild.setProperty("dublincore", "title", "Some sample text");
064                newGrantChild = session.createDocument(newGrantChild);
065                for (String token : TOKENS) {
066                    final String id = token + a;
067                    newGrantChild = session.createDocumentModel(newChild.getPathAsString() + "/", id, "File");
068                    newGrantChild.setProperty("dublincore", "title", id);
069                    newGrantChild = session.createDocument(newGrantChild);
070                }
071            }
072            a++;
073        }
074    }
075
076}