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