001/* 002 * (C) Copyright 2006-2007 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 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.content.template.factories; 021 022import java.util.List; 023import java.util.Map; 024 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.DocumentRef; 027import org.nuxeo.ecm.core.api.security.ACE; 028import org.nuxeo.ecm.core.api.security.ACL; 029import org.nuxeo.ecm.core.api.security.ACP; 030import org.nuxeo.ecm.platform.content.template.service.ACEDescriptor; 031import org.nuxeo.ecm.platform.content.template.service.PropertyDescriptor; 032import org.nuxeo.ecm.platform.content.template.service.TemplateItemDescriptor; 033 034public class SimpleTemplateBasedFactory extends BaseContentFactory { 035 036 protected List<TemplateItemDescriptor> template; 037 038 protected List<ACEDescriptor> acl; 039 040 protected boolean isTargetEmpty(DocumentModel eventDoc) { 041 // If we already have children : exit !!! 042 return session.getChildren(eventDoc.getRef()).isEmpty(); 043 } 044 045 public void createContentStructure(DocumentModel eventDoc) { 046 initSession(eventDoc); 047 048 if (eventDoc.isVersion() || !isTargetEmpty(eventDoc)) { 049 return; 050 } 051 052 setAcl(acl, eventDoc.getRef()); 053 054 for (TemplateItemDescriptor item : template) { 055 String itemPath = eventDoc.getPathAsString(); 056 if (item.getPath() != null) { 057 itemPath += "/" + item.getPath(); 058 } 059 DocumentModel newChild = session.createDocumentModel(itemPath, item.getId(), item.getTypeName()); 060 newChild.setProperty("dublincore", "title", item.getTitle()); 061 newChild.setProperty("dublincore", "description", item.getDescription()); 062 setProperties(item.getProperties(), newChild); 063 newChild = session.createDocument(newChild); 064 setAcl(item.getAcl(), newChild.getRef()); 065 } 066 } 067 068 protected void setProperties(List<PropertyDescriptor> properties, DocumentModel doc) { 069 if (properties != null && !properties.isEmpty()) { 070 for (PropertyDescriptor property : properties) { 071 doc.setPropertyValue(property.getXpath(), property.getValue()); 072 } 073 } 074 } 075 076 protected void setAcl(List<ACEDescriptor> aces, DocumentRef ref) { 077 if (aces != null && !aces.isEmpty()) { 078 ACP acp = session.getACP(ref); 079 ACL existingACL = acp.getOrCreateACL(); 080 081 // clean any existing ACL (should a merge strategy be adopted 082 // instead?) 083 existingACL.clear(); 084 085 // add the the ACL defined in the descriptor 086 for (ACEDescriptor ace : aces) { 087 existingACL.add(new ACE(ace.getPrincipal(), ace.getPermission(), ace.getGranted())); 088 } 089 // read the acl to invalidate the ACPImpl cache 090 acp.addACL(existingACL); 091 session.setACP(ref, acp, true); 092 } 093 } 094 095 public boolean initFactory(Map<String, String> options, List<ACEDescriptor> rootAcl, 096 List<TemplateItemDescriptor> template) { 097 this.template = template; 098 acl = rootAcl; 099 return true; 100 } 101 102}