Zaibon

Untitled

Jun 13th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5.  
  6.  
  7. namespace activite
  8. {
  9.  
  10.     [Serializable()]
  11.     class Portfolio
  12.     {
  13.         [NonSerialized] private User linkedUser;
  14.         [NonSerialized] private List<Agenda> _listAgenda = null;
  15.         private List<String> _listStringAgenda = null;
  16.  
  17.         public Portfolio()
  18.         {
  19.             listAgenda = new List<Agenda>();
  20.             listStringAgenda = new List<string>();
  21.             linkedUser = new User();
  22.         }
  23.  
  24.         public Portfolio(User user) :this()
  25.         {
  26.             try
  27.             {
  28.                 load();
  29.             }
  30.             catch (Exception e)
  31.             {
  32.                 //le portfilio n'existe pas encore
  33.                 //on créer et on sauve
  34.                 linkedUser = user;
  35.                 save();
  36.             }
  37.            
  38.         }
  39.  
  40.         public void save()
  41.         {
  42.             save(getFilePath());
  43.         }
  44.  
  45.  
  46.         private void save(string filePath)
  47.         {
  48.             //on vérifie qu'on a bien tout les nom des agenda dans la liste*/
  49.             foreach(Agenda agenda in listAgenda)
  50.             {
  51.                 if(!listStringAgenda.Contains(agenda.nom))
  52.                 {
  53.                     listStringAgenda.Add(agenda.nom);
  54.                 }
  55.             }
  56.             listStringAgenda.Sort();
  57.  
  58.              //on sérialise
  59.             Stream streamPort = File.Open(filePath, FileMode.OpenOrCreate);
  60.             BinaryFormatter formatter = new BinaryFormatter();
  61.             //le portfolio
  62.             formatter.Serialize(streamPort, this);
  63.  
  64.             foreach(Agenda agenda in listAgenda)
  65.             {//et tout les agenda qu'il contient
  66.                 Stream streamAgenda = File.Open(getAgendaFilePath(agenda), FileMode.OpenOrCreate);
  67.                 formatter.Serialize(streamAgenda,agenda);
  68.                 streamAgenda.Close();
  69.             }
  70.             streamPort.Close();
  71.         }
  72.  
  73.         public void load()
  74.         {
  75.             load(getFilePath());
  76.         }
  77.  
  78.  
  79.         private void  load(String filePath)
  80.         {
  81.             try
  82.             {
  83.                 Stream stream = File.Open(filePath, FileMode.Open);
  84.                 BinaryFormatter formater = new BinaryFormatter();
  85.  
  86.                 Portfolio obj =  (Portfolio) formater.Deserialize(stream);
  87.                 //on récupére la liste des nom des agenda contenu dans ce portfolio
  88.                 this.listStringAgenda = obj.listStringAgenda;
  89.                 //on charge les agende et on els met dans la liste d'agenda
  90.                 loadAgendas(listStringAgenda,listAgenda);
  91.  
  92.                 stream.Close();
  93.             }
  94.             catch(FileNotFoundException e)
  95.             {
  96.                 //il n'existe pas encore de portfilo avec ce nom
  97.                 Console.WriteLine("Pas de portfolio avec ce nom");
  98.                 throw new Exception();
  99.             }  
  100.         }
  101.  
  102.  
  103.         /**
  104.          * recrée la liste d'agenda depuis les fichier serializé
  105.          **/
  106.         private void loadAgendas(List<String> listStr, List<Agenda> listAgenda)
  107.         {
  108.             foreach(String name in listStr)
  109.             {
  110.                 Agenda tmp ;
  111.                 tmp = new Agenda(name);
  112.                 tmp.load(getAgendaFilePath(tmp));
  113.                 listAgenda.Add(tmp);
  114.                 tmp = null;
  115.             }
  116.         }
  117.  
  118.         public string getFilePath()
  119.         {
  120.             return (linkedUser == null)? null : linkedUser.login + "-" + ".por";
  121.         }
  122.  
  123.         private string getAgendaFilePath(Agenda agenda)
  124.         {
  125.             return agenda.nom + "-" + this.linkedUser.login + ".age";
  126.         }
  127.        
  128.         public void setUser(User user)
  129.         {
  130.             if (user != null)
  131.             {
  132.                 linkedUser.login = user.login;
  133.                 linkedUser.nom = user.nom;
  134.                 linkedUser.prenom = user.prenom;
  135.                 linkedUser.avatar = user.avatar;
  136.             }
  137.             else
  138.             {
  139.                 throw new Exception("user null");
  140.             }
  141.  
  142.         }
  143.  
  144.  
  145.         //proriétés
  146.         public List<Agenda> listAgenda
  147.         {
  148.             set { _listAgenda = value; }
  149.             get { return _listAgenda; }
  150.         }
  151.  
  152.         public List<String> listStringAgenda
  153.         {
  154.             set { _listStringAgenda = value; }
  155.             get { return _listStringAgenda; }
  156.         }
  157.  
  158.         public User user
  159.         {
  160.             get { return linkedUser; }
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment