Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace activite
- {
- [Serializable()]
- class Portfolio
- {
- [NonSerialized] private User linkedUser;
- [NonSerialized] private List<Agenda> _listAgenda = null;
- private List<String> _listStringAgenda = null;
- public Portfolio()
- {
- listAgenda = new List<Agenda>();
- listStringAgenda = new List<string>();
- linkedUser = new User();
- }
- public Portfolio(User user) :this()
- {
- try
- {
- load();
- }
- catch (Exception e)
- {
- //le portfilio n'existe pas encore
- //on créer et on sauve
- linkedUser = user;
- save();
- }
- }
- public void save()
- {
- save(getFilePath());
- }
- private void save(string filePath)
- {
- //on vérifie qu'on a bien tout les nom des agenda dans la liste*/
- foreach(Agenda agenda in listAgenda)
- {
- if(!listStringAgenda.Contains(agenda.nom))
- {
- listStringAgenda.Add(agenda.nom);
- }
- }
- listStringAgenda.Sort();
- //on sérialise
- Stream streamPort = File.Open(filePath, FileMode.OpenOrCreate);
- BinaryFormatter formatter = new BinaryFormatter();
- //le portfolio
- formatter.Serialize(streamPort, this);
- foreach(Agenda agenda in listAgenda)
- {//et tout les agenda qu'il contient
- Stream streamAgenda = File.Open(getAgendaFilePath(agenda), FileMode.OpenOrCreate);
- formatter.Serialize(streamAgenda,agenda);
- streamAgenda.Close();
- }
- streamPort.Close();
- }
- public void load()
- {
- load(getFilePath());
- }
- private void load(String filePath)
- {
- try
- {
- Stream stream = File.Open(filePath, FileMode.Open);
- BinaryFormatter formater = new BinaryFormatter();
- Portfolio obj = (Portfolio) formater.Deserialize(stream);
- //on récupére la liste des nom des agenda contenu dans ce portfolio
- this.listStringAgenda = obj.listStringAgenda;
- //on charge les agende et on els met dans la liste d'agenda
- loadAgendas(listStringAgenda,listAgenda);
- stream.Close();
- }
- catch(FileNotFoundException e)
- {
- //il n'existe pas encore de portfilo avec ce nom
- Console.WriteLine("Pas de portfolio avec ce nom");
- throw new Exception();
- }
- }
- /**
- * recrée la liste d'agenda depuis les fichier serializé
- **/
- private void loadAgendas(List<String> listStr, List<Agenda> listAgenda)
- {
- foreach(String name in listStr)
- {
- Agenda tmp ;
- tmp = new Agenda(name);
- tmp.load(getAgendaFilePath(tmp));
- listAgenda.Add(tmp);
- tmp = null;
- }
- }
- public string getFilePath()
- {
- return (linkedUser == null)? null : linkedUser.login + "-" + ".por";
- }
- private string getAgendaFilePath(Agenda agenda)
- {
- return agenda.nom + "-" + this.linkedUser.login + ".age";
- }
- public void setUser(User user)
- {
- if (user != null)
- {
- linkedUser.login = user.login;
- linkedUser.nom = user.nom;
- linkedUser.prenom = user.prenom;
- linkedUser.avatar = user.avatar;
- }
- else
- {
- throw new Exception("user null");
- }
- }
- //proriétés
- public List<Agenda> listAgenda
- {
- set { _listAgenda = value; }
- get { return _listAgenda; }
- }
- public List<String> listStringAgenda
- {
- set { _listStringAgenda = value; }
- get { return _listStringAgenda; }
- }
- public User user
- {
- get { return linkedUser; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment