Browse Source

Lots of stuffs <3

Martin Passard 7 years ago
parent
commit
7249a1ba46
6 changed files with 205 additions and 9 deletions
  1. 27 2
      src/data/ISP.java
  2. 64 4
      src/data/ISPDAO.java
  3. 13 2
      src/main/Bot.java
  4. 62 0
      src/main/Cache.java
  5. 7 1
      src/main/Main.java
  6. 32 0
      src/verif_saisie/EntierPositifNonVide.java

+ 27 - 2
src/data/ISP.java

@@ -1,6 +1,7 @@
 package data;
 
-import java.util.Date;
+import java.util.LinkedList;
+import java.util.List;
 
 public class ISP {
 	
@@ -85,9 +86,33 @@ public class ISP {
 	public String toString() {
 		String res="";
 		res+=name+" : ";
-		res+="Est membre: "+isFFDNMember()+" ";
+		res+="Est membre: "+booleanToOuiNon(isFFDNMember())+" ";
 		res+="Nombre de membres: "+getMembersCount()+" Nombre d'abonnements:"+getSubscribersCount(); 
 		return res;
 	}
+	
+	private String booleanToOuiNon(boolean b) {
+		if(b) {
+			return "oui";
+		}
+		return "non";
+		
+	}
+	
+	/**
+	 * Renvoie une Liste de chaine de caractères pour permettre l'affichage sur IRC ligne par ligne, bien que le \n ne soit pas interprété.
+	 * @return Une Liste de chaine correspondant à toutes les lignes que le bot doit écrire
+	 */
+	
+	public List<String> toStringIRC () {
+		List<String> res = new LinkedList<>();
+		res.add(name+" : ");
+		res.add("Est membre de la fédération : "+booleanToOuiNon(isFFDNMember()));
+		res.add("Nombre de Membres : "+getMembersCount());
+		res.add("Nombre d'abonnements : "+getSubscribersCount());
+		
+		
+		return res;
+	}
 
 }

+ 64 - 4
src/data/ISPDAO.java

@@ -16,6 +16,7 @@ import java.util.Scanner;
 
 import javax.net.ssl.HttpsURLConnection;
 
+import org.json.JSONException;
 import org.json.JSONObject;
 
 public class ISPDAO {
@@ -115,9 +116,9 @@ public class ISPDAO {
 		String json = executeGet(dbAdress+number+'/');
 		System.out.println(json);
 		JSONObject jsonObj = new JSONObject(json);
-		String name = jsonObj.getJSONObject("ispformat").getString("name");
-		int id = jsonObj.getInt("id");
-		boolean member = jsonObj.getBoolean("is_ffdn_member");
+		String name = getName(jsonObj);
+		int id = getId(jsonObj);
+		boolean member = getIsMember(jsonObj);
 		ISPdata ispData = new ISPdata(jsonObj.getJSONObject("ispformat"));
 		String date_added = jsonObj.getString("date_added");
 		String last_update = jsonObj.getString("last_update");
@@ -125,7 +126,66 @@ public class ISPDAO {
 		return isp;
 
 	}
-
+	/**
+	 * Recupere le nom de l'ISP dans le JSON
+	 * @param json Objet JSON généré a partir de la requete principale
+	 * @return Le nom si displonible ou null
+	 */
+	private String getName(JSONObject json) {
+		try {
+		return json.getJSONObject("ispformat").getString("name");
+		}catch(JSONException jo) {
+			System.err.println("Erreur au moment de récuperer le champ nom dans le JSON : ");
+			jo.printStackTrace();
+			return null;
+		}
+	}
+	
+	private int getId(JSONObject json) {
+		try {
+			return json.getInt("id");
+		}catch(JSONException jo) {
+			System.err.println("Erreur au moment de récuperer le champ id dans le JSON : ");
+			jo.printStackTrace();
+			return -1;
+		}
+	}
+	
+	private Boolean getIsMember(JSONObject json) {
+		try {
+			return json.getBoolean("is_ffdn_member");
+		}catch(JSONException jo) {
+			System.err.println("Erreur au moment de récuperer le champ is_ffdn_member dans le JSON : ");
+			jo.printStackTrace();
+			return null;
+		}
+	}
+	
+	private String getDateAdded(JSONObject json) {
+		try {
+			return json.getString("date_added");
+		}catch(JSONException jo) {
+			System.err.println("Erreur au moment de récuperer le champ date_added dans le JSON : ");
+			jo.printStackTrace();
+			return null;
+		}
+	}
+	/**
+	 * 
+	 * @param json Objet json a l'origine
+	 * @return chaine representant la date sous forme string:ISO8601 ou nu
+	 */
+	private String getDateUpdated(JSONObject json) {
+		try {
+			return json.getString("last_update");
+		}catch(JSONException jo) {
+			//System.err.println("Erreur au moment de récuperer le champ date_added dans le JSON : ");
+			//jo.printStackTrace();
+			return null;
+		}
+	}
+	
+	
 	/**
 	 *TODO : implements this in Isp
 	 * @param date String correspondant a une date au format aaaa-mm-jj ou aaaa-mm

+ 13 - 2
src/main/Bot.java

@@ -1,5 +1,7 @@
 package main;
 
+import java.util.List;
+
 import org.jibble.pircbot.PircBot;
 
 import data.ISPDAO;
@@ -22,9 +24,18 @@ public class Bot extends PircBot {
 		
 		if (message.contains("+info")) {
 			
-			String res="";
 			int  id = Integer.parseInt(message.substring(message.indexOf(' ')+1));
-			sendMessage(channel,idao.getISP(id).toString());
+			List<String> s = idao.getISP(id).toStringIRC();
+			for(String response : s) {
+				sendMessage(channel,response);
+			}
+			
 		}
+		
+		//easter Egg
+		if (message.contains("Ehlo UneFede")) {
+			sendMessage(channel, "Ehlo "+sender+"!!");
+		}
+		
 	}
 }

+ 62 - 0
src/main/Cache.java

@@ -0,0 +1,62 @@
+package main;
+
+import java.util.List;
+
+import data.ISP;
+import data.ISPDAO;
+
+public class Cache {
+	/**
+	 * Cette classe sert de cache pour les infos de DB
+	 * Elle implemente le Design Pattern Singleton dans la mesure où cette classe doit être l'unique instance de référence peu importe le Thread l'utilisant, et que le cache est unique.
+	 */
+	
+	public static volatile Cache instance = null;
+	private List<ISP> cache;
+	
+	private Cache() {
+		ISPDAO idao = ISPDAO.getInstance();
+		try {
+			cache = idao.getISPs();
+		} catch (Exception e) {
+			System.err.println("Le cache n'as pas pu être géré à cause de l'exception suivante");
+			e.printStackTrace();
+		}
+	}
+	
+	
+	/**
+	 * Méthode du design patern singleton. Permet de récuperer et si besoin d'initialiser, l'unique instance de la classe.
+	 * @return Instance du Cache
+	 */
+	public final static Cache getInstance() {
+		if (Cache.instance == null) {
+			synchronized (ISPDAO.class) {
+				if(Cache.instance == null) {
+					Cache.instance = new Cache();
+				}
+			}
+		}
+		return Cache.instance;
+	}
+	
+	/**
+	 * Cette methode vient mettre à jour le cache des FAI. Pour cela, il récupere l'instance du DAO et récupère la liste des ISP. Si tout cse passe correctement, il supprime le cache précédent et le remplace par les nouvelles valeurs. Sinon, il maaintient le cache précédent.
+	 * @return True si l'operation s'est passée correctement, False sinon et affiche la cause
+	 */
+	public boolean reload() {
+		try {
+			ISPDAO idao = ISPDAO.getInstance();
+			List<ISP> l = idao.getISPs();
+			if(l != null) {
+				cache.clear();
+				cache.addAll(l);
+			}
+		}catch (Exception e) {
+			System.err.println("Erreur au moment du reload() :\n "+e.getMessage());
+			return false;
+		}
+		return true;
+	}
+
+}

+ 7 - 1
src/main/Main.java

@@ -7,6 +7,7 @@ public class Main {
 	public static final String SERVER = "irc.geeknode.net";
 	public static final int PORT = 6667;
 	public static final String[] CHANNELS = { "#marmat", "#ffdn" };
+	public static final Cache cache = Cache.getInstance();
 	
 	public static void main(String[] args) throws Exception {
 		try {
@@ -17,8 +18,13 @@ public class Main {
         bot.setVerbose(true);
         
         // Connect to the IRC server.
-        
         bot.connect(SERVER,PORT);
+        
+        // Get All the infomations and store in a cache
+        
+        
+        
+        
         // Join the #pircbot channel.
         for(int i = 0; i< CHANNELS.length; i++) {
         bot.joinChannel(CHANNELS[i]);

+ 32 - 0
src/verif_saisie/EntierPositifNonVide.java

@@ -0,0 +1,32 @@
+package verif_saisie;
+
+public class EntierPositifNonVide {
+
+	public static boolean verifie(String s){
+		if( s == null || s.equals("") ){
+			return false;
+		}else{
+			for(int i=0; i<s.length(); i++){
+				if(s.charAt(i)>'9' || s.charAt(i)<'0'){
+					return false;
+				}
+			}
+		}
+		return true;
+	}
+	/**
+	 * 
+	 * @param s Chaine à verifier
+	 * @param min minimum inclu du nombre testé
+	 * @param max maximum inclu du numbre testé
+	 * @return Vrai si inclu, faux sinon ou si non entier
+	 */
+	public static boolean entre(String s, int min, int max){
+		if(verifie(s)){
+			if(Integer.valueOf(s)<=max && Integer.valueOf(s)>=min){
+				return true;
+			}
+		}
+		return false;
+	}
+}