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