From 3061b1aaa223a7816a753489e7632a3aab6b78d1 Mon Sep 17 00:00:00 2001 From: Gabriele Zigurella Date: Thu, 18 May 2023 17:24:26 +0200 Subject: [PATCH] Add basic pratica creation --- .../bridge/persistence/PraticaValidator.java | 74 +++++++++++++++++++ .../bridge/api/QuarkusBridgeConstants.java | 12 +++ .../scheduler/QuarkusBridgeScheduler.java | 68 ++++++++++++++++- 3 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/liferay/bridge/persistence/PraticaValidator.java create mode 100644 liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/quarkus/bridge/api/QuarkusBridgeConstants.java diff --git a/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/liferay/bridge/persistence/PraticaValidator.java b/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/liferay/bridge/persistence/PraticaValidator.java new file mode 100644 index 00000000..94179fb4 --- /dev/null +++ b/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/liferay/bridge/persistence/PraticaValidator.java @@ -0,0 +1,74 @@ +package portos.nextmind.liferay.bridge.persistence; + +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.exception.SystemException; +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.util.StringPool; +import com.liferay.portal.kernel.util.Validator; +import com.liferay.portal.model.RoleConstants; +import com.liferay.portal.model.User; +import com.liferay.portal.service.RoleLocalServiceUtil; +import com.liferay.portal.service.ServiceContext; +import com.liferay.portal.service.UserLocalServiceUtil; + +import it.tref.liferay.portos.bo.model.Comune; +import it.tref.liferay.portos.bo.model.Territorio; +import it.tref.liferay.portos.bo.service.ComuneLocalServiceUtil; +import it.tref.liferay.portos.bo.service.TerritorioLocalServiceUtil; + +import java.util.List; +import java.util.Locale; + +public class PraticaValidator { + static Log _log = LogFactoryUtil.getLog(PraticaValidator.class); + + public static Comune fetchComune(long companyId, String codiceIstat) throws SystemException { + Comune comune = ComuneLocalServiceUtil.fetchByC_CI(companyId, codiceIstat); + if (comune == null) { + _log.warn("#[!]# Nessun Comune trovato con la coppia companyId "+companyId+ + " e codiceIstat "+codiceIstat); + _log.warn("#[!]# La richiesta sarà ignorata, verificare sul middleware Quarkus"); + return null; + } + return comune; + } + + public static Territorio fetchTerritorio(long comuneId) throws SystemException{ + List territori = TerritorioLocalServiceUtil.findByComuneId(comuneId, 0, 1); + if (territori.isEmpty()) { + _log.warn("#[!]# Nessun Territorio trovato per il comuneId "+comuneId); + _log.warn("#[!]# La richiesta sarà ignorata, verificare sul middleware Quarkus"); + return null; + } + return territori.get(0); + } + + public static User fetchUser(long companyId, long groupId, String codiceFiscale, String email, String nome, String cognome, long comuneId) throws SystemException, PortalException{ + User user = UserLocalServiceUtil.fetchUserByScreenName(companyId, + codiceFiscale.trim().toLowerCase()); + if(Validator.isNotNull(user)) { + return user; + } + + // Crea utente se assente in database + long admingRoleId = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ADMINISTRATOR).getRoleId(); + User admin = UserLocalServiceUtil.getRoleUsers(admingRoleId).get(0); + + // Alter serviceContext to bypass Liferay user creation workflow + ServiceContext userServiceContext = new ServiceContext(); + userServiceContext.setScopeGroupId(groupId); + userServiceContext.setCompanyId(companyId); + userServiceContext.setUserId(admin.getUserId()); + + user = UserLocalServiceUtil.addUser( + admin.getUserId(), companyId, true, StringPool.BLANK, StringPool.BLANK, + false, codiceFiscale.trim().toLowerCase(), email, 0l, StringPool.BLANK, Locale.ITALY, + nome, StringPool.BLANK, cognome, + 0, 0, true, 1, 1, 1980, StringPool.BLANK, + new long[]{groupId}, new long[]{}, + new long[]{}, new long[]{}, false, userServiceContext); + + return user; + } +} diff --git a/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/quarkus/bridge/api/QuarkusBridgeConstants.java b/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/quarkus/bridge/api/QuarkusBridgeConstants.java new file mode 100644 index 00000000..708f2723 --- /dev/null +++ b/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/quarkus/bridge/api/QuarkusBridgeConstants.java @@ -0,0 +1,12 @@ +package portos.nextmind.quarkus.bridge.api; + +public class QuarkusBridgeConstants { + public final static String HEADER_REQUEST = "HeaderPratica"; + public final static String BODY_REQUEST = "SportelloPratica"; + public final static String codiceIstat = "cadastralCode"; + public final static String codiceFiscale = "digitalHolderTaxCode"; + public final static String email = "address"; + public final static String nome = "name"; + public final static String cognome = "surname"; + public final static String tipoProcedura = "proceedingType"; +} diff --git a/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/quarkus/bridge/scheduler/QuarkusBridgeScheduler.java b/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/quarkus/bridge/scheduler/QuarkusBridgeScheduler.java index 9fe4b6d8..48487116 100644 --- a/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/quarkus/bridge/scheduler/QuarkusBridgeScheduler.java +++ b/liferay-plugins-sdk-6.2/portlets/quarkus-bridge-portlet/docroot/WEB-INF/src/portos/nextmind/quarkus/bridge/scheduler/QuarkusBridgeScheduler.java @@ -12,17 +12,31 @@ import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.messaging.Message; import com.liferay.portal.kernel.messaging.MessageListener; import com.liferay.portal.kernel.messaging.MessageListenerException; +import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.model.Company; +import com.liferay.portal.model.User; import com.liferay.portal.service.CompanyLocalServiceUtil; +import com.liferay.portal.service.ServiceContext; +import com.liferay.portal.service.UserLocalServiceUtil; +import integratore.String; +import it.tref.liferay.portos.bo.model.Comune; +import it.tref.liferay.portos.bo.model.DettPratica; import it.tref.liferay.portos.bo.model.IntPratica; +import it.tref.liferay.portos.bo.model.Territorio; +import it.tref.liferay.portos.bo.service.ComuneLocalServiceUtil; +import it.tref.liferay.portos.bo.service.DettPraticaLocalServiceUtil; import it.tref.liferay.portos.bo.service.IntPraticaLocalServiceUtil; +import it.tref.liferay.portos.bo.service.TerritorioLocalServiceUtil; import java.util.ArrayList; import java.util.List; +import portos.nextmind.liferay.bridge.persistence.PraticaValidator; +import portos.nextmind.quarkus.bridge.api.QuarkusBridgeConstants; import portos.nextmind.quarkus.bridge.api.QuarkusBridgeMessageFactoryUtil; +import portos.nextmind.service.ScheduledActionsLocalServiceUtil; public class QuarkusBridgeScheduler implements MessageListener { @@ -42,7 +56,7 @@ public class QuarkusBridgeScheduler implements MessageListener { return; } for(Company company : companies){ - JSONObject createPayload = prepareCreateBatch(); + JSONObject createPayload = prepareCreateBatch(company.getCompanyId()); //TODO: persist pratiche JSONObject updatePayload = prepareUpdateBatch(company.getCompanyId()); //TODO: persist pratiche @@ -50,10 +64,60 @@ public class QuarkusBridgeScheduler implements MessageListener { _log.info("### Quarkus Bridge Scheduler batch ready."); } - private JSONObject prepareCreateBatch(){ + private JSONObject prepareCreateBatch(long companyId){ final String clientContext = "PAESAGGISTICA"; JSONObject object = QuarkusBridgeMessageFactoryUtil.readNewData(clientContext); _log.debug(object.toString()); + JSONArray pratiche = object.getJSONArray("result"); + for(int i = 0; i < pratiche.length() ; i++){ + JSONObject pratica = pratiche.getJSONObject(i); + JSONObject Header = pratica.getJSONObject(QuarkusBridgeConstants.HEADER_REQUEST); + //TODO: check requestAction da Header? + //TODO: check operation da Header? + JSONObject Sportello = pratica.getJSONObject(QuarkusBridgeConstants.BODY_REQUEST); + String codiceIstat = Sportello.getString(QuarkusBridgeConstants.codiceIstat); + try { + // Cerca comune di appartenenza + Comune comune = PraticaValidator.fetchComune(companyId, codiceIstat); + if(Validator.isNull(comune)) continue; + // Cerca territorio di appartenenza + long comuneId = comune.getComuneId(); + Territorio territorio = PraticaValidator.fetchTerritorio(comuneId); + if(Validator.isNull(territorio)) continue; + // Cerca utente associato alla pratica + long groupId = territorio.getGroupId(); + String codiceFiscale = Sportello.getString(QuarkusBridgeConstants.codiceFiscale); + String email = Sportello.getString(QuarkusBridgeConstants.email, StringPool.BLANK); + String nome = Sportello.getString(QuarkusBridgeConstants.nome, StringPool.BLANK); + String cognome = Sportello.getString(QuarkusBridgeConstants.cognome, StringPool.BLANK); + if(Validator.isBlank(email) || Validator.isBlank(nome) || Validator.isBlank(cognome)){ + _log.error("#[!]# Missing one or more of the required fields - First Name, Last Name, E-Mail"); + continue; + } + User user = PraticaValidator.fetchUser(companyId, groupId, + codiceFiscale, email, nome, cognome, comuneId); + // Recupera la procedura della pratica + String tipoProcedura = Sportello.getString(QuarkusBridgeConstants.tipoProcedura); + + //TODO: verifica che la procedura non esista già + + ServiceContext serviceContext = new ServiceContext(); + serviceContext.setScopeGroupId(groupId); + serviceContext.setCompanyId(companyId); + serviceContext.setUserId(user.getUserId()); + + // Aggiungi DettPratica + long territorioId = territorio.getTerritorioId(); + DettPratica dettPratica = DettPraticaLocalServiceUtil + .addDettPratica(territorioId, tipoProcedura, serviceContext); + + // Aggiorna IntPratica + IntPraticaLocalServiceUtil.addDatiSuap(dettPratica.getIntPraticaId(), new long[]{}); + + } catch (SystemException e) { + _log.error(e ,e); + } + } return object; }