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