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