final Object pEntityOwner) throws Exception { List<T> result = new ArrayList<T>(); TableSql tableSqlOwner = getTablesMap().get(pEntityOwner.getClass() .getSimpleName()); Field fieldIdOwner = getUtlReflection().retrieveField(pEntityOwner .getClass(), tableSqlOwner.getIdName()); fieldIdOwner.setAccessible(true); idOwnerStr = idOwner.toString(); String ownerFieldName = evalOwnerFieldName(pEntityClass, pEntityOwner.getClass()); IRecordSet<RS> recordSet = null; try { String query = evalSqlSelect(pEntityClass) + " where " + pEntityClass.getSimpleName().toUpperCase() + "." + ownerFieldName.toUpperCase() + " = " + idOwnerStr + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query); if (recordSet.moveToFirst()) { do { result.add(retrieveEntity(pEntityClass, recordSet)); } while (recordSet.moveToNext());
if (this.srvOrm == null) { this.srvOrm = instantiateSrvOrm(); this.srvOrm.setSrvRecordRetriever(lazyGetSrvRecordRetriever()); this.srvOrm.setNewDatabaseId(this.newDatabaseId); this.srvOrm.setSrvDatabase(lazyGetSrvDatabase()); this.srvOrm.setSrvSqlEscape(new SrvSqlEscape()); this.srvOrm.setHlpInsertUpdate(lazyGetHlpInsertUpdate()); this.srvOrm.setLogger(lazyGetLogger()); MngSettings mngOrmSettings = new MngSettings(); mngOrmSettings.setLogger(lazyGetLogger()); this.srvOrm.setMngSettings(mngOrmSettings); this.srvOrm.loadConfiguration(this.ormSettingsDir, this.ormSettingsBaseFile); this.srvOrm.initializeDatabase(); this.entitiesMap = new HashMap<String, Class<?>>(); for (Class<?> clazz : this.srvOrm.getMngSettings() .getClasses()) { this.entitiesMap.put(clazz.getSimpleName(), clazz);
T entity = constructor.newInstance(); if (APersistableBase.class.isAssignableFrom(pEntityClass)) { initPersistableBase(entity); // always for current DB!!! TableSql tableSqlEntityOwner = getTablesMap() .get(pEntityOwnerClass.getSimpleName()); Field fieldIdEntityOwner = getUtlReflection().retrieveField( pEntityOwnerClass, tableSqlEntityOwner.getIdName()); fieldIdEntityOwner.setAccessible(true); throw new ExceptionWithCode(ExceptionWithCode.NOT_YET_IMPLEMENTED, msg); String ownerFieldName = evalOwnerFieldName(pEntityClass, pEntityOwnerClass); Field fieldEntityOwner = getUtlReflection().retrieveField(pEntityClass, ownerFieldName); fieldEntityOwner.setAccessible(true); fieldEntityOwner.set(entity, entityOwner); if (APersistableBase.class.isAssignableFrom(pEntityClass)) { if (getSrvDatabase() != null //for test purpose, otherwise it must be set && !((APersistableBase) entity).getIdDatabaseBirth() .equals(getIdDatabase())) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "can_not_change_foreign_src"); initPersistableBase(entity); // always for current DB!!!
/** * <p>Retrieve a list of all entities.</p> * @param <T> - type of business object, * @param pAddParam additional param, e.g. already retrieved TableSql * @param pEntityClass entity class * @return list of all business objects or empty list, not null * @throws Exception - an exception */ @Override public final <T> List<T> retrieveList( final Map<String, Object> pAddParam, final Class<T> pEntityClass) throws Exception { String query = evalSqlSelect(pAddParam, pEntityClass) + ";\n"; return retrieveListByQuery(pAddParam, pEntityClass, query); }
/** * <p>Retrieve entity from DB by given ID.</p> * @param <T> entity type * @param pEntityClass entity class * @param pId ID * @return entity or null * @throws Exception - an exception **/ @Override public final <T> T retrieveEntityById( final Class<T> pEntityClass, final Object pId) throws Exception { String query = evalSqlSelect(pEntityClass, pId); return retrieveEntity(pEntityClass, query); }
/** * <p>Retrieve a page of entities.</p> * @param <T> - type of business object, * @param pAddParam additional param, e.g. already retrieved TableSql * @param pEntityClass entity class * @param pQueryConditions not null e.g. "WHERE name='U1' ORDER BY id" * @param pFirst number of the first record (from 0) * @param pPageSize page size (max records) * @return list of business objects or empty list, not null * @throws Exception - an exception */ @Override public final <T> List<T> retrievePageWithConditions( final Map<String, Object> pAddParam, final Class<T> pEntityClass, final String pQueryConditions, final Integer pFirst, final Integer pPageSize) throws Exception { String query = evalSqlSelect(pAddParam, pEntityClass) + " " + pQueryConditions; return retrievePageByQuery(pAddParam, pEntityClass, query, pFirst, pPageSize); }
if (srvOrm == null) { srvOrm = instantiateSrvOrm(); srvOrm.setNewDatabaseId(this.newDatabaseId); srvOrm.setSrvDatabase(lazyGetSrvDatabase()); srvOrm.setHlpInsertUpdate(lazyGetHlpInsertUpdate()); srvOrm.setLogger(lazyGetLogger()); srvOrm.setUtlReflection(lazyGetUtlReflection()); srvOrm.setFctFillersObjectFields(lazyGetFctFillersObjectFields()); srvOrm.setEntitiesFactoriesFatory(this.factoryBldServices .lazyGetFctBcFctSimpleEntities()); FctBcCnvEntityToColumnsValues fctBcCnvEntityToColumnsValues = lazyGetFctBcCnvEntityToColumnsValues(); srvOrm.setFactoryCnvEntityToColumnsValues( fctBcCnvEntityToColumnsValues); FillerEntitiesFromRs<RS> fillerEntitiesFromRs = lazyGetFillerEntitiesFromRs(); srvOrm.setFillerEntitiesFromRs(fillerEntitiesFromRs); MngSettings mngOrmSettings = new MngSettings(); mngOrmSettings.setLogger(lazyGetLogger()); mngOrmSettings.setUtlReflection(lazyGetUtlReflection()); mngOrmSettings.setUtlProperties(lazyGetUtlProperties()); srvOrm.setMngSettings(mngOrmSettings); srvOrm.loadConfiguration(this.ormSettingsDir, this.ormSettingsBaseFile); fctBcCnvEntityToColumnsValues .setTablesMap(srvOrm.getTablesMap()); lazyGetFctBnCnvBnFromRs().setTablesMap(srvOrm.getTablesMap()); lazyGetFctBnCnvIbnToColumnValues().setTablesMap(srvOrm.getTablesMap()); fillerEntitiesFromRs.setTablesMap(srvOrm.getTablesMap());
boolean ifAllTablesCreated = true; try { for (Map.Entry<String, TableSql> entry : getTablesMap().entrySet()) { IRecordSet rs = srvDatabase.retrieveRecords(queryExistanceTable .replace(ISrvOrm.TABLE_PARAM_NAME_IN_EXISTENCE_QUERY, if (!rs.moveToFirst()) { ifCreatedOrAdded = true; String ddlStatment = evalSqlCreateTable(entry.getKey()); srvDatabase.executeQuery(ddlStatment); } else { String initSql = loadString(dirPath + useSubFolder + "/" + "init.sql"); if (initSql != null) { getLogger().info(ASrvOrm.class, "init.sql found, try to execute."); for (String initSingle : initSql.split("\n")) { if (initSingle.trim().length() > 1 && !initSingle.startsWith("/")) { getLogger().info(ASrvOrm.class, "init.sql not found."); getLogger().info(ASrvOrm.class, "all tables has been created."); DatabaseInfo databaseInfo = new DatabaseInfo(); int dbVer = Integer.parseInt(this.mngSettings .getAppSettings().get("databaseVersion")); databaseInfo.setDatabaseVersion(dbVer); databaseInfo.setDatabaseId(getNewDatabaseId()); databaseInfo.setDescription(this.mngSettings .getAppSettings().get("title"));
for (Map.Entry<String, TableSql> entry : getTablesMap().entrySet()) { IRecordSet rs = srvDatabase.retrieveRecords(queryExistanceTable .replace(TABLE_PARAM_NAME_IN_EXISTENCE_QUERY, if (!rs.moveToFirst()) { ifCreatedOrAdded = true; String ddlStatment = evalSqlCreateTable(entry.getKey()); getLogger().info(null, ASrvOrm.class, "Try to execute DDL: " + ddlStatment); srvDatabase.executeQuery(ddlStatment); tryUgradeDatabaseAnyWay(dirPath + useSubFolder + "/"); } finally { this.srvDatabase.releaseResources(); //close connection String initSql = loadString(dirPath + useSubFolder + "/" + "init.sql"); if (initSql != null) { getLogger().info(null, ASrvOrm.class, "init.sql found, try to execute."); for (String initSingle : initSql.split("\n")) { if (initSingle.trim().length() > 1 && !initSingle.startsWith("/")) { getLogger().info(null, ASrvOrm.class, "Try to execute INIT query: " + initSingle); srvDatabase.executeQuery(initSingle); getLogger().info(null, ASrvOrm.class, "init.sql not found."); getLogger().info(null, ASrvOrm.class, "all tables has been created."); DatabaseInfo databaseInfo = new DatabaseInfo();
final Long pDrawingOwnerId) throws Exception { if (!pEntity.getIdDatabaseBirth() .equals(getSrvOrm().getIdDatabase())) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "can_not_make_di_entry_for_foreign_src"); List<T> diel = getSrvOrm().retrieveListWithConditions(pAddParam, getDrawItemEntryClass(), " where DRAWINGTYPE=" + pEntity.constTypeCode() + " and " + tblNm + ".IDDATABASEBIRTH=" + getSrvOrm().getIdDatabase() + " and DRAWINGID=" + pEntity.getReversedId()); BigDecimal quantityToLeaveRst = pEntity.getItsQuantity(); die.setIdDatabaseBirth(getSrvOrm().getIdDatabase()); die.setSourceType(dies.getSourceType()); die.setSourceId(dies.getSourceId()); die.setDescription(makeDescription(pAddParam, pEntity, dies) + " " + getSrvI18n().getMsg("reversed_entry_n", langDef) + getSrvOrm().getIdDatabase() + "-" + dies.getItsId()); getSrvOrm().insertEntity(pAddParam, die); die.setIsNew(false); @SuppressWarnings("unchecked") .get(dies.getSourceType())); IDrawItemSource drawed = fctDis.create(pAddParam); String fldIdName = this.srvOrm.getTablesMap() .get(drawed.getClass().getSimpleName()).getIdFieldName(); Method setterId = this.settersRapiHolder .getFor(drawed.getClass(), fldIdName);
/** * <p>Evaluate SQL create table statement.</p> * @param pEntityName entity simple name * @return SQL create table **/ public final String evalSqlCreateTable(final String pEntityName) { TableSql tableSql = getTablesMap().get(pEntityName); StringBuffer result = new StringBuffer("create table " + pEntityName.toUpperCase() + " (\n"); boolean isFirstField = true; for (String fieldName : tableSql.getFieldsMap().keySet()) { if (isFirstField) { isFirstField = false; } else { result.append(",\n"); } result.append(fieldName.toUpperCase() + " " + tableSql.getFieldsMap().get(fieldName).getDefinition()); } if (tableSql.getConstraint() != null) { result.append(",\n" + tableSql.getConstraint()); } result.append(");\n"); return result.toString(); }
/** * <p>Process entity request.</p> * @param pAddParam additional param, e.g. return this line's * document in "nextEntity" for farther process * @param pRequestData Request Data * @param pEntity Entity to process * @return Entity processed for farther process or null * @throws Exception - an exception **/ @Override public final T process( final Map<String, Object> pAddParam, final T pEntity, final IRequestData pRequestData) throws Exception { if (pEntity.getIsNew()) { this.srvOrm.insertEntity(pAddParam, pEntity); pEntity.setIsNew(false); } else { this.srvOrm.updateEntity(pAddParam, pEntity); } String ownerFieldName = this.srvOrm.getTablesMap() .get(pEntity.getClass().getSimpleName()).getOwnerFieldName(); Method getter = this.gettersRapiHolder .getFor(pEntity.getClass(), ownerFieldName); @SuppressWarnings("unchecked") T owner = (T) getter.invoke(pEntity); pAddParam.put("nameOwnerEntity", owner.getClass().getSimpleName()); pAddParam.put("nextEntity", owner); return null; }
/** * <p>Retrieve a list of all entities.</p> * @param <T> - type of business object, * @param pEntityClass entity class * @return list of all business objects or empty list, not null * @throws Exception - an exception */ @Override public final <T> List<T> retrieveList( final Class<T> pEntityClass) throws Exception { List<T> result = new ArrayList<T>(); IRecordSet<RS> recordSet = null; try { String query = evalSqlSelect(pEntityClass) + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query); if (recordSet.moveToFirst()) { do { result.add(retrieveEntity(pEntityClass, recordSet)); } while (recordSet.moveToNext()); } } finally { if (recordSet != null) { recordSet.close(); } } return result; }
final Map<String, Object> pAddParam, final T pEntity, final IRequestData pRequestData) throws Exception { T entity = this.srvOrm.retrieveEntity(pAddParam, pEntity); if (entity.getIdDatabaseBirth() != getSrvOrm().getIdDatabase()) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "can_not_change_foreign_src"); TableSql tableSqlEfol = this.srvOrm.getTablesMap() .get(entityFolClass.getSimpleName()); for (Map.Entry<String, FieldSql> entry : tableSqlEfol pAddParam.put(ooids, 1); //only ID List<Object> entities = this.srvOrm .retrieveListForField(pAddParam, entityFol, ownerFieldName); for (Object efol : entities) { fillerEfol.fill(pAddParam, efol, entity, ownerFieldName);
final T pEntity) throws Exception { ColumnsValues columnsValues = new ColumnsValues(); TableSql tableSql = getTablesMap().get( pEntity.getClass().getSimpleName()); columnsValues.setIdName(tableSql.getIdName()); for (String fieldName : tableSql.getFieldsMap().keySet()) { try { Field field = getUtlReflection().retrieveField(pEntity.getClass(), fieldName); field.setAccessible(true); if (tableSqlForeign != null) { Object foreignEntity = field.get(pEntity); Field fieldId = getUtlReflection().retrieveField(field.getType(), tableSqlForeign.getIdName()); getLogger().debug(ASrvOrm.class, "ORM: try to fill column FID " + fieldName + " with " + foreignEntity + " in " + pEntity.getClass().getSimpleName() + " type PID: " if (!tryToFillColumnIdable(columnsValues, fieldName, fieldId, foreignEntity, fieldId.getType())) { String msg = } else if (!fillSimpleColumnAndFillNewVersion( columnsValues, field, pEntity)) { String msg = "There is no rule to fill column value from field "
/** * <p>Delete entity with NON-COMPLEX ID from DB.</p> * @param <T> entity type * @param <K> ID type * @param pEntity entity * @throws Exception - an exception **/ @Override public final <T> void deleteEntity( final T pEntity) throws Exception { TableSql tableSql = this.getTablesMap().get(pEntity.getClass() .getSimpleName()); Field fieldId = this.utlReflection.retrieveField(pEntity.getClass(), tableSql.getIdName()); fieldId.setAccessible(true); Object id = fieldId.get(pEntity); deleteEntity(pEntity.getClass(), id); }
final Long pDrawingOwnerId) throws Exception { if (!pEntity.getIdDatabaseBirth() .equals(getSrvOrm().getIdDatabase())) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "can_not_make_di_entry_for_foreign_src"); String query = lazyGetQuery(drawItemSourceLine.getFileName()); query = query.replace(":IDDATABASEBIRTH", String.valueOf(getSrvOrm() .getIdDatabase())); query = query.replace(":INVITEM", pEntity.getInvItem() .getItsId().toString()); .get(source.getSourceType())); IDrawItemSource drawed = fctDis.create(pAddParam); String fldIdName = this.srvOrm.getTablesMap() .get(drawed.getClass().getSimpleName()).getIdFieldName(); Method setterId = this.settersRapiHolder .getFor(drawed.getClass(), fldIdName); setterId.invoke(drawed, source.getSourceId()); drawed = srvOrm.retrieveEntity(pAddParam, drawed); BigDecimal quantityToDraw; if (quantityToDrawRest.compareTo(drawed.getTheRest()) < 0) {
/** * <p>Retrieve copy of entity from DB by given ID.</p> * @param <T> entity type * @param pEntityClass entity class * @param pId ID * @return entity or null * @throws Exception - an exception **/ @Override public final <T> T retrieveCopyEntity( final Class<T> pEntityClass, final Object pId) throws Exception { T entity = retrieveEntityById(pEntityClass, pId); TableSql tableSql = getTablesMap().get(pEntityClass.getSimpleName()); Field fieldId = getUtlReflection().retrieveField(pEntityClass, tableSql.getIdName()); fieldId.setAccessible(true); fieldId.set(entity, null); if (APersistableBase.class.isAssignableFrom(pEntityClass)) { initPersistableBase(entity); } return entity; }
/** * <p>Refresh entity from DB by given entity with ID.</p> * @param <T> entity type * @param pEntity entity * @return entity or null * @throws Exception - an exception **/ @Override public final <T> T retrieveEntity(final T pEntity) throws Exception { @SuppressWarnings("unchecked") Class<T> entityClass = (Class<T>) pEntity.getClass(); TableSql tableSql = getTablesMap().get(entityClass.getSimpleName()); Field fieldId = getUtlReflection().retrieveField(entityClass, tableSql.getIdName()); fieldId.setAccessible(true); Object id = fieldId.get(pEntity); return retrieveEntityById(entityClass, id); }
final Map<String, Object> pAddParam, final T pEntity, final IRequestData pRequestData) throws Exception { T entity = this.srvOrm.retrieveEntity(pAddParam, pEntity); entity.setIsNew(false); pRequestData.setAttribute("entity", entity); TableSql tableSqlEfol = this.srvOrm.getTablesMap() .get(entityFolClass.getSimpleName()); for (Map.Entry<String, FieldSql> entry : tableSqlEfol pAddParam.put(ooids, 1); //only ID List<Object> entities = this.srvOrm .retrieveListForField(pAddParam, entityFol, ownerFieldName); for (Object efol : entities) { fillerEfol.fill(pAddParam, efol, entity, ownerFieldName);