package dataBase;

import java.sql.*;
import java.util.*;

/**
 * Created by hcyue on 2016/12/3.
 */
public class CollectionModel {
    DBConnection connection;
    public CollectionModel(DBConnection conn) {
        connection = conn;
    }
    
    /**
     * getColletionsByJK
     * ûJKŻȡcollectionInfo list
     * @param jk
     * @return List<CollectionInfo>
     * @throws SQLException
     */
    public List<CollectionInfo> getColletionsByJK(int jk) throws SQLException {
        ResultSet rs = connection.query("SELECT * FROM collection where user_id=" + jk);
        ArrayList<CollectionInfo> res = new ArrayList<>();
        while (rs.next()) {
            res.add(new CollectionInfo(rs));
        }
        rs.close();
        return res;
    }
    
    /**
     * addUserToCollection
     * @param jk
     * @param coll_id
     * @return
     * @throws SQLException
     */
    public int addUserToCollection(int jk, int coll_id) throws SQLException {
        return connection.update(String.format("INSERT INTO collection_entry (user_id, collection_id) VALUES (%d, %d)", jk, coll_id));
    }
    
    /**
     * createCollection
     * @param jk
     * @param collName
     * @return
     * @throws SQLException
     */
    public CollectionInfo createCollection(int jk, String collName) throws SQLException {
        String sql = String.format("INSERT INTO collection (name, user_id) VALUES ('%s', %d)", collName, jk);
        int id = connection.insertAndGet(sql);
        return getCollection(id);
    }
    
    /**
     * getCollection
     * @param id
     * @return
     * @throws SQLException
     */
    public CollectionInfo getCollection(int id) throws SQLException {
        String sql = String.format("SELECT * FROM collection where collection_id=%d", id);
        ResultSet rs = connection.query(sql);
        return new CollectionInfo(rs);
    }
    
    /**
     * removeCollection
     * @param id
     * @return
     * @throws SQLException
     */
    public int removeCollection(int id) throws SQLException {
        String sql = String.format("DELETE FROM collection WHERE collection_id=%d", id);
        return connection.update(sql);
    }
    
    /**
     * getCollectionsByUser
     * @param user
     * @return
     * @throws Exception
     */
    public List<CollectionInfo> getCollectionsByUser(UserInfo user) throws Exception {
        int jk = user.getJKNum();
        return getColletionsByJK(jk);
    }
    
    /**
     * getCollectionsByUser
     * @param jk
     * @return
     * @throws SQLException
     */
    public List<CollectionInfo> getCollectionsByUser(int jk) throws SQLException {
        return getColletionsByJK(jk);
    }
    /*
    public static void main(String args[]) throws SQLException {
        DBConnection db = DBConnection.getInstance();
        //UserModel userModel = new UserModel(db);
        CollectionModel collectionModel = new CollectionModel(db);
        //UserInfo user = userModel.getUserByJK(0);
        List<CollectionInfo>  coll = collectionModel.getColletionsByJK(0);
		List<UserInfo> testlist;
        for(int j = 0; j<coll.size();j++){
        	try {
        		System.out.println(coll.get(j).toString());
				testlist = coll.get(j).getMembers();
				for(int i = 0; i < testlist.size();i++){
		        	System.out.println(testlist.get(i).getNickName());
		        }
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
    }*/
}

@@ -125,24 +125,32 @@
 Model %7B%0A    
+private 
 DBConnection
@@ -321,32 +321,35 @@
      * @param jk
+ JK
 %0A     * @return 
@@ -388,32 +388,37 @@
 ows SQLException
+ SQL%EC%B3%A3
 %0A     */%0A    pub
@@ -815,32 +815,36 @@
      * @param jk
+ %C3%BBJK
 %0A     * @param c
@@ -849,16 +849,20 @@
  coll_id
+ %D0%B1id
 %0A     * 
@@ -860,32 +860,35 @@
 d%0A     * @return
+ %D3%B5%C4%BF
 %0A     * @throws 
@@ -891,32 +891,37 @@
 ows SQLException
+ SQL%EC%B3%A3
 %0A     */%0A    pub
@@ -1177,32 +1177,36 @@
      * @param jk
+ %C3%BBJK
 %0A     * @param c
@@ -1212,16 +1212,18 @@
 collName
+ %D0%B1
 %0A     * 
@@ -1221,32 +1221,35 @@
 %D0%B1%0A     * @return
+ %C2%BD%D0%B1
 %0A     * @throws 
@@ -1252,32 +1252,37 @@
 ows SQLException
+ SQL%EC%B3%A3
 %0A     */%0A    pub
@@ -1587,32 +1587,44 @@
 * getCollection%0A
+     * %C8%A1%D6%B8%C4%BA%D0%B1%0A
      * @param id
@@ -1615,32 +1615,36 @@
      * @param id
+ %D0%B1ID
 %0A     * @return%0A
@@ -1634,32 +1634,40 @@
 D%0A     * @return
+ %D2%B5%D0%B1%CE%AAnull
 %0A     * @throws 
@@ -1670,32 +1670,37 @@
 ows SQLException
+ SQL%EC%B3%A3
 %0A     */%0A    pub
@@ -1911,49 +1911,457 @@
     
-return new CollectionInfo(rs);%0A    %7D
+CollectionInfo result = new CollectionInfo(rs);%0A        rs.close();%0A        return result;%0A    %7D%0A    public CollectionInfo getCollectionByNameAndOwner(String name, int jk) throws SQLException %7B%0A        String sql = String.format(%22SELECT * FROM collection where user_id=%25d AND name=%60%25s%60%22, jk, name);%0A        ResultSet rs = connection.query(sql);%0A        CollectionInfo result = new CollectionInfo(rs);%0A        rs.close();%0A        return result;
 %0A    
+%7D
 %0A   

Add method to select by name