Skip to content

Commit

Permalink
DAOs query & webservice
Browse files Browse the repository at this point in the history
  • Loading branch information
lmanelphe committed Sep 29, 2023
1 parent 24d6773 commit 85dde55
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 110 deletions.
23 changes: 5 additions & 18 deletions arc-web/src/main/java/fr/insee/arc/web/gui/index/dao/IndexDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> defaultInputFields = new HashMap<>();
// initialize vobject
vObjectService.initialize(viewIndex, query, dataObjectService.getView(dataModelIndex), defaultInputFields);
vObjectService.initialize(viewIndex, query, nameOfViewIndex, defaultInputFields);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> defaultInputFields = new HashMap<>();
// initialize vobject
vObjectService.initialize(viewParameters, query, dataObjectService.getView(dataModelParameters), defaultInputFields);
vObjectService.initialize(viewParameters, query, nameOfViewParameters, defaultInputFields);
}

}
84 changes: 84 additions & 0 deletions arc-web/src/main/java/fr/insee/arc/web/gui/query/dao/QueryDao.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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 ";
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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;
import org.springframework.stereotype.Service;
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
Expand All @@ -22,16 +23,22 @@ public class InteractorQuery extends ArcWebGenericService<ModelQuery> 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;

protected String mySchema;

@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()));

Expand All @@ -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
Expand All @@ -57,44 +64,16 @@ protected String getActionName() {
return "databaseManagement";
}

public void initializeQuery() {
HashMap<String, String> 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<String, String> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ArrayList<String>> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> defaultInputFields = buildDefaultInputFieldsWithFirstSelectedRecord(ColumnEnum.SERVICE_NAME, ColumnEnum.CALL_ID);
// initialize vobject
vObjectService.initialize(viewWsQuery, query, nameOfViewWsQuery, defaultInputFields);
}

}
Loading

0 comments on commit 85dde55

Please sign in to comment.