/** * <code>deleteEntry</code> deletes one entry from database. * * @param pCurrentEntry entry to delete * @return entry to display after deletion */ @Override public final E deleteEntry(final String pCurrentEntry) { E resultValue = null; final AbstractDomainUser thisUser = this.getUser(); if (thisUser != null) { final int mandator = thisUser.getMandator(); final String user = thisUser.getUser(); try { // connect to database final InitialContext ic = new InitialContext(); final DataSource lDataSource = (DataSource) ic.lookup(this.getLookUpDataBase()); try (final Connection thisDataBase = lDataSource.getConnection()) { if (this.allowedToChange()) { final E dbEntry = this.readEntry(pCurrentEntry); // invalidate head number if (this.getInvalidateHeadSQL() != null) { try (final PreparedStatement invalidateHeadSQLStatement = thisDataBase.prepareStatement(this.getInvalidateHeadSQL())) { invalidateHeadSQLStatement.clearParameters(); invalidateHeadSQLStatement.setInt(1, mandator); invalidateHeadSQLStatement.setString(2, pCurrentEntry); invalidateHeadSQLStatement.executeUpdate(); this.insertEntry(thisDataBase, mandator, user, dbEntry, true); } } try (PreparedStatement invalidatePosSQLStatement = thisDataBase.prepareStatement(this.invalidatePosSQL)) { int numPos = 0; if (dbEntry.getKeyPos() != null) { numPos = dbEntry.getKeyPos().length; } for (int i = 0; i < numPos; i++) { int sqlPos = 1; invalidatePosSQLStatement.clearParameters(); invalidatePosSQLStatement.setInt(sqlPos++, mandator); invalidatePosSQLStatement.setString(sqlPos++, pCurrentEntry); invalidatePosSQLStatement.setString(sqlPos++, dbEntry.getKeyPos()[i]); invalidatePosSQLStatement.executeUpdate(); this.insertPositionEntry(thisDataBase, mandator, user, dbEntry, true, i); } } } resultValue = this.readNextEntry(pCurrentEntry); } ic.close(); } catch (final SQLException e) { resultValue = null; } catch (final NamingException e) { resultValue = null; } } return resultValue; }
/** * <code>saveEntry</code> saves or inserts a entry to database. * * @param pCurrentEntry entry that has to be saved * @param pDbEntry entry from database to compare * @param pThisDataBase database connection * @param pMandator mandator number * @param pUser name of the user * @param pSaveKeyString key of the entry to save * @throws SQLException if something's going wrong */ @Override protected final void saveEntry( final E pCurrentEntry, final E pDbEntry, final Connection pThisDataBase, final int pMandator, final String pUser, final String pSaveKeyString) throws SQLException { // Entry already exists in Database? if (!pCurrentEntry.equals(pDbEntry)) { try (final PreparedStatement invalidatePosSQLStatement = pThisDataBase.prepareStatement(this.invalidatePosSQL); final PreparedStatement invalidateHeadSQLStatement = pThisDataBase.prepareStatement(this.getInvalidateHeadSQL())) { if (!pCurrentEntry.equalsEntry(pDbEntry) && (this.getInvalidateHeadSQL() != null)) { // Invalidate old entry invalidateHeadSQLStatement.clearParameters(); invalidateHeadSQLStatement.setInt(1, pMandator); invalidateHeadSQLStatement.setString(2, pSaveKeyString); invalidateHeadSQLStatement.executeUpdate(); // Insert new entry this.insertEntry(pThisDataBase, pMandator, pUser, pCurrentEntry, false); pCurrentEntry.setKeyCur(pSaveKeyString); } // Positions // Take a look if position differ and invalidate old int numPos = 0; if (pCurrentEntry.getKeyPos() != null) { numPos = pCurrentEntry.getKeyPos().length; } int numDBPos = 0; if (pDbEntry != null && pDbEntry.getKeyPos() != null) { numDBPos = pDbEntry.getKeyPos().length; } for (int i = 0; i < numDBPos; i++) { boolean isremoved = true; for (int j = 0; j < numPos && isremoved; j++) { if (pDbEntry.getKeyPos()[i].equals(pCurrentEntry.getKeyPos()[j])) { isremoved = false; } } if (isremoved) { int invPos = 1; invalidatePosSQLStatement.clearParameters(); invalidatePosSQLStatement.setInt(invPos++, pMandator); invalidatePosSQLStatement.setString(invPos++, pSaveKeyString); invalidatePosSQLStatement.setString(invPos++, pDbEntry.getKeyPos()[i]); invalidatePosSQLStatement.executeUpdate(); this.insertPositionEntry(pThisDataBase, pMandator, pUser, pDbEntry, true, i); // Invalidate old entry } } // Take a look if position differ and insert new for (int i = 0; i < numPos; i++) { boolean isnew = true; for (int j = 0; j < numDBPos && isnew; j++) { if (pDbEntry.getKeyPos()[j].equals(pCurrentEntry.getKeyPos()[i])) { isnew = false; // Entry already exists, look for changes if (!pCurrentEntry.equalsPosition(pDbEntry, i, j)) { int invPos = 1; // Invalidate old entry invalidatePosSQLStatement.clearParameters(); invalidatePosSQLStatement.setInt(invPos++, pMandator); invalidatePosSQLStatement.setString(invPos++, pSaveKeyString); invalidatePosSQLStatement.setString(invPos++, pDbEntry.getKeyPos()[i]); invalidatePosSQLStatement.executeUpdate(); this.insertPositionEntry(pThisDataBase, pMandator, pUser, pCurrentEntry, false, i); } } } if (isnew) { // Insert new position this.insertPositionEntry(pThisDataBase, pMandator, pUser, pCurrentEntry, false, i); } } } } }