/** * <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 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 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 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> retrievePage( final Map<String, Object> pAddParam, final Class<T> pEntityClass, final Integer pFirst, final Integer pPageSize) throws Exception { String query = evalSqlSelect(pAddParam, pEntityClass); return retrievePageByQuery(pAddParam, pEntityClass, query, pFirst, pPageSize); }
/** * <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); }
/** * <p>Evaluate SQL SELECT statement for the entity with ID.</p> * @param <T> entity type * @param pEntityClass entity class * @param pId ID * @return String SQL DML query * @throws Exception - an exception **/ public final <T> String evalSqlSelect( final Class<T> pEntityClass, final Object pId) throws Exception { TableSql tableSql = this.getTablesMap().get(pEntityClass.getSimpleName()); String idStr; if (pId instanceof String) { idStr = "'" + pId.toString() + "'"; } else { idStr = pId.toString(); } return evalSqlSelect(pEntityClass) + " where " + pEntityClass.getSimpleName().toUpperCase() + "." + tableSql.getIdName().toUpperCase() + "=" + idStr + ";\n"; }
/** * <p>Entity's lists with filter "field" (e.g. invoice lines for invoice).</p> * @param <T> - type of entity * @param pAddParam additional param, e.g. already retrieved TableSql * @param pEntity - Entity e.g. an invoice line with filled invoice * @param pFieldFor - Field For name e.g. "invoice" * @return list of business objects or empty list, not null * @throws Exception - an exception */ @Override public final <T> List<T> retrieveListForField( final Map<String, Object> pAddParam, final T pEntity, final String pFieldFor) throws Exception { String whereStr = evalWhereForField(pAddParam, pEntity, pFieldFor); String query = evalSqlSelect(pAddParam, pEntity.getClass()) + whereStr + ";"; @SuppressWarnings("unchecked") Class<T> entityClass = (Class<T>) pEntity.getClass(); return retrieveListByQuery(pAddParam, entityClass, query); }
/** * <p>Refresh entity from DB.</p> * @param <T> entity type * @param pAddParam additional param, e.g. already retrieved TableSql * @param pEntity entity * @return entity or null * @throws Exception - an exception **/ @Override public final <T> T retrieveEntity(final Map<String, Object> pAddParam, final T pEntity) throws Exception { String query = evalSqlSelect(pAddParam, pEntity.getClass()); pAddParam.put("isOnlyId", Boolean.TRUE); ColumnsValues columnsValues = evalColumnsValues(pAddParam, pEntity); pAddParam.remove("isOnlyId"); String whereStr = evalWhereId(pEntity, columnsValues); @SuppressWarnings("unchecked") Class<T> entityClass = (Class<T>) pEntity.getClass(); return retrieveEntity(pAddParam, entityClass, query + " where " + whereStr); }
/** * <p>Retrieve a list 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" * @return list of business objects or empty list, not null * @throws Exception - an exception */ @Override public final <T> List<T> retrieveListWithConditions( final Map<String, Object> pAddParam, final Class<T> pEntityClass, final String pQueryConditions) throws Exception { if (pQueryConditions == null) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "param_null_not_accepted"); } String query = evalSqlSelect(pAddParam, pEntityClass) + " " + pQueryConditions + ";\n"; return retrieveListByQuery(pAddParam, pEntityClass, query); }
/** * <p>Retrieve entity from DB by given query conditions. * The first record in recordset will be used.</p> * @param <T> entity type * @param pEntityClass entity class * @param pQueryConditions e.g. "WHERE name='U1' ORDER BY id" * @return entity or null * @throws Exception - an exception **/ @Override public final <T> T retrieveEntityWithConditions( final Class<T> pEntityClass, final String pQueryConditions) throws Exception { String query = evalSqlSelect(pEntityClass); if (pQueryConditions == null) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "param_null_not_accepted"); } return retrieveEntity(pEntityClass, query + " " + pQueryConditions + ";\n"); }
/** * <p>Retrieve entity from DB by given query conditions. * The first record in record-set will be returned.</p> * @param <T> entity type * @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" * or "" that means without filter/order * @return entity or null * @throws Exception - an exception **/ @Override public final <T> T retrieveEntityWithConditions( final Map<String, Object> pAddParam, final Class<T> pEntityClass, final String pQueryConditions) throws Exception { if (pQueryConditions == null) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "param_null_not_accepted"); } String query = evalSqlSelect(pAddParam, pEntityClass); return retrieveEntity(pAddParam, pEntityClass, query + " " + pQueryConditions + ";\n"); }
IRecordSet<RS> recordSet = null; try { String query = evalSqlSelect(pEntityClass) + " where " + pEntityClass.getSimpleName().toUpperCase() + "." + ownerFieldName.toUpperCase() + " = " + idOwnerStr + ";\n";
ownerIdStr = pEntityOwnerId.toString(); String query = evalSqlSelect(pEntityClass) + " where " + pEntityClass.getSimpleName().toUpperCase() + "." + ownerFieldName.toUpperCase() + "=" + ownerIdStr + ";\n";
IRecordSet<RS> recordSet = null; try { String query = evalSqlSelect(pEntityClass) + " limit " + pPageSize + " offset " + pFirst + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query);
/** * <p>Refresh entity from DB by its ID.</p> * @param <T> entity type * @param pAddParam additional param * @param pEntityClass entity class * @param pItsId entity ID * @return entity or null * @throws Exception - an exception **/ @Override public final <T> T retrieveEntityById(final Map<String, Object> pAddParam, final Class<T> pEntityClass, final Object pItsId) throws Exception { String query = evalSqlSelect(pAddParam, pEntityClass); @SuppressWarnings("unchecked") IFactorySimple<T> facEn = (IFactorySimple<T>) this.entitiesFactoriesFatory .lazyGet(pAddParam, pEntityClass); T entity = facEn.create(pAddParam); @SuppressWarnings("unchecked") IFillerObjectFields<T> filler = (IFillerObjectFields<T>) this.fctFillersObjectFields.lazyGet(pAddParam, pEntityClass); String idFldName = this.tablesMap .get(pEntityClass.getSimpleName()).getIdFieldName(); filler.fill(pAddParam, entity, pItsId, idFldName); pAddParam.put("isOnlyId", Boolean.TRUE); ColumnsValues columnsValues = evalColumnsValues(pAddParam, entity); pAddParam.remove("isOnlyId"); String whereId = evalWhereId(entity, columnsValues); return retrieveEntity(pAddParam, pEntityClass, query + " where " + whereId + ";\n"); }
/** * <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; }
IRecordSet<RS> recordSet = null; try { String query = evalSqlSelect(pEntityClass) + " " + pQueryConditions + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query);
IRecordSet<RS> recordSet = null; try { String query = evalSqlSelect(pEntityClass) + " " + pQueryConditions + " limit " + pPageSize + " offset " + pFirst + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query);