001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.item.server.logic.checksum;
018
019import com.echothree.util.common.message.ExecutionErrors;
020import com.echothree.util.server.message.ExecutionErrorAccumulator;
021import javax.enterprise.context.ApplicationScoped;
022import javax.enterprise.inject.spi.CDI;
023import javax.inject.Inject;
024
025@ApplicationScoped
026public class UpcEChecksumLogic
027        extends BaseChecksumLogic
028        implements ItemAliasChecksumInterface {
029
030    @Inject
031    UpcAChecksumLogic upcAChecksumLogic;
032
033    protected UpcEChecksumLogic() {
034        super();
035    }
036
037    public static UpcEChecksumLogic getInstance() {
038        return CDI.current().select(UpcEChecksumLogic.class).get();
039    }
040
041    private static String expandUpcEToUpcA(String upcE) {
042        char numberSystem = upcE.charAt(0);
043        char[] mfr = upcE.substring(1, 6).toCharArray();
044        char lastDigit = upcE.charAt(6);
045        char checksum = upcE.charAt(7);
046
047        return switch(lastDigit) {
048            case '0', '1', '2' ->
049                    "" + numberSystem + mfr[0] + mfr[1] + lastDigit + "0000" + mfr[2] + mfr[3] + mfr[4] + checksum;
050            case '3' ->
051                    "" + numberSystem + mfr[0] + mfr[1] + mfr[2] + "00000" + mfr[3] + mfr[4] + checksum;
052            case '4' ->
053                    "" + numberSystem + mfr[0] + mfr[1] + mfr[2] + mfr[3] + "00000" + mfr[4] + checksum;
054            default -> // '5' to '9'
055                    "" + numberSystem + mfr[0] + mfr[1] + mfr[2] + mfr[3] + mfr[4] + "0000" + lastDigit + checksum;
056        };
057    }
058
059    @Override
060    public void checkChecksum(final ExecutionErrorAccumulator eea, final String alias) {
061        if(alias.length() == 8) {
062            if(alias.matches("\\d{8}")) {
063                switch(getDigit(alias, 0)) {
064                    case 0, 1 -> upcAChecksumLogic.checkChecksum(eea, expandUpcEToUpcA(alias));
065                    default -> eea.addExecutionError(ExecutionErrors.IncorrectUpcENumberSystem.name(), alias);
066                }
067            } else {
068                eea.addExecutionError(ExecutionErrors.IncorrectUpcECharacter.name(), alias);
069            }
070        } else {
071            eea.addExecutionError(ExecutionErrors.IncorrectUpcELength.name(), alias);
072        }
073    }
074
075}