001/*
002 * (C) Copyright 2006-2008 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.pictures.tiles.api.adapter;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.PropertyException;
029import org.nuxeo.ecm.core.api.adapter.DocumentAdapterFactory;
030import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter;
031import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTilingService;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Factory method for the DocumentModelAdapter for PictureTiles Contains the logic to choose the correct implementation
036 * according to DocumentModel.
037 *
038 * @author tiry
039 */
040public class PictureTilesAdapterFactory implements DocumentAdapterFactory {
041
042    private static final Log log = LogFactory.getLog(PictureTilesAdapterFactory.class);
043
044    protected static final String ORIGINAL_JPEG_VIEW_NAME = "OriginalJpeg";
045
046    /**
047     * @deprecated since 7.2. The Original view does not exist anymore. See NXP-16070.
048     */
049    @Deprecated
050    protected static final String ORIGINAL_VIEW_NAME = "Original";
051
052    public Object getAdapter(DocumentModel doc, Class itf) {
053        PictureTilingService tilingService = Framework.getService(PictureTilingService.class);
054        String blobProperty = tilingService.getBlobProperty(doc.getType());
055        PictureTilesAdapter ptAdapter = getPictureTilesAdapterFor(doc, blobProperty);
056        if (ptAdapter != null) {
057            return ptAdapter;
058        }
059        // else fall back on default
060        if (doc.hasSchema("file")) {
061            Blob blob = (Blob) doc.getProperty("file", "content");
062            if (blob == null) {
063                return null;
064            }
065            PictureTilesAdapter adapter = new PictureTilesAdapterImpl(doc, "file:content");
066            adapter.setFileName((String) doc.getProperty("file", "filename"));
067            return adapter;
068        } else {
069            return new PictureTilesAdapterImpl(doc);
070        }
071    }
072
073    private PictureTilesAdapter getPictureTilesAdapterFor(DocumentModel doc, String blobProperty)
074            {
075        if (blobProperty != null) {
076            try {
077                return getPictureTilesAdapter(doc, blobProperty);
078            } catch (PropertyException | IndexOutOfBoundsException e) {
079                return getPictureTilesAdapterForPicture(doc);
080            }
081        }
082        return getPictureTilesAdapterForPicture(doc);
083    }
084
085    private PictureTilesAdapter getPictureTilesAdapterForPicture(DocumentModel doc) {
086        if (doc.hasSchema("picture")) {
087            PictureResourceAdapter adapter = doc.getAdapter(PictureResourceAdapter.class);
088            // try OriginalJpeg view xpath
089            String blobProperty = adapter.getViewXPath(ORIGINAL_JPEG_VIEW_NAME) + "content";
090            return getPictureTilesAdapter(doc, blobProperty);
091        }
092        return null;
093    }
094
095    private PictureTilesAdapter getPictureTilesAdapter(DocumentModel doc, String blobProperty) {
096        Blob blob = (Blob) doc.getPropertyValue(blobProperty);
097        if (blob != null) {
098            PictureTilesAdapter adapter = new PictureTilesAdapterImpl(doc, blobProperty);
099            adapter.setFileName(blob.getFilename());
100            return adapter;
101        }
102        return null;
103    }
104
105}