001/*
002 * (C) Copyright 2010 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 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.platform.content.template.factories;
021
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.IterableQueryResult;
024import org.nuxeo.ecm.core.query.sql.NXQL;
025import org.nuxeo.ecm.platform.content.template.listener.RepositoryInitializationListener;
026import org.nuxeo.ecm.platform.content.template.service.TemplateItemDescriptor;
027
028/**
029 * Specific factory for Root. Since some other {@link RepositoryInitializationListener} have run before, root won't be
030 * empty but we may still have to run this initializer.
031 *
032 * @author Thierry Delprat
033 */
034public class SimpleTemplateBasedRootFactory extends SimpleTemplateBasedFactory {
035
036    @Override
037    public void createContentStructure(DocumentModel eventDoc) {
038        initSession(eventDoc);
039
040        if (!shouldCreateContent(eventDoc)) {
041            return;
042        }
043
044        for (TemplateItemDescriptor item : template) {
045            String itemPath = eventDoc.getPathAsString();
046            if (item.getPath() != null) {
047                itemPath += "/" + item.getPath();
048            }
049            DocumentModel newChild = session.createDocumentModel(itemPath, item.getId(), item.getTypeName());
050            newChild.setProperty("dublincore", "title", item.getTitle());
051            newChild.setProperty("dublincore", "description", item.getDescription());
052            setProperties(item.getProperties(), newChild);
053            newChild = session.createDocument(newChild);
054            setAcl(item.getAcl(), newChild.getRef());
055        }
056        // init root ACL if really empty
057        setAcl(acl, eventDoc.getRef());
058    }
059
060    /**
061     * Returns {@code false} if the type of one of the children documents matches a template item type, {@code true}
062     * otherwise.
063     */
064    protected boolean shouldCreateContent(DocumentModel eventDoc) {
065        for (TemplateItemDescriptor item : template) {
066            // don't use getChildren, which can be costly
067            // if the folder has a huge number of children
068            String query = "SELECT ecm:uuid FROM Document WHERE ecm:parentId = '%s' AND ecm:primaryType = '%s'";
069            query = String.format(query, eventDoc.getId(), item.getTypeName());
070            IterableQueryResult it = session.queryAndFetch(query, NXQL.NXQL);
071            try {
072                if (it.iterator().hasNext()) {
073                    return false;
074                }
075            } finally {
076                it.close();
077            }
078        }
079        return true;
080    }
081}