/////// for testing public static void main(String[] args) { if (args.length != 4) { System.out.println("usage: java RTFGenerator dtabaseurl dbuser password xmlfile"); return; } RTFGenerator rt = new RTFGenerator(); try { DBInterface.initialize(args[0], args[1], args[2]); } catch (Exception exp) { String e = exp.toString(); e = exp.getMessage(); e = ""; } try { rt.initialize("BS_ORDER", "RTF_GENERATOR"); } catch (Exception exp) { exp.printStackTrace(); } try { String xmlmessage = FileUtils.readFile(args[3]); rt.execute(null, xmlmessage); } catch (Exception exp) { exp.printStackTrace(); } }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String profiledata = ""; System.out.println("User Profile servlet"); PrintWriter out = response.getWriter(); String username; username = request.getParameter("username"); System.out.println("username:"******":"); username = splitdata[1].toString().trim(); DBInterface connect = DBInterface.getInstance(); if (connect.isConnectionOpen()) { profiledata = connect.getUserProfile(username); System.out.println(profiledata); } out.print(profiledata); } catch (Exception e) { System.out.println("Exception in returning user profile"); out.print("Exception occured"); } }
/** * Add the event to the end of queue. * * @param event The event to add to the queue. * @exception FrameworkException Thrown on errors. */ public void add(Event event) throws FrameworkException { Debug.log(Debug.MSG_STATUS, "QUEUE OPERATION: Adding event to database queue ..."); Connection dbConn = null; PreparedStatement ps = null; long startTime = -1; if (Debug.isLevelEnabled(Debug.BENCHMARK)) startTime = System.currentTimeMillis(); try { event.id = PersistentSequence.getNextSequenceValue(SEQUENCE_NAME); dbConn = DBConnectionPool.getInstance().acquireConnection(); event.arrivalTime = new java.sql.Timestamp(System.currentTimeMillis()); if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "\n" + LINE + "\nExecuting SQL:\n" + INSERT_EVENT_SQL); if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "Event being inserted into database:\n" + event.describe()); if (Debug.isLevelEnabled(Debug.MSG_DATA)) Debug.log(Debug.MSG_DATA, "Event contents:\n" + event.message); ps = dbConn.prepareStatement(INSERT_EVENT_SQL); ps.setString(1, event.channelName); ps.setInt(2, event.id); DBLOBUtils.setCLOB(ps, 3, event.message); ps.setTimestamp(4, event.arrivalTime); int numRows = ps.executeUpdate(); if (numRows != 1) { String errMsg = "Execution of SQL statement [" + INSERT_EVENT_SQL + "] affected [" + numRows + "] rows."; Debug.error(errMsg); throw new FrameworkException(errMsg); } DBConnectionPool.getInstance().commit(dbConn); if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "Successfully committed SQL operation.\n" + LINE); // NOTE: We don't add the item just inserted into the database into the in-memory // queue, as we want it loaded by the separate dequeueing thread. } catch (SQLException sqle) { throw new DatabaseException( "ERROR: Could not execute SQL statement:\n" + DBInterface.getSQLErrorMessage(sqle)); } catch (Exception e) { throw new DatabaseException("ERROR: Could not execute SQL statement:\n" + e.toString()); } finally { releaseDatabaseResources(dbConn, ps); if (Debug.isLevelEnabled(Debug.BENCHMARK) && (startTime > 0)) { long stopTime = System.currentTimeMillis(); Debug.log( Debug.BENCHMARK, "ELAPSED TIME [" + (stopTime - startTime) + "] msec: " + "SQL: Time to insert event into PersistentEvent database table."); } } }
/** * Reset any events meeting the given criteria so that they can be retried. * * @param criteria An event containing the event-selection criteria. * @return The number of events reset. * @exception FrameworkException Thrown on errors. */ protected static int reset(Event criteria) throws FrameworkException { Debug.log( Debug.MSG_STATUS, "QUEUE OPERATION: Resetting events in database for database queue ..."); if (!StringUtils.hasValue(criteria.channelName)) { throw new FrameworkException( "ERROR: Event channel name is a required queue search criteria."); } Connection dbConn = null; PreparedStatement ps = null; long startTime = -1; if (Debug.isLevelEnabled(Debug.BENCHMARK)) startTime = System.currentTimeMillis(); try { dbConn = DBConnectionPool.getInstance().acquireConnection(); if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log( Debug.DB_DATA, "Criteria used to reset events in database:\n" + criteria.describe()); // If no identifier was given that uniquely identifies a single event ... if (criteria.id == 0) { // Use last error time and error count, if available. if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "\n" + LINE + "\nExecuting SQL:\n" + UPDATE_EVENT_RETRY_SQL); ps = dbConn.prepareStatement(UPDATE_EVENT_RETRY_SQL); ps.setString(1, criteria.channelName); if (criteria.lastErrorTime == null) ps.setNull(2, Types.DATE); else ps.setTimestamp(2, criteria.lastErrorTime); if (criteria.errorCount < 1) ps.setNull(3, Types.INTEGER); else ps.setInt(3, criteria.errorCount); } else { // An Id was given which should uniquely identify a single event, so we should // skip using any other qualifying criteria, if present. if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log( Debug.DB_DATA, "\n" + LINE + "\nExecuting SQL:\n" + UPDATE_EVENT_RETRY_BY_ID_SQL); ps = dbConn.prepareStatement(UPDATE_EVENT_RETRY_BY_ID_SQL); ps.setString(1, criteria.channelName); ps.setInt(2, criteria.id); } int numRows = ps.executeUpdate(); DBConnectionPool.getInstance().commit(dbConn); if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log( Debug.DB_DATA, "Committed SQL execution affected [" + numRows + "] rows.\n" + LINE); return numRows; } catch (SQLException sqle) { throw new DatabaseException( "ERROR: Could not execute SQL statement:\n" + DBInterface.getSQLErrorMessage(sqle)); } catch (Exception e) { throw new DatabaseException("ERROR: Could not execute SQL statement:\n" + e.toString()); } finally { releaseDatabaseResources(dbConn, ps); if (Debug.isLevelEnabled(Debug.BENCHMARK) && (startTime > 0)) { long stopTime = System.currentTimeMillis(); Debug.log( Debug.BENCHMARK, "ELAPSED TIME [" + (stopTime - startTime) + "] msec: " + "SQL: Time to reset event(s) in PersistentEvent database table."); } } }
/** * Load any available events from the database up to the configured maximum. * * @param criteria An event containing the event-selection criteria. * @return The next available event on the queue. * @exception FrameworkException Thrown on errors. */ private void loadFromDatabase(Event criteria) throws FrameworkException { Debug.log(Debug.MSG_STATUS, "QUEUE OPERATION: Loading events from database into queue ..."); if (!StringUtils.hasValue(criteria.channelName)) { throw new FrameworkException( "ERROR: Event channel name is a required queue search criteria."); } Connection dbConn = null; PreparedStatement ps = null; long startTime = -1; if (Debug.isLevelEnabled(Debug.BENCHMARK)) startTime = System.currentTimeMillis(); try { dbConn = DBConnectionPool.getInstance().acquireConnection(); if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "\n" + LINE + "\nExecuting SQL:\n" + QUERY_EVENT_SQL); if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log( Debug.DB_DATA, "Criteria used in query against database:\n" + criteria.describe()); ps = dbConn.prepareStatement(QUERY_EVENT_SQL); ps.setString(1, criteria.channelName); ResultSet rs = ps.executeQuery(); for (int counter = 0; (counter < maxDatabaseEventLoadSize) && rs.next(); counter++) { Event event = new Event(); event.channelName = rs.getString(CHANNEL_NAME_COL); event.id = rs.getInt(ID_COL); event.message = DBLOBUtils.getCLOB(rs, MESSAGE_COL); if (Debug.isLevelEnabled(Debug.MSG_LIFECYCLE)) Debug.log(Debug.MSG_LIFECYCLE, "Event contents:\n" + event.message); event.arrivalTime = rs.getTimestamp(ARRIVAL_TIME_COL); event.errorStatus = rs.getString(ERROR_STATUS_COL); event.errorCount = rs.getInt(ERROR_COUNT_COL); event.lastErrorMessage = rs.getString(LAST_ERROR_MESSAGE_COL); event.lastErrorTime = rs.getTimestamp(LAST_ERROR_TIME_COL); // Add item to in-memory buffer. if (Debug.isLevelEnabled(Debug.MSG_STATUS)) Debug.log( Debug.MSG_STATUS, "Adding event [" + event.describe() + "] to in-memory queue buffer."); queue.add(event); if (Debug.isLevelEnabled(Debug.MSG_STATUS)) Debug.log(Debug.MSG_STATUS, "In-memory queue buffer size [" + queue.size() + "]."); } if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "\n" + LINE); } catch (SQLException sqle) { throw new DatabaseException( "ERROR: Could not execute SQL statement:\n" + DBInterface.getSQLErrorMessage(sqle)); } catch (Exception e) { throw new DatabaseException("ERROR: Could not execute SQL statement:\n" + e.toString()); } finally { releaseDatabaseResources(dbConn, ps); if (Debug.isLevelEnabled(Debug.BENCHMARK) && (startTime > 0)) { long stopTime = System.currentTimeMillis(); Debug.log( Debug.BENCHMARK, "ELAPSED TIME [" + (stopTime - startTime) + "] msec: " + "SQL: Time to load event(s) from PersistentEvent database table."); } } }
/** * Update the given event as indicated. * * @param event The event to update. * @param eventStatus The event delivery status. * @exception FrameworkException Thrown on errors. */ public void update(Event event, EventStatus eventStatus) throws FrameworkException { if (Debug.isLevelEnabled(Debug.MSG_STATUS)) Debug.log( Debug.MSG_STATUS, "QUEUE OPERATION: Updating database queue using event status [" + eventStatus.name + "] ..."); // If no consumers were available, the event delivery wasn't attempted so leave // the queue in its current state. if (eventStatus == EventStatus.NO_CONSUMERS_AVAILABLE) { Debug.log( Debug.MSG_STATUS, "Skipping queue update, as no consumers were available to process it."); return; } Connection dbConn = null; PreparedStatement ps = null; long startTime = -1; if (Debug.isLevelEnabled(Debug.BENCHMARK)) startTime = System.currentTimeMillis(); try { dbConn = DBConnectionPool.getInstance().acquireConnection(); if (eventStatus == EventStatus.DELIVERY_SUCCESSFUL) { // If the event was successfully delivered, update status in database to delivered. if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "\n" + LINE + "\nExecuting SQL:\n" + UPDATE_EVENT_SUCCESS_SQL); ps = dbConn.prepareStatement(UPDATE_EVENT_SUCCESS_SQL); java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis()); ps.setTimestamp(1, ts); ps.setString(2, event.channelName); ps.setInt(3, event.id); } else if (eventStatus == EventStatus.DELIVERY_FAILED) { // If the event delivery failed, we mark it as failed in the database. if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "\n" + LINE + "\nExecuting SQL:\n" + UPDATE_EVENT_ERROR_SQL); // Truncate error message if it's larger than database column. if ((event.lastErrorMessage != null) && (event.lastErrorMessage.length() > MAX_ERROR_MESSAGE_LENGTH)) event.lastErrorMessage = event.lastErrorMessage.substring(0, MAX_ERROR_MESSAGE_LENGTH); event.lastErrorTime = new java.sql.Timestamp(System.currentTimeMillis()); ps = dbConn.prepareStatement(UPDATE_EVENT_ERROR_SQL); ps.setTimestamp(1, event.lastErrorTime); ps.setString(2, event.lastErrorMessage); ps.setString(3, event.channelName); ps.setInt(4, event.id); } else { throw new FrameworkException( "ERROR: Invalid event update type [" + eventStatus.name + "]."); } if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "Event being operated on in database:\n" + event.describe()); int numRows = ps.executeUpdate(); if (numRows > 1) { String errMsg = "Execution of update SQL statement affected [" + numRows + "] rows."; Debug.error(errMsg); throw new FrameworkException(errMsg); } DBConnectionPool.getInstance().commit(dbConn); if (Debug.isLevelEnabled(Debug.DB_DATA)) Debug.log(Debug.DB_DATA, "Successfully committed SQL operation.\n" + LINE); // At this point, the event should be removed from the in-memory buffer of events as well, // irrespective of processing outcome. if (Debug.isLevelEnabled(Debug.MSG_STATUS)) Debug.log( Debug.MSG_STATUS, "Removing event [" + event.describe() + "] from in-memory queue buffer."); boolean removed = queue.remove(event); if (Debug.isLevelEnabled(Debug.MSG_STATUS)) Debug.log( Debug.MSG_STATUS, "Event removed? [" + removed + "]. In-memory queue buffer size [" + queue.size() + "]."); } catch (SQLException sqle) { throw new DatabaseException( "ERROR: Could not execute SQL statement:\n" + DBInterface.getSQLErrorMessage(sqle)); } catch (Exception e) { throw new DatabaseException("ERROR: Could not execute SQL statement:\n" + e.toString()); } finally { releaseDatabaseResources(dbConn, ps); if (Debug.isLevelEnabled(Debug.BENCHMARK) && (startTime > 0)) { long stopTime = System.currentTimeMillis(); Debug.log( Debug.BENCHMARK, "ELAPSED TIME [" + (stopTime - startTime) + "] msec: " + "SQL: Time to update event in PersistentEvent database table."); } } }
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.activity_main, menu); /* * Get a handle to the application type list spinner, and load the list of options * from a resource file. The order of items in this resource file must match the constants * (e.g. APP_TYPE_USER_OPTION_POSITION) in this java file. */ MenuItem mSpinner = menu.findItem(R.id.appListMenu_appTypeSpinner); final Spinner appTypeSpinner = (Spinner) mSpinner.getActionView(); final SpinnerAdapter spinnerAdapter = (SpinnerAdapter) ArrayAdapter.createFromResource( context, R.array.app_type_selection_options, android.R.layout.simple_spinner_dropdown_item); appTypeSpinner.setAdapter(spinnerAdapter); /* * Set the app spinner to be the 'current' option */ if (currentAppType.equals(Preferences.APPLIST_LAST_APP_TYPE_USER)) { appTypeSpinner.setSelection(APP_TYPE_USER_OPTION_POSITION); } else if (currentAppType.equals(Preferences.APPLIST_LAST_APP_TYPE_SYSTEM)) { appTypeSpinner.setSelection(APP_TYPE_SYSTEM_OPTION_POSITION); } else if (currentAppType.equals(Preferences.APPLIST_LAST_APP_TYPE_ALL)) { appTypeSpinner.setSelection(APP_TYPE_ALL_OPTION_POSITION); } // Add the handler for the spinner option being changed appTypeSpinner.setOnItemSelectedListener(new AppTypeSpinnerListener()); /* * Load the setting group titles and present them in a spinner for filtering */ // TODO: move the query parts of this off into the DBInterface, where they really belong // It may be better to create a specific adapter for this, since we are relying on searching by // the group titles mSpinner = menu.findItem(R.id.appListMenu_filterByGroupSpinner); final Spinner groupSpinner = (Spinner) mSpinner.getActionView(); // get a list of all the setting groups from the database SQLiteDatabase db = DBInterface.getInstance(context).getDBHelper().getReadableDatabase(); Cursor groupNamesCursor = db.rawQuery( "SELECT DISTINCT " + DBInterface.SettingTable.COLUMN_NAME_GROUP_TITLE + " FROM " + DBInterface.SettingTable.TABLE_NAME + " ORDER BY " + DBInterface.SettingTable.COLUMN_NAME_GROUP_TITLE, null); if (groupNamesCursor == null || groupNamesCursor.getCount() < 1) { throw new DatabaseUninitialisedException("AppListActivity: No settings groups found."); } // if there is already a list of settings groups (although will this ever happen??) // clear it; otherwise initialise an arraylist for it if (this.settingGroups == null) { this.settingGroups = new ArrayList<String>(groupNamesCursor.getCount() + 1); } else { this.settingGroups.clear(); } this.settingGroupAllOption = context.getResources().getString(R.string.applist_setting_filter_spinner_all_option_title); this.settingGroups.add(this.settingGroupAllOption); if (groupNamesCursor.moveToFirst()) { int groupTitleColumnNum = groupNamesCursor.getColumnIndex(DBInterface.SettingTable.COLUMN_NAME_GROUP_TITLE); do { settingGroups.add(groupNamesCursor.getString(groupTitleColumnNum)); } while (groupNamesCursor.moveToNext()); } groupNamesCursor.close(); // db.close(); // Create an array adapter for the spinner from the values loaded from the database, and assign // to the spinner final SpinnerAdapter groupSpinnerAdapter = (SpinnerAdapter) new ArrayAdapter<String>( context, android.R.layout.simple_spinner_dropdown_item, settingGroups); groupSpinner.setAdapter(groupSpinnerAdapter); // Add handler for when the selected option changes groupSpinner.setOnItemSelectedListener(new GroupSpinnerListener()); }