1   package ch.ige.edossier.web.server.dao;
2   
3   import java.sql.*;
4   import java.util.ArrayList;
5   import java.util.List;
6   import ch.ige.edossier.util.DBHelper;
7   import ch.ige.edossier.web.vo.PriorityVO;
8   
9   /**
10   * B32.03 - eDossier-Interceptions - Diplomarbeit an der Software-Schule Schweiz<br>
11   * Data Access Objekt für die Tabelle PRIORITY
12   * <p>
13   * Copyright (c) 2004, Eidgenössisches Institut für Geistiges Eigentum
14   * @author    Anita Rueegsegger, Marc Bouquet
15   * @version   $Id: PriorityDAO.java,v 1.10 2004/11/01 13:52:26 ruegsegger Exp $
16   */
17  public class PriorityDAO
18  {
19    /**
20     * Selektierte alle Einträge mit der übergebenen Dossier-Id aus der Tablle PRIORITY
21     * @param dossierId int Nummer zum selektieren der Datensätze
22     * @return List mit Value Objects AccountVO
23     * @throws SQLException Unerwarteter SQL-Fehler
24     */
25    public List select( int dossierId ) throws SQLException
26    {
27      Connection con = null;
28      PreparedStatement ps = null;
29      ResultSet rs = null;
30      List list = new ArrayList();
31      PriorityVO vo = null;
32  
33      try
34      {
35        con = DBHelper.getInstance().getEsolutionConnection();
36        ps = con.prepareStatement( getSelectStatement() + "where dossier_id = ? ORDER BY country_cd" );
37        ps.setInt( 1, dossierId );
38        rs = ps.executeQuery();
39  
40        while( rs.next() )
41        {
42          vo = this.fillVO( rs );
43          list.add( vo );
44        }
45      }
46      finally
47      {
48        DBHelper.getInstance().close( con, ps );
49      }
50      return list;
51    }
52  
53    /**
54     * Fügt einen Datensatz in die Tabelle PRIORITY ein
55     * @param priorityVO PriorityVO Value-Object
56     * @exception Exception Unerwarteter SQL-Fehler oder Konvertierungsfehler
57     */
58    public void insert( PriorityVO priorityVO ) throws Exception
59    {
60      Connection con = null;
61      PreparedStatement ps = null;
62  
63      try
64      {
65        con = DBHelper.getInstance().getEsolutionConnection();
66  
67        ps = con.prepareStatement(
68            "INSERT INTO edossier.priority(" +
69            "priority_id, " +
70            "dossier_id, " +
71            "country_cd, " +
72            "prio_dat ) " +
73            "VALUES(EDOSSIER.SEQ_PRIORITY_ID.nextval,?,?,?)"
74            );
75  
76        ps.setInt( 1, priorityVO.getDossierId() );
77        ps.setString( 2, priorityVO.getCountryCd() );
78        ps.setDate( 3, priorityVO.getPrioDat() );
79  
80        if( ps.executeUpdate() != 1 )
81        {
82          throw new SQLException();
83        }
84  
85      }
86      finally
87      {
88        DBHelper.getInstance().close( con, ps );
89      }
90    }
91  
92    /**
93     * Aktualisiert einen Datensatz in die Tabelle PRIORITY ein.
94     * @param priorityVO PriorityVO Value-Object
95     * @exception Exception Unerwarteter SQL-Fehler oder Konvertierungsfehler
96     */
97    public void update( PriorityVO priorityVO ) throws Exception
98    {
99      Connection con = null;
100     PreparedStatement ps = null;
101 
102     try
103     {
104       con = DBHelper.getInstance().getEsolutionConnection();
105       ps = con.prepareStatement( "UPDATE edossier.priority " +
106                                  "SET country_cd = ?, prio_dat = ?, changed_dat = sysdate " +
107                                  "WHERE priority_id = ? " +
108                                  "AND dossier_id = ?" );
109 
110       ps.setString( 1, priorityVO.getCountryCd() );
111       ps.setDate( 2, priorityVO.getPrioDat() );
112       ps.setInt( 3, priorityVO.getPriorityId() );
113       ps.setInt( 4, priorityVO.getDossierId() );
114 
115       if( ps.executeUpdate() != 1 )
116       {
117         throw new SQLException();
118       }
119     }
120     finally
121     {
122       DBHelper.getInstance().close( con, ps );
123     }
124   }
125 
126   /**
127    * Löscht einen Datensatz aus der Tabelle PRIORITY
128    * @param priorityId int PK FehlerId
129    * @return int Anzahl gelöschter Datensätze
130    * @exception SQLException Unerwarteter SQL-Fehler
131    */
132   public int delete( int priorityId ) throws SQLException
133   {
134     Connection con = null;
135     PreparedStatement ps = null;
136 
137     try
138     {
139       con = DBHelper.getInstance().getEsolutionConnection();
140 
141       ps = con.prepareStatement(
142           "DELETE FROM edossier.priority where priority_id = ? "
143           );
144       ps.setInt( 1, priorityId );
145 
146       return ps.executeUpdate();
147     }
148     finally
149     {
150       DBHelper.getInstance().close( con, ps );
151     }
152   }
153 
154   /**
155    * Gibt das SQL-Statement für den select aller Attribute ohne where-Bedingngung zurück
156    * @return String Select-Statement
157    */
158   private String getSelectStatement()
159   {
160     return
161         ( "select priority_id, dossier_id, country_cd, prio_dat, created_dat, changed_dat " +
162           "from edossier.priority " );
163   }
164 
165   /**
166    * Füllt einen Datensatz anhand des ResultSet<br>
167    * @param rs ResultSet Gültiges resultSet
168    * @return PriorityVO Value-Object
169    * @throws SQLException Unerwarteter SQL-Fehler
170    */
171   private PriorityVO fillVO( ResultSet rs ) throws SQLException
172   {
173     PriorityVO priorityVO = new PriorityVO();
174 
175     priorityVO.setPriorityId( rs.getInt( "priority_id" ) );
176     priorityVO.setDossierId( rs.getInt( "dossier_id" ) );
177     priorityVO.setCountryCd( rs.getString( "country_cd" ) );
178     priorityVO.setPrioDat( rs.getDate( "prio_dat" ) );
179     priorityVO.setCreatedDat( rs.getDate( "created_dat" ) );
180     priorityVO.setChangedDat( rs.getDate( "changed_dat" ) );
181 
182     return priorityVO;
183   }
184 }
185