001/*
002 * (C) Copyright 2006-2011 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
018 */
019package org.nuxeo.ecm.platform.picture.operation;
020
021import org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Operation;
023import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
024import org.nuxeo.ecm.automation.core.annotations.Param;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.picture.api.PictureView;
028import org.nuxeo.ecm.platform.picture.api.adapters.MultiviewPicture;
029
030/**
031 * Simple Operation to extract an image view from a Picture Document.
032 * <p>
033 * This operation is needed because using the default blob operation is too complicated in the case of the Picture
034 * DocumentType.
035 *
036 * @author Tiry (tdelprat@nuxeo.com)
037 */
038@Operation(id = GetPictureView.ID, category = Constants.CAT_CONVERSION, label = "Get image view", description = "Get an image from a Picture document.", aliases = { "Picture.getView" })
039public class GetPictureView {
040
041    public static final String ID = "Picture.GetView";
042
043    @Param(name = "viewName", required = false)
044    protected String viewName;
045
046    @OperationMethod
047    public Blob run(DocumentModel doc) {
048
049        MultiviewPicture mvp = doc.getAdapter(MultiviewPicture.class);
050
051        if (mvp == null) {
052            return null;
053        }
054
055        if (viewName == null) {
056            viewName = mvp.getOrigin();
057        }
058
059        PictureView pv = mvp.getView(viewName);
060
061        if (pv == null) {
062            return null;
063        }
064
065        return pv.getBlob();
066    }
067
068}