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 *     Tiago Cardoso <tcardoso@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.threed.adapter;
020
021import org.nuxeo.ecm.core.api.Blob;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
024import org.nuxeo.ecm.platform.threed.*;
025
026import java.io.Serializable;
027import java.util.Collection;
028import java.util.List;
029import java.util.Map;
030import java.util.stream.Collectors;
031
032import static org.nuxeo.ecm.platform.threed.ThreeDDocumentConstants.RENDER_VIEWS_PROPERTY;
033import static org.nuxeo.ecm.platform.threed.ThreeDDocumentConstants.TRANSMISSIONS_PROPERTY;
034import static org.nuxeo.ecm.platform.threed.ThreeDRenderView.TITLE;
035import static org.nuxeo.ecm.platform.threed.TransmissionThreeD.NAME;
036
037/**
038 * Default implementation of {@link ThreeDDocument}.
039 *
040 * @since 8.4
041 */
042public class ThreeDDocumentAdapter implements ThreeDDocument {
043
044    final DocumentModel docModel;
045
046    public ThreeDDocumentAdapter(DocumentModel threed) {
047        docModel = threed;
048
049    }
050
051    @Override
052    public ThreeD getThreeD() {
053        BlobHolder bh = docModel.getAdapter(BlobHolder.class);
054        List<Blob> resources = ((List<Map<String, Object>>) docModel.getPropertyValue(
055                "files:files")).stream().map(file -> (Blob) file.get("file")).collect(Collectors.toList());
056        Map<String, Serializable> infoMap = (Map<String, Serializable>) docModel.getPropertyValue("threed:info");
057        ThreeDInfo info = (infoMap != null) ? new ThreeDInfo(infoMap) : null;
058        return new ThreeD(bh.getBlob(), resources, info);
059    }
060
061    @SuppressWarnings("unchecked")
062    @Override
063    public Collection<TransmissionThreeD> getTransmissionThreeDs() {
064        List<Map<String, Serializable>> list = (List<Map<String, Serializable>>) docModel.getPropertyValue(
065                TRANSMISSIONS_PROPERTY);
066        return list.stream().map(TransmissionThreeD::new).collect(Collectors.toList());
067    }
068
069    @SuppressWarnings("unchecked")
070    @Override
071    public TransmissionThreeD getTransmissionThreeD(String name) {
072        List<Map<String, Serializable>> list = (List<Map<String, Serializable>>) docModel.getPropertyValue(
073                TRANSMISSIONS_PROPERTY);
074        return list.stream()
075                   .filter(item -> ((String) item.get(NAME)) != null && name.equals(item.get(NAME)))
076                   .map(TransmissionThreeD::new)
077                   .findFirst()
078                   .orElse(null);
079    }
080
081    @SuppressWarnings("unchecked")
082    @Override
083    public Collection<ThreeDRenderView> getRenderViews() {
084        List<Map<String, Serializable>> list = (List<Map<String, Serializable>>) docModel.getPropertyValue(
085                RENDER_VIEWS_PROPERTY);
086        return list.stream().map(ThreeDRenderView::new).collect(Collectors.toList());
087    }
088
089    @SuppressWarnings("unchecked")
090    @Override
091    public ThreeDRenderView getRenderView(String title) {
092        List<Map<String, Serializable>> list = (List<Map<String, Serializable>>) docModel.getPropertyValue(
093                RENDER_VIEWS_PROPERTY);
094        return list.stream()
095                   .filter(item -> title.equals(item.get(TITLE)))
096                   .map(ThreeDRenderView::new)
097                   .findFirst()
098                   .orElse(null);
099    }
100}