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 * Contributors:
014 * Nuxeo - initial API and implementation
015 */
016
017package org.nuxeo.ecm.platform.rendition.impl;
018
019import java.io.File;
020import java.util.Calendar;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.api.Blob;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.PropertyException;
027import org.nuxeo.ecm.platform.rendition.Rendition;
028import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
029
030/**
031 * Base implementation of the {@link Rendition} interface that mainly wrapps the {@link RenditionDefinition}
032 *
033 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
034 */
035public abstract class AbstractRendition implements Rendition {
036
037    protected final RenditionDefinition definition;
038
039    protected static final Log log = LogFactory.getLog(AbstractRendition.class);
040
041    public AbstractRendition(RenditionDefinition definition) {
042        this.definition = definition;
043    }
044
045    @Override
046    public String getIcon() {
047        return definition.getIcon();
048    }
049
050    @Override
051    public String getName() {
052        return definition.getName();
053    }
054
055    @Override
056    public String getCmisName() {
057        return definition.getCmisName();
058    }
059
060    @Override
061    public String getLabel() {
062        return definition.getLabel();
063    }
064
065    @Override
066    public String getKind() {
067        return definition.getKind();
068    }
069
070    protected RenditionDefinition getDefinition() {
071        return definition;
072    }
073
074    @Override
075    public Calendar getModificationDate() {
076        if (isStored()) {
077            DocumentModel hdoc = getHostDocument();
078            if (hdoc != null) {
079                try {
080                    return (Calendar) hdoc.getPropertyValue("dc:modified");
081                } catch (PropertyException e) {
082                    log.error(e);
083                }
084            }
085        } else if (isCompleted()) {
086            Calendar cal = Calendar.getInstance();
087            Blob blob = getBlob();
088            if (blob != null) {
089                File file = blob.getFile();
090                if (file != null) {
091                    cal.setTimeInMillis(file.lastModified());
092                }
093            }
094            return cal;
095        }
096        return null;
097    }
098
099    @Override
100    public String getProviderType() {
101        return definition.getProviderType();
102    }
103}