package realcix20.classes;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.sql.ResultSet;
import java.util.Iterator;
import javax.crypto.SealedObject;
import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Cell;
import realcix20.classes.basic.ClassManager;
import realcix20.classes.basic.Row;
import realcix20.guis.utils.DialogManager;
import realcix20.guis.utils.TxtManager;
import realcix20.guis.views.MainView;
import realcix20.utils.DAO;
import realcix20.utils.ObjectUtil;
import realcix20.utils.PasswordManager;
import realcix20.utils.Resources;
public class UserClass extends BaseClass {
public UserClass(int clsId) {
super(clsId);
}
public static Row createSuperUser(Row familyRow, SealedObject password, String lang) {
UserClass user = (UserClass)ClassManager.createClass(140);
Row superUserRow = new Row(user.getRowType());
ObjectUtil.findNewCell(superUserRow, "RUSER", "RUSER").setColumnValue(ObjectUtil.findNewCell(familyRow, "FAMILY", "FAMILY").getColumnValue());
ObjectUtil.findNewCell(superUserRow, "RUSER", "TITLE").setColumnValue(TxtManager.getTxt("SUPERUSER.TITLE", lang));
ObjectUtil.findNewCell(superUserRow, "RUSER", "FNAME").setColumnValue(ObjectUtil.findNewCell(familyRow, "FAMILY", "FNAME").getColumnValue());
ObjectUtil.findNewCell(superUserRow, "RUSER", "LNAME").setColumnValue(ObjectUtil.findNewCell(familyRow, "FAMILY", "LNAME").getColumnValue());
ObjectUtil.findNewCell(superUserRow, "RUSER", "EMAIL").setColumnValue(ObjectUtil.findNewCell(familyRow, "FAMILY", "EMAIL").getColumnValue());
ObjectUtil.findNewCell(superUserRow, "RUSER", "WEBSITE").setColumnValue(ObjectUtil.findNewCell(familyRow, "FAMILY", "DOMAIN").getColumnValue());
ObjectUtil.findNewCell(superUserRow, "RUSER", "CO").setColumnValue(ObjectUtil.findNewCell(familyRow, "FAMILY", "CO").getColumnValue());
ObjectUtil.findNewCell(superUserRow, "RUSER", "TXT").setColumnValue(TxtManager.getTxt("SUPERUSER.TEXT", lang));
ObjectUtil.findNewCell(superUserRow, "RUSER", "PASSWORD").setColumnValue(password);
user.classInsert_WholeObject(superUserRow, false);
return superUserRow;
}
public static String getSuperUserPassword() {
String password = null;
Row superUserRow = UserClass.getSuperUser();
if (superUserRow != null) {
SealedObject so = (SealedObject)ObjectUtil.findNewCell(superUserRow, "RUSER", "PASSWORD").getColumnValue();
try {
password = PasswordManager.getPassword(so);
} catch (Exception e) {
e.printStackTrace();
}
}
return password;
}
public static Row getSuperUser() {
Row superUserRow = null;
FamilyClass family = (FamilyClass)ClassManager.createClass(110);
UserClass user = (UserClass)ClassManager.createClass(140);
Row familyRow = (Row)family.getRows().get(0);
Iterator userRowsIter = user.getRows().iterator();
while (userRowsIter.hasNext()) {
Row userRow = (Row)userRowsIter.next();
String superUser = ObjectUtil.findNewCell(familyRow, "FAMILY", "FAMILY").getColumnValue().toString();
String ruser = ObjectUtil.findNewCell(userRow, "RUSER", "RUSER").getColumnValue().toString();
if (ruser.equals(superUser)) {
superUserRow = userRow;
break;
}
}
return superUserRow;
}
public static void deleteDependThings(Row userRow) {
//delete partner
String user = ObjectUtil.findNewCell(userRow, "RUSER", "RUSER").getColumnValue().toString();
PartnerClass partner = (PartnerClass)ClassManager.createClass(250);
StringBuffer sb = new StringBuffer("DELETE FROM " + partner.getMainTableName() + " WHERE NS='ME' AND P=?");
String sql = sb.toString();
DAO dao = DAO.getInstance();
dao.setAutoCommit(false);
dao.update(sql);
dao.setObject(1, user);
dao.executeUpdate();
sb = new StringBuffer("DELETE FROM " + partner.getChildTableName() + " WHERE NS='ME' AND P=?");
sql = sb.toString();
dao.update(sql);
dao.setObject(1, user);
dao.executeUpdate();
dao.commit();
dao.setAutoCommit(true);
}
public static boolean validateDelete(Row userRow, MainView container) {
boolean result = true;
if (FamilyClass.isThisUserFamily(userRow)) {
result = false;
DialogManager.showMessageDialog(container, TxtManager.getTxt("INFORMATION.CANNTDELETESUPERUSER"));
}
if (result) {
if (LedgerClass.isThisUserLd(userRow)) {
result = false;
DialogManager.showMessageDialog(container, TxtManager.getTxt("INFORMATION.CANDELETEUSEDINLDRUSER"));
}
}
if (result) {
if (PartnerClass.isThisUserUsedInPA(userRow)) {
int n = DialogManager.showYesNoDialog(container, TxtManager.getTxt("INFORMATION.DELETEUSERANDPA"));
if (n == 1)
result = false;
}
}
return result;
}
//add me account,generate public/private keys, used by PartnerClass
public static void createMePartner(Row userRow) {
String ns = "ME";
Cell ruserCell = ObjectUtil.findNewCell(userRow, "RUSER", "RUSER");
String p = (String)ruserCell.getColumnValue();
String defpa = "1";
String defaddr = null;
Cell fnameCell = ObjectUtil.findNewCell(userRow, "RUSER", "FNAME");
String fname = (String)fnameCell.getColumnValue();
Cell lnameCell = ObjectUtil.findNewCell(userRow, "RUSER", "LNAME");
String lname = (String)lnameCell.getColumnValue();
Cell websiteCell = ObjectUtil.findNewCell(userRow, "RUSER", "WEBSITE");
String domain = (String)websiteCell.getColumnValue();
Cell emailCell = ObjectUtil.findNewCell(userRow, "RUSER", "EMAIL");
String email = (String)emailCell.getColumnValue();
Cell coCell = ObjectUtil.findNewCell(userRow, "RUSER", "CO");
String co = (String)coCell.getColumnValue();
DAO dao = DAO.getInstance();
dao.update(Resources.INSERT_P_SQL);
dao.setObject(1, ns);
dao.setObject(2, p);
dao.setObject(3, fname);
dao.setObject(4, lname);
dao.setObject(5, domain);
dao.setObject(6, email);
dao.setObject(7, co);
dao.executeUpdate();
String ownername = p;
String ruser = p;
String cld = getDEFLD("C");
String txt256 = TxtManager.getTxt("VIEW.OBJECTEDIT.RUSER.DEFAULTACCOUNT");
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();
}
boolean block = false;
dao.update(Resources.INSERT_PA_SQL);
dao.setObject(1, ns);
dao.setObject(2, p);
dao.setObject(3, defpa);
dao.setObject(4, ownername);
dao.setObject(5, null);
dao.setObject(6, null);
dao.setObject(7, txt256);
dao.setObject(8, prikey);
dao.setObject(9, pubkey);
dao.setObject(10, block);
dao.executeUpdate();
}
//Will be modified
public static String getDEFLD(String lt) {
String defld = null;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_DEFLD_FROM_LT_SQL);
dao.setObject(1, lt);
ResultSet rs = dao.executeQuery();
try {
if (rs.next())
defld = rs.getString("DEFLD");
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return defld;
}
public static String getCurrentRUser() {
String ruser = null;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_CURRENT_RUSER_SQL);
ResultSet rs = dao.executeQuery();
try {
if (rs.next())
ruser = rs.getString("VVALUE");
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return ruser;
}
}