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