001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.causeway.extensions.pdfjs.wkt.ui.components;
020
021import jakarta.inject.Inject;
022
023import org.apache.wicket.Component;
024import org.apache.wicket.model.IModel;
025
026import org.apache.causeway.applib.value.Blob;
027import org.apache.causeway.applib.value.NamedWithMimeType.CommonMimeType;
028import org.apache.causeway.core.metamodel.object.ManagedObject;
029import org.apache.causeway.extensions.pdfjs.metamodel.facet.PdfJsViewerFacet;
030import org.apache.causeway.viewer.commons.model.components.UiComponentType;
031import org.apache.causeway.viewer.wicket.model.models.UiAttributeWkt;
032import org.apache.causeway.viewer.wicket.ui.ComponentFactoryAbstract;
033
034@org.springframework.stereotype.Component
035public class PdfJsViewerPanelComponentFactory extends ComponentFactoryAbstract {
036
037    @Inject
038    public PdfJsViewerPanelComponentFactory() {
039        super(UiComponentType.ATTRIBUTE_NAME_AND_VALUE, PdfJsViewerPanel.class);
040    }
041
042    @Override
043    public ApplicationAdvice appliesTo(final IModel<?> model) {
044        if (!(model instanceof UiAttributeWkt)) {
045            return ApplicationAdvice.DOES_NOT_APPLY;
046        }
047
048        var attributeModel = (UiAttributeWkt) model;
049        if(!attributeModel.getMetaModel().containsFacet(PdfJsViewerFacet.class)) {
050            return ApplicationAdvice.DOES_NOT_APPLY;
051        }
052
053        return appliesIf(isPdf(attributeModel.getObject()));
054    }
055
056    private static boolean isPdf(final ManagedObject objectAdapter) {
057        if (objectAdapter == null) {
058            return false;
059        }
060        final Object objectPojo = objectAdapter.getPojo();
061        if (!(objectPojo instanceof Blob)) {
062            return false;
063        }
064        final Blob blob = (Blob) objectPojo;
065        return CommonMimeType.PDF.matches(blob.mimeType());
066    }
067
068    @Override
069    public Component createComponent(final String id, final IModel<?> model) {
070        return new PdfJsViewerPanel(id, (UiAttributeWkt) model);
071    }
072}