新增GS1解析,去掉一些没必要的查询,
							parent
							
								
									774057a4fe
								
							
						
					
					
						commit
						9897ed7a8d
					
				@ -1,64 +0,0 @@
 | 
				
			|||||||
package com.glxp.udidl.admin.thread;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.controller.device.DownloadDeviceHelper;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.dao.udid.DevicedownloadMapper;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.entity.udid.Devicedownload;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.entity.udid.TokenEntity;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.req.udid.DeviceRequest;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.req.udid.DownloadDiRequest;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.service.dataSync.DeviceDownloadService;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.service.dataSync.DeviceSyncService;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.service.info.CompanyService;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.service.inout.DeviceService;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.service.inout.ProductInfoService;
 | 
					 | 
				
			||||||
import com.glxp.udidl.admin.service.udi.UdiCompanyService;
 | 
					 | 
				
			||||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
					 | 
				
			||||||
import org.springframework.scheduling.annotation.Async;
 | 
					 | 
				
			||||||
import org.springframework.stereotype.Service;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import javax.annotation.Resource;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@Service
 | 
					 | 
				
			||||||
public class AsyncDownloadTask {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Resource
 | 
					 | 
				
			||||||
    DeviceService deviceService;
 | 
					 | 
				
			||||||
    @Resource
 | 
					 | 
				
			||||||
    CompanyService companyService;
 | 
					 | 
				
			||||||
    @Resource
 | 
					 | 
				
			||||||
    UdiCompanyService udiCompanyService;
 | 
					 | 
				
			||||||
    @Resource
 | 
					 | 
				
			||||||
    ProductInfoService productInfoService;
 | 
					 | 
				
			||||||
    @Resource
 | 
					 | 
				
			||||||
    DeviceDownloadService deviceDownloadService;
 | 
					 | 
				
			||||||
    @Resource
 | 
					 | 
				
			||||||
    DeviceSyncService deviceSyncService;
 | 
					 | 
				
			||||||
    @Resource
 | 
					 | 
				
			||||||
    DownloadDeviceHelper downloadDeviceHelper;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Async
 | 
					 | 
				
			||||||
    public void downloadByDi(String primaryDeviceId) {
 | 
					 | 
				
			||||||
        String token = deviceDownloadService.getToken();
 | 
					 | 
				
			||||||
        DownloadDiRequest downloadDiRequest = new DownloadDiRequest();
 | 
					 | 
				
			||||||
        downloadDiRequest.setAccessToken(token);
 | 
					 | 
				
			||||||
        downloadDiRequest.setPrimaryDeviceId(primaryDeviceId);
 | 
					 | 
				
			||||||
        downloadDeviceHelper.downloadBydi(downloadDiRequest, deviceService, productInfoService);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //https://www.udims.com/UDI_DL_Server/udidl/device/dlBymonth?month=2021-10&page=1
 | 
					 | 
				
			||||||
    //按月下载UDI
 | 
					 | 
				
			||||||
    @Async
 | 
					 | 
				
			||||||
    public void downloadByMonth(String month, int page) {
 | 
					 | 
				
			||||||
        deviceDownloadService.downloadByMonth(month, page);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Async
 | 
					 | 
				
			||||||
    public void downloadByMonthTest(String month, int page) {
 | 
					 | 
				
			||||||
        deviceDownloadService.downloadByMonthTest(month, page);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -0,0 +1,14 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public interface AI {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  String getCode();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  String getDescription();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  String getFormat();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public final class AiFactory {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private AiFactory() {
 | 
				
			||||||
 | 
					    super();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static AI create(String code, String description, String format) {
 | 
				
			||||||
 | 
					    return new AI() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      @Override
 | 
				
			||||||
 | 
					      public String getFormat() {
 | 
				
			||||||
 | 
					        return format;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      @Override
 | 
				
			||||||
 | 
					      public String getDescription() {
 | 
				
			||||||
 | 
					        return description;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      @Override
 | 
				
			||||||
 | 
					      public String getCode() {
 | 
				
			||||||
 | 
					        return code;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,71 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.StringUtils;
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.Validate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					final class Format {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static final String JOIN_SYMBOL = "+";
 | 
				
			||||||
 | 
					  private final List<Session> sessions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Format(List<Session> sessions) {
 | 
				
			||||||
 | 
					    this.sessions = Validate.notEmpty(sessions);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Format(Session... sessions) {
 | 
				
			||||||
 | 
					    this(Arrays.asList(sessions));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Session getIdentifier() {
 | 
				
			||||||
 | 
					    return sessions.get(0);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public List<Session> getDataSessions() {
 | 
				
			||||||
 | 
					    return sessions.subList(1, sessions.size());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public String toString() {
 | 
				
			||||||
 | 
					    return sessions.stream().map(String::valueOf).collect(Collectors.joining(JOIN_SYMBOL));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public boolean isValid(String value) {
 | 
				
			||||||
 | 
					    if (StringUtils.isBlank(value)) {
 | 
				
			||||||
 | 
					      return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (!validLength(value)) {
 | 
				
			||||||
 | 
					      return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return validate(value);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private boolean validate(String value) {
 | 
				
			||||||
 | 
					    return getDataSessions().stream().allMatch(s -> s.getType().isValid(Gs1128Utils.value(s, value)));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private boolean validLength(String value) {
 | 
				
			||||||
 | 
					    return isVaried() ? value.length() <= getLength() : value.length() == getLength();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public int getLength() {
 | 
				
			||||||
 | 
					    return getDataSessions().stream().mapToInt(Session::getLength).sum();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public boolean isVaried() {
 | 
				
			||||||
 | 
					    return getDataSessions().stream().anyMatch(Session::isVaried);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static Format valueOf(String value) {
 | 
				
			||||||
 | 
					    String[] split = StringUtils.split(value, JOIN_SYMBOL);
 | 
				
			||||||
 | 
					    List<Session> list = Arrays.stream(split).map(Session::valueOf).collect(Collectors.toList());
 | 
				
			||||||
 | 
					    return new Format(list);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,57 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.StringUtils;
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.Validate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.*;
 | 
				
			||||||
 | 
					import java.util.function.Function;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public final class Gs1128Decoder {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private final Collection<AI> ais;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Gs1128Decoder(Collection<AI> ais) {
 | 
				
			||||||
 | 
					    this.ais = ais;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Map<AI, String> decode(final String barcode) {
 | 
				
			||||||
 | 
					    validateBarcode(barcode);
 | 
				
			||||||
 | 
					    String barcodeValue = StringUtils.remove(barcode, Gs1128Utils.PREFIX);
 | 
				
			||||||
 | 
					    return extract(barcodeValue);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private Map<AI, String> extract(String barcode) {
 | 
				
			||||||
 | 
					    final Iterator<Character> iterator = Gs1128Utils.iterator(barcode);
 | 
				
			||||||
 | 
					    final Map<AI, String> result = new WeakHashMap<>(4);
 | 
				
			||||||
 | 
					    StringBuilder builder = new StringBuilder();
 | 
				
			||||||
 | 
					    while (iterator.hasNext()) {
 | 
				
			||||||
 | 
					      builder.append(iterator.next());
 | 
				
			||||||
 | 
					      Optional<Map<AI, String>> mapAi = mapAi(iterator, builder.toString());
 | 
				
			||||||
 | 
					      if (mapAi.isPresent()) {
 | 
				
			||||||
 | 
					        result.putAll(mapAi.get());
 | 
				
			||||||
 | 
					        builder = new StringBuilder();
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return result;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private Optional<Map<AI, String>> mapAi(Iterator<Character> iterator, String builder) {
 | 
				
			||||||
 | 
					    return ais.stream().filter(v -> v.getCode().equals(builder)).findFirst().map(aiValue(iterator));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private Function<AI, Map<AI, String>> aiValue(Iterator<Character> iterator) {
 | 
				
			||||||
 | 
					    return ai -> {
 | 
				
			||||||
 | 
					      String value = Gs1128Utils.value(ai, iterator);
 | 
				
			||||||
 | 
					      return Map.of(ai, value);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private void validateBarcode(String barcode) {
 | 
				
			||||||
 | 
					    Validate.notBlank(barcode, "Barcode cannot be blank");
 | 
				
			||||||
 | 
					    Validate.isTrue(StringUtils.startsWith(barcode, Gs1128Utils.PREFIX), "Barcode must start with prefix Gs1-128 \"]C1\"");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.StringUtils;
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.Validate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Collection;
 | 
				
			||||||
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public final class Gs1128Encoder {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Gs1128Encoder() {
 | 
				
			||||||
 | 
					    super();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public String encode(Collection<Segment> segments) {
 | 
				
			||||||
 | 
					    Validate.notEmpty(segments);
 | 
				
			||||||
 | 
					    String barcode = joinSegments(segments);
 | 
				
			||||||
 | 
					    return checkBarcode(barcode);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private String checkBarcode(String barcode) {
 | 
				
			||||||
 | 
					    return StringUtils.removeEnd(barcode, String.valueOf(Gs1128Utils.END_AI_VARIED));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private String joinSegments(Collection<Segment> segments) {
 | 
				
			||||||
 | 
					    String joinValue = segments.stream().map(Segment::encode).collect(Collectors.joining());
 | 
				
			||||||
 | 
					    return Gs1128Utils.PREFIX.concat(joinValue);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,35 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Collections;
 | 
				
			||||||
 | 
					import java.util.EnumSet;
 | 
				
			||||||
 | 
					import java.util.HashSet;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public final class Gs1128Engine {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private final Set<AI> aisRegistred = new HashSet<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Gs1128Engine() {
 | 
				
			||||||
 | 
					    aisRegistred.addAll(EnumSet.allOf(AIs.class));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public void registerAi(AI ai) {
 | 
				
			||||||
 | 
					    aisRegistred.add(ai);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Set<AI> getAisRegistred() {
 | 
				
			||||||
 | 
					    return Collections.unmodifiableSet(aisRegistred);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Gs1128Encoder encoder() {
 | 
				
			||||||
 | 
					    return new Gs1128Encoder();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Gs1128Decoder decoder() {
 | 
				
			||||||
 | 
					    return new Gs1128Decoder(aisRegistred);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,59 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Iterator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					final class Gs1128Utils {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static final String PREFIX = "]C1";
 | 
				
			||||||
 | 
					  public static final char END_AI_VARIED = (char) 29;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private Gs1128Utils() {
 | 
				
			||||||
 | 
					    super();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static Iterator<Character> iterator(String value) {
 | 
				
			||||||
 | 
					    return value.chars().mapToObj(c -> (char) c).iterator();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static String value(AI ai, Iterator<Character> iterator) {
 | 
				
			||||||
 | 
					    Format format = Format.valueOf(ai.getFormat());
 | 
				
			||||||
 | 
					    if (format.isVaried()) {
 | 
				
			||||||
 | 
					      return variedValue(iterator);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return fixedValue(format.getLength(), iterator);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static String value(Session session, String value) {
 | 
				
			||||||
 | 
					    Iterator<Character> iterator = iterator(value);
 | 
				
			||||||
 | 
					    if (session.isVaried()) {
 | 
				
			||||||
 | 
					      return variedValue(iterator);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return fixedValue(session.getLength(), iterator);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static String fixedValue(int size, Iterator<Character> iterator) {
 | 
				
			||||||
 | 
					    StringBuilder builder = new StringBuilder();
 | 
				
			||||||
 | 
					    for (int i = 0; i < size; i++) {
 | 
				
			||||||
 | 
					      builder.append(iterator.next());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return builder.toString();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static String variedValue(Iterator<Character> iterator) {
 | 
				
			||||||
 | 
					    StringBuilder builder = new StringBuilder();
 | 
				
			||||||
 | 
					    while (iterator.hasNext()) {
 | 
				
			||||||
 | 
					      char value = iterator.next();
 | 
				
			||||||
 | 
					      if (value == END_AI_VARIED) {
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      builder.append(value);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return builder.toString();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,35 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.Validate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public final class Segment {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private final AI ai;
 | 
				
			||||||
 | 
					  private final String value;
 | 
				
			||||||
 | 
					  private final Format format;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Segment(AI ai, String value) {
 | 
				
			||||||
 | 
					    this.ai = Validate.notNull(ai, "Invalid AI");
 | 
				
			||||||
 | 
					    format = Format.valueOf(ai.getFormat());
 | 
				
			||||||
 | 
					    this.value = validateValue(format, value);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private String validateValue(Format format, String value) {
 | 
				
			||||||
 | 
					    Validate.notBlank(value, "Invalid segment value");
 | 
				
			||||||
 | 
					    Validate.notNull(format, "Invalid format");
 | 
				
			||||||
 | 
					    Validate.isTrue(format.isValid(value));
 | 
				
			||||||
 | 
					    return value;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  String encode() {
 | 
				
			||||||
 | 
					    String codeValue = ai.getCode().concat(value);
 | 
				
			||||||
 | 
					    if (format.isVaried()) {
 | 
				
			||||||
 | 
					      return codeValue + Gs1128Utils.END_AI_VARIED;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return codeValue;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,69 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.StringUtils;
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.Validate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					final class Session {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static final String VARIED_CONSTANT = "..";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private final SessionType type;
 | 
				
			||||||
 | 
					  private final boolean varied;
 | 
				
			||||||
 | 
					  private final int length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public Session(SessionType type, int length, boolean varied) {
 | 
				
			||||||
 | 
					    this.type = Validate.notNull(type);
 | 
				
			||||||
 | 
					    Validate.isTrue(length > 0, "Session invalid lenght");
 | 
				
			||||||
 | 
					    this.length = length;
 | 
				
			||||||
 | 
					    this.varied = varied;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public SessionType getType() {
 | 
				
			||||||
 | 
					    return type;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public int getLength() {
 | 
				
			||||||
 | 
					    return length;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public boolean isVaried() {
 | 
				
			||||||
 | 
					    return varied;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static Session valueOf(String value) {
 | 
				
			||||||
 | 
					    Validate.notBlank(value, "Session cannot be blank");
 | 
				
			||||||
 | 
					    SessionType type = sessionType(value);
 | 
				
			||||||
 | 
					    String strLength = value.substring(type.length());
 | 
				
			||||||
 | 
					    boolean varied = strLength.startsWith(VARIED_CONSTANT);
 | 
				
			||||||
 | 
					    int length = createLength(strLength, varied);
 | 
				
			||||||
 | 
					    return new Session(type, length, varied);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static SessionType sessionType(String value) {
 | 
				
			||||||
 | 
					    return SessionType.valueByPrefix(value).orElseThrow(() -> new IllegalArgumentException("Invalid session type"));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static Integer createLength(String strLength, boolean varied) {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      if (varied) {
 | 
				
			||||||
 | 
					        String lengthValue = StringUtils.substringAfter(strLength, VARIED_CONSTANT);
 | 
				
			||||||
 | 
					        return Integer.parseInt(lengthValue);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return Integer.valueOf(strLength);
 | 
				
			||||||
 | 
					    } catch (NumberFormatException ex) {
 | 
				
			||||||
 | 
					      throw new IllegalArgumentException(String.format("Invalid length: '%s'", strLength), ex);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public String toString() {
 | 
				
			||||||
 | 
					    return new StringBuilder(type.getSymbol())
 | 
				
			||||||
 | 
					      .append(varied ? VARIED_CONSTANT : StringUtils.EMPTY)
 | 
				
			||||||
 | 
					      .append(length)
 | 
				
			||||||
 | 
					      .toString();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,50 @@
 | 
				
			|||||||
 | 
					package com.glxp.udidl.admin.util.gs1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.apache.commons.lang3.StringUtils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.EnumSet;
 | 
				
			||||||
 | 
					import java.util.Optional;
 | 
				
			||||||
 | 
					import java.util.function.Predicate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author guilherme.pacheco
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					enum SessionType {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  ALPHANUMERIC("an", v -> Character.isAlphabetic(v) || Character.isDigit(v)),
 | 
				
			||||||
 | 
					  ALPHABETIC("a", v -> Character.isAlphabetic(v)),
 | 
				
			||||||
 | 
					  NUMERIC("n", v -> Character.isDigit(v));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private final String symbol;
 | 
				
			||||||
 | 
					  private final Predicate<Character> validator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private SessionType(String symbol, Predicate<Character> validator) {
 | 
				
			||||||
 | 
					    this.symbol = symbol;
 | 
				
			||||||
 | 
					    this.validator = validator;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public boolean isValid(String value) {
 | 
				
			||||||
 | 
					    if (StringUtils.isBlank(value)) {
 | 
				
			||||||
 | 
					      return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return value.chars().mapToObj(c -> (char) c).allMatch(validator::test);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public String getSymbol() {
 | 
				
			||||||
 | 
					    return symbol;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public int length() {
 | 
				
			||||||
 | 
					    return symbol.length();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static Optional<SessionType> valueByPrefix(String value) {
 | 
				
			||||||
 | 
					    if (StringUtils.isBlank(value)) {
 | 
				
			||||||
 | 
					      return Optional.empty();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return EnumSet.allOf(SessionType.class).stream()
 | 
				
			||||||
 | 
					      .filter(v -> StringUtils.startsWithIgnoreCase(value, v.symbol))
 | 
				
			||||||
 | 
					      .findFirst();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue