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 BooklandEanChecksumLogic
027        extends BaseChecksumLogic
028        implements ItemAliasChecksumInterface {
029
030    @Inject
031    Ean13ChecksumLogic ean13ChecksumLogic;
032
033    protected BooklandEanChecksumLogic() {
034        super();
035    }
036
037    public static BooklandEanChecksumLogic getInstance() {
038        return CDI.current().select(BooklandEanChecksumLogic.class).get();
039    }
040
041    @Override
042    public void checkChecksum(final ExecutionErrorAccumulator eea, final String alias) {
043        if(alias.length() == 13) {
044            if(alias.matches("\\d{13}")) {
045                switch(Integer.parseInt(alias.substring(0, 3))) {
046                    case 978, 979 -> ean13ChecksumLogic.checkChecksum(eea, alias);
047                    default -> eea.addExecutionError(ExecutionErrors.IncorrectBooklandEanPrefix.name(), alias);
048                }
049            } else {
050                eea.addExecutionError(ExecutionErrors.IncorrectBooklandEanCharacter.name(), alias);
051            }
052        } else {
053            eea.addExecutionError(ExecutionErrors.IncorrectBooklandEanLength.name(), alias);
054        }
055    }
056
057}