schwertmeister
Grünschnabel
Hallo an alle Helfenden:
Ich habe bis Sonntag noch Zeit für folgende Aufgabe:
" Struktieren Sie Ihren Code so, dass Sie eines der Architektur-Muster Table-Data-Gateway,
Row-Data-Gateway oder Data-Access-Object verwenden."
Kann mir jemand dabei helfen? Gerne auch PN an mich. Danke im Voraus.
Mein Code:
Ich habe bis Sonntag noch Zeit für folgende Aufgabe:
" Struktieren Sie Ihren Code so, dass Sie eines der Architektur-Muster Table-Data-Gateway,
Row-Data-Gateway oder Data-Access-Object verwenden."
Kann mir jemand dabei helfen? Gerne auch PN an mich. Danke im Voraus.
Mein Code:
Java:
public class BelegungServiceImpl implements BelegungService {
protected DataSource dataSource = null;
public BelegungServiceImpl(String connectUri, String uname,
String passwd) {
JdbcUtils.loadDriver("oracle.jdbc.driver.OracleDriver");
dataSource = JdbcUtils.getPooledDataSource(connectUri, uname, passwd,
false, true);
}
@Override
public Integer vergebePlaetze(Long verNr) throws OverbookException,
DataAccessException {
zuweisungRangstufe1(verNr);
zuweisungRangstufe2(verNr);
zuweisungRangstufe3(verNr);
zuweisungRangstufe4(verNr);
int freiePlaetze = zulassung(verNr);
return freiePlaetze;
}
private Integer ermittleMaxTeilnehmer(long verNr) {
Connection connection = null;
PreparedStatement ps = null;
String query = null;
ResultSet rs = null;
int maxTeilnehmer;
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "select F.max_teilnehmer from Veranstaltung V INNER JOIN Fach F ON V.Fachnr=F.Fachnr where vernr = ?";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
rs = ps.executeQuery();
if(!rs.next()) {
throw new DataAccessException();
} else {
maxTeilnehmer = rs.getInt("max_teilnehmer");
}
}
catch (SQLException e)
{
throw new RuntimeException();
}
catch (NullPointerException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
return maxTeilnehmer;
}
private Integer ermittleZugeleasseneTeilnehmer(long verNr) {
Connection connection = null;
PreparedStatement ps = null;
String query = null;
ResultSet rs = null;
int zugelasseneTeilnehmer;
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "SELECT COUNT(*) AS zugelassene_teilnehmer FROM Belegung WHERE Status= 'ZU' AND VerNr = ?";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
rs = ps.executeQuery();
rs.next();
zugelasseneTeilnehmer = rs.getInt("zugelassene_teilnehmer");
}
catch (SQLException e)
{
throw new RuntimeException();
}
catch (NullPointerException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
return zugelasseneTeilnehmer;
}
private void zuweisungRangstufe(long verNr, long matrNr, int rangstufe) throws RuntimeException {
Connection connection = null;
PreparedStatement ps = null;
String query = null;
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "UPDATE Belegung SET RANGSTUFE = ? WHERE VERNR = ? and MATRNR = ?";
ps = connection.prepareStatement(query);
ps.setInt(1, rangstufe);
ps.setLong(2, verNr);
ps.setLong(3, matrNr);
ps.executeQuery();
}
catch (SQLException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
}
private void zuweisungRangstufe1(long verNr) {
Connection connection = null;
PreparedStatement ps = null;
String query = null;
ResultSet rs = null;
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "SELECT S.MATRNR FROM Veranstaltung V "
+ " INNER JOIN Fach F ON F.FACHNR = V.FACHNR"
+ " INNER JOIN Belegung B ON B.VERNR = V.VERNR"
+ " INNER JOIN Student S ON B.MATRNR = S.MATRNR"
+ " INNER JOIN Studium FS ON FS.MATRNR = S.MATRNR"
+ " WHERE B.VERNR=? AND B.Status != 'ZU'"
+ " AND FS.SGKN = F.SGKN AND FS.FACHSEMESTER = F.VORGEG_SEMESTER";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
rs = ps.executeQuery();
while(rs.next()) {
long matrNr = rs.getLong("MATRNR");
zuweisungRangstufe(verNr, matrNr, 1);
}
}
catch (SQLException e)
{
throw new RuntimeException();
}
catch (NullPointerException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
}
private void zuweisungRangstufe2(long verNr) {
Connection connection = null;
PreparedStatement ps = null;
String query = null;
ResultSet rs = null;
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "SELECT B1.MATRNR FROM Veranstaltung V1 "
+ " INNER JOIN Belegung B1 ON B1.VERNR = V1.VERNR"
+ " INNER JOIN Belegung B2 ON B2.MATRNR = B1.MATRNR"
+ " INNER JOIN Veranstaltung V2 ON B2.VERNR = V2.VERNR"
+ " WHERE V1.FACHNR = V2.FACHNR AND V1.VERNR != V2.VERNR"
+ " AND B1.Status = 'AN' AND B2.Status = 'ZU'"
+ " AND B1.Rangstufe IS NULL AND V1.VERNR=?";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
rs = ps.executeQuery();
while(rs.next()) {
long matrNr = rs.getLong("MATRNR");
zuweisungRangstufe(verNr, matrNr, 2);
}
}
catch (SQLException e)
{
throw new RuntimeException();
}
catch (NullPointerException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
}
private void zuweisungRangstufe3(long verNr) {
Connection connection = null;
PreparedStatement ps = null;
String query = null;
ResultSet rs = null;
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "SELECT S.MATRNR FROM Veranstaltung V"
+ " INNER JOIN Fach F ON F.FACHNR = V.FACHNR"
+ " INNER JOIN Belegung B ON B.VERNR = V.VERNR"
+ " INNER JOIN Student S ON B.MATRNR = S.MATRNR"
+ " INNER JOIN Studium FS ON FS.MATRNR = S.MATRNR"
+ " WHERE FS.SGKN = F.SGKN AND B.Status = 'AN'"
+ " AND B.Rangstufe IS NULL AND B.VERNR=?";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
rs = ps.executeQuery();
while(rs.next()) {
long matrNr = rs.getLong("MATRNR");
zuweisungRangstufe(verNr, matrNr, 3);
}
}
catch (SQLException e)
{
throw new RuntimeException();
}
catch (NullPointerException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
}
private void zuweisungRangstufe4(long verNr)
{
Connection connection = null;
PreparedStatement ps = null;
String query = null;
ResultSet rs = null;
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "SELECT S.MATRNR FROM Veranstaltung V"
+ " INNER JOIN Fach F ON F.FACHNR = V.FACHNR"
+ " INNER JOIN Belegung B ON B.VERNR = V.VERNR"
+ " INNER JOIN Student S ON B.MATRNR = S.MATRNR"
+ " WHERE B.Status = 'AN'"
+ " AND B.Rangstufe IS NULL AND B.VERNR=?";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
rs = ps.executeQuery();
while (rs.next())
{
long matrNr = rs.getLong("MATRNR");
zuweisungRangstufe(verNr, matrNr, 4);
}
}
catch (SQLException e)
{
throw new RuntimeException();
}
catch (NullPointerException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
}
private int zulassung(long verNr) {
Connection connection = null;
PreparedStatement ps = null;
String query = null;
ResultSet rs = null;
int maxTeilnehmer = ermittleMaxTeilnehmer(verNr);
int zugelasseneTeilnehmer = ermittleZugeleasseneTeilnehmer(verNr);
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "SELECT MATRNR FROM Belegung"
+ " WHERE Status ='AN' AND VERNR=?"
+ " ORDER BY Rangstufe ASC, dbms_random.random ASC";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
rs = ps.executeQuery();
while (rs.next() && maxTeilnehmer > zugelasseneTeilnehmer)
{
long matrNr = rs.getLong("MATRNR");
query = "UPDATE Belegung SET Status = 'ZU' WHERE vernr = ? and matrnr = ?";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
ps.setLong(2, matrNr);
ps.executeQuery();
zugelasseneTeilnehmer += 1;
}
ablehnungOffenerBelegungen(verNr);
}
catch (SQLException e)
{
throw new RuntimeException();
}
catch (NullPointerException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
int freiePlaetze = maxTeilnehmer - zugelasseneTeilnehmer;
if (freiePlaetze < 0) {
throw new OverbookException();
}
return freiePlaetze;
}
private void ablehnungOffenerBelegungen(long verNr) {
Connection connection = null;
PreparedStatement ps = null;
String query = null;
try
{
connection = JdbcUtils.getConnectionViaDataSource(dataSource);
query = "UPDATE Belegung SET Status = 'AB' WHERE Status = 'AN' AND VerNr = ?";
ps = connection.prepareStatement(query);
ps.setLong(1, verNr);
ps.executeQuery();
}
catch (SQLException e)
{
throw new RuntimeException();
}
catch (NullPointerException e)
{
throw new RuntimeException();
}
finally
{
JdbcUtils.closeStatementQuietly(ps);
JdbcUtils.closeConnectionQuietly(connection);
}
}
}
Zuletzt bearbeitet: