AccordiServizioParteComuneResourcesRepresentationChange.java
/*
* GovWay - A customizable API Gateway
* https://govway.org
*
* Copyright (c) 2005-2024 Link.it srl (https://link.it).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.openspcoop2.web.ctrlstat.servlet.apc;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.openspcoop2.core.registry.AccordoServizioParteComune;
import org.openspcoop2.core.registry.IdSoggetto;
import org.openspcoop2.core.registry.Resource;
import org.openspcoop2.core.registry.ResourceRepresentation;
import org.openspcoop2.core.registry.ResourceRepresentationJson;
import org.openspcoop2.core.registry.ResourceRepresentationXml;
import org.openspcoop2.core.registry.ResourceRequest;
import org.openspcoop2.core.registry.ResourceResponse;
import org.openspcoop2.core.registry.constants.RepresentationType;
import org.openspcoop2.core.registry.constants.RepresentationXmlType;
import org.openspcoop2.core.registry.constants.StatiAccordo;
import org.openspcoop2.core.registry.driver.IDAccordoFactory;
import org.openspcoop2.message.constants.MessageType;
import org.openspcoop2.message.constants.ServiceBinding;
import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
import org.openspcoop2.protocol.engine.utils.NamingUtils;
import org.openspcoop2.protocol.sdk.IProtocolFactory;
import org.openspcoop2.utils.rest.api.ApiResponse;
import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
import org.openspcoop2.web.ctrlstat.servlet.apc.api.ApiCostanti;
import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
import org.openspcoop2.web.lib.mvc.Costanti;
import org.openspcoop2.web.lib.mvc.DataElement;
import org.openspcoop2.web.lib.mvc.ForwardParams;
import org.openspcoop2.web.lib.mvc.GeneralData;
import org.openspcoop2.web.lib.mvc.PageData;
import org.openspcoop2.web.lib.mvc.Parameter;
import org.openspcoop2.web.lib.mvc.ServletUtils;
import org.openspcoop2.web.lib.mvc.TipoOperazione;
/**
* AccordiServizioParteComuneResourcesRepresentationChange
*
* @author Andrea Poli (apoli@link.it)
* @author $Author$
* @version $Rev$, $Date$
*
*/
public final class AccordiServizioParteComuneResourcesRepresentationChange extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession(true);
// Inizializzo PageData
PageData pd = new PageData();
// Inizializzo GeneralData
GeneralHelper generalHelper = new GeneralHelper(session);
GeneralData gd = generalHelper.initGeneralData(request);
String userLogin = ServletUtils.getUserLoginFromSession(session);
IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();
// Parametri relativi al tipo operazione
TipoOperazione tipoOp = TipoOperazione.CHANGE;
try {
AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore();
SoggettiCore soggettiCore = new SoggettiCore(apcCore);
AccordiServizioParteComuneHelper apcHelper = new AccordiServizioParteComuneHelper(request, pd, session);
String editMode = apcHelper.getParameter(Costanti.DATA_ELEMENT_EDIT_MODE_NAME);
String id = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID);
long idAccordoLong = Long.parseLong(id);
String nomeRisorsa = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME);
if (nomeRisorsa == null) {
nomeRisorsa = "";
}
String statusS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_STATUS);
Integer status = null;
try {
if(statusS!=null)
status = Integer.parseInt(statusS);
} catch(Exception e) {
// ignore
}
String isReq = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCE_REQUEST);
boolean isRequest = ServletUtils.isCheckBoxEnabled(isReq);
String descr = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_DESCRIZIONE);
if (descr == null) {
descr = "";
}
String idRep = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_ID);
int idRepInt = Integer.parseInt(idRep);
String messageProcessorS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_MESSAGE_TYPE);
MessageType messageType = (StringUtils.isNotEmpty(messageProcessorS) && !messageProcessorS.equals(AccordiServizioParteComuneCostanti.DEFAULT_VALUE_PARAMETRO_APC_MESSAGE_TYPE_DEFAULT)) ? MessageType.valueOf(messageProcessorS) : null;
String mediaType = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_MEDIA_TYPE);
String tipoS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_TIPO);
RepresentationType tipo = (StringUtils.isNotEmpty(tipoS) && !messageProcessorS.equals(AccordiServizioParteComuneCostanti.DEFAULT_VALUE_PARAMETRO_APC_RESOURCES_REPRESENTATION_TIPO_NON_DEFINITO)) ? RepresentationType.toEnumConstant(tipoS) : null;
String nome = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_NOME);
String tipoJson = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_JSON_TYPE);
String namespaceXml = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_XML_NAMESPACE);
String nomeXml = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_XML_NAME);
String xmlTypeS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_XML_TIPO);
RepresentationXmlType xmlType = StringUtils.isNotEmpty(xmlTypeS) ? RepresentationXmlType.toEnumConstant(xmlTypeS) : null;
String tipoAccordo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_TIPO_ACCORDO);
if("".equals(tipoAccordo))
tipoAccordo = null;
// Preparo il menu
apcHelper.makeMenu();
// Prendo il nome
AccordoServizioParteComune as = apcCore.getAccordoServizioFull(idAccordoLong);
String uriAS = idAccordoFactory.getUriFromAccordo(as);
String labelASTitle = apcHelper.getLabelIdAccordo(as);
String protocollo = null;
//calcolo del protocollo implementato dall'accordo
IdSoggetto soggettoReferente = as.getSoggettoReferente();
String tipoSoggettoReferente = soggettoReferente.getTipo();
protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(tipoSoggettoReferente);
ServiceBinding serviceBinding;
//calcolo del serviceBinding dall'accordo
serviceBinding = apcCore.toMessageServiceBinding(as.getServiceBinding());
IProtocolFactory<?> protocolFactory = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocollo);
Resource risorsa = null;
for (int j = 0; j < as.sizeResourceList(); j++) {
risorsa = as.getResource(j);
if (nomeRisorsa.equals(risorsa.getNome())) {
break;
}
}
if(risorsa==null) {
throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
}
Long idResponse = null;
Long idResource = null;
ResourceRequest resourceRequest = null;
ResourceResponse resourceResponse = null;
ResourceRepresentation resourceRepresentationOLD = null;
List<ResourceRepresentation> representationList = null;
if(isRequest) {
resourceRequest = risorsa.getRequest();
idResource = risorsa.getId();
if(resourceRequest != null)
representationList = resourceRequest.getRepresentationList();
} else {
if(risorsa.getResponseList() != null) {
for (int i = 0; i < risorsa.getResponseList().size(); i++) {
resourceResponse = risorsa.getResponse(i);
if (resourceResponse.getStatus() == status) {
idResponse = resourceResponse.getId();
break;
}
}
if(resourceResponse != null)
representationList = resourceResponse.getRepresentationList();
}
}
String oldMediaType = null;
if(representationList != null && !representationList.isEmpty()) {
for (ResourceRepresentation resourceRepresentation : representationList) {
if(resourceRepresentation.getId().intValue() == idRepInt) {
resourceRepresentationOLD = resourceRepresentation;
break;
}
}
}
if(resourceRepresentationOLD!=null) {
oldMediaType = resourceRepresentationOLD.getMediaType();
}
Parameter pTipoAccordo = AccordiServizioParteComuneUtilities.getParametroAccordoServizio(tipoAccordo);
Parameter pIdAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID, id);
Parameter pNomeAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_NOME, uriAS);
Parameter pNomeRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME, risorsa.getNome());
Parameter pIsRequest = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCE_REQUEST, isRequest+"");
Parameter pIdRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_ID, risorsa.getId()+"");
Parameter pResponseStatus = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_STATUS, statusS);
Boolean isModalitaVistaApiCustom = ServletUtils.getBooleanAttributeFromSession(ApiCostanti.SESSION_ATTRIBUTE_VISTA_APC_API, session, request, false).getValue();
List<Parameter> listaParams = apcHelper.getTitoloApc(TipoOperazione.CHANGE, as, tipoAccordo, labelASTitle, null, false);
String labelRisorse = (isModalitaVistaApiCustom!=null && isModalitaVistaApiCustom.booleanValue()) ? AccordiServizioParteComuneCostanti.LABEL_RISORSE : AccordiServizioParteComuneCostanti.LABEL_RISORSE + " di " + labelASTitle;
listaParams.add(new Parameter(labelRisorse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo));
String labelRisorsa = NamingUtils.getLabelResource(risorsa);
listaParams.add(new Parameter(labelRisorsa, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_CHANGE, pIdAccordo, pNomeAccordo, pTipoAccordo,pIdRisorsa));
if(!isRequest) {
String labelResponse = AccordiServizioParteComuneCostanti.LABEL_RISPOSTE;
listaParams.add(new Parameter(labelResponse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_RISPOSTE_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo,pNomeRisorsa));
String labelRisposta = AccordiServizioParteComuneCostanti.LABEL_PARAMETRO_APC_RESOURCES_RESPONSE_STATUS +" "+
(ApiResponse.isDefaultHttpReturnCode(status)? AccordiServizioParteComuneCostanti.LABEL_PARAMETRO_APC_RESOURCES_RESPONSE_STATUS_DEFAULT : statusS);
listaParams.add(new Parameter(labelRisposta, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_RISPOSTE_CHANGE, pIdAccordo, pTipoAccordo, pNomeAccordo, pNomeRisorsa,pResponseStatus));
}
String labelRepresentation = AccordiServizioParteComuneCostanti.LABEL_REPRESENTATION;
listaParams.add(new Parameter(labelRepresentation, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_REPRESENTATIONS_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo,pNomeRisorsa,pIsRequest, pResponseStatus));
listaParams.add(new Parameter(oldMediaType, null));
// Se idhid = null, devo visualizzare la pagina per la
// modifica dati
if(ServletUtils.isEditModeInProgress(editMode)){
// setto la barra del titolo
ServletUtils.setPageDataTitle(pd,listaParams);
// Prendo i dati dell'accordo
if(mediaType == null){
mediaType = resourceRepresentationOLD.getMediaType();
descr = resourceRepresentationOLD.getDescrizione();
nome = resourceRepresentationOLD.getNome();
messageType = apcCore.toMessageMessageType(resourceRepresentationOLD.getMessageType());
tipo = resourceRepresentationOLD.getRepresentationType();
if(tipo != null) {
switch (tipo) {
case JSON:
ResourceRepresentationJson json = resourceRepresentationOLD.getJson();
tipoJson = json.getTipo();
break;
case XML:
default:
ResourceRepresentationXml xml = resourceRepresentationOLD.getXml();
namespaceXml = xml.getNamespace();
nomeXml = xml.getNome();
xmlType = xml.getXmlType();
break;
}
}
}
// preparo i campi
List<DataElement> dati = new ArrayList<>();
dati.add(ServletUtils.getDataElementForEditModeFinished());
dati = apcHelper.addAccordiResourceRepresentationToDati(tipoOp, dati, id, as.getStatoPackage(),tipoAccordo,protocollo,
protocolFactory,serviceBinding, nomeRisorsa, isRequest, statusS, idRepInt, mediaType, nome, descr, messageType, tipo, tipoJson, nomeXml, namespaceXml, xmlType);
pd.setDati(dati);
if( apcHelper.isModalitaStandard() || (apcHelper.isShowGestioneWorkflowStatoDocumenti() && StatiAccordo.finale.toString().equals(as.getStatoPackage()))){
pd.disableEditMode();
}
ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
return ServletUtils.getStrutsForwardEditModeInProgress(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.CHANGE());
}
// Controlli sui campi immessi
boolean isOk = apcHelper.accordiResourceRepresentationCheckData(tipoOp, id, nomeRisorsa, isRequest, statusS, mediaType, nome, descr, messageType, tipo, tipoJson, nomeXml, namespaceXml, xmlType, idResource,idResponse, oldMediaType);
if (!isOk) {
// setto la barra del titolo
ServletUtils.setPageDataTitle(pd,listaParams);
// preparo i campi
List<DataElement> dati = new ArrayList<>();
dati.add(ServletUtils.getDataElementForEditModeFinished());
dati = apcHelper.addAccordiResourceRepresentationToDati(tipoOp, dati, id, as.getStatoPackage(),tipoAccordo,protocollo,
protocolFactory,serviceBinding, nomeRisorsa, isRequest, statusS, idRepInt, mediaType, nome, descr, messageType, tipo, tipoJson, nomeXml, namespaceXml, xmlType);
pd.setDati(dati);
ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.CHANGE());
}
// Inserisco la risorsa nel db
ResourceRepresentation newRepresentation = new ResourceRepresentation();
newRepresentation.setMessageType(apcCore.fromMessageMessageType(messageType));
newRepresentation.setDescrizione(descr);
newRepresentation.setNome(nome);
newRepresentation.setMediaType(mediaType);
newRepresentation.setRepresentationType(tipo);
if(tipo != null) {
switch (tipo) {
case JSON:
ResourceRepresentationJson json = new ResourceRepresentationJson();
json.setTipo(tipoJson);
newRepresentation.setJson(json);
break;
case XML:
ResourceRepresentationXml xml = new ResourceRepresentationXml();
xml.setXmlType(xmlType);
xml.setNome(nomeXml);
xml.setNamespace(namespaceXml);
newRepresentation.setXml(xml);
break;
default:
break;
}
}
int idx = -1;
if(representationList != null && !representationList.isEmpty()) {
for (int i = 0; i < representationList.size(); i++) {
ResourceRepresentation resourceRepresentation = representationList.get(i);
if(resourceRepresentation.getId().intValue() == idRepInt) {
idx = i;
break;
}
}
if(idx > -1) {
representationList.remove(idx);
representationList.add(idx, newRepresentation);
}
}
// effettuo le operazioni
apcCore.performUpdateOperation(userLogin, apcHelper.smista(), as);
// Devo rileggere l'accordo dal db, perche' altrimenti
// manca l'id dei nuovi port-type
as = apcCore.getAccordoServizioFull(idAccordoLong);
// Preparo la lista
ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
risorsa = null;
for (int j = 0; j < as.sizeResourceList(); j++) {
risorsa = as.getResource(j);
if (nomeRisorsa.equals(risorsa.getNome())) {
break;
}
}
if(risorsa==null) {
throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
}
idResponse = null;
idResource = null;
resourceRequest = null;
resourceResponse = null;
if(isRequest) {
resourceRequest = risorsa.getRequest();
idResource = risorsa.getId();
} else {
if(risorsa.getResponseList() != null) {
for (int i = 0; i < risorsa.getResponseList().size(); i++) {
resourceResponse = risorsa.getResponse(i);
if (resourceResponse.getStatus() == status) {
idResponse = resourceResponse.getId();
break;
}
}
}
}
List<ResourceRepresentation> lista = apcCore.accordiResourceRepresentationsList(idResource, isRequest, idResponse, ricerca);
apcHelper.prepareAccordiResourcesRepresentationsList(id, as, lista, ricerca, tipoAccordo, isRequest, risorsa, resourceRequest, resourceResponse);
ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
return ServletUtils.getStrutsForwardEditModeFinished(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.CHANGE());
} catch (Exception e) {
return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.CHANGE());
}
}
}