/* update title string */ public static void updateAccountString( Account account, String stringID, String description, String singular, String plural) throws DBException { /* valid account? */ if (account == null) { throw new DBException("Account not specified."); } /* delete? */ // delete if both singular/plural values are empty/null if (((singular == null) || singular.equals("")) && ((plural == null) || plural.equals(""))) { String acctID = account.getAccountID(); AccountString.Key key = new AccountString.Key(acctID, stringID); key.delete(true); // also delete dependencies (if any) return; } /* get/create AccountString */ AccountString str = AccountString.getAccountString(account, stringID); if (str == null) { str = AccountString.getAccountString(account, stringID, true); } /* insert/update */ str.setDescription(description); str.setSingularTitle(singular); str.setPluralTitle((plural != null) ? plural : singular); str.save(); }
public static boolean exists(String acctID, String strID) throws DBException // if error occurs while testing existance { if ((acctID != null) && (strID != null)) { AccountString.Key strKey = new AccountString.Key(acctID, strID); return strKey.exists(); } return false; }
/* get string */ public static AccountString getAccountString(Account account, String strID) throws DBException { if ((account != null) && (strID != null)) { String acctID = account.getAccountID(); AccountString.Key key = new AccountString.Key(acctID, strID); if (key.exists()) { AccountString str = key.getDBRecord(true); str.setAccount(account); return str; } else { // AccountString does not exist return null; } } else { return null; // just say it doesn't exist } }
// Note: does NOT return null public static AccountString getAccountString(Account account, String strID, boolean create) throws DBException { /* account-id specified? */ if (account == null) { throw new DBNotFoundException("Account not specified."); } String acctID = account.getAccountID(); /* string-id specified? */ if ((strID == null) || strID.equals("")) { throw new DBNotFoundException("String-ID not specified for account: " + acctID); } /* get/create */ AccountString str = null; AccountString.Key strKey = new AccountString.Key(acctID, strID); if (!strKey.exists()) { if (create) { str = strKey.getDBRecord(); str.setAccount(account); str.setCreationDefaultValues(); return str; // not yet saved! } else { throw new DBNotFoundException("String-ID does not exists: " + strKey); } } else if (create) { // we've been asked to create the AccountString, and it already exists throw new DBAlreadyExistsException("String-ID already exists '" + strKey + "'"); } else { str = AccountString.getAccountString(account, strID); if (str == null) { throw new DBException("Unable to read existing String-ID: " + strKey); } return str; } }
public static void main(String args[]) { DBConfig.cmdLineInit(args, true); // main String acctID = RTConfig.getString(ARG_ACCOUNT, ""); String strID = RTConfig.getString(ARG_STRING, ""); /* account-id specified? */ if (StringTools.isBlank(acctID)) { Print.logError("Account-ID not specified."); usage(); } /* get account */ Account acct = null; try { acct = Account.getAccount(acctID); // may throw DBException if (acct == null) { Print.logError("Account-ID does not exist: " + acctID); usage(); } } catch (DBException dbe) { Print.logException("Error loading Account: " + acctID, dbe); // dbe.printException(); System.exit(99); } /* string-id specified? */ if ((strID == null) || strID.equals("")) { Print.logError("String-ID not specified."); usage(); } /* string exists? */ boolean stringExists = false; try { stringExists = AccountString.exists(acctID, strID); } catch (DBException dbe) { Print.logError("Error determining if AccountString exists: " + _fmtStrID(acctID, strID)); System.exit(99); } /* option count */ int opts = 0; /* delete */ if (RTConfig.getBoolean(ARG_DELETE, false) && !acctID.equals("") && !strID.equals("")) { opts++; if (!stringExists) { Print.logWarn("AccountString does not exist: " + _fmtStrID(acctID, strID)); Print.logWarn("Continuing with delete process ..."); } try { AccountString.Key strKey = new AccountString.Key(acctID, strID); strKey.delete(true); // also delete dependencies Print.logInfo("AccountString deleted: " + _fmtStrID(acctID, strID)); stringExists = false; } catch (DBException dbe) { Print.logError("Error deleting AccountString: " + _fmtStrID(acctID, strID)); dbe.printException(); System.exit(99); } System.exit(0); } /* create */ if (RTConfig.getBoolean(ARG_CREATE, false)) { opts++; if (stringExists) { Print.logWarn("AccountString already exists: " + _fmtStrID(acctID, strID)); } else { try { AccountString.createNewAccountString(acct, strID); Print.logInfo("Created AccountString: " + _fmtStrID(acctID, strID)); stringExists = true; } catch (DBException dbe) { Print.logError("Error creating AccountString: " + _fmtStrID(acctID, strID)); dbe.printException(); System.exit(99); } } } /* edit */ if (RTConfig.getBoolean(ARG_EDIT, false)) { opts++; if (!stringExists) { Print.logError("AccountString does not exist: " + _fmtStrID(acctID, strID)); } else { try { AccountString str = AccountString.getAccountString(acct, strID, false); // may throw DBException DBEdit editor = new DBEdit(str); editor.edit(true); // may throw IOException } catch (IOException ioe) { if (ioe instanceof EOFException) { Print.logError("End of input"); } else { Print.logError("IO Error"); } } catch (DBException dbe) { Print.logError("Error editing AccountString: " + _fmtStrID(acctID, strID)); dbe.printException(); } } System.exit(0); } /* no options specified */ if (opts == 0) { Print.logWarn("Missing options ..."); usage(); } }