content: /*************************************************************************
  *                                                                        *
  *  This file is part of the 20n/act project.                             *
  *  20n/act enables DNA prediction for synthetic biology/bioengineering.  *
  *  Copyright (C) 2017 20n Labs, Inc.                                     *
  *                                                                        *
  *  Please direct all queries to act@20n.com.                             *
  *                                                                        *
  *  This program is free software: you can redistribute it and/or modify  *
  *  it under the terms of the GNU General Public License as published by  *
  *  the Free Software Foundation, either version 3 of the License, or     *
  *  (at your option) any later version.                                   *
  *                                                                        *
  *  This program is distributed in the hope that it will be useful,       *
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  *  GNU General Public License for more details.                          *
  *                                                                        *
  *  You should have received a copy of the GNU General Public License     *
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  *                                                                        *
  *************************************************************************/
  
  package com.act.lcms.db.model;
  
  import com.act.lcms.db.io.DB;
  import org.apache.commons.lang3.StringUtils;
  
  import java.io.IOException;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.List;
  
  public abstract class PlateWell<T extends PlateWell> extends BaseDBModel<T> {
    protected String makeGetByPlateIDQuery() {
      return StringUtils.join(new String[] {
          "SELECT", StringUtils.join(this.getAllFields(), ','),
          "from", this.getTableName(),
          "where plate_id = ?",
          "order by plate_row asc, plate_column asc"
      }, " ");
    }
    protected abstract String getGetByPlateIDQuery();
    public List<T> getByPlateId(DB db, Integer plateId) throws SQLException, IOException, ClassNotFoundException {
      try (PreparedStatement stmt = db.getConn().prepareStatement(getGetByPlateIDQuery())) {
        stmt.setInt(1, plateId);
        try (ResultSet resultSet = stmt.executeQuery()) {
          return fromResultSet(resultSet);
        }
      }
    }
  
    protected T insert(DB db, T toInsert) throws SQLException, IOException {
      return insert(db, toInsert,
          String.format("could not retrieve autogenerated key for well at %d @ %d x %d",
              plateId, plateRow, plateColumn)
      );
    }
  
    /* Use protected rather than private, as this class represents common attributes of all plate wells.  Extending
     * classes can/should have access to its fields, as its existence is simply an organizational convenience. */
    protected Integer plateId;
    protected Integer plateRow;
    protected Integer plateColumn;
  
    public Integer getPlateId() {
      return plateId;
    }
  
    public void setPlateId(Integer plateId) {
      this.plateId = plateId;
    }
  
    public Integer getPlateRow() {
      return plateRow;
    }
  
    public void setPlateRow(Integer plateRow) {
      this.plateRow = plateRow;
    }
  
    public Integer getPlateColumn() {
      return plateColumn;
    }
  
    public void setPlateColumn(Integer plateColumn) {
      this.plateColumn = plateColumn;
    }
  
    public String getCoordinatesString() {
      if (plateRow == null || plateColumn == null) {
        return "(unknown)";
      }
      return String.format("%s%d",
          Character.valueOf((char) (this.getPlateRow() + 'A')).toString(), this.getPlateColumn() + 1);
    }
  }
  