/** * Get the fields of a project * * @return List of FieldAccessors */ public List<FieldAccessor> getFields() { final Database database = new Database(); List<Object> response = Transaction.array( new Transaction() { @Override public List<Object> array() throws SQLException, ServerException { database.openConnection(); List<Object> accessorList = new ArrayList<Object>(); String query = "select * from 'fields' where project_id = " + getId() + ";"; ResultSet resultSet = database.query(query); while (resultSet.next()) { accessorList.add(FieldAccessor.buildFromResultSet(resultSet)); } return accessorList; } }, database); List<FieldAccessor> fieldAccessorList = new ArrayList<FieldAccessor>(); for (Object object : response) { fieldAccessorList.add((FieldAccessor) object); } return fieldAccessorList; }
/** * Get all projects in DB, super generic, this. * * @return List of ProjectAccessors */ public static List<ProjectAccessor> all() { final Database database = new Database(); List<Object> response = Transaction.array( new Transaction() { @Override public List<Object> array() throws SQLException, ServerException { database.openConnection(); List<Object> accessorList = new ArrayList<Object>(); String query = "select * from 'projects';"; ResultSet resultSet = database.query(query); while (resultSet.next()) { accessorList.add(buildFromResultSet(resultSet)); } return accessorList; } }, database); List<ProjectAccessor> projectAccessorList = new ArrayList<ProjectAccessor>(); for (Object object : response) { projectAccessorList.add((ProjectAccessor) object); } return projectAccessorList; }
@Override public boolean save() { // what about having the user add this as project? // ANSWER: user.save => project.toSQL() => not atomic though. return Transaction.logic( new Transaction() { @Override public boolean logic() throws SQLException, ServerException { database.openConnection(); database.addQuery(toSQL(Database.SPECIFIED_PRIMARY_KEY)); if (userAccessor != null) { database.addQuery(userAccessor.toSQL(Database.SPECIFIED_PRIMARY_KEY)); } database.commit(); // Update ID if (isNew()) setId(database.getLastIdForTable("projects")); return true; } }, database); }
public List<ValueAccessor> getValues() { List<Object> response = Transaction.array( new Transaction() { @Override public List<Object> array() throws SQLException, ServerException { database.openConnection(); List<Object> accessorList = new ArrayList<Object>(); String query = "select * from 'values' where record_id in " + "(select id from records where image_id in " + "(select id from images where project_id = " + getId() + "));"; ResultSet resultSet = database.query(query); while (resultSet.next()) { accessorList.add(ValueAccessor.buildFromResultSet(resultSet)); } return accessorList; } }, database); List<ValueAccessor> valueAccessorList = new ArrayList<ValueAccessor>(); for (Object object : response) { valueAccessorList.add((ValueAccessor) object); } return valueAccessorList; }
/** * Find a Projects_Res with a project_id * * @param id a Projects_Res's id * @return ProjectAccessor(Projects_Res) or null */ public static ProjectAccessor find(int id) { final Database database = new Database(); final int projectId = id; return (ProjectAccessor) Transaction.object( new Transaction() { @Override public Object object() throws SQLException, ServerException { database.openConnection(); String query = "select * from 'projects' where id = " + projectId + ";"; ResultSet resultSet = database.query(query); if (resultSet.next()) { return buildFromResultSet(resultSet); } return null; } }, database); }