| 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.date.SimpleDateFormatter; |
| import org.eclipse.osbp.ui.api.user.IUser; |
| import org.eclipse.osbp.user.User; |
| 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.POSITION as \"position\",UserAccount.PROFILEIMAGE as \"profileimage\",UserAccount.NOT_REGISTERED as \"notRegistered\",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 Map<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 Map<String, String> aliasMap = new HashMap<String,String>() {{ |
| put("position","org.eclipse.osbp.authentication.account.entities.UserAccount.position"); |
| put("userName","org.eclipse.osbp.authentication.account.entities.UserAccount.userName"); |
| put("notRegistered","org.eclipse.osbp.authentication.account.entities.UserAccount.notRegistered"); |
| put("email","org.eclipse.osbp.authentication.account.entities.UserAccount.email"); |
| put("profileimage","org.eclipse.osbp.authentication.account.entities.UserAccount.profileimage"); |
| }}; |
| |
| private Map<String, IDataMart.AttributeVisibility> hiddenMap = new HashMap<String,AttributeVisibility>() {{ |
| }}; |
| |
| private Map<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 Map<String, IDataMart.EType> typesMap = 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 Map<String, String> typesProp = new HashMap<String, String>() {{ |
| put("position", ""); |
| }}; |
| |
| private Map<String, String> resultAttributes = new HashMap<String,String>(); |
| |
| public Map<String, IDataMart.EType> getIdMap() { |
| return this.idMap; |
| } |
| |
| public List<DatamartPrimary<?>> getPrimaryList() { |
| return this.primaryList; |
| } |
| |
| public Map<Integer, ArrayList<String>> getAxisMap() { |
| return this.axisMap; |
| } |
| |
| public Map<String, String> getAliasMap() { |
| return this.aliasMap; |
| } |
| |
| public Map<String, IDataMart.AttributeVisibility> getHiddenMap() { |
| return this.hiddenMap; |
| } |
| |
| public Map<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","", false)); |
| }}; |
| |
| } |
| |
| public String getPrimaryFilterId() { |
| return "UserAccount.id"; |
| |
| } |
| |
| @Override |
| public void clearCache() { |
| |
| } |
| |
| @Override |
| public String getResultAttribute(final String attributeName) { |
| return resultAttributes.get(attributeName); |
| } |
| |
| public Map<String, IDataMart.EType> getTypesMap() { |
| return typesMap; |
| |
| } |
| |
| 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())) { |
| try (ResultSet 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(), vendorConvertToDateSQL( date ), false); |
| } else { |
| filter.addItem(SimpleDateFormatter.getFormat("LONGDATE", user.getLocale()).format(date), vendorConvertToDateSQL( date ), false); |
| } |
| } |
| break; |
| case java.sql.Types.TIMESTAMP: |
| Timestamp timestamp = rs.getTimestamp(1); |
| if (timestamp != null) { |
| if (user == null) { |
| filter.addItem(timestamp.toString(), vendorConvertToTimestampSQL( timestamp ), false); |
| } else { |
| filter.addItem(SimpleDateFormatter.getFormat("LONGDATE", user.getLocale()).format(timestamp), vendorConvertToTimestampSQL( timestamp ), 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); |
| } |
| } |
| } |
| } |
| 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) { |
| SqlCellSet cellSet = null; |
| java.sql.ResultSetMetaData metaData = null; |
| connect(); |
| if (connection != null) { |
| String query = applyFilters(); |
| if (query.length()>0) { |
| try (ResultSet rs = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY).executeQuery(query)){ |
| cellSet = new SqlCellSet(rs, getAxisMap(), getIdMap(), getAliasMap(), getHiddenMap(), datamartDtoMapper, operativeDtoClass, operativeDtos, getPrimaryList(), org.eclipse.osbp.authentication.account.datamarts.DatamartsServiceBinder.getUserAccessService()); |
| registerFirstLine(rs, user); |
| metaData = rs.getMetaData(); |
| disconnect(); |
| return (new DerivedCellSet(cellSet, |
| metaData, |
| org.eclipse.osbp.authentication.account.datamarts.DatamartsServiceBinder.getDSLMetadataService(), user)); |
| } |
| catch (java.sql.SQLException e) { |
| log.error("Statement: {} error {}", query, e); |
| } |
| } |
| } |
| disconnect(); |
| return null; |
| } |
| |
| public void registerFirstLine(final ResultSet rs, final IUser user) throws SQLException { |
| if (rs != null && !rs.isClosed() && 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)); |
| } |
| } |