|
@@ -1,15 +1,21 @@
|
|
|
package microfolie.persistance.table;
|
|
|
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.logging.Logger;
|
|
|
|
|
|
+import db.Database;
|
|
|
import db.DatabaseTable;
|
|
|
import db.annotation.DbTable;
|
|
|
+import db.mapper.DatabaseMapper;
|
|
|
import microfolie.persistance.entity.Usager;
|
|
|
|
|
|
@DbTable(name = "Usager", entity = Usager.class)
|
|
|
public class UsagerTable extends DatabaseTable<Usager> {
|
|
|
|
|
|
+ private static final Logger LOGGER = Logger.getLogger(UsagerTable.class.getName());
|
|
|
private static UsagerTable instance;
|
|
|
|
|
|
private UsagerTable() {
|
|
@@ -36,6 +42,36 @@ public class UsagerTable extends DatabaseTable<Usager> {
|
|
|
return !findByCode(code).isPresent();
|
|
|
}
|
|
|
|
|
|
+ public long getNumberUsagerInDatabase() {
|
|
|
+ Optional<Long> opt = Database.query("Select count(*) From Usager", rs -> {
|
|
|
+ try {
|
|
|
+ rs.next();
|
|
|
+ return rs.getLong(1);
|
|
|
+ } catch (SQLException e) {
|
|
|
+ LOGGER.warning(e.getMessage());
|
|
|
+ return 0l;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (opt.isPresent()) {
|
|
|
+ return opt.get();
|
|
|
+ }
|
|
|
+ return 0l;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Usager> getpage(int num, int perPage) {
|
|
|
+ String sql = "Select * From "
|
|
|
+ + "(Select ROW_NUMBER() OVER() as rownum, Usager.* From Usager) as tmp "
|
|
|
+ + "Where rownum > ? And rownum <= ?";
|
|
|
+ List<Object> params = new ArrayList<>();
|
|
|
+ params.add(perPage * (num - 1));
|
|
|
+ params.add(perPage * num);
|
|
|
+ Optional<List<Usager>> opt = Database.query(sql, params, DatabaseMapper.listMapper(getClass().getAnnotation(DbTable.class)));
|
|
|
+ if (opt.isPresent()) {
|
|
|
+ return opt.get();
|
|
|
+ }
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
public static UsagerTable getInstance() {
|
|
|
if(instance == null) {
|
|
|
instance = new UsagerTable();
|