blob: 1ba6e005441289c94a5443f438c28f7dc5ac176d [file] [log] [blame]
package org.eclipse.osbp.authentication.account.datamarts;
import com.vaadin.ui.Notification;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.eclipse.osbp.bpm.api.BPMTaskSummary;
import org.eclipse.osbp.core.api.persistence.IPersistenceService;
import org.eclipse.osbp.dsl.common.datatypes.IDto;
import org.eclipse.osbp.preferences.ProductConfiguration;
import org.eclipse.osbp.runtime.common.event.IDualData;
import org.eclipse.osbp.runtime.common.i18n.ITranslator;
import org.eclipse.osbp.ui.api.datamart.DatamartData;
import org.eclipse.osbp.ui.api.datamart.DatamartFilter;
import org.eclipse.osbp.ui.api.datamart.DatamartPrimary;
import org.eclipse.osbp.ui.api.datamart.IDataMart;
import org.eclipse.osbp.ui.api.user.IUser;
import org.eclipse.osbp.user.User;
import org.eclipse.osbp.utils.vaadin.SimpleDateFormatter;
import org.eclipse.osbp.xtext.datamart.common.AEntityDatamart;
import org.eclipse.osbp.xtext.datamart.common.DatamartDtoMapper;
import org.eclipse.osbp.xtext.datamart.common.olap.DerivedAxis;
import org.eclipse.osbp.xtext.datamart.common.olap.DerivedCellSet;
import org.eclipse.osbp.xtext.datamart.common.olap.DerivedMember;
import org.eclipse.osbp.xtext.datamart.common.olap.DerivedPosition;
import org.eclipse.osbp.xtext.datamart.common.sql.SqlCellSet;
import org.junit.Test;
import org.olap4j.Axis;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
@SuppressWarnings("serial")
public class UserAccountDatamart extends AEntityDatamart {
private static Logger log = org.slf4j.LoggerFactory.getLogger("datamarts");
private IUser user;
private Map<String, Map<String, String>> dateFilterAttributeProperties = new HashMap<String, Map<String, String>>();
private String statement = "select UserAccount.USER_NAME as \"userName\",UserAccount.EMAIL as \"email\",UserAccount.NOT_REGISTERED as \"notRegistered\",UserAccount.POSITION as \"position\",UserAccount.PROFILEIMAGE as \"profileimage\",UserAccount.id as \"__UserAccount__ID__\" from USER_ACCOUNT UserAccount";
private DatamartDtoMapper datamartDtoMapper = new DatamartDtoMapper()
.add("org.eclipse.osbp.authentication.account.dtos.UserAccountDto", "id", EType.STRING, "__UserAccount__ID__");
private Connection connection = null;
private HashMap<Integer, ArrayList<String>> axisMap = new HashMap<Integer,ArrayList<String>>() {{
put(0,new ArrayList<String>() {{
add("position");
add("userName");
add("notRegistered");
add("email");
add("profileimage");
add("__UserAccount__ID__");
}});
}};
private HashMap<String, IDataMart.EType> idMap = new HashMap<String,EType>() {{
put("__UserAccount__ID__", EType.STRING);
}};
private ArrayList<DatamartPrimary<?>> primaryList = new ArrayList<DatamartPrimary<?>>() {{
add(new DatamartPrimary<String>("__UserAccount__ID__", "id", "org.eclipse.osbp.authentication.account.entities.UserAccount"));
}};
private HashMap<String, HashMap<String, IDataMart.EType>> typesMap = new HashMap<String,HashMap<String,EType>>() {{
new HashMap<String,EType>(){{
put("UserAccount.position", EType.STRING);
put("UserAccount.profileimage", EType.BLOPMAPPING);
put("UserAccount.notRegistered", EType.BOOLEAN);
put("UserAccount.userName", EType.STRING);
put("UserAccount.email", EType.STRING);
}};
}};
private HashMap<String, String> typesProp = new HashMap<String, String>() {{
put("position", "");
}};
private HashMap<String, String> resultAttributes = new HashMap<String,String>();
public HashMap<String, IDataMart.EType> getIdMap() {
return this.idMap;
}
public ArrayList<DatamartPrimary<?>> getPrimaryList() {
return this.primaryList;
}
public HashMap<Integer, ArrayList<String>> getAxisMap() {
return this.axisMap;
}
public HashMap<String, String> getTypesProp() {
return this.typesProp;
}
public IUser getUser() {
return this.user;
}
public void setUser(final IUser user) {
this.user = user;
}
protected ArrayList<DatamartFilter> initializeFilterMap() {
return new ArrayList<DatamartFilter>() {{
add(new DatamartFilter(org.eclipse.osbp.authentication.account.datamarts.DatamartsServiceBinder.getDSLMetadataService(), user, DatamartFilter.FilterType.BY_ID, "UserAccount.id",""));
}};
}
public String getPrimaryFilterId() {
return "UserAccount.id";
}
@Override
public void clearCache() {
}
@Override
public String getResultAttribute(final String attributeName) {
return resultAttributes.get(attributeName);
}
public HashMap<String, IDataMart.EType> getTypesMap() {
return typesMap.get("Administrator");
}
public String getPersistenceUnit() {
return "authentication";
}
public Connection connect() {
if (connection == null) {
try {
connection = org.eclipse.osbp.authentication.account.datamarts.DatamartsServiceBinder.getPersistenceService().getPersistenceUnitConnection(getPersistenceUnit());
org.eclipse.osbp.authentication.account.datamarts.DatamartsServiceBinder.getPersistenceService().registerPersistenceUnit(getPersistenceUnit(), org.eclipse.osbp.authentication.account.entities.UserAccount.class);
} catch (SQLException e) {
log.error("{}", e);
return (connection);
}
}
return (connection);
}
public void disconnect() {
if (connection != null) {
try {
connection.close();
connection = null;
} catch (SQLException e) {
log.error("{}", e);
}
}
}
public String insertCondition(final String filteredStatement, final String condition) {
int index = filteredStatement.indexOf("order by");
if(index > -1) {
return filteredStatement.substring(0, index-1) + condition + filteredStatement.substring(index-1);
} else {
return filteredStatement + condition;
}
}
public void renderFilters() {
connect();
if (connection != null) {
for (DatamartFilter filter : getFilters()) {
if((DatamartFilter.FilterType.BY_ID != filter.getType()) && (DatamartFilter.FilterType.BETWEEN != filter.getType()) && (DatamartFilter.FilterType.BETWEEN_DATE != filter.getType())) {
ResultSet rs = null;
try {
rs = connection.createStatement().executeQuery(getVendorSql(filter));
if (rs != null) {
while (rs.next()) {
int type = rs.getMetaData().getColumnType(1);
switch (type) {
case java.sql.Types.DATE:
Date date = rs.getDate(1);
if (date != null) {
if (user == null) {
filter.addItem(date.toString(), getVendorToDateSQLMethod()+"('"+date.toString().replace(".0","")+"','YYYY-MM-DD HH24:MI:SS')", false);
} else {
filter.addItem(SimpleDateFormatter.getFormat("LONGDATE", user.getLocale()).format(date), getVendorToDateSQLMethod()+"('"+date.toString().replace(".0","")+"','YYYY-MM-DD HH24:MI:SS')", false);
}
}
break;
case java.sql.Types.TIMESTAMP:
Timestamp timestamp = rs.getTimestamp(1);
if (timestamp != null) {
if (user == null) {
filter.addItem(timestamp.toString(), getVendorToTimestampSQLMethod()+"('"+timestamp.toString().replace(".0","")+"','YYYY-MM-DD HH24:MI:SS')", false);
} else {
filter.addItem(SimpleDateFormatter.getFormat("LONGDATE", user.getLocale()).format(timestamp), getVendorToTimestampSQLMethod()+"('"+timestamp.toString().replace(".0","")+"','YYYY-MM-DD HH24:MI:SS')", false);
}
}
break;
case java.sql.Types.DECIMAL:
BigDecimal bigDecimal = rs.getBigDecimal(1);
if (bigDecimal != null) {
filter.addItem(bigDecimal.toPlainString(), bigDecimal.toPlainString(), false);
}
break;
case java.sql.Types.NUMERIC:
case java.sql.Types.DOUBLE:
filter.addItem(Double.toString(rs.getDouble(1)), Double.toString(rs.getDouble(1)), false);
break;
case java.sql.Types.INTEGER:
case java.sql.Types.BIGINT:
filter.addItem(Integer.toString(rs.getInt(1)), Integer.toString(rs.getInt(1)), false);
break;
case java.sql.Types.FLOAT:
filter.addItem(Float.toString(rs.getFloat(1)), Float.toString(rs.getFloat(1)), false);
break;
case java.sql.Types.CHAR:
case java.sql.Types.NCHAR:
case java.sql.Types.NVARCHAR:
case java.sql.Types.VARCHAR:
case java.sql.Types.LONGVARCHAR:
filter.addItem(rs.getString(1), rs.getString(1), true);
break;
}
}
}
} catch (SQLException e) {
log.error("{}", e);
} finally {
try {
rs.close();
} catch (java.sql.SQLException e) {}
}
}
}
}
disconnect();
}
public String applyFilters() {
String filteredStatement = statement;
computeConditions();
for (DatamartFilter filter : getFilters()) {
if (DatamartFilter.FilterType.BY_ID == filter.getType()) {
if(filter.getCondition() != null) {
if (!filteredStatement.toLowerCase().contains(" where ")) {
if ((" "+filter.getCondition()).toLowerCase().contains("where ")) {
filteredStatement = insertCondition(filteredStatement, " "+filter.getCondition());
}
else {
filteredStatement = insertCondition(filteredStatement, " where "+filter.getCondition());
}
}
else {
if (filter.getCondition().trim().toLowerCase().startsWith("and ") ||
filter.getCondition().trim().toLowerCase().startsWith("or ") ) {
filteredStatement = insertCondition(filteredStatement, filter.getCondition());
}
else {
filteredStatement = insertCondition(filteredStatement, " and "+filter.getCondition());
}
}
}
}
else {
filteredStatement = filteredStatement.replace(filter.getDecoratedName(), filter.getCondition());
}
}
log.debug("UserAccount:"+filteredStatement);
return filteredStatement;
}
public DerivedCellSet getResults(final Class operativeDtoClass, final List<IDto> operativeDtos) {
ResultSet rs = null;
SqlCellSet cellSet = null;
java.sql.ResultSetMetaData metaData = null;
// --- if filler text is enabled ---
if (fFillerTextEnabled) {
// --- try to generate filler text ---
rs = generateFillerTextResultSet();
}
// --- if no result set is generated by now ---
connect();
if (rs == null) {
if (connection != null) {
String query = applyFilters();
if (query.length()>0) {
try {
rs = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY).executeQuery(query);
} catch (SQLException e) {
log.error("Query: "+query);
log.error("{}", e);
}
}
}
}
if (rs != null) {
try {
cellSet = new SqlCellSet(rs, getAxisMap(), getIdMap(), datamartDtoMapper, operativeDtoClass, operativeDtos, getPrimaryList());
registerFirstLine(rs, user);
metaData = rs.getMetaData();
rs.close();
}
catch (java.sql.SQLException e) {
try {
log.error("Statement: "+rs.getStatement().toString());
} catch (SQLException e1) {}
log.error("{}", e);
}
return (new DerivedCellSet(cellSet,
metaData,
org.eclipse.osbp.authentication.account.datamarts.DatamartsServiceBinder.getDSLMetadataService(), user));
}
disconnect();
return null;
}
public void registerFirstLine(final ResultSet rs, final IUser user) throws SQLException {
if (rs != null && rs.first()) {
for (int column = 1; column <= rs.getMetaData().getColumnCount(); column++) {
int type = rs.getMetaData().getColumnType(column);
switch (type) {
case java.sql.Types.DATE:
if(rs.getDate(column) != null) {
if (user == null) {
resultAttributes.put(rs.getMetaData().getColumnName(column), rs.getDate(column).toString());
} else {
resultAttributes.put(rs.getMetaData().getColumnName(column),
SimpleDateFormatter.getFormat("LONGDATE", user.getLocale()).format(rs.getDate(column)));
}
}
break;
case java.sql.Types.TIMESTAMP:
if(rs.getTimestamp(column) != null) {
if (user == null) {
resultAttributes.put(rs.getMetaData().getColumnName(column), rs.getTimestamp(column).toString());
} else {
resultAttributes.put(rs.getMetaData().getColumnName(column), SimpleDateFormatter
.getFormat("LONGDATE", user.getLocale()).format(rs.getTimestamp(column)));
}
}
break;
case java.sql.Types.DECIMAL:
if(rs.getBigDecimal(column) != null) {
resultAttributes.put(rs.getMetaData().getColumnName(column),
rs.getBigDecimal(column).toPlainString());
}
break;
case java.sql.Types.NUMERIC:
case java.sql.Types.DOUBLE:
resultAttributes.put(rs.getMetaData().getColumnName(column), Double.toString(rs.getDouble(column)));
break;
case java.sql.Types.FLOAT:
resultAttributes.put(rs.getMetaData().getColumnName(column), Float.toString(rs.getFloat(column)));
break;
case java.sql.Types.CHAR:
case java.sql.Types.NCHAR:
case java.sql.Types.NVARCHAR:
case java.sql.Types.VARCHAR:
case java.sql.Types.LONGVARCHAR:
resultAttributes.put(rs.getMetaData().getColumnName(column), rs.getString(column));
break;
}
}
}
}
@Test
public void TestUserAccount() {
renderFilters();
org.junit.Assert.assertNotNull(getResults(null, null));
}
}