package realcix20.classes;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Cell;
import realcix20.classes.basic.ClassManager;
import realcix20.classes.basic.Row;
import realcix20.classes.plugins.CixFile;
import realcix20.classes.plugins.RealCIXFile;
import realcix20.guis.listeners.MainListener;
import realcix20.guis.utils.DialogManager;
import realcix20.guis.utils.TxtManager;
import realcix20.guis.views.MainView;
import realcix20.guis.views.ObjectEdit;
import realcix20.utils.DAO;
import realcix20.utils.GlobalValueManager;
import realcix20.utils.ObjectUtil;
import realcix20.utils.Resources;
public class PartnerClass extends BaseClass {
public PartnerClass(int clsId) {
super(clsId);
}
public static void deleteDependThings(Row partnerRow) {
String ns = ObjectUtil.findNewCell(partnerRow, "P", "NS").getColumnValue().toString();
String p = ObjectUtil.findNewCell(partnerRow, "P", "P").getColumnValue().toString();
DAO dao = DAO.getInstance();
String sql = ("DELETE FROM PB WHERE PB.NS=? AND PB.P=?");
dao.update(sql);
dao.setObject(1, ns);
dao.setObject(2, p);
dao.executeUpdate();
sql = ("DELETE FROM ADDR WHERE ADDR.NS=? AND ADDR.P=?");
dao.update(sql);
dao.setObject(1, ns);
dao.setObject(2, p);
dao.executeUpdate();
}
public static boolean validateDelete(Row partnerRow, MainView container) {
boolean result = true;
String ns = ObjectUtil.findNewCell(partnerRow, "P", "NS").getColumnValue().toString();
if (ns.equals("ME")) {
result = false;
DialogManager.showMessageDialog(container, TxtManager.getTxt("INFORMATION.PLSGOTOUSERTODELETE"));
}
if (result) {
if (ns.equals("TAX")) {
result = false;
DialogManager.showMessageDialog(container, TxtManager.getTxt("INFORMATION.PLSGOTOTAXDTODELETE"));
}
}
if (result) {
if (CorrespondenceClass.isPReferencedInCorrespondence(partnerRow)) {
result = false;
DialogManager.showMessageDialog(container, TxtManager.getTxt("INFORMATION.CANNTDELETEPREFINC"));
}
}
if (result) {
if (RegClass.isPReferencedInReg(partnerRow)) {
result = false;
DialogManager.showMessageDialog(container, TxtManager.getTxt("INFORMATION.CANNTDELETEPREFINR"));
}
}
if (result) {
int n = DialogManager.showYesNoDialog(container, TxtManager.getTxt("INFORMATION.DELETRELATIVEPBANDADDRESS"));
result = (n == 0);
}
return result;
}
public static boolean isThisLDUsedInPA(Row ldRow) {
boolean result = false;
String ld = ObjectUtil.findNewCell(ldRow, "LD", "LD").getColumnValue().toString();
PartnerClass partner = (PartnerClass)ClassManager.createClass(250);
StringBuffer sb = new StringBuffer(partner.getBasicSQL());
sb.append(" WHERE PA.LD=?");
String sql = sb.toString();
DAO dao = DAO.getInstance();
dao.query(sql);
dao.setObject(1, ld);
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
result = true;
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return result;
}
public static boolean isThisNSUsedInP(Row nsRow) {
boolean result = false;
String ns = ObjectUtil.findNewCell(nsRow, "NS", "NS").getColumnValue().toString();
PartnerClass partner = (PartnerClass)ClassManager.createClass(250);
StringBuffer sb = new StringBuffer(partner.getBasicSQL());
sb.append(" WHERE P.NS=?");
String sql = sb.toString();
DAO dao = DAO.getInstance();
dao.setObject(1, ns);
ResultSet rs = dao.executeQuery();
try {
if ( (rs.next()) && (ns.equals(rs.getObject("NS"))) ) {
result = true;
}
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return result;
}
public static boolean isThisUserUsedInPA(Row userRow) {
boolean result = false;
String user = ObjectUtil.findNewCell(userRow, "RUSER", "RUSER").getColumnValue().toString();
PartnerClass partner = (PartnerClass)ClassManager.createClass(250);
String sql = partner.getBasicSQL();
DAO dao = DAO.getInstance();
dao.query(sql);
ResultSet rs = dao.executeQuery();
try {
ResultSetMetaData rsmd = rs.getMetaData();
int nsColumn = -1;
int ruserColumn = -1;
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
if ( (rsmd.getTableName(i).equals("PA")) && (rsmd.getColumnName(i).equals("NS")) ) {
nsColumn = i;
} else if ( (rsmd.getTableName(i).equals("PA")) && (rsmd.getColumnName(i).equals("RUSER")) ) {
ruserColumn = i;
}
}
while (rs.next()) {
if ( (user.equals(rs.getObject(ruserColumn))) && (!"ME".equals(rs.getObject(nsColumn))) ) {
result = true;
}
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return result;
}
public static void analyPartnerCIXFile(Vector datas, MainListener listener) {
Row partnerRow = null;
Vector partnerAccountRows = null;
Vector publicKeys = null;
Vector banks = null;
Vector addresses = null;
Row familyRow = null;
Vector mePartnerAccountRows = null;
partnerRow = (Row)datas.get(0);
partnerAccountRows = (Vector)datas.get(1);
publicKeys = (Vector)datas.get(2);
banks = (Vector)datas.get(3);
addresses = (Vector)datas.get(4);
familyRow = (Row)datas.get(5);
mePartnerAccountRows = (Vector)datas.get(6);
Iterator partnerAccountRowsIter = partnerAccountRows.iterator();
Iterator publicKeysIter = publicKeys.iterator();
Iterator mePartnerAccountRowsIter = mePartnerAccountRows.iterator();
//Output
while (partnerAccountRowsIter.hasNext()) {
Row row = (Row)partnerAccountRowsIter.next();
row.print();
}
Vector newPartnerAccountRows = new Vector();
Vector newPublicKeys = new Vector();
Vector newMePartnerAccountRows = new Vector();
Vector oldPartnerAccountRows = new Vector();
Vector oldPublicKeys = new Vector();
Vector oldMePartnerAccountRows = new Vector();
Vector ruserPartnerRow;
boolean useFamilyOrRuser = false;//true:family / false:ruser
int n = 0;
String ruser = null;
Row templatePartnerAccountRow = null;
partnerAccountRowsIter = partnerAccountRows.iterator();
while (partnerAccountRowsIter.hasNext()) {
Row partnerAccountRow = (Row)partnerAccountRowsIter.next();
PublicKey publicKey = (PublicKey)publicKeysIter.next();
Row mePartnerAccountRow = (Row)mePartnerAccountRowsIter.next();
String urns = null;
String urp = null;
String urpa = null;
if ( (ObjectUtil.findNewCell(partnerAccountRow, "PA", "URNS").getColumnValue() != null) &&
(!ObjectUtil.findNewCell(partnerAccountRow, "PA", "URNS").toString().trim().equals("")) )
urns = (String)ObjectUtil.findNewCell(partnerAccountRow, "PA", "URNS").getColumnValue();
if ( (ObjectUtil.findNewCell(partnerAccountRow, "PA", "URP").getColumnValue() != null) &&
(!ObjectUtil.findNewCell(partnerAccountRow, "PA", "URP").toString().trim().equals("")) )
urp = (String)ObjectUtil.findNewCell(partnerAccountRow, "PA", "URP").getColumnValue();
if ( (ObjectUtil.findNewCell(partnerAccountRow, "PA", "URPA").getColumnValue() != null) &&
(!ObjectUtil.findNewCell(partnerAccountRow, "PA", "URPA").toString().trim().equals("")) )
urpa = (String)ObjectUtil.findNewCell(partnerAccountRow, "PA", "URPA").getColumnValue();
// System.err.println("urns = " + urns + " urp = " + urp + " urpa = " + urpa);
// System.err.println("ObjectUtil.findNewCell(partnerAccountRow, 'PA', 'RUSER').getColumnValue() = " +
// ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSER").getColumnValue());
templatePartnerAccountRow = mePartnerAccountRow;
if ( (urns == null) || (urp == null) || (urns.trim().equals("")) || (urp.trim().equals("")) ) {
//templatePartnerAccountRow = mePartnerAccountRow;
/*if (n == 0) {
ruser = ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSER").getColumnValue().toString();
} else if (!useFamilyOrRuser) {
if (!ruser.equals(ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSER").getColumnValue().toString()))
useFamilyOrRuser = true;
}*/
n++;
newPartnerAccountRows.add(partnerAccountRow);
newPublicKeys.add(publicKey);
newMePartnerAccountRows.add(mePartnerAccountRow);
} else {
oldPartnerAccountRows.add(partnerAccountRow);
oldPublicKeys.add(publicKey);
oldMePartnerAccountRows.add(mePartnerAccountRow);
}
}
// System.err.println("newPartnerAccountRows.size() = " + newPartnerAccountRows.size());
// System.err.println("oldMePartnerAccountRows.size() = " + oldMePartnerAccountRows.size());
// System.err.println("banks.size() = " + banks.size());
// System.err.println("addresses.size() = " + addresses.size());
DialogManager.showMessageDialog(listener.getContainer(), TxtManager.getTxt("INFORMATION.IMPORT.IMPORTSUCCESS"));
boolean hasNewPartnerAccount = (newPartnerAccountRows.size() > 0);
boolean hasOldPartnerAccount = (oldPartnerAccountRows.size() > 0);
if (hasOldPartnerAccount) {
GlobalValueManager.setValue("APPLICATION.IMPORT", "import");
DialogManager.showMessageDialog(listener.getContainer(), TxtManager.getTxt("INFORMATION.IMPORT.UPDATEPARTNERPART"));
StringBuffer sb = new StringBuffer("");
Iterator oldPartnerAccountRowsIter = oldPartnerAccountRows.iterator();
Iterator oldPublicKeysIter = oldPublicKeys.iterator();
Iterator oldMePartnerAccountRowsIter = oldMePartnerAccountRows.iterator();
PartnerClass partner = (PartnerClass)ClassManager.createClass(250);
while (oldPartnerAccountRowsIter.hasNext()) {
Row oldPartnerAccountRow = (Row)oldPartnerAccountRowsIter.next();
PublicKey publicKey = (PublicKey)oldPublicKeysIter.next();
Row oldMePartnerAccountRow = (Row)oldMePartnerAccountRowsIter.next();
String urns = null;
String urp = null;
String urpa = null;
if ( (ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URNS").getColumnValue() != null) &&
(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URNS").toString().trim().length() > 0) )
urns = ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URNS").getColumnValue().toString();
if ( (ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URP").getColumnValue() != null) &&
(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URP").toString().trim().length() > 0) )
urp = ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URP").getColumnValue().toString();
if ( (ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URPA").getColumnValue() != null) &&
(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URPA").toString().trim().length() > 0) )
urpa = ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "URPA").getColumnValue().toString();
System.err.println("old urns = " + urns + " urp = " + urp + " urpa = " + urpa);
if ( (urpa != null) && (!urpa.trim().equals("")) ) {
Row partnerAccountRow = findPartnerAccountRow(partner, urns, urp, urpa);
if (partnerAccountRow == null) {
sb.append(TxtManager.getTxt("INFORMATION.IMPORT.UPDATEPARTNERFAILPART1"));
sb.append(urns + "/" + urp + "/" + urpa + " " + TxtManager.getTxt("INFORMATION.IMPORT.UPDATEPARTNERFAILPART2") + "\n");
} else {
partnerAccountRow.setModify(true);
ObjectUtil.findNewCell(partnerAccountRow, "P", "CO").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "CO").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "P", "FNAME").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "FNAME").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "P", "LNAME").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "LNAME").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "P", "DOMAIN").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "DOMAIN").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "P", "EMAIL").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "EMAIL").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "PA", "URNS").setColumnValue(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "NS").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "PA", "URP").setColumnValue(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "P").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "PA", "URPA").setColumnValue(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "PA").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "PA", "PUBKEY").setColumnValue(publicKey);
ObjectUtil.findNewCell(partnerAccountRow, "PA", "BLOCK").setColumnValue(false);
ObjectUtil.findNewCell(partnerAccountRow, "PA", "APPPASS").setColumnValue(null);
partner.classUpdate_WholeObject(partnerAccountRow, false);
/*Iterator addressRowsIter = addresses.iterator();
while (addressRowsIter.hasNext()) {
Row addressRow = (Row)addressRowsIter.next();
updateAddress(addressRow, urns, urp);
}*/
updateAddresses(addresses, urns, urp);
Iterator bankRowsIter = banks.iterator();
while (bankRowsIter.hasNext()) {
Row bankRow = (Row)bankRowsIter.next();
updateBank(bankRow, urns, urp);
}
sb.append(TxtManager.getTxt("INFORMATION.IMPORT.UPDATEPARTNERSUCCESSPART1"));
sb.append(urns + "/" + urp + "/" + urpa + " " + TxtManager.getTxt("INFORMATION.IMPORT.UPDATEPARTNERSUCCESSPART2") + "\n");
}
} else {
Row partnerAccountRow = findPartnerRow(partner, urns, urp);
if (partnerAccountRow == null) {
sb.append(TxtManager.getTxt("INFORMATION.IMPORT.ADDNEWPARTNERACCOUNTFAILPART1"));
sb.append(urns + "/" + urp + " " + TxtManager.getTxt("INFORMATION.IMPORT.ADDNEWPARTNERACCOUNTFAILPART2") + "\n");
} else {
Row newAccountRow = ObjectUtil.cloneRow(partner, partnerAccountRow);
ObjectUtil.findNewCell(partnerAccountRow, "P", "CO").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "CO").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "P", "FNAME").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "FNAME").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "P", "LNAME").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "LNAME").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "P", "DOMAIN").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "DOMAIN").getColumnValue());
ObjectUtil.findNewCell(partnerAccountRow, "P", "EMAIL").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "EMAIL").getColumnValue());
ObjectUtil.findNewCell(newAccountRow, "PA", "URNS").setColumnValue(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "NS").getColumnValue());
ObjectUtil.findNewCell(newAccountRow, "PA", "URP").setColumnValue(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "P").getColumnValue());
ObjectUtil.findNewCell(newAccountRow, "PA", "URPA").setColumnValue(ObjectUtil.findNewCell(oldPartnerAccountRow, "PA", "PA").getColumnValue());
ObjectUtil.findNewCell(newAccountRow, "PA", "PUBKEY").setColumnValue(publicKey);
ObjectUtil.findNewCell(newAccountRow, "PA", "PA").setColumnValue(getNewPA(urns, urp));
ObjectUtil.findNewCell(newAccountRow, "PA", "BLOCK").setColumnValue(false);
ObjectUtil.findNewCell(newAccountRow, "PA", "APPPASS").setColumnValue(null);
newAccountRow.setAdd(true);
partnerAccountRow.getRowSet().getRows().add(newAccountRow);
sb.append(TxtManager.getTxt("INFORMATION.IMPORT.ADDNEWPARTNERACCOUNTSUCCESSPART1"));
sb.append(urns + "/" + urp + "/" + getNewPA(urns, urp) + " " + TxtManager.getTxt("INFORMATION.IMPORT.ADDNEWPARTNERACCOUNTSUCCESSPART2") + "\n");
partner.classUpdate_WholeObject(partnerAccountRow, false);
updateAddresses(addresses, urns, urp);
/*Iterator addressRowsIter = addresses.iterator();
while (addressRowsIter.hasNext()) {
Row addressRow = (Row)addressRowsIter.next();
updateAddress(addressRow, urns, urp);
}*/
Iterator bankRowsIter = banks.iterator();
while (bankRowsIter.hasNext()) {
Row bankRow = (Row)bankRowsIter.next();
updateBank(bankRow, urns, urp);
}
}
}
}
DialogManager.showMessageDialog(listener.getContainer(), sb.toString());
GlobalValueManager.setValue("APPLICATION.IMPORT", "noimport");
}
listener.showObjectSettingView(250);
listener.getContainer().updateTable();
if (hasNewPartnerAccount) {
GlobalValueManager.setValue("APPLICATION.IMPORT", "import");
DialogManager.showMessageDialog(listener.getContainer(), TxtManager.getTxt("INFORMATION.IMPORT.CREATEPARTNERPART"));
Row newRow = new Row(listener.getContainer().getCurrentObject().getRowType());
ObjectUtil.findNewCell(newRow, "P", "CO").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "CO").getColumnValue());
ObjectUtil.findNewCell(newRow, "P", "FNAME").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "FNAME").getColumnValue());
ObjectUtil.findNewCell(newRow, "P", "LNAME").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "LNAME").getColumnValue());
ObjectUtil.findNewCell(newRow, "P", "DOMAIN").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "DOMAIN").getColumnValue());
ObjectUtil.findNewCell(newRow, "P", "EMAIL").setColumnValue(ObjectUtil.findNewCell(templatePartnerAccountRow, "P", "EMAIL").getColumnValue());
Iterator newPartnerAccountRowsIter = newPartnerAccountRows.iterator();
Iterator newPublicKeysIter = newPublicKeys.iterator();
Iterator newMePartnerAccountRowsIter = newMePartnerAccountRows.iterator();
n = 1;
while (newPartnerAccountRowsIter.hasNext()) {
Row templateRow = (Row)newPartnerAccountRowsIter.next();
PublicKey publicKey = (PublicKey)newPublicKeysIter.next();
Row mePartnerAccountRow = (Row)newMePartnerAccountRowsIter.next();
Row childRow = ObjectUtil.addnewChildRow(listener.getContainer().getCurrentObject(), templateRow);
ObjectUtil.findNewCell(childRow, "PA", "URNS").setColumnValue(ObjectUtil.findNewCell(childRow, "PA", "NS").getColumnValue());
ObjectUtil.findNewCell(childRow, "PA", "NS").setColumnValue(null);
ObjectUtil.findNewCell(childRow, "PA", "URP").setColumnValue(ObjectUtil.findNewCell(childRow, "PA", "P").getColumnValue());
ObjectUtil.findNewCell(childRow, "PA", "P").setColumnValue(null);
ObjectUtil.findNewCell(childRow, "PA", "URPA").setColumnValue(ObjectUtil.findNewCell(childRow, "PA", "PA").getColumnValue());
ObjectUtil.findNewCell(childRow, "PA", "PA").setColumnValue(n);
ObjectUtil.findNewCell(childRow, "PA", "OWNERNAME").setColumnValue(null);
ObjectUtil.findNewCell(childRow, "PA", "RUSER").setColumnValue(null);
ObjectUtil.findNewCell(childRow, "PA", "RUSERPA").setColumnValue(null);
ObjectUtil.findNewCell(childRow, "PA", "LD").setColumnValue(null);
ObjectUtil.findNewCell(childRow, "PA", "TXT256").setColumnValue(null);
ObjectUtil.findNewCell(childRow, "PA", "PUBKEY").setColumnValue(publicKey);
ObjectUtil.findNewCell(childRow, "PA", "BLOCK").setColumnValue(false);
ObjectUtil.findNewCell(childRow, "PA", "APPPASS").setColumnValue(null);
newRow.getRowSet().getRows().add(childRow);
listener.getContainer().getCurrentObject().getRows().add(childRow);
n++;
}
listener.getContainer().getCurrentObject().getRows().add(newRow);
new ObjectEdit(listener.getContainer(), listener.getContainer().getCurrentObject(), newRow, "Add", "PartnerAdd", addresses, banks);
listener.getContainer().setEnabled(false);
}
}
public static void updateAddresses(Vector addressRows, String ns, String p) {
AddressClass address = (AddressClass)ClassManager.createClass(280);
Iterator addressRowsIter = addressRows.iterator();
Row tempAddressRow = null;
while (addressRowsIter.hasNext()) {
addressRowsIter.next();
tempAddressRow = findAddressRow(address, ns, p);
if (tempAddressRow != null) {
address.classDelete_WholeObject(tempAddressRow);
}
}
addressRowsIter = addressRows.iterator();
while (addressRowsIter.hasNext()) {
Row addressRow = (Row)addressRowsIter.next();
tempAddressRow = ObjectUtil.cloneRow(address, addressRow);
ObjectUtil.findNewCell(tempAddressRow, "ADDR", "ADDR").setColumnValue(getMAXADD() + 1);
ObjectUtil.findNewCell(tempAddressRow, "ADDR", "NS").setColumnValue(ns);
ObjectUtil.findNewCell(tempAddressRow, "ADDR", "P").setColumnValue(p);
address.getRows().add(tempAddressRow);
address.classInsert_WholeObject(tempAddressRow, false);
}
}
public static void updateAddress(Row addressRow, String ns, String p) {
System.err.println("addressRow = " );
addressRow.print();
AddressClass address = (AddressClass)ClassManager.createClass(280);
Row tempAddressRow = findAddressRow(address, ns, p);
if (tempAddressRow != null) {
address.classDelete_WholeObject(tempAddressRow);
}
tempAddressRow = ObjectUtil.cloneRow(address, addressRow);
ObjectUtil.findNewCell(tempAddressRow, "ADDR", "ADDR").setColumnValue(getMAXADD() + 1);
ObjectUtil.findNewCell(tempAddressRow, "ADDR", "NS").setColumnValue(ns);
ObjectUtil.findNewCell(tempAddressRow, "ADDR", "P").setColumnValue(p);
address.getRows().add(tempAddressRow);
address.classInsert_WholeObject(tempAddressRow, false);
}
public static void updateBank(Row bankRow, String ns, String p) {
System.err.println("bankRow = ");
bankRow.print();
PartnerBankClass bank = (PartnerBankClass)ClassManager.createClass(260);
Row tempBankRow = ObjectUtil.cloneRow(bank, bankRow);
ObjectUtil.findNewCell(tempBankRow, "PB", "NS").setColumnValue(ns);
ObjectUtil.findNewCell(tempBankRow, "PB", "P").setColumnValue(p);
System.err.println("ns = " + ns);
System.err.println("tempBankRow = ");
tempBankRow.print();
bank.classDelete_WholeObject(tempBankRow);
bank.getRows().add(tempBankRow);
bank.classInsert_WholeObject(tempBankRow, false);
}
public static String getNewPA(String ns, String p) {
String pa = null;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_MAX_PA_SQL);
dao.setString(1, ns);
dao.setString(2, p);
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
pa = rs.getString(1) + "1";
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return pa;
}
public static int getMAXADD() {
int maxADD = 0;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_MAX_ADDR_SQL);
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
maxADD = rs.getInt(1);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return maxADD;
}
public static Row findBankRow(PartnerBankClass bank, String ns, String p) {
Row bankRow = null;
Iterator bankRowsIter = bank.getRows().iterator();
while (bankRowsIter.hasNext()) {
Row tempBankRow = (Row)bankRowsIter.next();
if ( (ObjectUtil.findNewCell(tempBankRow, "PB", "NS").getColumnValue().equals(ns)) &&
(ObjectUtil.findNewCell(tempBankRow, "PB", "P").getColumnValue().equals(p)) ) {
bankRow = tempBankRow;
break;
}
}
return bankRow;
}
public static Row findAddressRow(AddressClass address, String ns, String p) {
Row addressRow = null;
Iterator addressRowsIter = address.getRows().iterator();
while (addressRowsIter.hasNext()) {
Row tempAddressRow = (Row)addressRowsIter.next();
if ( (ObjectUtil.findNewCell(tempAddressRow, "ADDR", "NS").getColumnValue().equals(ns)) &&
(ObjectUtil.findNewCell(tempAddressRow, "ADDR", "P").getColumnValue().equals(p)) ) {
addressRow = tempAddressRow;
break;
}
}
return addressRow;
}
public static Row findPartnerRow(PartnerClass partner, String ns, String p) {
Row partnerAccountRow = null;
Iterator partnerAccountRowsIter = partner.getRows().iterator();
while (partnerAccountRowsIter.hasNext()) {
Row tempPartnerAccountRow = (Row)partnerAccountRowsIter.next();
if ( (ObjectUtil.findNewCell(tempPartnerAccountRow, "PA", "NS").getColumnValue().equals(ns)) &&
(ObjectUtil.findNewCell(tempPartnerAccountRow, "PA", "P").getColumnValue().equals(p)) ) {
partnerAccountRow = tempPartnerAccountRow;
break;
}
}
return partnerAccountRow;
}
public static Row findPartnerAccountRow(PartnerClass partner, String ns, String p, String pa) {
Row partnerAccountRow = null;
Iterator partnerAccountRowsIter = partner.getRows().iterator();
while (partnerAccountRowsIter.hasNext()) {
Row tempPartnerAccountRow = (Row)partnerAccountRowsIter.next();
if ( (ObjectUtil.findNewCell(tempPartnerAccountRow, "PA", "NS").getColumnValue().equals(ns)) &&
(ObjectUtil.findNewCell(tempPartnerAccountRow, "PA", "P").getColumnValue().equals(p)) &&
(ObjectUtil.findNewCell(tempPartnerAccountRow, "PA", "PA").getColumnValue().equals(pa)) ) {
partnerAccountRow = tempPartnerAccountRow;
break;
}
}
return partnerAccountRow;
}
public static RealCIXFile exportPartnerCIXFile(Row partnerRow, Vector banks, Vector addresses) {
RealCIXFile realCIXFile = null;
String type = "Partner";
Vector datas = new Vector();
datas.add(partnerRow);
Vector partnerAccountRows = new Vector();
Vector mePartnerAccountRows = new Vector();
Vector publicKeys = new Vector();
Iterator partnerAccountRowsIter = partnerRow.getRowSet().getRows().iterator();
while (partnerAccountRowsIter.hasNext()) {
Row partnerAccountRow = (Row)partnerAccountRowsIter.next();
if ( ( (partnerRow.isAdd()) && (partnerAccountRow != partnerRow) ) || (!partnerRow.isAdd()) ) {
PublicKey publicKey = getPublicKey(partnerAccountRow);
publicKeys.add(publicKey);
Row mePartnerAccountRow = getMePartnerAccount(partnerAccountRow);
mePartnerAccountRows.add(mePartnerAccountRow);
partnerAccountRows.add(partnerAccountRow);
}
}
datas.add(partnerAccountRows);
datas.add(publicKeys);
datas.add(banks);
datas.add(addresses);
FamilyClass family = (FamilyClass)ClassManager.createClass(110);
Row familyRow = (Row)family.getRows().get(0);
datas.add(familyRow);
datas.add(mePartnerAccountRows);
Row firstPartnerAccountRow = (Row)partnerAccountRows.get(0);
PublicKey publicKey = getPublicKey(firstPartnerAccountRow);
PrivateKey privateKey = getPrivateKey(firstPartnerAccountRow);
realCIXFile = new RealCIXFile(type, privateKey, publicKey, datas);
return realCIXFile;
}
public static Row getMePartnerAccount(Row partnerAccountRow) {
Row mePartnerAccountRow = null;
PartnerClass partner = (PartnerClass)ClassManager.createClass(250);
String ruser = (String)ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSER").getColumnValue();
String ruserPA = (String)ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSERPA").getColumnValue();
Iterator partnerRowsIter = partner.getRows().iterator();
while (partnerRowsIter.hasNext()) {
Row tempPartnerAccountRow = (Row)partnerRowsIter.next();
String ns = (String)ObjectUtil.findNewCell(tempPartnerAccountRow, "PA", "NS").getColumnValue();
String p = (String)ObjectUtil.findNewCell(tempPartnerAccountRow, "PA", "P").getColumnValue();
String pa = (String)ObjectUtil.findNewCell(tempPartnerAccountRow, "PA", "PA").getColumnValue();
if ( (ns.equals("ME")) && (ruser.equals(p)) && (ruserPA.equals(pa)) ) {
mePartnerAccountRow = tempPartnerAccountRow;
break;
}
}
return mePartnerAccountRow;
}
public static PublicKey getPublicKey(Row partnerAccountRow) {
PublicKey publicKey = null;
String ruser = (String)ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSER").getColumnValue();
String ruserPA = (String)ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSERPA").getColumnValue();
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_PA_SQL);
dao.setString(1, "ME");
dao.setString(2, ruser);
dao.setString(3, ruserPA);
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
publicKey = (PublicKey)rs.getObject("PUBKEY");
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return publicKey;
}
public static PrivateKey getPrivateKey(Row partnerAccountRow) {
PrivateKey privateKey = null;
String ruser = ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSER").getColumnValue().toString();
String ruserPA = ObjectUtil.findNewCell(partnerAccountRow, "PA", "RUSERPA").getColumnValue().toString();
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_PA_SQL);
dao.setString(1, "ME");
dao.setString(2, ruser);
dao.setString(3, ruserPA);
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
privateKey = (PrivateKey)rs.getObject("PRIKEY");
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return privateKey;
}
public boolean isMe(Row mainRow) {
boolean result = false;
Cell nsCell = ObjectUtil.findNewCell(mainRow, "P", "NS");
String ns = (String)nsCell.getColumnValue();
result = ns.equals("ME");
return result;
}
public Object dealWithPartner(Row mainRow) {
Object obj = null;//specify use
Cell nsCell = ObjectUtil.findNewCell(mainRow, "P", "NS");
String ns = (String)nsCell.getColumnValue();
if (ns.equals("ME")) {
dealWithMe(mainRow);
} else {
obj = dealWithPacc(mainRow);
}
return obj;
}
private void dealWithMe(Row mainRow) {
Iterator childRowsIter = mainRow.getRowSet().getRows().iterator();
while (childRowsIter.hasNext()) {
Row childRow = (Row)childRowsIter.next();
if ( (!childRow.isDelete()) && (childRow.isAdd()) ) {
Cell prikeyCell = ObjectUtil.findNewCell(childRow, "PA", "PRIKEY");
Cell pubkeyCell = ObjectUtil.findNewCell(childRow, "PA", "PUBKEY");
PrivateKey prikey = null;
PublicKey pubkey = null;
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
prikey = key.getPrivate();
pubkey = key.getPublic();
} catch (Exception e) {
e.printStackTrace();
}
prikeyCell.setColumnValue(prikey);
pubkeyCell.setColumnValue(pubkey);
}
}
}
private Vector dealWithPacc(Row mainRow) {
Vector data = null;
CixFile file = null;
Cell urnsCell = ObjectUtil.findNewCell(mainRow, "P", "URNS");
Cell urpCell = ObjectUtil.findNewCell(mainRow, "P", "URP");
String stat = "Create New Partner";//other is 'Receive An Invite'
if ( (urnsCell.getColumnValue() == null) || (urpCell.getColumnValue() == null) ||
(((String)urnsCell.getColumnValue()).trim().equals("")) ||
(((String)urpCell.getColumnValue()).trim().equals("")) ) {
stat = "Create New Partner";
} else {
stat = "Receive An Invite";
}
if (stat.equals("Create New Partner")) {
data = new Vector();
file = dealWithPacc_stat_1(mainRow);
data.add(file);
String fileName = generateCixFileName(mainRow, 1);
data.add(fileName);
} else if (stat.equals("Receive An Invite")) {
data = new Vector();
file = dealWithPacc_stat_2(mainRow);
data.add(file);
String fileName = generateCixFileName(mainRow, 2);
data.add(fileName);
}
return data;
}
private CixFile dealWithPacc_stat_1(Row mainRow) {
CixFile file = null;
int cixFileStat = 1;
Vector noEncryData = new Vector();
PublicKey pubKey = null;
byte[] wrappedKey = null;
PrivateKey priKey = null;
Vector encryData = new Vector();
SealedObject sealedEncryData = null;
//use the first account's keypair to encry the data;
Iterator childRowsIter = mainRow.getRowSet().getRows().iterator();
int currentRow = 0;
Vector pubKeys = new Vector();
Vector pas = new Vector();
while (childRowsIter.hasNext()) {
Row childRow = (Row)childRowsIter.next();
if (!childRow.isDelete()) {
Cell ruserCell = ObjectUtil.findNewCell(childRow, "PA", "RUSER");
Cell ruserPaCell = ObjectUtil.findNewCell(childRow, "PA", "RUSERPA");
if ( (ruserCell.getColumnValue() != null) && (ruserPaCell.getColumnValue() != null) ) {
currentRow++;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_PUBKEY_PRIKEY_FROM_PA_SQL);
dao.setObject(1, "ME");
dao.setObject(2, ruserCell.getColumnValue());
dao.setObject(3, ruserPaCell.getColumnValue());
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
if (currentRow == 1) {
pubKey = (PublicKey)rs.getObject("PUBKEY");
priKey = (PrivateKey)rs.getObject("PRIKEY");
}
Cell paCell = ObjectUtil.findNewCell(childRow, "PA", "PA");
pas.add(paCell.getColumnValue());
pubKeys.add(rs.getObject("PUBKEY"));
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
encryData.add(mainRow);
Vector pairData = new Vector();
pairData.add(pubKeys);
pairData.add(pas);
encryData.add(pairData);
try {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecureRandom random = new SecureRandom();
keyGen.init(random);
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
sealedEncryData = new SealedObject(encryData, cipher);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE, priKey);
wrappedKey = cipher.wrap(key);
} catch (Exception e) {
e.printStackTrace();
}
file = new CixFile(cixFileStat, noEncryData, pubKey, wrappedKey, sealedEncryData);
return file;
}
private CixFile dealWithPacc_stat_2(Row mainRow) {
CixFile file = null;
int cixFileStat = 2;
Vector noEncryData = new Vector();
PublicKey pubKey = null;
byte[] wrappedKey = null;
PrivateKey priKey = null;
Vector encryData = new Vector();
SealedObject sealedEncryData = null;
//use the first account's keypair to encry the data;
Iterator childRowsIter = mainRow.getRowSet().getRows().iterator();
int currentRow = 0;
Vector pubKeys = new Vector();
Vector urpas = new Vector();
Vector pas = new Vector();
while (childRowsIter.hasNext()) {
Row childRow = (Row)childRowsIter.next();
if (!childRow.isDelete()) {
Cell ruserCell = ObjectUtil.findNewCell(childRow, "PA", "RUSER");
Cell ruserPaCell = ObjectUtil.findNewCell(childRow, "PA", "RUSERPA");
if ( (ruserCell.getColumnValue() != null) && (ruserPaCell.getColumnValue() != null) ) {
currentRow++;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_PUBKEY_PRIKEY_FROM_PA_SQL);
dao.setObject(1, "ME");
dao.setObject(2, ruserCell.getColumnValue());
dao.setObject(3, ruserPaCell.getColumnValue());
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
if (currentRow == 1) {
pubKey = (PublicKey)rs.getObject("PUBKEY");
priKey = (PrivateKey)rs.getObject("PRIKEY");
}
Cell urpaCell = ObjectUtil.findNewCell(childRow, "PA", "URPA");
Cell paCell = ObjectUtil.findNewCell(childRow, "PA", "PA");
if ( (urpaCell.getColumnValue() != null) && (!((String)urpaCell.getColumnValue()).trim().equals("")) ) {
pubKeys.add(rs.getObject("PUBKEY"));
urpas.add(urpaCell.getColumnValue());
pas.add(paCell.getColumnValue());
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
encryData.add(mainRow);
Vector pairData = new Vector();
pairData.add(pubKeys);
pairData.add(pas);
pairData.add(urpas);
encryData.add(pairData);
try {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecureRandom random = new SecureRandom();
keyGen.init(random);
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
sealedEncryData = new SealedObject(encryData, cipher);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE, priKey);
wrappedKey = cipher.wrap(key);
} catch (Exception e) {
e.printStackTrace();
}
file = new CixFile(cixFileStat, noEncryData, pubKey, wrappedKey, sealedEncryData);
return file;
}
private String generateCixFileName(Row mainRow, int stat) {
StringBuffer sb = new StringBuffer("");
Cell nsCell = ObjectUtil.findNewCell(mainRow, "P", "NS");
Cell pCell = ObjectUtil.findNewCell(mainRow, "P", "P");
java.sql.Date now = new java.sql.Date(System.currentTimeMillis());
String time = now.toString();
sb.append(nsCell.getColumnValue() + "_");
sb.append(pCell.getColumnValue() + "_");
sb.append(stat + "_");
sb.append(time);
return sb.toString();
}
}