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