diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/index/dao/IndexDao.java b/arc-web/src/main/java/fr/insee/arc/web/gui/index/dao/IndexDao.java index d9ead523c..75ceae017 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/index/dao/IndexDao.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/index/dao/IndexDao.java @@ -29,29 +29,16 @@ public IndexDao(VObjectService vObjectService, DataObjectService dataObjectServi */ public void initializeViewIndex(VObject viewIndex) { ViewEnum dataModelIndex = ViewEnum.EXT_ETAT_JEUDEREGLE; + String nameOfViewIndex = dataObjectService.getView(dataModelIndex); // view query - ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); - query.append(SQL.SELECT); - query.append("nullif(substring(id from '[0123456789]+'),'')"); // ? - query.append(SQL.CAST_OPERATOR); - query.append("int"); - query.append(SQL.AS); - query.append(ColumnEnum.ID); - query.append(SQL.COMMA); - query.append("upper(substring(id from '\\.(.*)'))"); // ? - query.append(SQL.AS); - query.append(ColumnEnum.VAL); - query.append(SQL.COMMA); - query.append(ColumnEnum.ENV_DESCRIPTION); - query.append(SQL.FROM); - query.append(dataObjectService.getView(dataModelIndex)); - query.append(SQL.WHERE); - query.append(ColumnEnum.ISENV); + query.build(SQL.SELECT, "nullif(substring(id from '[0123456789]+'),'')", SQL.CAST_OPERATOR, "int", SQL.AS, ColumnEnum.ID, SQL.COMMA); + query.build("upper(substring(id from '\\.(.*)'))", SQL.AS, ColumnEnum.VAL, SQL.COMMA, ColumnEnum.ENV_DESCRIPTION); + query.build(SQL.FROM, nameOfViewIndex, SQL.WHERE, ColumnEnum.ISENV); // default value HashMap defaultInputFields = new HashMap<>(); // initialize vobject - vObjectService.initialize(viewIndex, query, dataObjectService.getView(dataModelIndex), defaultInputFields); + vObjectService.initialize(viewIndex, query, nameOfViewIndex, defaultInputFields); } } diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceparametre/dao/MaintenanceParametreDao.java b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceparametre/dao/MaintenanceParametreDao.java index fcb3df36b..1619b4204 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceparametre/dao/MaintenanceParametreDao.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceparametre/dao/MaintenanceParametreDao.java @@ -3,6 +3,7 @@ import java.util.HashMap; import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder; +import fr.insee.arc.core.dataobjects.ColumnEnum; import fr.insee.arc.core.dataobjects.DataObjectService; import fr.insee.arc.core.dataobjects.ViewEnum; import fr.insee.arc.utils.dao.SQL; @@ -28,20 +29,16 @@ public MaintenanceParametreDao(VObjectService vObjectService, DataObjectService */ public void initializeViewParameters(VObject viewParameters) { ViewEnum dataModelParameters = ViewEnum.PARAMETER; + String nameOfViewParameters = dataObjectService.getView(dataModelParameters); // view query ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); - query.append(SQL.SELECT); - query.append("row_number() over (order by description,key,val)"); // ? - query.append(SQL.AS); - query.append("i"); - query.append(SQL.COMMA); - query.append(query.sqlListeOfColumnsFromModel(dataModelParameters)); - query.append(SQL.FROM); - query.append(dataObjectService.getView(dataModelParameters)); + StringBuilder columns = query.sqlListeOfColumnsFromModel(dataModelParameters); + query.build(SQL.SELECT, "row_number() over (order by description,key,val)", SQL.AS, ColumnEnum.I, SQL.COMMA, columns); + query.build(SQL.FROM, nameOfViewParameters); // default value HashMap defaultInputFields = new HashMap<>(); // initialize vobject - vObjectService.initialize(viewParameters, query, dataObjectService.getView(dataModelParameters), defaultInputFields); + vObjectService.initialize(viewParameters, query, nameOfViewParameters, defaultInputFields); } } diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/query/dao/QueryDao.java b/arc-web/src/main/java/fr/insee/arc/web/gui/query/dao/QueryDao.java new file mode 100644 index 000000000..ad7f75776 --- /dev/null +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/query/dao/QueryDao.java @@ -0,0 +1,84 @@ +package fr.insee.arc.web.gui.query.dao; + +import java.util.HashMap; + +import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder; +import fr.insee.arc.core.dataobjects.ColumnEnum; +import fr.insee.arc.core.dataobjects.DataObjectService; +import fr.insee.arc.core.dataobjects.ViewEnum; +import fr.insee.arc.utils.dao.SQL; +import fr.insee.arc.utils.dao.UtilitaireDao; +import fr.insee.arc.web.gui.all.util.VObject; +import fr.insee.arc.web.gui.all.util.VObjectHelperDao; +import fr.insee.arc.web.gui.all.util.VObjectService; + +public class QueryDao extends VObjectHelperDao { + + private VObjectService vObjectService; + private DataObjectService dataObjectService; + + public QueryDao(VObjectService vObjectService, DataObjectService dataObjectService) { + super(); + this.vObjectService = vObjectService; + this.dataObjectService = dataObjectService; + } + + /** + * dao call to build query vobject + * + * @param viewWsContext + */ + public void initializeQuery(VObject viewQuery, String myQuery) { + HashMap defaultInputFields = new HashMap<>(); + + if (myQuery!=null){ + String m=myQuery.trim(); + if (m.endsWith(";")) + { + m=m.substring(0, m.length()-1); + } + + ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(m); + + if (Boolean.TRUE.equals(UtilitaireDao.get(0).testResultRequest(null, query))) + { + this.vObjectService.initialize(viewQuery, query, "arc.ihm_query", defaultInputFields); + } + else + { + try { + UtilitaireDao.get(0).executeImmediate(null, myQuery); + this.vObjectService.destroy(viewQuery); + viewQuery.setMessage("query.complete"); + } catch (Exception e) { + this.vObjectService.destroy(viewQuery); + viewQuery.setMessage(e.getMessage()); + } + + } + } + } + + /** + * dao call to build tables vobject + * + * @param viewWsContext + */ + public void initializeTable(VObject viewTable, String mySchema) { + ViewEnum dataModelTable = ViewEnum.PG_TABLES; + String nameOfViewTable = dataObjectService.getView(dataModelTable); + // view query + ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); + query.build(SQL.SELECT, ColumnEnum.TABLENAME, SQL.FROM, nameOfViewTable); + query.build(SQL.WHERE, ColumnEnum.SCHEMANAME, "='" + mySchema + "'"); + // default value + HashMap defaultInputFields = new HashMap<>(); + // initialize vobject + vObjectService.initialize(viewTable, query, "arc.ihm_table", defaultInputFields); + } + + public static String queryTableSelected(String mySchema, String tableName) { + return "select * from " + mySchema + "." + tableName + " limit 10 "; + } + +} diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/query/service/InteractorQuery.java b/arc-web/src/main/java/fr/insee/arc/web/gui/query/service/InteractorQuery.java index 000131f9e..d0a1d6b40 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/query/service/InteractorQuery.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/query/service/InteractorQuery.java @@ -1,7 +1,7 @@ package fr.insee.arc.web.gui.query.service; -import java.util.HashMap; - +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; @@ -9,10 +9,11 @@ import org.springframework.ui.Model; import org.springframework.web.context.WebApplicationContext; -import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder; -import fr.insee.arc.utils.dao.UtilitaireDao; import fr.insee.arc.utils.textUtils.IConstanteCaractere; +import fr.insee.arc.utils.utils.LoggerHelper; import fr.insee.arc.web.gui.all.service.ArcWebGenericService; +import fr.insee.arc.web.gui.all.util.VObject; +import fr.insee.arc.web.gui.query.dao.QueryDao; import fr.insee.arc.web.gui.query.model.ModelQuery; @Service @@ -22,6 +23,7 @@ public class InteractorQuery extends ArcWebGenericService implements protected static final String RESULT_SUCCESS = "/jsp/gererQuery.jsp"; private static final String DEFAULT_SCHEMA = "arc"; + private static final Logger LOGGER = LogManager.getLogger(InteractorQuery.class); protected String myQuery; @@ -29,9 +31,14 @@ public class InteractorQuery extends ArcWebGenericService implements @Autowired protected ModelQuery views; + + private QueryDao dao; @Override protected void putAllVObjects(ModelQuery model) { + + dao = new QueryDao(vObjectService, dataObjectService); + views.setViewQuery(vObjectService.preInitialize(model.getViewQuery())); views.setViewTable(vObjectService.preInitialize(model.getViewTable())); @@ -42,8 +49,8 @@ protected void putAllVObjects(ModelQuery model) { } this.myQuery = model.getMyQuery(); - putVObject(views.getViewQuery(), t -> initializeQuery()); - putVObject(views.getViewTable(), t -> initializeTable()); + putVObject(views.getViewQuery(), t -> initializeQuery(t)); + putVObject(views.getViewTable(), t -> initializeTable(t)); } @Override @@ -57,44 +64,16 @@ protected String getActionName() { return "databaseManagement"; } - public void initializeQuery() { - HashMap defaultInputFields = new HashMap<>(); - - if (this.myQuery!=null){ - String m=this.myQuery.trim(); - if (m.endsWith(";")) - { - m=m.substring(0, m.length()-1); - } - - ArcPreparedStatementBuilder requete=new ArcPreparedStatementBuilder(m); - - if (Boolean.TRUE.equals(UtilitaireDao.get(0).testResultRequest(null, requete))) - { - this.vObjectService.initialize(views.getViewQuery(), requete, "arc.ihm_query", defaultInputFields); - } - else - { - try { - UtilitaireDao.get(0).executeImmediate(null, this.myQuery); - this.vObjectService.destroy(views.getViewQuery()); - this.views.getViewQuery().setMessage("query.complete"); - } catch (Exception e) { - this.vObjectService.destroy(views.getViewQuery()); - this.views.getViewQuery().setMessage(e.getMessage()); - } - - } - } - + public void initializeQuery(VObject viewQuery) { + LoggerHelper.debug(LOGGER, "/* initializeQuery */"); + dao.initializeTable(viewQuery, this.myQuery); } // Table list - public void initializeTable() { - HashMap defaultInputFields = new HashMap<>(); - this.vObjectService.initialize(views.getViewTable(), new ArcPreparedStatementBuilder("select tablename from pg_tables where schemaname='" + this.mySchema+"'"), "arc.ihm_Table", defaultInputFields); - + public void initializeTable(VObject viewTable) { + LoggerHelper.debug(LOGGER, "/* initializeTable */"); + dao.initializeTable(viewTable, this.mySchema); } } \ No newline at end of file diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/query/service/ServiceViewTable.java b/arc-web/src/main/java/fr/insee/arc/web/gui/query/service/ServiceViewTable.java index 855bd900c..3a984877c 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/query/service/ServiceViewTable.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/query/service/ServiceViewTable.java @@ -6,13 +6,16 @@ import org.springframework.stereotype.Service; import org.springframework.ui.Model; +import fr.insee.arc.core.dataobjects.ColumnEnum; +import fr.insee.arc.web.gui.query.dao.QueryDao; + @Service public class ServiceViewTable extends InteractorQuery { public String seeTable(Model model) { HashMap> mapContentSelected = views.getViewTable().mapContentSelected(); if (!mapContentSelected.isEmpty()) { - this.myQuery = "select * from " + this.mySchema+"." + mapContentSelected.get("tablename").get(0) + " limit 10 "; + this.myQuery = QueryDao.queryTableSelected(mySchema, mapContentSelected.get(ColumnEnum.TABLENAME.getColumnName()).get(0)); model.addAttribute("myQuery", myQuery); } return basicAction(model, RESULT_SUCCESS); diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/webservice/dao/WebserviceDao.java b/arc-web/src/main/java/fr/insee/arc/web/gui/webservice/dao/WebserviceDao.java new file mode 100644 index 000000000..13ac69d0b --- /dev/null +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/webservice/dao/WebserviceDao.java @@ -0,0 +1,60 @@ +package fr.insee.arc.web.gui.webservice.dao; + +import java.util.HashMap; + +import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder; +import fr.insee.arc.core.dataobjects.ColumnEnum; +import fr.insee.arc.core.dataobjects.DataObjectService; +import fr.insee.arc.core.dataobjects.ViewEnum; +import fr.insee.arc.utils.dao.SQL; +import fr.insee.arc.web.gui.all.util.VObject; +import fr.insee.arc.web.gui.all.util.VObjectHelperDao; +import fr.insee.arc.web.gui.all.util.VObjectService; + +public class WebserviceDao extends VObjectHelperDao { + + private VObjectService vObjectService; + private DataObjectService dataObjectService; + + public WebserviceDao(VObjectService vObjectService, DataObjectService dataObjectService) { + super(); + this.vObjectService = vObjectService; + this.dataObjectService = dataObjectService; + } + + /** + * dao call to build webservice vobject + * + * @param viewWsContext + */ + public void initializeWebserviceContext(VObject viewWsContext) { + ViewEnum dataModelWsContext = ViewEnum.IHM_WS_CONTEXT; + String nameOfViewWsContext = dataObjectService.getView(dataModelWsContext); + // view query + ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); + query.build(SQL.SELECT, "*", SQL.FROM, nameOfViewWsContext); + // default value + HashMap defaultInputFields = new HashMap<>(); + // initialize vobject + vObjectService.initialize(viewWsContext, query, nameOfViewWsContext, defaultInputFields); + } + + /** + * dao call to build selected webservice vobject + * + * @param viewWsQuery + */ + public void initializeWebserviceQuery(VObject viewWsQuery) { + ViewEnum dataModelWsQuery = ViewEnum.IHM_WS_QUERY; + String nameOfViewWsQuery = dataObjectService.getView(dataModelWsQuery); + // view query + ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); + query.build(SQL.SELECT, "*", SQL.FROM, nameOfViewWsQuery); + query.build(SQL.WHERE, sqlEqualWithFirstSelectedRecord(ColumnEnum.SERVICE_NAME), SQL.AND, sqlEqualWithFirstSelectedRecord(ColumnEnum.CALL_ID)); + // default value + HashMap defaultInputFields = buildDefaultInputFieldsWithFirstSelectedRecord(ColumnEnum.SERVICE_NAME, ColumnEnum.CALL_ID); + // initialize vobject + vObjectService.initialize(viewWsQuery, query, nameOfViewWsQuery, defaultInputFields); + } + +} diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/webservice/service/InteractorWebservice.java b/arc-web/src/main/java/fr/insee/arc/web/gui/webservice/service/InteractorWebservice.java index 21a918470..8eba1995e 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/webservice/service/InteractorWebservice.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/webservice/service/InteractorWebservice.java @@ -1,7 +1,6 @@ package fr.insee.arc.web.gui.webservice.service; import java.util.ArrayList; -import java.util.HashMap; import java.util.Map; import org.apache.logging.log4j.LogManager; @@ -12,9 +11,10 @@ import org.springframework.stereotype.Service; import org.springframework.web.context.WebApplicationContext; -import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder; +import fr.insee.arc.utils.utils.LoggerHelper; import fr.insee.arc.web.gui.all.service.ArcWebGenericService; import fr.insee.arc.web.gui.all.util.VObject; +import fr.insee.arc.web.gui.webservice.dao.WebserviceDao; import fr.insee.arc.web.gui.webservice.model.ModelWebservice; @Service @@ -26,62 +26,45 @@ public class InteractorWebservice extends ArcWebGenericService @Autowired protected ModelWebservice views; + + private WebserviceDao dao; @Override public void putAllVObjects(ModelWebservice model) { + + dao = new WebserviceDao(vObjectService, dataObjectService); + views.setViewWebserviceContext(vObjectService.preInitialize(model.getViewWebserviceContext())); views.setViewWebserviceQuery(vObjectService.preInitialize(model.getViewWebserviceQuery())); - putVObject(views.getViewWebserviceContext(), - t -> initializeWebserviceContext(t, "arc.ihm_ws_context")); - // - putVObject(views.getViewWebserviceQuery(), t -> initializeWebserviceQuery(t, views.getViewWebserviceContext(), - "arc.ihm_ws_query")); + putVObject(views.getViewWebserviceContext(), t -> initializeWebserviceContext(t)); + putVObject(views.getViewWebserviceQuery(), t -> initializeWebserviceQuery(t, views.getViewWebserviceContext())); } - - /** * Retourne le nom des tables de WebserviceQuery présentes dans la base ainsi que le descriptif associé à chaque table */ - public void initializeWebserviceContext(VObject c, String t) { - loggerDispatcher.debug("/* initializeWebserviceContext */", LOGGER); - HashMap defaultInputFields = new HashMap<>(); - - - ArcPreparedStatementBuilder requete = new ArcPreparedStatementBuilder(); - requete.append("\n SELECT * FROM "+t+" "); - - vObjectService.initialize(c, requete, t, defaultInputFields); + public void initializeWebserviceContext(VObject viewWsContext) { + LoggerHelper.debug(LOGGER, "/* initializeWebserviceContext */"); + dao.initializeWebserviceContext(viewWsContext); } - - - private void initializeWebserviceQuery(VObject c, VObject d, String t) { + private void initializeWebserviceQuery(VObject viewWsQuery, VObject viewWsContext) { // visual des Calendriers - loggerDispatcher.debug("/* initializeWebserviceQuery */",LOGGER); - - Map> selection = d.mapContentSelected(); - - if (!selection.isEmpty()) { - HashMap type = d.mapHeadersType(); - - // requete de la vue - ArcPreparedStatementBuilder requete = new ArcPreparedStatementBuilder(); - requete.append("\n SELECT * FROM "+t+" "); - requete.append("\n WHERE service_name " + requete.sqlEqual(selection.get("service_name").get(0), type.get("service_name")) + " "); - requete.append("\n AND call_id " + requete.sqlEqual(selection.get("call_id").get(0), type.get("call_id")) + " "); - requete.append("\n "); + LoggerHelper.debug(LOGGER, "/* initializeWebserviceQuery */"); - // // construction des valeurs par défaut pour les ajouts - HashMap defaultInputFields = new HashMap(); - defaultInputFields.put("service_name", selection.get("service_name").get(0)); - defaultInputFields.put("call_id", selection.get("call_id").get(0)); + // get the webservice selected records + Map> viewWsContextSelectedRecords = viewWsContext.mapContentSelected(); - this.vObjectService.initialize(c, requete, "arc.ihm_ws_query", defaultInputFields); + // if a webservice is selected, trigger the call to dao to construct query view + if (!viewWsContextSelectedRecords.isEmpty()) { + + dao.setSelectedRecords(viewWsContextSelectedRecords); + dao.initializeWebserviceQuery(viewWsQuery); + } else { - this.vObjectService.destroy(c); + vObjectService.destroy(viewWsQuery); } } diff --git a/arc-web/src/test/java/fr/insee/arc/web/gui/norme/dao/GererNormeDaoTest.java b/arc-web/src/test/java/fr/insee/arc/web/gui/norme/dao/GererNormeDaoTest.java index a9b256239..ac89c3441 100644 --- a/arc-web/src/test/java/fr/insee/arc/web/gui/norme/dao/GererNormeDaoTest.java +++ b/arc-web/src/test/java/fr/insee/arc/web/gui/norme/dao/GererNormeDaoTest.java @@ -124,8 +124,8 @@ public void initializeViewChargement() throws ArcException { // select the first record of viewRulesSet and set it as the selected record ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); - query.build(SQL.SELECT, "*", SQL.FROM, pdao.getDataObjectService().getView(ViewEnum.IHM_JEUDEREGLE), - SQL.WHERE, "id_norme='v2008-11'", SQL.AND, "version='vConformite'"); + query.build(SQL.SELECT, "*", SQL.FROM, pdao.getDataObjectService().getView(ViewEnum.IHM_JEUDEREGLE)); + query.build(SQL.WHERE, "id_norme='v2008-11'", SQL.AND, "version='vConformite'"); Map> viewRulesSetSelectedRecords = new GenericBean(UtilitaireDao.get(0).executeRequest(c, query)).mapContent(); pdao.setSelectedRecords(viewRulesSetSelectedRecords);