Commit fe1cda84 authored by Alessandro Portosa's avatar Alessandro Portosa
Browse files

Several fixing regarding Metaweb: now it uses the catalog and schema of the...

Several fixing regarding Metaweb: now it uses the catalog and schema of the datasource, changes on getting the quote delimiter
parent 99ee5f8c
......@@ -17,23 +17,6 @@
*/
package it.eng.spagobi.api.v2;
import it.eng.spago.error.EMFUserError;
import it.eng.spago.security.IEngUserProfile;
import it.eng.spagobi.api.AbstractSpagoBIResource;
import it.eng.spagobi.commons.constants.SpagoBIConstants;
import it.eng.spagobi.commons.dao.DAOFactory;
import it.eng.spagobi.commons.utilities.messages.MessageBuilder;
import it.eng.spagobi.services.rest.annotations.ManageAuthorization;
import it.eng.spagobi.services.rest.annotations.UserConstraint;
import it.eng.spagobi.services.serialization.JsonConverter;
import it.eng.spagobi.tools.datasource.bo.DataSource;
import it.eng.spagobi.tools.datasource.bo.DataSourceModel;
import it.eng.spagobi.tools.datasource.bo.IDataSource;
import it.eng.spagobi.tools.datasource.dao.IDataSourceDAO;
import it.eng.spagobi.utilities.exceptions.SpagoBIRestServiceException;
import it.eng.spagobi.utilities.exceptions.SpagoBIServiceException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
......@@ -44,8 +27,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.naming.AuthenticationException;
import javax.naming.Context;
......@@ -75,12 +56,15 @@ import it.eng.spago.security.IEngUserProfile;
import it.eng.spagobi.api.AbstractSpagoBIResource;
import it.eng.spagobi.commons.constants.SpagoBIConstants;
import it.eng.spagobi.commons.dao.DAOFactory;
import it.eng.spagobi.commons.utilities.messages.MessageBuilder;
import it.eng.spagobi.services.rest.annotations.UserConstraint;
import it.eng.spagobi.services.serialization.JsonConverter;
import it.eng.spagobi.tools.datasource.bo.DataSource;
import it.eng.spagobi.tools.datasource.bo.DataSourceModel;
import it.eng.spagobi.tools.datasource.bo.IDataSource;
import it.eng.spagobi.tools.datasource.dao.IDataSourceDAO;
import it.eng.spagobi.utilities.database.DataBase;
import it.eng.spagobi.utilities.database.IDataBase;
import it.eng.spagobi.utilities.exceptions.SpagoBIRestServiceException;
import it.eng.spagobi.utilities.exceptions.SpagoBIServiceException;
......@@ -252,13 +236,12 @@ public class DataSourceResource extends AbstractSpagoBIResource {
} else {
message += ", " + objName;
}
if(i == objNames.get(key).size()-1){
if (i == objNames.get(key).size() - 1) {
message += ") ";
}
i++;
}
message += "\n";
}
}
......@@ -350,52 +333,53 @@ public class DataSourceResource extends AbstractSpagoBIResource {
return tableContent.toString();
}
private JSONObject getTableMetadata(Connection conn) throws HibernateException, JSONException, SQLException {
JSONObject tableContent = new JSONObject();
ResultSet rs = null;
try {
DatabaseMetaData meta = conn.getMetaData();
if (conn.getMetaData().getDatabaseProductName().toLowerCase().contains("oracle")) {
// String q =
// "SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS WHERE OWNER = '"
// + userName + "'";
String q = "SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM USER_TAB_COLUMNS";
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(q);
while (rs.next()) {
if (!tableContent.has(rs.getString(1))) {
tableContent.put(rs.getString(1), new JSONObject());
}
tableContent.getJSONObject(rs.getString(1)).put(rs.getString(2), rs.getString(3));
}
} else {
final String[] TYPES = { "TABLE", "VIEW" };
final String tableNamePattern = "%";
final String catalog = null;
rs = meta.getTables(catalog, null, tableNamePattern, TYPES);
while (rs.next()) {
String tableName = rs.getString(3);
JSONObject column = new JSONObject();
ResultSet tabCol = meta.getColumns(rs.getString(1), rs.getString(2), tableName, "%");
while (tabCol.next()) {
column.put(tabCol.getString(4), "null");
}
tabCol.close();
tableContent.put(tableName, column);
if (conn.getMetaData().getDatabaseProductName().toLowerCase().contains("oracle")) {
// String q =
// "SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS WHERE OWNER = '"
// + userName + "'";
String q = "SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM USER_TAB_COLUMNS";
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(q);
while (rs.next()) {
if (!tableContent.has(rs.getString(1))) {
tableContent.put(rs.getString(1), new JSONObject());
}
tableContent.getJSONObject(rs.getString(1)).put(rs.getString(2), rs.getString(3));
}
} finally {
if (rs != null) {
rs.close();
}
if (!conn.isClosed()) {
conn.close();
} else {
final String[] TYPES = { "TABLE", "VIEW" };
final String tableNamePattern = "%";
final IDataBase database = DataBase.getDataBase(dataSource);
final String catalog = database.getCatalog(conn);
final String schema = database.getSchema(conn);
logger.debug("This connection has been configured with the catalog [" + catalog + "] and schema [" + schema + "]");
rs = meta.getTables(catalog, schema, tableNamePattern, TYPES);
while (rs.next()) {
String tableName = rs.getString(3);
JSONObject column = new JSONObject();
ResultSet tabCol = meta.getColumns(rs.getString(1), rs.getString(2), tableName, "%");
while (tabCol.next()) {
column.put(tabCol.getString(4), "null");
}
tabCol.close();
tableContent.put(tableName, column);
}
}
} finally {
if (rs != null) {
rs.close();
}
if (!conn.isClosed()) {
conn.close();
}
}
return tableContent;
}
......
......@@ -17,6 +17,15 @@
*/
package it.eng.knowage.meta.generator.jpamapping.wrappers.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaColumn;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaRelationship;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable;
......@@ -29,23 +38,15 @@ import it.eng.knowage.meta.model.business.BusinessRelationship;
import it.eng.knowage.meta.model.physical.PhysicalTable;
import it.eng.knowage.meta.model.util.JDBCTypeMapper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract class extended by <code>JpaTable</code> and <code>JpaViewInnerTable</code>
*
*
* @author Andrea Gioia (andrea.gioia@eng.it)
*/
public abstract class AbstractJpaTable implements IJpaTable {
PhysicalTable physicalTable;
protected String quoteString;
// cache
List<IJpaColumn> jpaColumns;
......@@ -107,7 +108,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#getRelationships()
*/
@Override
......@@ -133,7 +134,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/**
* TODO .. da implementare
*
*
* @return
*/
@Override
......@@ -149,7 +150,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#getPackage()
*/
@Override
......@@ -175,7 +176,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#getCompositeKeyClassName()
*/
@Override
......@@ -190,7 +191,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#getQualifiedClassName()
*/
@Override
......@@ -209,7 +210,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#getSimpleColumns(boolean, boolean, boolean)
*/
@Override
......@@ -264,7 +265,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#getSimpleColumns()
*/
@Override
......@@ -274,7 +275,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#getImportStatements()
*/
@Override
......@@ -309,7 +310,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/**
* @return the composite key property name
*
*
* * TODO .... da implementare
*/
@Override
......@@ -334,7 +335,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#getPrimaryKeyColumns()
*/
@Override
......@@ -383,7 +384,7 @@ public abstract class AbstractJpaTable implements IJpaTable {
}
/**
*
*
* @return the expresion that compute the hash code for a given primary key
*/
public String getPrimaryKeyHashCodeClause() {
......@@ -413,4 +414,38 @@ public abstract class AbstractJpaTable implements IJpaTable {
return null;
}
@Override
public String getCatalog() {
String useCatalog = getUseCatalog();
if ((useCatalog != null) && (useCatalog.equalsIgnoreCase("true"))) {
logger.debug("Catalog is: " + getModel().getPhysicalModel().getCatalog());
String catalog = getModel().getPhysicalModel().getCatalog();
if (catalog != null) {
if (!quoteString.equals(" ")) {
catalog = quoteString + catalog + quoteString;
}
}
return catalog;
} else {
return null;
}
}
@Override
public String getSchema() {
String useSchema = getUseSchema();
if ((useSchema != null) && (useSchema.equalsIgnoreCase("true"))) {
logger.debug("Schema is: " + getModel().getPhysicalModel().getSchema());
String schema = getModel().getPhysicalModel().getSchema();
if (schema != null && !schema.equals("")) {
if (!quoteString.equals(" ")) {
schema = quoteString + schema + quoteString;
}
}
return schema;
} else {
return null;
}
}
}
......@@ -17,6 +17,12 @@
*/
package it.eng.knowage.meta.generator.jpamapping.wrappers.impl;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaCalculatedColumn;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaColumn;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaRelationship;
......@@ -34,21 +40,14 @@ import it.eng.knowage.meta.model.business.SimpleBusinessColumn;
import it.eng.knowage.meta.model.physical.PhysicalModel;
import it.eng.knowage.meta.model.physical.PhysicalTable;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class wrap a business table and provide all the utility methods used by the template engine in order to generate the java class mapping
*
*
* @author Andrea Gioia (andrea.gioia@eng.it)
*/
public class JpaTable extends AbstractJpaTable {
BusinessTable businessTable;
String quoteString;
List<IJpaSubEntity> allSubEntities = new ArrayList<IJpaSubEntity>();
List<BusinessColumnSet> parents;
List<IJpaCalculatedColumn> jpaCalculatedColumns;
......@@ -91,45 +90,6 @@ public class JpaTable extends AbstractJpaTable {
return businessTable;
}
@Override
public String getCatalog() {
String useCatalog = getUseCatalog();
if ((useCatalog != null) && (useCatalog.equalsIgnoreCase("true"))) {
logger.debug("Catalog is: " + getModel().getPhysicalModel().getCatalog());
String catalog = getModel().getPhysicalModel().getCatalog();
if (catalog != null) {
if (!quoteString.equals(" ")) {
catalog = "`" + catalog + "`";
}
}
// if(catalog!=null && !catalog.equals("") && getModel().getPhysicalModel().getDatabaseName().contains("PostgreSQL")){
// catalog = "\\\""+catalog+"\\\"";
// }
return catalog;
} else {
return null;
}
}
@Override
public String getSchema() {
String useSchema = getUseSchema();
if ((useSchema != null) && (useSchema.equalsIgnoreCase("true"))) {
logger.debug("Schema is: " + getModel().getPhysicalModel().getSchema());
String schema = getModel().getPhysicalModel().getSchema();
if (schema != null && !schema.equals("")) {
if (!quoteString.equals(" ")) {
schema = quoteString + schema + quoteString;
}
}
return schema;
} else {
return null;
}
}
@Override
public PhysicalTable getPhysicalTable() {
return businessTable.getPhysicalTable();
......@@ -168,7 +128,7 @@ public class JpaTable extends AbstractJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#hasFakePrimaryKey()
*/
@Override
......@@ -178,7 +138,7 @@ public class JpaTable extends AbstractJpaTable {
/*
* (non-Javadoc)
*
*
* @see it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaTable#hasCompositeKey()
*/
@Override
......
......@@ -17,6 +17,13 @@
*/
package it.eng.knowage.meta.generator.jpamapping.wrappers.impl;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaCalculatedColumn;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaColumn;
import it.eng.knowage.meta.generator.jpamapping.wrappers.IJpaRelationship;
......@@ -35,13 +42,6 @@ import it.eng.knowage.meta.model.physical.PhysicalColumn;
import it.eng.knowage.meta.model.physical.PhysicalModel;
import it.eng.knowage.meta.model.physical.PhysicalTable;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* This class wrap a physical table used within a business view and provide all the utility methods used by the template engine in order to generate the java
......@@ -54,7 +54,6 @@ public class JpaViewInnerTable extends AbstractJpaTable {
private BusinessView businessView;
private List<IJpaCalculatedColumn> jpaCalculatedColumns;
private List<BusinessColumn> businessColumnOfInnerTable;
String quoteString;
private static Logger logger = LoggerFactory.getLogger(JpaViewInnerTable.class);
......@@ -298,36 +297,6 @@ public class JpaViewInnerTable extends AbstractJpaTable {
return false;
}
@Override
public String getCatalog() {
String useCatalog = getUseCatalog();
if ((useCatalog != null) && (useCatalog.equalsIgnoreCase("true"))) {
String catalog = getModel().getPhysicalModel().getCatalog();
if (catalog != null && !catalog.equals("")) {
catalog = "`" + catalog + "`";
}
return catalog;
} else {
return null;
}
}
@Override
public String getSchema() {
String useSchema = getUseSchema();
if ((useSchema != null) && (useSchema.equalsIgnoreCase("true"))) {
String schema = getModel().getPhysicalModel().getSchema();
if (schema != null && !schema.equals("")) {
schema = "`" + schema + "`";
}
return schema;
} else {
return null;
}
}
@Override
public String getName() {
return businessView.getName() + " > " + StringUtils.initUpper(physicalTable.getName().replace("_", " "));
......@@ -343,12 +312,6 @@ public class JpaViewInnerTable extends AbstractJpaTable {
return physicalTable.getName();
}
@Override
public String getQuotedMappingTableName() {
String name = physicalTable.getName();
return "`" + name + "`";
}
@Override
public String getAttribute(String name) {
ModelProperty property = businessView.getProperties().get(name);
......@@ -389,4 +352,13 @@ public class JpaViewInnerTable extends AbstractJpaTable {
return property != null ? property.getValue() : "";
}
@Override
public String getQuotedMappingTableName() {
String name = physicalTable.getName();
if (!quoteString.equals(" ")) {
name = quoteString + name + quoteString;
}
return name;
}
}
......@@ -17,24 +17,6 @@
*/
package it.eng.knowage.meta.initializer;
import it.eng.knowage.meta.initializer.properties.IPropertiesInitializer;
import it.eng.knowage.meta.initializer.properties.PhysicalModelPropertiesFromFileInitializer;
import it.eng.knowage.meta.model.Model;
import it.eng.knowage.meta.model.ModelObject;
import it.eng.knowage.meta.model.ModelProperty;
import it.eng.knowage.meta.model.business.BusinessIdentifier;
import it.eng.knowage.meta.model.business.BusinessRelationship;
import it.eng.knowage.meta.model.physical.PhysicalColumn;
import it.eng.knowage.meta.model.physical.PhysicalForeignKey;
import it.eng.knowage.meta.model.physical.PhysicalModel;
import it.eng.knowage.meta.model.physical.PhysicalModelFactory;
import it.eng.knowage.meta.model.physical.PhysicalPrimaryKey;
import it.eng.knowage.meta.model.physical.PhysicalTable;
import it.eng.knowage.meta.model.util.JDBCTypeMapper;
import it.eng.spagobi.commons.dao.DAOFactory;
import it.eng.spagobi.tools.datasource.bo.DataSource;
import it.eng.spagobi.tools.datasource.dao.IDataSourceDAO;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
......@@ -55,6 +37,26 @@ import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
import it.eng.knowage.meta.initializer.properties.IPropertiesInitializer;
import it.eng.knowage.meta.initializer.properties.PhysicalModelPropertiesFromFileInitializer;
import it.eng.knowage.meta.model.Model;
import it.eng.knowage.meta.model.ModelObject;
import it.eng.knowage.meta.model.ModelProperty;
import it.eng.knowage.meta.model.business.BusinessIdentifier;
import it.eng.knowage.meta.model.business.BusinessRelationship;
import it.eng.knowage.meta.model.physical.PhysicalColumn;
import it.eng.knowage.meta.model.physical.PhysicalForeignKey;
import it.eng.knowage.meta.model.physical.PhysicalModel;
import it.eng.knowage.meta.model.physical.PhysicalModelFactory;
import it.eng.knowage.meta.model.physical.PhysicalPrimaryKey;
import it.eng.knowage.meta.model.physical.PhysicalTable;
import it.eng.knowage.meta.model.util.JDBCTypeMapper;
import it.eng.spagobi.commons.dao.DAOFactory;
import it.eng.spagobi.tools.datasource.bo.IDataSource;
import it.eng.spagobi.tools.datasource.dao.IDataSourceDAO;
import it.eng.spagobi.utilities.database.DataBase;
import it.eng.spagobi.utilities.database.IDataBase;
/**
* @author Andrea Gioia (andrea.gioia@eng.it)
*
......@@ -74,7 +76,7 @@ public class PhysicalModelInitializer {
}
public PhysicalModel initializeLigth(Connection conn, List<String> selectedTables) {
public PhysicalModel initializeLigth(PhysicalModel originalPM, List<String> selectedTables) {
PhysicalModel model;
DatabaseMetaData dbMeta;
model = FACTORY.createPhysicalModel();
......@@ -84,11 +86,13 @@ public class PhysicalModelInitializer {
}