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.util.server.persistence.translator;
018
019import com.echothree.model.control.core.server.control.CoreControl;
020import com.echothree.model.control.core.server.control.EntityInstanceControl;
021import com.echothree.model.control.invoice.common.InvoiceTypes;
022import com.echothree.model.control.invoice.server.control.InvoiceControl;
023import com.echothree.model.control.invoice.server.logic.InvoiceLogic;
024import com.echothree.model.control.sequence.common.SequenceTypes;
025import com.echothree.model.data.core.server.entity.EntityInstance;
026import com.echothree.model.data.invoice.common.pk.InvoicePK;
027import com.echothree.model.data.invoice.server.entity.InvoiceDetail;
028import com.echothree.model.data.invoice.server.factory.InvoiceFactory;
029import com.echothree.util.common.persistence.EntityNames;
030import com.echothree.util.common.persistence.Names;
031import com.echothree.util.common.persistence.Targets;
032import com.echothree.util.common.transfer.MapWrapper;
033import com.echothree.util.server.persistence.EntityPermission;
034import com.echothree.util.server.persistence.Session;
035import java.util.Collections;
036import java.util.HashMap;
037import java.util.Map;
038
039public class InvoiceNameTranslator
040        implements EntityInstanceTranslator, SequenceTypeTranslator {
041
042    private final static Map<String, String> invoiceTypesToTargets;
043    private final static Map<String, String> sequenceTypesToInvoiceTypes;
044    private final static Map<String, String> sequenceTypesToTargets;
045
046    static {
047        var invoiceTypesToTargetsMap = new HashMap<String, String>();
048
049        invoiceTypesToTargetsMap.put(InvoiceTypes.PURCHASE_INVOICE.name(), Targets.PurchaseInvoice.name());
050        invoiceTypesToTargetsMap.put(InvoiceTypes.SALES_INVOICE.name(), Targets.SalesInvoice.name());
051
052        invoiceTypesToTargets = Collections.unmodifiableMap(invoiceTypesToTargetsMap);
053
054        var sequenceTypesToInvoiceTypesMap = new HashMap<String, String>();
055
056        sequenceTypesToInvoiceTypesMap.put(SequenceTypes.PURCHASE_INVOICE.name(), InvoiceTypes.PURCHASE_INVOICE.name());
057        sequenceTypesToInvoiceTypesMap.put(SequenceTypes.SALES_INVOICE.name(), InvoiceTypes.SALES_INVOICE.name());
058        
059        sequenceTypesToInvoiceTypes = Collections.unmodifiableMap(sequenceTypesToInvoiceTypesMap);
060
061        var sequenceTypesToTargetsMap = new HashMap<String, String>();
062
063        sequenceTypesToTargetsMap.put(SequenceTypes.PURCHASE_INVOICE.name(), Targets.PurchaseInvoice.name());
064        sequenceTypesToTargetsMap.put(SequenceTypes.SALES_INVOICE.name(), Targets.SalesInvoice.name());
065        
066        sequenceTypesToTargets = Collections.unmodifiableMap(sequenceTypesToTargetsMap);
067    }
068
069    private EntityNames getNames(final Map<String, String> targetMap, final String key, final InvoiceDetail invoiceDetail) {
070        EntityNames result = null;
071        var target = targetMap.get(key);
072
073        if(target != null) {
074            var names = new MapWrapper<String>(1);
075
076            names.put(Names.InvoiceName.name(), invoiceDetail.getInvoiceName());
077
078            result = new EntityNames(target, names);
079        }
080
081        return result;
082    }
083    
084    @Override
085    public EntityNames getNames(final EntityInstance entityInstance) {
086        var invoiceDetail = InvoiceFactory.getInstance().getEntityFromPK(EntityPermission.READ_ONLY,
087                new InvoicePK(entityInstance.getEntityUniqueId())).getLastDetail();
088        var invoiceTypeName = invoiceDetail.getInvoiceType().getLastDetail().getInvoiceTypeName();
089        
090        return getNames(invoiceTypesToTargets, invoiceTypeName, invoiceDetail);
091    }
092
093    @Override
094    public EntityInstanceAndNames getNames(final String sequenceTypeName, final String invoiceName,
095            final boolean includeEntityInstance) {
096        EntityInstanceAndNames result = null;
097        var invoiceControl = Session.getModelController(InvoiceControl.class);
098        var invoiceTypeName = sequenceTypesToInvoiceTypes.get(sequenceTypeName);
099
100        if(invoiceTypeName != null) {
101            var invoiceType = InvoiceLogic.getInstance().getInvoiceTypeByName(null, invoiceTypeName);
102            var invoice = invoiceControl.getInvoiceByName(invoiceType, invoiceName);
103
104            if(invoice != null) {
105                var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
106                var entityNames = getNames(sequenceTypesToTargets, sequenceTypeName, invoice.getLastDetail());
107                
108                result = entityNames == null ? null : new EntityInstanceAndNames(includeEntityInstance ? entityInstanceControl.getEntityInstanceByBasePK(invoice.getPrimaryKey()) : null, entityNames);
109            }
110        }
111        
112        return result;
113    }
114    
115}
116