package gov.cia.jdbc;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import javax.naming.*;
import javax.sql.*;
import java.sql.*;

import com.ibm.commerce.datatype.*;
import com.ibm.commerce.server.*;
import com.ibm.commerce.util.nc_crypt;
import com.ibm.commerce.base.objects.ServerJDBCHelperAccessBean;


import com.ibm.commerce.common.objects.StoreEntityAccessBean;

public class JdbcUpdaterCmdImpl extends com.ibm.commerce.command.ControllerCommandImpl {

	public void performExecute() throws com.ibm.commerce.exception.ECException {

		TypedProperty req = getRequestProperties();
		TypedProperty res = new TypedProperty();
		Connection con = null;

		try {

			// EJB/アクセスビーンとの混在テスト
			StoreEntityAccessBean ab = new StoreEntityAccessBean();
			ab = ab.findByIdentifierAndMemberId("fashionArchive", new Long("7000000000000000002"));
			ab.setDefaultCurrency("DEM");
			ab.commitCopyHelper();	// 表への書き込みは上位で行っている。

			//((com.ibm.ejs.container.EJBCacheControl) getCommandContext().getSessionContext()).flush();
			ServerJDBCHelperAccessBean serverjdbchelperaccessbean = new ServerJDBCHelperAccessBean();
			serverjdbchelperaccessbean.flush();

			InitialContext initialcontext = new InitialContext();

			DataSource datasource =
				(DataSource) initialcontext.lookup("jdbc/"
				+ WcsApp.configProperties.getValue("Websphere/DatasourceName"));

			String passwd = nc_crypt.decrypt(WcsApp.configProperties.getValue("Database/DB/DBUserPwd"), null);
			con = datasource.getConnection(WcsApp.configProperties.getValue("Database/DB/DBUserID"), passwd);

			// Perform dynamic SQL using JDBC
			PreparedStatement pstmt1 = con.prepareStatement(

			// 更新対象の列名を指定するんだと。
			"select storeent_id,identifier from storeent where storeent_id = ? for update of setccurr");

			// set cursor name for the positioned update statement
			pstmt1.setCursorName("c1");

			long storeent_id = 0;
			try {
				storeent_id = Long.parseLong(req.getString("storeent_id"));
			} catch(Exception n) {
			}
			pstmt1.setLong(1, storeent_id); 

			ResultSet rs = pstmt1.executeQuery();
 
			PreparedStatement pstmt2 = con.prepareStatement(
			"update storeent set setccurr = ? where current of c1");

			String ccurr = null;
			ccurr = req.getString("ccurr");
			pstmt2.setString(1, ccurr); 

			while(rs.next()) {
				String name = rs.getString("storeent_id");
				pstmt2.executeUpdate();
			}
 
			rs.close();
			pstmt1.close();
			pstmt2.close();

			String rollback = null;
			try {
				rollback = req.getString("rollback");
			} catch (Exception r) {}

			// トランザクション制御のテスト。トランザクション制御は禁じ手。
			if (rollback != null) {
				com.ibm.commerce.server.TransactionManager.rollback();
			}

			String throwE = null;
			try {
				throwE = req.getString("throw");
			} catch (Exception t) {}
				
			// 例外のスローテスト
			if (throwE != null) {
				throw new com.ibm.commerce.exception.ECApplicationException();
			}

		} catch (Exception e) {

			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			System.out.println(sw.toString());
			throw new com.ibm.commerce.exception.ECApplicationException();

		} finally {
			if (con != null) {
				try {con.close();}catch (Exception ignore){}
			}		
		}

		res.put(ECConstants.EC_VIEWTASKNAME, "JdbcStoreEntView");
		setResponseProperties(res);
	}
}
// End of FILE.


2002/12 ugya@lycos.com