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 UpcAChecksumLogic
026        extends BaseChecksumLogic
027        implements ItemAliasChecksumInterface {
028
029    protected UpcAChecksumLogic() {
030        super();
031    }
032
033    public static UpcAChecksumLogic getInstance() {
034        return CDI.current().select(UpcAChecksumLogic.class).get();
035    }
036
037    @Override
038    public void checkChecksum(final ExecutionErrorAccumulator eea, final String alias) {
039        if(alias.length() == 12) {
040            var hasCharacterError = false;
041            var totalA = 0;
042            var totalB = 0;
043
044            for(var i = 0; i < 11; i++) {
045                var digit = getDigit(alias, i);
046
047                if(digit == -1) {
048                    hasCharacterError = true;
049                    break;
050                } else {
051                    if(i % 2 == 0) {
052                        totalA += digit;
053                    } else {
054                        totalB += digit;
055                    }
056                }
057            }
058
059            if(!hasCharacterError) {
060                var checkDigit = getDigit(alias, 11);
061
062                hasCharacterError = checkDigit == -1;
063
064                if(!hasCharacterError) {
065                    var intermediate = (10 - (totalA * 3 + totalB) % 10) % 10;
066
067                    if(intermediate != checkDigit) {
068                        eea.addExecutionError(ExecutionErrors.IncorrectUpcAChecksum.name(), alias);
069                    }
070                }
071            }
072
073            if(hasCharacterError) {
074                eea.addExecutionError(ExecutionErrors.IncorrectUpcACharacter.name(), alias);
075            }
076        } else {
077            eea.addExecutionError(ExecutionErrors.IncorrectUpcALength.name(), alias);
078        }
079    }
080
081}