001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Dragos Mihalache
011 *     Florent Guillaume
012 */
013
014package org.nuxeo.ecm.core.versioning;
015
016import org.nuxeo.ecm.core.api.DocumentModel;
017import org.nuxeo.ecm.core.api.facet.VersioningDocument;
018import org.nuxeo.runtime.api.Framework;
019
020/**
021 * Adapter showing the versioning aspects of documents.
022 */
023public class VersioningDocumentAdapter implements VersioningDocument {
024
025    public final DocumentModel doc;
026
027    public final VersioningService service;
028
029    public VersioningDocumentAdapter(DocumentModel doc) {
030        service = Framework.getService(VersioningService.class);
031        this.doc = doc;
032    }
033
034    @Override
035    public Long getMajorVersion() {
036        return Long.valueOf(getValidVersionNumber(VersioningService.MAJOR_VERSION_PROP));
037    }
038
039    @Override
040    public Long getMinorVersion() {
041        return Long.valueOf(getValidVersionNumber(VersioningService.MINOR_VERSION_PROP));
042    }
043
044    @Override
045    public String getVersionLabel() {
046        return service.getVersionLabel(doc);
047    }
048
049    private long getValidVersionNumber(String propName) {
050        Object propVal = doc.getPropertyValue(propName);
051        return (propVal == null || !(propVal instanceof Long)) ? 0 : ((Long) propVal).longValue();
052    }
053
054}