//===========================================================================
// 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.user.*;
import jp.gr.java_conf.bugslife.system.*;

import org.apache.struts.action.*;
import org.apache.struts.util.*;

/**
 *<PRE>
 * 
 * BugActivityクラス
 * 
 *</PRE>
 * @version	0.0, 2001/09/10
 * @author	鰐川　正寛
 */
//===========================================================================
public class BugActivity implements IUpdatable, ILoadable
{
//===========================================================================
// attributes
//===========================================================================
	/**
	 *	bno
	 */
	private int m_bno = 0;
	/**
	 *	seq
	 */
	private int m_seq = 0;
	/**
	 *	item
	 */
	private String m_item;
	/**
	 *	oldValue
	 */
	private String m_oldValue;
	/**
	 *	newValue
	 */
	private String m_newValue;
	/**
	 *	更新者
	 */
	private int m_updateUno = 0;
	/**
	 *	更新者名
	 */
	private String m_updateUname;
	/**
	 *	更新日時
	 */
	private Date m_updateDate = new Date();
	/**
	 * message resources
	 */
	private static MessageResources m_messages =
		MessageResources.getMessageResources("ApplicationResources");
	/**
	 * Log
	 */
	private Log log = LogFactory.getLog(this.getClass().getName());
//=====================================
// DB用項目定義
//=====================================
	/**
	 * 排他制御
	 */
	private static final boolean EXCLUSION = false;		//なし
	/**
	 * AutoIncrement
	 */
	private static final boolean ATOINCREMENT = true;	//なし
	/**
	 * PK 
	 */
	private static final String[] PK = {"seq"};
	/**
	 * 更新項目名
	 */
	private static final String[] DB_ITEMS = {
									"bno","seq",
									"item", "oldValue", "newValue",
									"updateUno", "updateDate" };
//===========================================================================
// methods
//===========================================================================
	/**
	 *	デフォルトコンストラクタ
	 *
	 */
	public BugActivity()
	{
		
	}
//======================================
// db aBugActivityess methods
//======================================
	/**
	 *	insertDB
	 *
	 * @return	処理結果
	 */
	public void insert(int updateUno)
	{
		//注意！
		//履歴の表示をしたときに該当バグの
		//日付順（逆順）、アイテム順に出したいが為に
		//更新日付をここではセットせず、addBugActivityでセットしている
		
//		setSeq(0);
		setUpdateUno(updateUno);
		int seq = DAO.insert(this);
//		setSeq(seq);
	}
	/**
	 *	load
	 *
	 * @return	処理結果
	 */
	public void load()
	{
		DAO.load(this);
	}
	/**
	 *	findAll
	 *
	 * @return	処理結果
	 */
	public static Collection findAll()
	{
		return DAO.findAll(new BugActivity());
	}
	/**
	 *	BugActivityの取得（バグ番号指定）
	 *
	 * @return	処理結果
	 */
	public static Collection getBugActivity(int bno)
	{
		String sql = 
		"SELECT" +
		" A.bno as bno," +
		" A.seq as seq," +
		" A.item as item," +
		" A.oldValue as oldValue," +
		" A.newValue as newValue," +
		" A.updateUno as updateUno," +
		" A.updateDate as updateDate," +
		" B.nickname as updateUname" +
		" FROM BugActivity AS A" +
		" LEFT JOIN UserInfo AS B ON A.updateUno = B.uno" +
		" WHERE A.bno = ?" +
		" ORDER BY A.bno,A.updateDate DESC, A.seq";
		
		ArrayList params = new ArrayList();
		params.add(new Integer(bno));
		
		return DAO.find(new BugActivity(), sql, params);
	}
	/**
	 *	BugActivityの保存
	 *
	 * @return	処理結果
	 */
	public static void addBugActivity(Collection col, int bno, int uno)
	{
		
		BugActivity bAct = new BugActivity();
		bAct.setBno(bno);
		bAct.setUpdateDate(new Date());
		
		Iterator ite = col.iterator();
		while(ite.hasNext()) {
			Activity act = (Activity)ite.next();
			bAct.setSeq(0);
			bAct.setItem(act.getId());
			bAct.setOldValue(act.getOldValueString());
			bAct.setNewValue(act.getNewValueString());
			bAct.insert(uno);
		}
	}
//======================================
// 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;
	}
	/**
	 * seqの取得
	 * 
	 * @return seq
	 */
	public int getSeq()
	{
		return m_seq;
	}
	/**
	 * seqのセット
	 * 
	 * @param seq seq
	 */
	public void setSeq(int seq)
	{
		m_seq = seq;
	}
	/**
	 * itemの取得
	 * 
	 * @return item
	 */
	public String getItem()
	{
		return m_item;
	}
	/**
	 * itemのセット
	 * 
	 * @param item item
	 */
	public void setItem(String item)
	{
		m_item = item;
	}
	/**
	 * oldValueの取得
	 * 
	 * @return oldValue
	 */
	public String getOldValue()
	{
		return m_oldValue;
	}
	/**
	 * oldValueのセット
	 * 
	 * @param oldValue oldValue
	 */
	public void setOldValue(String oldValue)
	{
		m_oldValue = oldValue;
	}
	/**
	 * newValueの取得
	 * 
	 * @return newValue
	 */
	public String getNewValue()
	{
		return m_newValue;
	}
	/**
	 * newValueのセット
	 * 
	 * @param newValue newValue
	 */
	public void setNewValue(String newValue)
	{
		m_newValue = newValue;
	}
	/**
	 * 更新者名の取得
	 * 
	 * @return 更新者名
	 */
	public String getUpdateUname()
	{
		return m_updateUname;
	}
	/**
	 * 更新者名のセット
	 * 
	 * @param updateUname 更新者名
	 */
	public void setUpdateUname(String updateUname)
	{
		m_updateUname = updateUname;
	}
	/**
	 * 更新者の取得
	 * 
	 * @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, Converter.DATE_TIME);
	}
	/**
	 *	更新日のセット
	 *
	 * @return	update		更新日文字列
	 */
	public void setUpdateDate(Date date)
	{
		m_updateDate = date;
	}
	/**
	 *	アイテム名の取得
	 *
	 * @return	アイテム名
	 */
	public String getName() {
		String s = m_messages.getMessage(m_item + ".displayname");
		//名前が取れなかった場合項目名を埋める
		if(s == null) {
			s = "(" + m_item + ")";
		}
		return s;
	}
//===========================================================================
// methods
//===========================================================================
	/**
	 *	クラス情報
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("--- [BugActivity] --- \n");
		sb.append("bno       : " + m_bno + "\n");
		sb.append("seq       : " + m_seq + "\n");
		sb.append("item      : " + m_item + "\n");
		sb.append("name      : " + getName() + "\n");
		sb.append("oldValue  : " + m_oldValue + "\n");
		sb.append("newValue  : " + m_newValue + "\n");
		sb.append("upUname   : " + m_updateUname + "\n");
		sb.append("updateUno : " + m_updateUno + "\n");
		sb.append("updateDate: " + getUpdateDateString() + "\n");
		sb.append("--- [BugActivity] --- \n");
		return sb.toString();
	}
	/**
	 *	テストメイン
	 */
	public static void main(String[] args)
	{
		System.out.println("------- << BugActivity Test Start >> -----------");
		unitTest1();	//新規作成テスト
		
		System.out.println("------- << BugActivity Test End >> -----------");
	}
	
	/**
	 *	unitTest1
	 *
	 * 新規作成テスト
	 *
	 */
	public static void unitTest1()
	{
		System.out.println("------- unitTest1 -----------");
		
		
		String[] items = {
						"bno", "bid", "title","location",
						"subsystem","functionName","browser",
						"os","tid", "appVersion"
						};
		
		//値のセット
		BugActivity BugActivity = new BugActivity();
		
		for(int i = 0; i < 10; i++) {
			BugActivity.setBno(2);
			BugActivity.setItem(items[i]);
			BugActivity.setOldValue("old value" + i+1);
			BugActivity.setNewValue("new value" + i+2);
			BugActivity.insert(1);	//追加
		}
		
		//表示
		display(BugActivity.getBugActivity(2));
		
	}
	/**
	 * リストの表示
	 *
	 */
	private static void display(Collection list)
	{
		Iterator ite = list.iterator();
		
		while(ite.hasNext()) {
			BugActivity BugActivity = (BugActivity)ite.next();
			
			System.out.println(BugActivity.toString());
		}
	}
}