/* return true if StatusCode exists */ public static boolean exists(String accountID, String deviceID, int code) throws DBException // if error occurs while testing existance { if ((accountID != null) && (deviceID != null)) { StatusCode.Key scKey = new StatusCode.Key(accountID, deviceID, code); return scKey.exists(); } return false; }
/* return StatusCode */ public static StatusCode findStatusCode(String accountID, String deviceID, int statusCode) { /* check account status codes */ if (!StringTools.isBlank(accountID)) { // first, try account/device if (!StringTools.isBlank(deviceID)) { try { StatusCode.Key codeKey = new StatusCode.Key(accountID, deviceID, statusCode); if (codeKey.exists()) { // may throw DBException StatusCode code = codeKey.getDBRecord(true); if (code != null) { // should not be null return code; } } } catch (DBException dbe) { // ignore error } } // next, try just the account try { StatusCode.Key codeKey = new StatusCode.Key(accountID, statusCode); if (codeKey.exists()) { // may throw DBException StatusCode code = codeKey.getDBRecord(true); if (code != null) { // should not be null return code; } } } catch (DBException dbe) { // ignore error } } /* check global status codes */ String sysAdmin = AccountRecord.getSystemAdminAccountID(); if (!StringTools.isBlank(sysAdmin)) { try { StatusCode.Key codeKey = new StatusCode.Key(sysAdmin, statusCode); if (codeKey.exists()) { // may throw DBException StatusCode code = codeKey.getDBRecord(true); if (code != null) { // should not be null return code; } } } catch (DBException dbe) { // ignore error } } /* icon selector not found */ return null; }
/* Return specified StatusCode, create if specified */ private static StatusCode _getStatusCode( String accountID, Account account, String deviceID, int code, boolean createOK) throws DBException { // does not return null if 'createOK' is true /* account-id specified? */ if (StringTools.isBlank(accountID)) { if (account == null) { throw new DBException("Account not specified."); } else { accountID = account.getAccountID(); } } else if ((account != null) && !account.getAccountID().equals(accountID)) { throw new DBException("Account does not match specified AccountID."); } /* device-id specified? */ if (StringTools.isBlank(deviceID)) { // throw new DBException("Device-ID not specified."); deviceID = ALL_DEVICES; } /* get/create entity */ StatusCode.Key scKey = new StatusCode.Key(accountID, deviceID, code); if (scKey.exists()) { // may throw DBException StatusCode sc = scKey.getDBRecord(true); if (account != null) { sc.setAccount(account); } return sc; } else if (createOK) { StatusCode sc = scKey.getDBRecord(); if (account != null) { sc.setAccount(account); } sc.setCreationDefaultValues(); return sc; // not yet saved! } else { // record doesn't exist, and caller doesn't want us to create it return null; } }
public static void main(String argv[]) { DBConfig.cmdLineInit(argv, true); // main String accountID = RTConfig.getString(ARG_ACCOUNT, ""); String deviceID = RTConfig.getString(ARG_DEVICE, ""); int statusCode = RTConfig.getInt(ARG_CODE, 0); boolean anyCode = true; // RTConfig.hasProperty(ARG_ECODE); /* account-id specified? */ if (StringTools.isBlank(accountID)) { Print.logError("Account-ID not specified."); usage(); } /* get account */ Account account = null; try { account = Account.getAccount(accountID); // may throw DBException if (account == null) { Print.logError("Account-ID does not exist: " + accountID); usage(); } } catch (DBException dbe) { Print.logException("Error loading Account: " + accountID, dbe); // dbe.printException(); System.exit(99); } /* device-id specified? */ if (StringTools.isBlank(deviceID) || deviceID.startsWith("/")) { deviceID = ALL_DEVICES; } /* check device existance */ if (!deviceID.equals(ALL_DEVICES)) { try { Device device = Device.getDevice(account, deviceID); // may throw DBException if (device == null) { Print.logError("Device-ID does not exist: " + accountID + " / " + deviceID); usage(); } } catch (DBException dbe) { Print.logException("Error loading Device: " + accountID + " / " + deviceID, dbe); System.exit(99); } } /* status-code specified? */ if ((statusCode > 0) && !anyCode && !StatusCodes.IsValid(statusCode, account.getPrivateLabel())) { Print.logError("Invalid Status Code specified."); usage(); } /* statusCode specified? */ if (statusCode <= 0) { Print.logError("StatusCode not specified."); usage(); } /* statusCode exists? */ boolean statusCodeExists = false; try { statusCodeExists = StatusCode.exists(accountID, deviceID, statusCode); } catch (DBException dbe) { Print.logError( "Error determining if StatusCode exists: " + accountID + "/" + deviceID + "/" + statusCode); System.exit(99); } /* option count */ int opts = 0; /* delete */ if (RTConfig.getBoolean(ARG_DELETE, false)) { opts++; if (!statusCodeExists) { Print.logWarn( "StatusCode does not exist: " + accountID + "/" + deviceID + "/" + statusCode); Print.logWarn("Continuing with delete process ..."); } try { StatusCode.Key scKey = new StatusCode.Key(accountID, deviceID, statusCode); scKey.delete(true); // also delete dependencies (if any) Print.logInfo("StatusCode deleted: " + accountID + "/" + deviceID + "/" + statusCode); statusCodeExists = false; } catch (DBException dbe) { Print.logError( "Error deleting StatusCode: " + accountID + "/" + deviceID + "/" + statusCode); dbe.printException(); System.exit(99); } System.exit(0); } /* create */ if (RTConfig.getBoolean(ARG_CREATE, false)) { opts++; if (statusCodeExists) { Print.logWarn( "StatusCode already exists: " + accountID + "/" + deviceID + "/" + statusCode); } else { try { StatusCode.createNewStatusCode(account, deviceID, statusCode); Print.logInfo("Created StatusCode: " + accountID + "/" + deviceID + "/" + statusCode); statusCodeExists = true; } catch (DBException dbe) { Print.logError( "Error creating StatusCode: " + accountID + "/" + deviceID + "/" + statusCode); dbe.printException(); System.exit(99); } } } /* edit */ if (RTConfig.getBoolean(ARG_EDIT, false)) { opts++; if (!statusCodeExists) { Print.logError( "StatusCode does not exist: " + accountID + "/" + deviceID + "/" + statusCode); } else { try { StatusCode sc = StatusCode.getStatusCode(account, deviceID, statusCode); // may throw DBException DBEdit editor = new DBEdit(sc); editor.edit(); // 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 StatusCode: " + accountID + "/" + deviceID + "/" + statusCode); dbe.printException(); } } System.exit(0); } /* list */ if (RTConfig.hasProperty(ARG_LIST)) { opts++; String listType = RTConfig.getString(ARG_LIST, null); // TODO: complete ... } /* no options specified */ if (opts == 0) { Print.logWarn("Missing options ..."); usage(); } }