/* return status codes for account/device */ public static int[] getStatusCodes(String accountID, String deviceID) throws DBException { /* account-id specified? */ if (StringTools.isBlank(accountID)) { return new int[0]; } /* device-id specified? */ if (StringTools.isBlank(deviceID)) { deviceID = ALL_DEVICES; } /* select */ // DBSelect: SELECT statucCode FROM StatusCode WHERE (accountID='acct') AND (deviceID='*') ORDER // BY statucCode DBSelect<StatusCode> dsel = new DBSelect<StatusCode>(StatusCode.getFactory()); dsel.setSelectedFields(StatusCode.FLD_statusCode); DBWhere dwh = dsel.createDBWhere(); dsel.setWhere( dwh.WHERE_( dwh.AND( dwh.EQ(StatusCode.FLD_accountID, accountID), dwh.EQ(StatusCode.FLD_deviceID, deviceID)))); dsel.setOrderByFields(StatusCode.FLD_statusCode); /* get list */ java.util.List<Integer> codeList = new Vector<Integer>(); Statement stmt = null; ResultSet rs = null; try { stmt = DBConnection.getDefaultConnection().execute(dsel.toString()); rs = stmt.getResultSet(); while (rs.next()) { int code = rs.getInt(StatusCode.FLD_statusCode); codeList.add(new Integer(code)); } } catch (SQLException sqe) { throw new DBException("Getting StatusCode List", sqe); } finally { if (rs != null) { try { rs.close(); } catch (Throwable t) { } } if (stmt != null) { try { stmt.close(); } catch (Throwable t) { } } } /* return array of status codes */ int codeListInt[] = new int[codeList.size()]; for (int i = 0; i < codeListInt.length; i++) { codeListInt[i] = codeList.get(i).intValue(); } return codeListInt; }
// [System][Manufacturer][SubSystem][Problem] eg. "P0071" Powertrain // References: // http://www.obd-codes.com/trouble_codes/ // http://obdcon.sourceforge.net/2010/06/obd-ii-pids/ public static long EncodeFault_OBDII(String dtcStr) { long faultCode = TYPE_OBDII; /* trim */ dtcStr = StringTools.trim(dtcStr); if (dtcStr.indexOf(",") >= 0) { dtcStr = dtcStr.substring(0, dtcStr.indexOf(",")).trim(); } if (dtcStr.equals("")) { return faultCode; } /* check length */ if (dtcStr.length() == 4) { dtcStr = "U" + dtcStr; // unknown } else if (dtcStr.length() != 5) { return faultCode; } /* active */ faultCode |= EncodeActive(true); // [ACTIVE_MASK] 0x0100000000000000 /* encode system cjaracter (ie. "Powertrain") */ faultCode |= EncodeSystem(dtcStr.charAt(0)); // [MID_MASK] 0x00FFFFFF00000000 /* encode manufacturer specific and subsystem */ int mfgCode = StringTools.parseInt(dtcStr.substring(1, 2), 0); int spid = (mfgCode != 0) ? 0x8000 : 0; int subSys = StringTools.parseInt(dtcStr.substring(2, 5), 0); spid |= (subSys & 0xFFF); faultCode |= EncodeSPID(spid); // [SPID_MASK] 0x00000000FFFF0000 /* return fault code */ return faultCode; }
/* return the DBSelect statement for the specified account/group */ protected static DBSelect _getUserListSelect(String acctId, String groupId) { /* empty/null account */ if (StringTools.isBlank(acctId)) { return null; } /* empty/null user */ if (StringTools.isBlank(groupId)) { return null; } /* get select */ // DBSelect: SELECT * FROM GroupList WHERE ((accountID='acct') and (groupID='group')) ORDER BY // userID DBSelect<GroupList> dsel = new DBSelect<GroupList>(GroupList.getFactory()); dsel.setSelectedFields(GroupList.FLD_userID); DBWhere dwh = dsel.createDBWhere(); dsel.setWhere( dwh.WHERE_( dwh.AND( dwh.EQ(GroupList.FLD_accountID, acctId), dwh.EQ(GroupList.FLD_groupID, groupId)))); dsel.setOrderByFields(GroupList.FLD_userID); return dsel; }
/** ** Scrambles String */ public static String _ens64(String d) { if (!StringTools.isBlank(d)) { return Base64.encode(StringTools.getBytes(d), getBase64Alphabet(), Base64Pad); } else { return ""; } }
/** ** Descrambles String */ public static String _des64(String e) { if (!StringTools.isBlank(e)) { byte b[] = Base64.decode(e, getBase64Alphabet(), Base64Pad); return StringTools.toStringValue(b, ' '); } else { return ""; } }
/** ** Decodes an RTP encoded argument */ public static RTProperties parseRTP(String rtpArg) { if (!StringTools.isBlank(rtpArg)) { String s = _des64(rtpArg); if (!StringTools.isBlank(s)) { return new RTProperties(s); } } return null; }
/* 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; }
/** ** Descrambles String */ public static String _des64(String e) { if (!StringTools.isBlank(e)) { try { byte b[] = Base64.decode(e, getBase64Alphabet(), Base64Pad); return StringTools.toStringValue(b, ' '); } catch (Base64.Base64DecodeException bde) { Print.logError("Invalid Base64 characters", bde); return ""; } } else { return ""; } }
public Field(String s) { String f[] = StringTools.parseString(s, FIELD_VALUE_SEPARATOR); if ((f.length > 0) && (f[0].length() > 0) && Character.isLetter(f[0].charAt(0))) { this.isHiRes = (f.length > 0) ? f[0].equalsIgnoreCase("H") : false; this.type = (f.length > 1) ? StringTools.parseInt(f[1], -1) : -1; } else { this.type = (f.length > 0) ? StringTools.parseInt(f[0], -1) : -1; this.isHiRes = (f.length > 1) ? f[1].equalsIgnoreCase("H") : false; } this.index = (f.length > 2) ? StringTools.parseInt(f[2], 0) : 0; this.length = (f.length > 3) ? StringTools.parseInt(f[3], 0) : 0; this.isValid = (f.length == 4) && (this.type >= 0) && (this.index >= 0) && (this.length > 0); }
/* return list of all Devices within the specified DeviceGroup (NOT SCALABLE BEYOND A FEW HUNDRED GROUPS) */ public static java.util.List<String> getUsersForGroup(String acctId, String groupId) throws DBException { /* valid account/groupId? */ if (StringTools.isBlank(acctId)) { return null; } else if (StringTools.isBlank(groupId)) { return null; } /* get db selector */ DBSelect dsel = GroupList._getUserListSelect(acctId, groupId); if (dsel == null) { return null; } /* read users for group */ java.util.List<String> usrList = new Vector<String>(); DBConnection dbc = null; Statement stmt = null; ResultSet rs = null; try { dbc = DBConnection.getDefaultConnection(); stmt = dbc.execute(dsel.toString()); rs = stmt.getResultSet(); while (rs.next()) { String usrId = rs.getString(GroupList.FLD_userID); usrList.add(usrId); } } catch (SQLException sqe) { throw new DBException("Get Group GroupeList", sqe); } finally { if (rs != null) { try { rs.close(); } catch (Throwable t) { } } if (stmt != null) { try { stmt.close(); } catch (Throwable t) { } } DBConnection.release(dbc); } /* return list */ return usrList; }
/* encode "type=<type> ..." into long value */ public static long EncodeFault(String faultProps) { if (!StringTools.isBlank(faultProps)) { return DTOBDFault.EncodeFault(new RTProperties(faultProps)); } else { return 0L; } }
/* set Role access level */ public static boolean deleteAccessLevel(Role role, String aclId) throws DBException { /* role specified? */ if (role == null) { return false; // quietly ignore } String acctId = role.getAccountID(); String roleId = role.getRoleID(); /* acl-id specified? */ if (StringTools.isBlank(aclId)) { return false; // quietly ignore } /* already deleted? */ boolean aclExists = RoleAcl.exists(acctId, roleId, aclId); if (!aclExists) { return false; } /* delete */ RoleAcl.Key aclKey = new RoleAcl.Key(acctId, roleId, aclId); aclKey.delete(true); // also delete dependencies return true; }
/** * ** Adds an argument to the URI ** @param key The key name of the argument to add ** @param * value The value of the new key ** @param encode True if <code>value</code> shoudl be hex * encoded ** @param obfuscate True if <code>value</code> should be obfuscated ** @return This * URIArg, with the argument added */ protected URIArg _addArg(String key, String value, boolean encode, boolean obfuscate) { if (!StringTools.isBlank(key)) { String val = encode ? this.encodeArg(value, obfuscate) : value; this.getKeyValList().add(new KeyVal(key, val)); } return this; }
/* get/create device list entry */ public static GroupList getGroupList(User user, String groupID, boolean createOK) throws DBException { // does not return null, if 'createOK' is true /* User specified? */ if (user == null) { throw new DBException("User not specified."); } String accountID = user.getAccountID(); String userID = user.getUserID(); /* group exists? */ if (StringTools.isBlank(groupID)) { throw new DBException("DeviceGroup ID not specified."); } else if (!DeviceGroup.exists(accountID, groupID)) { throw new DBException("DeviceGroup does not exist: " + accountID + "/" + groupID); } /* create/save record */ GroupList.Key grpListKey = new GroupList.Key(accountID, userID, groupID); if (grpListKey.exists()) { // may throw DBException // already exists GroupList listItem = grpListKey.getDBRecord(true); listItem.setUser(user); return listItem; } else if (createOK) { GroupList listItem = grpListKey.getDBRecord(); listItem.setCreationDefaultValues(); listItem.setUser(user); return listItem; } else { // record doesn't exist, and caller doesn't want us to create it return null; } }
/* set Role access level */ public static void setAccessLevel(Role role, String aclId, AccessLevel level) throws DBException { /* role specified? */ if (role == null) { throw new DBException("Role not specified."); } String acctId = role.getAccountID(); String roleId = role.getRoleID(); /* acl-id specified? */ if (StringTools.isBlank(aclId)) { throw new DBException("Acl-ID not specified."); } /* get/create role */ RoleAcl roleAcl = null; RoleAcl.Key aclKey = new RoleAcl.Key(acctId, roleId, aclId); if (aclKey.exists()) { // may throw DBException roleAcl = RoleAcl.getRoleAcl(role, aclId); // may throw DBException } else { roleAcl = aclKey.getDBRecord(); roleAcl.setRole(role); } /* set access level */ int levelInt = (level != null) ? level.getIntValue() : AccessLevel.NONE.getIntValue(); roleAcl.setAccessLevel(levelInt); /* save */ roleAcl.save(); // may throw DBException }
/** * ** Adds an argument to the URI ** @param key The key name of the argument to add ** @param rtp * The RTP encoded values of the new key ** @return This URIArg, with the argument added */ public URIArg addArg(String key, RTProperties rtp) { String r = (rtp != null) ? rtp.toString() : null; if (!StringTools.isBlank(r)) { return this._addArg(key, URIArg.encodeRTP(rtp), false /*encode*/, false /*obfuscate*/); } else { return this.addArg(key, ""); } }
public static void main(String argv[]) { RTConfig.setCommandLineArgs(argv); InitJ1587DescriptionProvider(); RTProperties cmdLineProps = RTConfig.getCommandLineProperties(); long fault = EncodeFault(cmdLineProps); Print.sysPrintln("Fault : " + fault + " [0x" + StringTools.toHexString(fault) + "]"); Print.sysPrintln("String: " + GetPropertyString(fault)); Print.sysPrintln("Desc : " + GetFaultDescription(fault, 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 String GetPropertyString_OBDII(String dtcStr) { StringBuffer sb = new StringBuffer(); sb.append(PROP_TYPE[0]).append("=").append(NAME_OBDII); sb.append(" "); if (!StringTools.isBlank(dtcStr)) { sb.append(PROP_MIL[0]).append("=").append("1"); sb.append(" "); sb.append(PROP_DTC[0]).append("=").append(dtcStr); } else { sb.append(PROP_MIL[0]).append("=").append("0"); } return sb.toString(); }
/** * ** Decodes the specified hex-encoded argument (not yet fully tested) ** @param sb The * StringBuffer where the decoded String argument will be placed ** @param s The String to decode * ** @return The StringBuffer where the decoded String will be placed */ public static StringBuffer decodeArg(StringBuffer sb, String s) { if (sb == null) { sb = new StringBuffer(); } if (s != null) { char ch[] = new char[s.length()]; s.getChars(0, s.length(), ch, 0); for (int i = 0; i < ch.length; i++) { if (ch[i] == '%') { if ((i + 2) < ch.length) { int ch1 = StringTools.hexIndex(ch[i + 1]); int ch2 = StringTools.hexIndex(ch[i + 2]); sb.append((char) (((ch1 << 4) | ch2) & 0xFF)); i += 2; } else { i = ch.length - 1; } } else { sb.append(ch[i]); } } } return sb; }
/* Return status code description (used by RuleInfo, RequestProperties) */ public static String getDescription( String accountID, int statusCode, BasicPrivateLabel pl, String dftDesc) { /* custom code (record) */ StatusCode code = StatusCode.findStatusCode(accountID, null, statusCode); if (code != null) { return code.getDescription(); } /* default */ if (!StringTools.isBlank(dftDesc)) { return dftDesc; } else { return StatusCodes.GetDescription(statusCode, pl); } }
/* Return status code description */ public static String getDescription( Device device, int statusCode, BasicPrivateLabel bpl, String dftDesc) { /* device code */ if (device != null) { StatusCode code = device.getStatusCode(statusCode); if (code != null) { return code.getDescription(); } } /* default */ if (!StringTools.isBlank(dftDesc)) { return dftDesc; } else { return StatusCodes.GetDescription(statusCode, bpl); } }
/** ** Sets the 'host' */ public boolean setHost(String _host) { String uri = this.getURI(); if (!StringTools.isBlank(_host) && URIArg.isAbsoluteURL(uri)) { try { URL oldURI = new URL(uri); String proto = oldURI.getProtocol(); String host = _host; int port = oldURI.getPort(); String file = oldURI.getFile(); URL newURI = new URL(proto, host, port, file); this._setURI(newURI.toString()); return true; } catch (MalformedURLException mue) { // error } } return false; }
/* return Role access level */ public static AccessLevel getAccessLevel(Role role, String aclId, AccessLevel dftAccess) { if (role == null) { return dftAccess; } else if (StringTools.isBlank(aclId)) { return dftAccess; } else { try { RoleAcl roleAcl = RoleAcl.getRoleAcl(role, aclId); // may throw DBException if (roleAcl != null) { return RoleAcl.getAccessLevel(roleAcl); } else { return dftAccess; } } catch (DBException dbe) { // error occurred return AccessLevel.NONE; } } }
/** ** Sets the URI from a string */ public void setURI(String uri) { int p = (uri != null) ? uri.indexOf("?") : -1; if (p >= 0) { this._setURI(uri.substring(0, p)); String a[] = StringTools.parseString(uri.substring(p + 1), "&"); for (int i = 0; i < a.length; i++) { String key = "", val = ""; int e = a[i].indexOf("="); if (e >= 0) { key = a[i].substring(0, e); val = a[i].substring(e + 1); } else { key = a[i]; val = ""; } this._addArg(key, val, false /*encode*/, false /*obfuscate*/); // assume already encoded } } else { this._setURI(uri); } }
/* Return specified role ACL, create if specified */ public static RoleAcl getRoleAcl(Role role, String aclId, boolean create) throws DBException { // does not return null /* role specified? */ if (role == null) { throw new DBNotFoundException("Role not specified."); } String acctId = role.getAccountID(); String roleId = role.getRoleID(); /* acl-id specified? */ if (StringTools.isBlank(aclId)) { throw new DBNotFoundException("Acl-ID not specified."); } /* get/create role */ RoleAcl roleAcl = null; RoleAcl.Key aclKey = new RoleAcl.Key(acctId, roleId, aclId); if (!aclKey.exists()) { // may throw DBException if (create) { roleAcl = aclKey.getDBRecord(); roleAcl.setRole(role); roleAcl.setCreationDefaultValues(); return roleAcl; // not yet saved! } else { throw new DBNotFoundException("Acl-ID does not exists '" + aclKey + "'"); } } else if (create) { // we've been asked to create the Acl, and it already exists throw new DBAlreadyExistsException("Acl-ID already exists '" + aclKey + "'"); } else { roleAcl = RoleAcl.getRoleAcl(role, aclId); // may throw DBException if (roleAcl == null) { throw new DBException("Unable to read existing Role-ID '" + aclKey + "'"); } return roleAcl; } }
/* create a new StatusCode */ public static StatusCode createNewStatusCode(Account account, String deviceID, int code) throws DBException { /* invalid account */ if (account == null) { throw new DBException("Invalid/Null Account specified"); } /* invalid code */ if ((code < 0) || (code > 0xFFFF)) { throw new DBException("Invalid StatusCode specified"); } /* default to 'ALL' devices */ if (StringTools.isBlank(deviceID)) { deviceID = ALL_DEVICES; } /* create status code */ StatusCode sc = StatusCode.getStatusCode(account, deviceID, code, true); // does not return null sc.save(); return sc; }
public String getGroupID() { String v = (String) this.getFieldValue(FLD_groupID); return StringTools.trim(v); }
private void setGroupID(String v) { this.setFieldValue(FLD_groupID, StringTools.trim(v)); }
/** * ** Adds a file extension to the end of this URI, ".xml" etc. The ** extension will be added to * the URI if doesn't already end with it ** @param ext The extension to add */ public void addExtension(String ext) { if (!StringTools.isBlank(ext) && !this.uri.endsWith(ext)) { this.uri += ext; } }