//===========================================================================
// package
//===========================================================================
package jp.gr.java_conf.bugslife.bug;

//===========================================================================
// import
//===========================================================================
// Java core API
import java.util.*;
import java.text.*;
import org.apache.commons.logging.*;

// DB API
import jp.gr.java_conf.bugslife.db.*;

// Util API
import jp.gr.java_conf.bugslife.util.*;
import jp.gr.java_conf.bugslife.selections.*;
//import jp.gr.java_conf.bugslife.system.*;


/**
 *<PRE>
 * 
 * AddTextクラス
 * 
 *</PRE>
 * @version	0.0, 2001/09/10
 * @author	鰐川　正寛
 */
//===========================================================================
public class AddText implements IUpdatable, ILoadable
{
//===========================================================================
// attributes
//===========================================================================
	/**
	 *	バグ番号
	 */
	private int m_bno = 0;
	/**
	 *	投入日時
	 */
	private Date m_entryDate = new Date();
	/**
	 *	投入者番号
	 */
	private int m_entryUno = 0;
	/**
	 *	投入者名
	 */
	private String m_entryUname = null;
	/**
	 *	投入者メールアドレス
	 */
	private String m_entryUmail = null;
	/**
	 *	テキストタイプ
	 */
	private byte m_textType = TYPE_TROUBLE;
	/**
	 *	内容
	 */
	private String m_content = null;
	/**
	 *	更新者
	 */
	private int m_updateUno = 0;
	/**
	 *	更新日時
	 */
	private Date m_updateDate = new Date();
	/**
	 * Log
	 */
	private Log log = LogFactory.getLog(this.getClass().getName());
//=====================================
// 定数定義
//=====================================
	/**
	 * ステータス
	 */
	public static final byte TYPE_TROUBLE = 1;		//故障内容
	public static final byte TYPE_PRINCIPAL = 2;	//対処方針／担当者連絡事項
	public static final byte TYPE_CAUSE = 3;		//故障原因
	public static final byte TYPE_UPDATE = 4;		//処置内容
	public static final byte TYPE_CONFIRM = 5;		//確認内容
//=====================================
// DB用項目定義
//=====================================
	/**
	 * 排他制御
	 */
	private static final boolean EXCLUSION = true;	//あり
	/**
	 * AutoIncrement
	 */
	private static final boolean ATOINCREMENT = false;	//なし
	/**
	 * PK 
	 */
	private static final String[] PK = {"bno", "entryDate"};
	/**
	 * 更新項目名
	 */
	private static final String[] DB_ITEMS = {
									"bno","entryDate","entryUno",
									"textType","content",
									"updateUno" };
//===========================================================================
// methods
//===========================================================================
	/**
	 *	デフォルトコンストラクタ
	 *
	 */
	public AddText()
	{
		
	}
//======================================
// db access methods
//======================================
	/**
	 *	insertDB
	 *
	 * @return	処理結果
	 */
	public void insert(int updateUno)
	{
		setEntryUno(updateUno);
		setUpdateUno(updateUno);
		DAO.insert(this);
//		load();		//最終更新日がほしいので再ロード
	}
	/**
	 *	update
	 *
	 * @return	処理結果
	 */
	public void update(int updateUno)
	{
		setEntryUno(updateUno);
		setUpdateUno(updateUno);
		DAO.update(this);
//		load();		//最終更新日がほしいので再ロード
	}
	/**
	 *	delete
	 *
	 * @return	処理結果
	 */
	public void delete(int updateUno)
	{
		setEntryUno(updateUno);
		setUpdateUno(updateUno);
		DAO.delete(this);
	}
	/**
	 *	load
	 *
	 * @return	処理結果
	 */
	public void load()
	{
		DAO.load(this);
	}
	/**
	 *	findAll
	 *
	 * @return	処理結果
	 */
	public static Collection findAll()
	{
		return DAO.findAll(new AddText());
	}
	/**
	 *	AddTextの取得（バグ番号指定）
	 *
	 * @return	処理結果
	 */
	public static Collection getAddText(int bno)
	{
		String sql = 
		"SELECT  A.bno as bno," +
		" A.entryDate as entryDate," +
		" A.entryUno as entryUno," +
		" B.nickname as entryUname," +
		" B.email as entryUmail," +
		" A.textType as textType," +
		" A.content as content," +
		" A.updateUno as updateUno," +
		" A.updateDate as updateDate" +
		" FROM AddText AS A" +
		" LEFT JOIN UserInfo AS B ON A.entryUno = B.uno" +
		" where A.bno = ? " +
		" ORDER BY A.bno,A.entryDate";
		ArrayList params = new ArrayList();
		params.add(new Integer(bno));
		
		return (DAO.find(new AddText(), sql, params));
		
	}
	/**
	 *	AddTextの取得（バグ番号指定,タイプ指定）
	 *
	 * @return	処理結果
	 */
	public static Collection getAddText(int bno, byte textType)
	{
		
		String sql = 
		"SELECT  A.bno as bno," +
		" A.entryDate as entryDate," +
		" A.entryUno as entryUno," +
		" B.nickname as entryUname," +
		" B.email as entryUmail," +
		" A.textType as textType," +
		" A.content as content," +
		" A.updateUno as updateUno," +
		" A.updateDate as updateDate" +
		" FROM AddText AS A" +
		" LEFT JOIN UserInfo AS B ON A.entryUno = B.uno" +
		" where A.bno = ? AND" +
		" A.textType = ?" +
		" ORDER BY A.bno,A.entryDate";
		ArrayList params = new ArrayList();
		params.add(new Integer(bno));
		params.add(new Byte(textType));
		
		return (DAO.find(new AddText(), sql, params));
		
	}
//======================================
// Db Interface
//======================================
	/**
	 * 
	 */
	public String[] getUpdateItemNames() {
		
		return DB_ITEMS;
		
	}
	/**
	 * PK items
	 */
	 public String[] getPKItems() {
		return PK;
	}
	/**
	 * 排他制御ありなし
	 */
	public boolean isExclusion() {
		return EXCLUSION;
	}
	/**
	 * AutoIncrementありなし
	 */
	public boolean isAutoIncrement() {
		return ATOINCREMENT;
	}
//======================================
// Property
//======================================
	/**
	 * バグＩＤの取得
	 * 
	 * @return バグＩＤ
	 */
	public int getBno()
	{
		return m_bno;
	}
	/**
	 * バグＩＤのセット
	 * 
	 * @param bno バグＩＤ
	 */
	public void setBno(int bno)
	{
		m_bno = bno;
	}
	/**
	 * 投入日時の取得
	 * 
	 * @return 投入日時
	 */
	public Date getEntryDate()
	{
		return m_entryDate;
	}
	/**
	 * 投入日時の取得(文字列)
	 * 
	 * @return 投入日時
	 */
	public String getEntryDateString()
	{
		return Converter.getDateString(m_entryDate);
	}
	/**
	 * 投入日時のセット
	 * 
	 * @param entryDate 投入日時
	 */
	public void setEntryDate(Date entryDate)
	{
		m_entryDate = entryDate;
	}
	/**
	 * 投入者番号の取得
	 * 
	 * @return 投入者番号
	 */
	public int getEntryUno()
	{
		return m_entryUno;
	}
	/**
	 * 投入者番号のセット
	 * 
	 * @param entryUno 投入者番号
	 */
	public void setEntryUno(int entryUno)
	{
		m_entryUno = entryUno;
	}
	/**
	 * 投入者名の取得
	 * 
	 * @return 投入者名
	 */
	public String getEntryUname()
	{
		return m_entryUname;
	}
	/**
	 * 投入者名のセット
	 * 
	 * @param entryUname 投入者名
	 */
	public void setEntryUname(String entryUname)
	{
		m_entryUname = entryUname;
	}
	/**
	 * 投入者メールアドレスの取得
	 * 
	 * @return 投入者メールアドレス
	 */
	public String getEntryUmail()
	{
		return m_entryUmail;
	}
	/**
	 * 投入者メールアドレスのセット
	 * 
	 * @param entryUmail 投入者メールアドレス
	 */
	public void setEntryUmail(String entryUmail)
	{
		m_entryUmail = entryUmail;
	}
	/**
	 * テキストタイプの取得
	 * 
	 * @return テキストタイプ
	 */
	public byte getTextType()
	{
		return m_textType;
	}
	/**
	 * テキストタイプの取得(文字列)
	 * 
	 * @return テキストタイプ
	 */
	public String getTextTypeString()
	{
		String s = null;
		switch(m_textType) {
		case TYPE_TROUBLE :
			s = "故障内容";
			break;
		case TYPE_PRINCIPAL :
			s = "対処方針";
			break;
		case TYPE_CAUSE :
			s = "故障原因";
			break;
		case TYPE_UPDATE :
			s = "処置内容";
			break;
		case TYPE_CONFIRM :
			s = "確認内容";
			break;
		default :
			s = "(unknown):" + m_textType;
			break;
		}
		return s;
	}
	/**
	 * テキストタイプのセット
	 * 
	 * @param textType テキストタイプ
	 */
	public void setTextType(byte textType)
	{
		m_textType = textType;
	}
	/**
	 * 内容の取得
	 * 
	 * @return 内容
	 */
	public String getContent()
	{
		return m_content;
	}
	/**
	 * 内容の取得(文字列)
	 * 
	 * @return 内容
	 */
	public String getContentString()
	{
		return Converter.StringToHtml2(m_content);
	}
	/**
	 * 内容のセット
	 * 
	 * @param content 内容
	 */
	public void setContent(String content)
	{
		m_content = content;
	}
	/**
	 * 更新者の取得
	 * 
	 * @return 更新者
	 */
	public int getUpdateUno()
	{
		return m_updateUno;
	}
	/**
	 * 更新者のセット
	 * 
	 * @param updateUno 更新者
	 */
	public void setUpdateUno(int updateUno)
	{
		m_updateUno = updateUno;
	}
	/**
	 *	更新日の取得
	 *
	 * @return	update		更新日文字列
	 */
	public Date getUpdateDate()
	{
		return m_updateDate;
	}
	/**
	 *	更新日の取得
	 *
	 * @return	update		更新日文字列
	 */
	public String getUpdateDateString()
	{
		return Converter.getDateString(m_updateDate);
	}
	/**
	 *	更新日のセット
	 *
	 * @return	update		更新日文字列
	 */
	public void setUpdateDate(Date date)
	{
		m_updateDate = date;
	}
	
//===========================================================================
// methods
//===========================================================================
	/**
	 *	クラス情報
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("--- [AddText] --- \n");
		sb.append("bno       : " + m_bno + "\n");
		sb.append("entryDate : " + getEntryDateString() + "\n");
		sb.append("entryUno  : " + m_entryUno + "\n");
		sb.append("entryDname: " + m_entryUname + "\n");
		sb.append("entryDmail: " + m_entryUmail + "\n");
		sb.append("textType  : " + getTextTypeString() + "\n");
		sb.append("updateUno : " + m_updateUno + "\n");
		sb.append("updateDat : " + getUpdateDateString() + "\n");
		sb.append("--- [AddText] --- \n");
		return sb.toString();
	}
	/**
	 *	テストメイン
	 */
	public static void main(String[] args)
	{
		System.out.println("------- << AddText Test Start >> -----------");
		unitTest1();	//追加検索テスト
		
		System.out.println("------- << AddText Test End >> -----------");
	}
	
	/**
	 *	unitTest1
	 *
	 * 新規作成テスト
	 *
	 */
	public static void unitTest1()
	{
		System.out.println("------- unitTest1 -----------");
		
		System.out.println("insert start");
		//故障内容を生成
		AddText addText = null;
		for (int i = 0; i < 5; i++) {
			addText = new AddText();
			addText.setBno(2);
			addText.setTextType(AddText.TYPE_TROUBLE);
			addText.setContent("故障内容" + i);
			addText.insert(i);	//追加
			sleep();
		}
		
		//
		addText = new AddText();
		addText.setBno(2);
		addText.setTextType(AddText.TYPE_PRINCIPAL);
		addText.setContent("対処方針／担当者連絡事項");
		addText.insert(7);	//追加
		//
		addText = new AddText();
		addText.setBno(2);
		addText.setTextType(AddText.TYPE_CAUSE);
		addText.setContent("故障原因");
		addText.insert(7);	//追加
		//
		addText = new AddText();
		addText.setBno(2);
		addText.setTextType(AddText.TYPE_UPDATE);
		addText.setContent("処置内容");
		addText.insert(7);	//追加
		//
		addText = new AddText();
		addText.setBno(2);
		addText.setTextType(AddText.TYPE_CONFIRM);
		addText.setContent("確認内容");
		addText.insert(7);	//追加
		
		
		Collection list = null;
		
		//バグ番号検索
		System.out.println("find bug#2 all types");
		list = AddText.getAddText(2);
		display(list);
		
		//バグ番号、タイプ検索
		System.out.println("find bug#2 TROUBLE types");
		list = AddText.getAddText(2, AddText.TYPE_TROUBLE);
		display(list);
		
		//バグ番号、タイプ検索
		System.out.println("find bug#2 CONFIRM types");
		list = AddText.getAddText(2, AddText.TYPE_CONFIRM);
		display(list);
	}
	/**
	 * Sleep
	 */
	private static void sleep() {
		try {
			Thread.sleep(1000);
		} catch (Exception e) {
			
		}
	}
	/**
	 * リストの表示
	 *
	 */
	private static void display(Collection list)
	{
		Iterator ite = list.iterator();
		
		while(ite.hasNext()) {
			AddText AddText = (AddText)ite.next();
			
			System.out.println(AddText.toString());
		}
	}
}