· 5 years ago · Sep 29, 2020, 01:24 PM
1package it.infocert.service.micro.normalize.util;
2
3import it.infocert.common.service.model.company.LbhubCompanyModel;
4import it.infocert.service.micro.common.service.feignclient.service.AnagraficaClienteService;
5import it.infocert.service.micro.common.service.feignclient.service.CompanyClientService;
6import it.infocert.service.micro.common.service.feignclient.service.NodeClientService;
7import it.infocert.service.micro.normalize.bean.StrategyInputBean;
8import it.infocert.service.micro.normalize.exception.NormalizeException;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11
12import java.util.EnumSet;
13import java.util.Set;
14import java.util.function.Function;
15import java.util.stream.Collectors;
16
17/**
18 * Choose a strategy to manage new company requests.
19 * Possible values are
20 * {@link #ERROR_COMPANY_ALREADY_EXISTS_AND_CANNOT_VIEW_DOCUMENT}
21 * {@link #ERROR_COMPANY_NOT_EXISTS_AND_IS_PASSPARTOUT}
22 * {@link #CREATE_COMPANY_NOT_PASSPARTOUT}
23 * {@link #COMPANY_EXISTS_AND_CAN_VIEW_INVOICE}
24 */
25public enum CompanyRetrieveStrategy {
26 /**
27 * if the company already exists but do not have the same value of the passed parameters throws exception
28 */
29 ERROR_COMPANY_ALREADY_EXISTS_AND_CANNOT_VIEW_DOCUMENT(
30 input -> input.getLbhubCompanyPersisted() != null &&
31 !LiCompanyHelper.doBelongsToCompany(input.getIdAngraficaMaster(),
32 input.getIdAnagrafica(),
33 input.getPIvaEmittente(),
34 input.getIdPaeseEmittente(),
35 input.getLbhubCompanyPersisted()) &&
36 !input.getIsPassepartout() &&
37 !input.getAnagraficaBin().equals(input.getLbhubCompanyPersisted().getIdAnagrafica()),
38 input -> {
39 input.getLogger().error(String.format(
40 "A company with partivaIva %s and idPaese %s already exists with different anagraficaMaster %s and anagrafica %s.",
41 input.getPIvaEmittente(),
42 input.getIdPaeseEmittente(),
43 input.getLbhubCompanyPersisted().getIdAnagraficaMaster(),
44 input.getLbhubCompanyPersisted().getIdAnagrafica()));
45 throw new NormalizeException(
46 String.format(
47 "A company with partivaIva %s and idPaese %s already exists with different anagraficaMaster and anagrafica.",
48 input.getPIvaEmittente(),
49 input.getIdPaeseEmittente()));
50 }),
51
52 /**
53 * if try to create a passpartout company throws exception
54 */
55 ERROR_COMPANY_NOT_EXISTS_AND_IS_PASSPARTOUT(
56 input -> input.getLbhubCompanyPersisted() == null && input.getIsPassepartout(),
57 input -> {
58 input.getLogger().error(String.format("A company with partivaIva %s and idPaese %s doesn't exist.",
59 input.getPIvaEmittente(),
60 input.getIdPaeseEmittente()));
61 throw new NormalizeException(
62 String.format("A company with partivaIva %s and idPaese %s doesn't exist.",
63 input.getPIvaEmittente(),
64 input.getIdPaeseEmittente()));
65 }),
66
67 /**
68 * create a new company not passpartout
69 */
70 CREATE_COMPANY_NOT_PASSPARTOUT(
71 input -> input.getLbhubCompanyPersisted() == null && !input.getIsPassepartout(),
72 input -> {
73 input.getLogger().warn("AnagraficaCliente with companyGroupCode '{}' doesn't exist.", input.getIdAnagrafica());
74 LbhubCompanyModel lbhubCompany = LiCompanyHelper.initCompany(input.getPIvaEmittente(),
75 input.getIdAngraficaMaster(),
76 input.getIdAnagrafica(),
77 input.getIdPaeseEmittente());
78 return LiCompanyHelper.createCompany(input.getCompanyService(),
79 input.getAnagraficaService(),
80 input.getNodeService(),
81 input.getIdAngraficaMaster(),
82 lbhubCompany);
83 }),
84
85 /**
86 * a company whose parameters matches the passed values already exists
87 */
88 COMPANY_EXISTS_AND_CAN_VIEW_INVOICE(
89 input -> input.getLbhubCompanyPersisted() != null && (
90 LiCompanyHelper.doBelongsToCompany(input.getIdAngraficaMaster(),
91 input.getIdAnagrafica(),
92 input.getPIvaEmittente(),
93 input.getIdPaeseEmittente(),
94 input.getLbhubCompanyPersisted()) ||
95 input.getIsPassepartout() ||
96 input.getAnagraficaBin().equals(input.getLbhubCompanyPersisted().getIdAnagrafica())),
97 StrategyInputBean::getLbhubCompanyPersisted);
98
99
100 private static final Logger logger = LoggerFactory.getLogger(CompanyRetrieveStrategy.class);
101 private final Function<StrategyInputBean, Boolean> condition;
102 private final Function<StrategyInputBean, LbhubCompanyModel> strategy;
103
104 CompanyRetrieveStrategy(Function<StrategyInputBean, Boolean> condition, Function<StrategyInputBean, LbhubCompanyModel> strategy) {
105 this.condition = condition;
106 this.strategy = strategy;
107 }
108
109 /**
110 * returns the strategy with which to retrieve the company corresponding to the passed parameters
111 * @param pIvaEmittente the document's partitaIvaEmittente
112 * @param idPaeseEmittente the document's idPaeseEmittente
113 * @param idAngraficaMaster the document's idAngraficaMaster
114 * @param idAnagrafica the document's idAnagrafica
115 * @param anagraficaBin the idAnagrafica of BIN company from configuration
116 * @param lbhubCompanyPersisted the company that have to be retrieved or created
117 * @param isPassepartout flag if the company can access all the documents
118 * @param anagraficaService the service for the access at the anagrafica table
119 * @param nodeService the service for the access at the node table
120 * @param companyService the service for the access at the company table
121 * @return
122 */
123 public static Function<StrategyInputBean, LbhubCompanyModel> retrieveStrategy(String pIvaEmittente,
124 String idPaeseEmittente,
125 String idAngraficaMaster,
126 String idAnagrafica,
127 String anagraficaBin,
128 LbhubCompanyModel lbhubCompanyPersisted,
129 Boolean isPassepartout,
130 AnagraficaClienteService anagraficaService, NodeClientService nodeService, CompanyClientService companyService) {
131 StrategyInputBean inputBean = new StrategyInputBean(logger,
132 pIvaEmittente,
133 idPaeseEmittente,
134 idAngraficaMaster,
135 idAnagrafica,
136 anagraficaBin,
137 lbhubCompanyPersisted,
138 isPassepartout,
139 anagraficaService,
140 nodeService,
141 companyService);
142
143 Set<CompanyRetrieveStrategy> startegySet = EnumSet.allOf(CompanyRetrieveStrategy.class).stream()
144 .filter(strategy -> strategy.condition.apply(inputBean))
145 .collect(Collectors.toSet());
146
147 return startegySet.stream().findFirst().orElse(COMPANY_EXISTS_AND_CAN_VIEW_INVOICE).strategy;
148 }
149}