Browse Source

Ajout DAO

Martin Passard 7 years ago
parent
commit
7329685076
5 changed files with 157 additions and 2 deletions
  1. BIN
      json-20171018.jar
  2. 27 0
      src/data/ISP.java
  3. 100 0
      src/data/ISPDAO.java
  4. 15 2
      src/data/ISPdata.java
  5. 15 0
      src/data/TestDAO.java

BIN
json-20171018.jar


+ 27 - 0
src/data/ISP.java

@@ -3,12 +3,32 @@ package data;
 import java.util.Date;
 
 public class ISP {
+	
+	private String name;
 	private int id;
 	private boolean isFFDNMember;
 	private Date date_added;
 	private Date last_update;
 	private ISPdata data;
 	
+	
+	public ISP(String name, int id, boolean isFFDNMember, Date date_added, Date last_update, ISPdata data) {
+		super();
+		this.name = name;
+		this.id = id;
+		this.isFFDNMember = isFFDNMember;
+		this.date_added = date_added;
+		this.last_update = last_update;
+		this.data = data;
+	}
+	
+	
+	public String getName() {
+		return name;
+	}
+	public ISPdata getData() {
+		return data;
+	}
 	public int getId() {
 		return id;
 	}
@@ -42,5 +62,12 @@ public class ISP {
 		return data.getSubscribersCount();
 	}
 	
+	public String toString() {
+		String res="";
+		res+=name+" : \n";
+		res+="Est membre: "+isFFDNMember()+" \n";
+		res+="Nombre de membres: "+getMembersCount()+" Nombre d'abonnements:"+getSubscribersCount(); 
+		return res;
+	}
 
 }

+ 100 - 0
src/data/ISPDAO.java

@@ -0,0 +1,100 @@
+package data;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.json.JSONObject;
+
+public class ISPDAO {
+
+	/**
+	 * This class implements the Sigleton Design Patern and is the DataAcess Object. It's role is to get informations from <a href="db.ffdn.org"> db.ffdn.org </a> and transform the into data Objects.
+	 */
+
+	public static volatile ISPDAO instance = null;
+	private final String dbAdress = "https://db.ffdn.org/api/v1/isp/";
+
+
+	private ISPDAO() {
+
+	}
+	
+	public final static ISPDAO getInstance() {
+		if (ISPDAO.instance == null) {
+			synchronized (ISPDAO.class) {
+				if(ISPDAO.instance == null) {
+					ISPDAO.instance = new ISPDAO();
+				}
+			}
+		}
+		return ISPDAO.instance;
+	}
+
+	/**
+	 * @param urlToRead URL the method will transform into a String
+	 * @return	String that the server Respond with the URL specified or null in case of an error
+	 * 
+	 */
+	public static String getHTML(String urlToRead) {
+		try {
+			StringBuilder result = new StringBuilder();
+			URL url = new URL(urlToRead);
+			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+			conn.setRequestMethod("GET");
+			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+			String line;
+			while ((line = rd.readLine()) != null) {
+				result.append(line);
+			}
+			rd.close();
+			
+			return result.toString();
+		}catch (Exception e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+
+
+
+	
+
+
+	/**
+	 * 
+	 * @return List of ISPs constructed.
+	 */
+	public List<ISP> getISPs() throws Exception{
+		String jsonInfo = getHTML(dbAdress+"?per_page=0");
+		JSONObject allISPCount = new JSONObject(jsonInfo);
+		int nbItems = allISPCount.getInt("total_items");
+		ArrayList<ISP> ar = new ArrayList<>(nbItems);
+
+		for (int i=0; i<nbItems; ++i) {
+			ISP isp = getISP(i);
+			if(isp != null) {
+				ar.add(isp);
+			}
+		}
+		return ar;
+
+	}
+
+
+	public ISP getISP(int number) {
+		String json = getHTML(dbAdress+number);
+		JSONObject jsonObj = new JSONObject(json);
+		
+		
+
+		return null;
+
+	}
+
+
+
+}

+ 15 - 2
src/data/ISPdata.java

@@ -10,10 +10,23 @@ public class ISPdata {
 	private int membersCount;
 	private int subscribersCount;
 	
-	public ISPdata(ISP i) {
-		this.ISP=i;
+	
+	
+	
+
+	public ISPdata(data.ISP iSP, String website, String description, String[] chatrooms, int progressStatus,
+			int membersCount, int subscribersCount) {
+		super();
+		ISP = iSP;
+		this.website = website;
+		this.description = description;
+		this.chatrooms = chatrooms;
+		this.progressStatus = progressStatus;
+		this.membersCount = membersCount;
+		this.subscribersCount = subscribersCount;
 	}
 
+
 	public String getWebsite() {
 		return website;
 	}

+ 15 - 0
src/data/TestDAO.java

@@ -0,0 +1,15 @@
+package data;
+
+public class TestDAO {
+
+	public TestDAO() {
+		// TODO Auto-generated constructor stub
+	}
+
+	public static void main(String[] args) {
+		ISPDAO dao = ISPDAO.getInstance();
+		
+
+	}
+
+}