001/*
002 * (C) Copyright 2006 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 *     Florent Guillaume
018 */
019package org.nuxeo.project.sample;
020
021import java.text.SimpleDateFormat;
022import java.util.Date;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.event.Event;
026import org.nuxeo.ecm.core.event.EventContext;
027import org.nuxeo.ecm.core.event.EventListener;
028import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
029
030public class BookEventListener implements EventListener {
031
032    @Override
033    public void handleEvent(Event event) {
034
035        EventContext ctx = event.getContext();
036
037        if (ctx instanceof DocumentEventContext) {
038
039            DocumentEventContext docCtx = (DocumentEventContext) ctx;
040            DocumentModel doc = docCtx.getSourceDocument();
041
042            if (doc != null) {
043                String type = doc.getType();
044                if ("Book".equals(type)) {
045                    process(doc);
046                }
047            }
048        }
049    }
050
051    public void process(DocumentModel doc) {
052        doc.setPropertyValue("dublincore:title", "Sample Book");
053        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
054        doc.setPropertyValue("dublincore:description", "(Created on " + date + ")");
055    }
056
057}