001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
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
017package com.echothree.model.control.document.server.logic;
018
019import com.echothree.model.control.core.common.EntityAttributeTypes;
020import com.echothree.model.control.core.common.MimeTypes;
021import com.echothree.model.control.document.server.control.DocumentControl;
022import com.echothree.model.data.core.server.entity.MimeType;
023import com.echothree.model.data.document.server.entity.Document;
024import com.echothree.model.data.document.server.entity.DocumentType;
025import com.echothree.model.data.document.server.entity.PartyDocument;
026import com.echothree.model.data.party.common.pk.PartyPK;
027import com.echothree.model.data.party.server.entity.Language;
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.persistence.type.ByteArray;
031import com.echothree.util.server.control.BaseLogic;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.Session;
034import com.lowagie.text.pdf.PdfReader;
035import java.io.IOException;
036import javax.enterprise.context.ApplicationScoped;
037import javax.enterprise.inject.spi.CDI;
038
039@ApplicationScoped
040public class DocumentLogic
041        extends BaseLogic {
042
043    protected DocumentLogic() {
044        super();
045    }
046
047    public static DocumentLogic getInstance() {
048        return CDI.current().select(DocumentLogic.class).get();
049    }
050
051    public Integer getPages(final MimeType mimeType, final ByteArray blob, final String clob) {
052        Integer pages = null;
053        var entityAttributeTypeName = mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
054
055        if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) {
056            var mimeTypeName = mimeType.getLastDetail().getMimeTypeName();
057
058            if(mimeTypeName.equals(MimeTypes.APPLICATION_PDF.mimeTypeName()) && blob != null) {
059                try {
060                    var pdfReader = new PdfReader(blob.getByteArrayInputStream());
061
062                    pages = pdfReader.getNumberOfPages();
063                } catch(IOException ioe) {
064                    // Nothing, pages stays null.
065                }
066            }
067        }
068
069        return pages;
070    }
071
072    public Document createDocument(final ExecutionErrorAccumulator ema, final DocumentType documentType, final MimeType mimeType,
073            final Language preferredLanguage, final String description, final ByteArray blob, final String clob, final PartyPK createdBy) {
074        var pages = getPages(mimeType, blob, clob);
075        Document document = null;
076        var hasErrors = false;
077
078        if(pages != null) {
079            var documentTypeDetail = documentType.getLastDetail();
080            var maximumPages = documentTypeDetail.getMaximumPages();
081
082            if(maximumPages != null) {
083                if(pages > maximumPages) {
084                    hasErrors = true;
085                    addExecutionError(ema, ExecutionErrors.DocumentExceedesMaximumPages.name());
086                }
087            }
088        }
089
090        if(!hasErrors) {
091            var documentControl = Session.getModelController(DocumentControl.class);
092
093            document = documentControl.createDocument(documentType, mimeType, pages, createdBy);
094
095            if(blob != null) {
096                documentControl.createDocumentBlob(document, blob, createdBy);
097            } else if(clob != null) {
098                documentControl.createDocumentClob(document, clob, createdBy);
099            }
100
101            if(description != null) {
102                documentControl.createDocumentDescription(document, preferredLanguage, description, createdBy);
103            }
104        }
105
106        return document;
107    }
108
109    public PartyDocument createPartyDocument(final ExecutionErrorAccumulator ema, final Party party, final DocumentType documentType, final MimeType mimeType,
110            final Boolean isDefault, final Integer sortOrder,  final Language preferredLanguage, final String description, final ByteArray blob,
111            final String clob, final PartyPK createdBy) {
112        var documentControl = Session.getModelController(DocumentControl.class);
113        var document = createDocument(ema, documentType, mimeType, preferredLanguage, description, blob, clob, createdBy);
114
115        return document == null ? null : documentControl.createPartyDocument(party, document, isDefault, sortOrder, createdBy);
116    }
117
118}