Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. package de.unikn.bioml.mpi2.stuvove.implementation.db;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.text.ParseException;
  10. import java.util.ArrayList;
  11. import java.util.HashSet;
  12. import java.util.List;
  13. import java.util.Set;
  14.  
  15. import de.unikn.bioml.mpi2.stuvove.definitions.core.IDeserializer;
  16. import de.unikn.bioml.mpi2.stuvove.definitions.core.ILecture;
  17. import de.unikn.bioml.mpi2.stuvove.definitions.core.ILecturer;
  18. import de.unikn.bioml.mpi2.stuvove.definitions.core.IManager;
  19. import de.unikn.bioml.mpi2.stuvove.definitions.core.IRoom;
  20. import de.unikn.bioml.mpi2.stuvove.definitions.core.IStudyCourse;
  21. import de.unikn.bioml.mpi2.stuvove.definitions.core.ITimeSlot;
  22. import de.unikn.bioml.mpi2.stuvove.implementation.core.Exercise;
  23. import de.unikn.bioml.mpi2.stuvove.implementation.core.Lecture;
  24. import de.unikn.bioml.mpi2.stuvove.implementation.core.Lecturer;
  25. import de.unikn.bioml.mpi2.stuvove.implementation.core.Room;
  26. import de.unikn.bioml.mpi2.stuvove.implementation.core.Seminar;
  27. import de.unikn.bioml.mpi2.stuvove.implementation.core.TimeSlot;
  28.  
  29. public class DBStudyCourseDeserializer implements IDeserializer<IStudyCourse> {
  30.  
  31.     String     driver     = null;
  32.    
  33.     private List<IStudyCourse> l_studyCourses = new ArrayList<IStudyCourse>();
  34.     private IManager manage;
  35.      
  36.       // --------------------------------------------------------------------------
  37.      
  38.       String     host       = null;
  39.      
  40.       String     port       = null;
  41.      
  42.       String     database   = null;
  43.      
  44.       String     user       = null;
  45.      
  46.       String     password   = null;
  47.      
  48.       // --------------------------------------------------------------------------
  49.      
  50.       Connection connection = null;
  51.      
  52.      
  53.      
  54.      
  55.       public DBStudyCourseDeserializer(String jdbcDriverClass, String dbUrl,
  56.                 String user, String pw, IManager manager)
  57.             throws SQLException, ClassNotFoundException {
  58.          
  59.           manage=manager;
  60.           driver = jdbcDriverClass;
  61.           host = dbUrl;
  62.           this.user = user;
  63.           password=pw;
  64.       }
  65.      
  66.       private void closeConnection ()
  67.       {
  68.         try
  69.         {
  70.           connection.close ();
  71.         }
  72.         catch (SQLException e)
  73.         {
  74.           e.printStackTrace ();
  75.           System.exit (1);
  76.         }
  77.      
  78.         System.out.println ("\nconnection closed");
  79.       }
  80.  
  81.       private void loadJdbcDriver ()
  82.       {
  83.         try
  84.         {
  85.           Class.forName (driver);
  86.         }
  87.         catch (ClassNotFoundException e)
  88.         {
  89.           e.printStackTrace ();
  90.           System.exit (1);
  91.         }
  92.      
  93.         System.out.println ("driver loaded");
  94.       }
  95.      
  96.       private void openConnection ()
  97.       {
  98.         try
  99.         {
  100.           connection = DriverManager.getConnection (host,
  101.                                                     user,
  102.                                                     password);
  103.         }
  104.         catch (SQLException e)
  105.         {
  106.           e.printStackTrace ();
  107.           System.exit (1);
  108.         }
  109.      
  110.         System.out.println ("connection opened");
  111.       }
  112.      
  113.     @Override
  114.     public List<IStudyCourse> deserialize() throws IOException, ParseException {
  115.        
  116.         loadJdbcDriver ();
  117.         openConnection ();
  118.        
  119.         int id,lec,room,maxstuds,lecture,sc_id,ts_id;
  120.         String type;
  121.         String title;
  122.         String description;
  123.        
  124.         try
  125.         {
  126.           Statement statement1 = connection.createStatement ();
  127.           Statement statement2 = connection.createStatement ();
  128.  
  129.           String str = "SELECT * from studycourses";
  130.           ResultSet resultSet = statement1.executeQuery (str);
  131.  
  132.           while (resultSet.next ())
  133.           {
  134.               String str1 = "SELECT * from study_times";
  135.               ResultSet resultSetagain = statement2.executeQuery (str1);
  136.              
  137.               Set<ITimeSlot> slots = new HashSet<ITimeSlot>();
  138.               id=resultSet.getInt(1);
  139.               type= resultSet.getString(2).replaceAll(" ", "");
  140.               title=resultSet.getString(3).replaceAll(" ", "");
  141.               lec=resultSet.getInt(4);
  142.               room=resultSet.getInt(5);
  143.               maxstuds=resultSet.getInt(6);
  144.               description=resultSet.getString(7).replaceAll(" ", "");
  145.              
  146.               ILecturer leccy = null;
  147.               IRoom roomy = null;
  148.               ILecture lsso = null;
  149.              
  150.               while(resultSetagain.next()){
  151.                   sc_id=resultSetagain.getInt(1);
  152.                   ts_id=resultSetagain.getInt(2);
  153.                   if(id==sc_id){
  154.                       List<ITimeSlot> timeSlots = manage.searchTimeSlots(new TimeSlot(ts_id, null, -1,-1));
  155.                       slots.add(timeSlots.get(0));
  156.                       timeSlots.clear();
  157.                   }
  158.               }
  159.               List<ILecturer> lecturer=manage.searchLecturers(new Lecturer(lec,null,null));
  160.               if(lecturer.size()>0){
  161.               leccy=lecturer.get(0);
  162.               lecturer.clear();
  163.               }
  164.               List<IRoom> rooms=manage.searchRooms(new Room(room,null,-1));
  165.               if(rooms.size()>0){
  166.               roomy=rooms.get(0);
  167.               rooms.clear();
  168.               }
  169.               if("ILecture".equals(type)){
  170.               l_studyCourses.add(new Lecture(id,title,leccy,slots,roomy,maxstuds,description));
  171.               }
  172.               if("ISeminar".equals(type)){
  173.               l_studyCourses.add(new Seminar(id,title,leccy,slots,roomy,maxstuds,description));
  174.               }
  175.               if("IExercise".equals(type)){
  176.                   lecture=resultSet.getInt(8);
  177.                   for(IStudyCourse isc : l_studyCourses){
  178.                       if(lecture==isc.getID()){
  179.                           lsso = (Lecture)isc;
  180.                           l_studyCourses.add(new Exercise(id,title,leccy,slots,roomy,maxstuds,description,lsso));
  181.                           break;
  182.                       }
  183.                   }
  184.               }
  185.               resultSetagain.close ();
  186.           }
  187.      
  188.           resultSet.close ();
  189.          
  190.           statement1.close ();
  191.           statement2.close ();
  192.         }
  193.         catch (SQLException e)
  194.         {
  195.              e.printStackTrace ();
  196.         }
  197.         closeConnection();
  198.         return l_studyCourses;
  199.     }
  200.  
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement