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