RecordOperation.java

  1. package com.gh.mygreen.xlsmapper.fieldprocessor.impl;

  2. import java.awt.Point;

  3. import org.apache.poi.ss.usermodel.Cell;

  4. import com.gh.mygreen.xlsmapper.annotation.XlsRecordOption;
  5. import com.gh.mygreen.xlsmapper.util.ArgUtils;
  6. import com.gh.mygreen.xlsmapper.util.CellPosition;


  7. /**
  8.  * シートのレコードの操作情報。
  9.  * レコードの書き込み後、セルの入力規則やシートの名前の範囲を修正するために利用する。
  10.  *
  11.  * @version 2.0
  12.  * @since 0.3
  13.  * @author T.TSUCHIE
  14.  *
  15.  */
  16. public class RecordOperation {
  17.    
  18.     /** レコード操作のアノテーション */
  19.     private final XlsRecordOption annotation;
  20.    
  21.     /** レコードのコピー回数 */
  22.     private int countCopyRecord;
  23.    
  24.     /** レコードの挿入回数 */
  25.     private int countInsertRecord;
  26.    
  27.     /** レコードの削除件数 */
  28.     private int countDeleteRecord;
  29.    
  30.     /** 左上のセルの位置 */
  31.     private Point topLeftPoisitoin;
  32.    
  33.     /** 右下のセルの位置 */
  34.     private Point bottomRightPosition;
  35.    
  36.     public RecordOperation(final XlsRecordOption annotation) {
  37.         this.annotation = annotation;
  38.         this.countCopyRecord = 0;
  39.         this.countInsertRecord = 0;
  40.         this.countDeleteRecord = 0;
  41.        
  42.     }
  43.    
  44.     /**
  45.      * レコードの操作用のアノテーションを取得する。
  46.      *
  47.      * @since 2.0
  48.      * @return 付与されていない場合は、属性がデフォルト値が設定される。
  49.      */
  50.     public XlsRecordOption getAnnotation() {
  51.         return annotation;
  52.     }
  53.    
  54.     /**
  55.      * レコードのコピー回数を1つ増やす
  56.      */
  57.     public void incrementCopyRecord() {
  58.         this.countCopyRecord++;
  59.     }
  60.    
  61.     /**
  62.      * レコードの挿入回数を1つ増やす
  63.      */
  64.     public void incrementInsertRecord() {
  65.         this.countInsertRecord++;
  66.     }
  67.    
  68.     /**
  69.      * レコードの削除回数を1つ増やす
  70.      */
  71.     public void incrementDeleteRecord() {
  72.         this.countDeleteRecord++;
  73.     }
  74.    
  75.     /**
  76.      * レコードの操作を行ったかどうか。
  77.      * コピー処理、挿入処理、削除処理が該当する。
  78.      * @return
  79.      */
  80.     public boolean isExecuteRecordOperation() {
  81.         return isExecuteOverRecordOperation() || isDeleteRecord();
  82.     }
  83.    
  84.     /**
  85.      * レコードの操作を行っていないかどうか。
  86.      * コピー処理、挿入処理、削除処理が該当する。
  87.      * @return
  88.      */
  89.     public boolean isNotExecuteRecordOperation() {
  90.         return !isExecuteRecordOperation();
  91.     }

  92.    
  93.     /**
  94.      * レコードが足りない時の操作を行ったかどうか。
  95.      * コピー処理、挿入処理が該当する。
  96.      * @return
  97.      */
  98.     public boolean isExecuteOverRecordOperation() {
  99.         return isCopyRecord() || isInsertRecord();
  100.     }
  101.    
  102.     /**
  103.      * レコードが足りない時の操作を行っていないかどうか。
  104.      * @return
  105.      */
  106.     public boolean isNotExecuteOverRecordOperation() {
  107.         return !isExecuteOverRecordOperation();
  108.     }
  109.    
  110.     /**
  111.      * レコードの挿入回数が1以上かどうか。
  112.      */
  113.     public boolean isCopyRecord() {
  114.         return countCopyRecord > 0;
  115.     }
  116.    
  117.     /**
  118.      * レコードの挿入回数が1以上かどうか。
  119.      */
  120.     public boolean isInsertRecord() {
  121.         return countInsertRecord > 0;
  122.     }
  123.    
  124.     /**
  125.      * レコードの削除回数が1以上かどうか。
  126.      */
  127.     public boolean isDeleteRecord() {
  128.         return countDeleteRecord > 0;
  129.     }
  130.    
  131.     public int getCountInsertRecord() {
  132.         return countInsertRecord;
  133.     }
  134.    
  135.     public int getCountDeleteRecord() {
  136.         return countDeleteRecord;
  137.     }
  138.    
  139.     /**
  140.      * セルの位置を元に左上、右下の端の位置を記憶する。
  141.      * @param cell セル情報
  142.      * @throws NullPointerException {@literal cell == null.}
  143.      */
  144.     public void setupCellPositoin(final Cell cell) {
  145.         ArgUtils.notNull(cell, "cell");
  146.         setupCellPositoin(cell.getRowIndex(), cell.getColumnIndex());
  147.     }
  148.    
  149.     /**
  150.      * アドレス情報を元に左上、右下の端の位置を記憶する。
  151.      * @param address アドレス情報
  152.      * @throws NullPointerException {@literal address == null.}
  153.      */
  154.     public void setupCellPositoin(final CellPosition address) {
  155.         ArgUtils.notNull(address, "address");
  156.         setupCellPositoin(address.getRow(), address.getColumn());
  157.     }
  158.    
  159.     /**
  160.      * セルの位置を元に左上、右下の端の位置を記憶する。
  161.      * @param rowIndex 行番号(0から始まる)
  162.      * @param columnIndex 列番号(0から始まる)
  163.      */
  164.     public void setupCellPositoin(final int rowIndex, final int columnIndex) {
  165.        
  166.         if(topLeftPoisitoin == null) {
  167.             this.topLeftPoisitoin = new Point(columnIndex, rowIndex);
  168.         }
  169.        
  170.         if(bottomRightPosition == null) {
  171.             this.bottomRightPosition = new Point(columnIndex, rowIndex);
  172.         }
  173.        
  174.         // 左上のセルの位置の設定
  175.         if(topLeftPoisitoin.x > columnIndex) {
  176.             this.topLeftPoisitoin.x = columnIndex;
  177.         }
  178.        
  179.         if(topLeftPoisitoin.y > rowIndex) {
  180.             this.topLeftPoisitoin.y = rowIndex;
  181.         }
  182.        
  183.         // 右下のセルの位置の設定
  184.         if(bottomRightPosition.x < columnIndex) {
  185.             this.bottomRightPosition.x = columnIndex;
  186.         }
  187.        
  188.         if(bottomRightPosition.y < rowIndex) {
  189.             this.bottomRightPosition.y = rowIndex;
  190.         }
  191.     }
  192.    
  193.     /**
  194.      * 左上端の座標を取得する。
  195.      * @return 左上端の座標
  196.      */
  197.     public Point getTopLeftPoisitoin() {
  198.         return topLeftPoisitoin;
  199.     }
  200.    
  201.     /**
  202.      * 右下端の座標を取得する。
  203.      * @return 右下端の座標
  204.      */
  205.     public Point getBottomRightPosition() {
  206.         return bottomRightPosition;
  207.     }
  208.    
  209. }