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.listener;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
029import org.nuxeo.ecm.core.event.Event;
030import org.nuxeo.ecm.core.event.EventListener;
031import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
032import org.nuxeo.ecm.platform.content.template.service.ContentTemplateService;
033import org.nuxeo.runtime.api.Framework;
034
035public class ContentCreationListener implements EventListener {
036
037    private static final Log log = LogFactory.getLog(ContentCreationListener.class);
038
039    @Override
040    public void handleEvent(Event event) {
041
042        DocumentEventContext docCtx = null;
043        if (event.getContext() instanceof DocumentEventContext) {
044            docCtx = (DocumentEventContext) event.getContext();
045        } else {
046            return;
047        }
048        String eventId = event.getName();
049        DocumentModel createdDocument = docCtx.getSourceDocument();
050
051        if (eventId.equals(DocumentEventTypes.DOCUMENT_CREATED)) {
052            try {
053                Framework.getService(ContentTemplateService.class).executeFactoryForType(createdDocument);
054            } catch (NuxeoException e) {
055                log.error("Error while executing content factory for type " + createdDocument.getType(), e);
056            }
057        }
058    }
059
060}