コード例 #1
0
        public ricettaStrumentoInsert(int idRicettaStrumento)
        {
            InitializeComponent();
            //Initializzation
            this.currentStep                = new HashSet <Step>();
            this.ingredientiSelezionati     = new Dictionary <Tuple <String, int>, int>();
            this.stepIngredientiSelezionati = new Dictionary <int, Tuple <String, int> >();
            this.idRicettaStrumento         = idRicettaStrumento;
            this.ingredientVisualizzator.HorizontalScrollbar = true;

            //Strumenti
            List <Strumento> strumenti        = (from c in db.Strumento select c).ToList();
            List <Strumento> elettrodomestici = (from c in strumenti
                                                 where c.Tipo.Equals("Elettrodomestico")
                                                 select c).ToList();

            elettrodomestici.ForEach(x => this.RSStrumentiSelector.Items.Add(x.Nome));
            strumenti.ForEach(x => this.strumentiStep.Items.Add(x.Nome + " " + x.Tipo));

            List <Ingrediente> ingredienti = (from c in db.Ingrediente select c).ToList();

            ingredienti.ForEach(x => this.ingredientiSelector.Items.Add(x.Nome));
            this.nomeStep.MaxLength = (int)textConst.NOME_STEP;

            //gestione list
            this.stepListing.HorizontalScrollbar = true;
            //Action
            this.ingredientiSelector.ItemCheck += (obj, args) =>
            {
                int keyIndex = ingredienti[args.Index].idIngrediente;
                if (args.CurrentValue == CheckState.Unchecked)
                {
                    Tuple <String, int> res = new selIngrediente(keyIndex).getQuantity();
                    Tuple <String, int> key = new Tuple <string, int>(res.Item1, keyIndex);
                    this.stepIngredientiSelezionati.Add(keyIndex, res);
                    if (this.ingredientiSelezionati.ContainsKey(key))
                    {
                        int qnt = this.ingredientiSelezionati[key];
                        qnt += res.Item2;
                        this.ingredientiSelezionati.Remove(key);
                        this.ingredientiSelezionati.Add(key, qnt);
                    }
                    else
                    {
                        this.ingredientiSelezionati.Add(key, res.Item2);
                    }
                    this.ingredientVisualizzator.Items.Clear();
                    this.refreshIngredient();
                }
                else
                {
                    Tuple <String, int> qnt = this.stepIngredientiSelezionati[keyIndex];
                    this.stepIngredientiSelezionati.Remove(keyIndex);
                    Tuple <String, int> key = new Tuple <String, int>(qnt.Item1, keyIndex);
                    int qntTot = this.ingredientiSelezionati[key];
                    if (qnt.Item2 == qntTot)
                    {
                        this.ingredientiSelezionati.Remove(key);
                    }
                    else
                    {
                        qntTot -= qnt.Item2;
                        this.ingredientiSelezionati.Remove(key);
                        this.ingredientiSelezionati.Add(key, qntTot);
                    }
                    this.ingredientVisualizzator.Items.Clear();
                    this.refreshIngredient();
                }
            };
            //Aggiungi step
            this.AggiungiStep.Click += (obj, args) =>
            {
                //Controllo dei parametri esatti
                if (this.nomeStep.TextLength == 0)
                {
                    AllertGestor.defaultError("Devi inserire un nome univoco per ogni step");
                }
                else if (this.descrizione.TextLength == 0)
                {
                    AllertGestor.defaultError("Devi inserire una descrizione");
                }
                else if (this.Immagine.Text.Equals(""))
                {
                    AllertGestor.defaultError("Devi inserire un'immagine");
                }
                else
                {
                    //Inserisco nello step le caratteristiche principali
                    Step main = new Step();
                    main.idRicettaStrum = this.idRicettaStrumento;
                    main.Nome           = this.nomeStep.Text;
                    main.Immagine       = this.Immagine.Text;
                    main.NumOrdine      = countStep;
                    main.Descrizione    = this.descrizione.Text;
                    this.currentStep.Add(main);
                    //Consumo table
                    this.stepIngredientiSelezionati.ToList().ForEach(x =>
                    {
                        Consumo cons = new Consumo();

                        cons.idIngrediente  = x.Key;
                        cons.idRicettaStrum = this.idRicettaStrumento;
                        cons.NomeStep       = this.nomeStep.Text;
                        cons.Quantità       = x.Value.Item2;
                        cons.NomeUDM        = x.Value.Item1;
                        main.Consumo.Add(cons);
                    });

                    //Uso table
                    foreach (int i in this.strumentiStep.CheckedIndices)
                    {
                        Uso uso = new Uso();
                        uso.idRicettaStrum = this.idRicettaStrumento;
                        uso.NomeStep       = this.nomeStep.Text;
                        uso.idStrumento    = strumenti[i].idStrumento;
                        main.Uso.Add(uso);
                    }
                    //Pulisco la pagina corrente
                    this.strumentiStep.Items.Clear();
                    strumenti.ForEach(x => this.strumentiStep.Items.Add(x.Nome + " " + x.Tipo));
                    this.ingredientiSelector.Items.Clear();
                    ingredienti.ForEach(x => this.ingredientiSelector.Items.Add(x.Nome));
                    this.nomeStep.Clear();
                    this.descrizione.Clear();
                    this.Immagine.Text = "";
                    this.stepIngredientiSelezionati.Clear();
                    this.countStep++;
                    this.stepListing.Items.Add(main.Nome + " " + main.NumOrdine);
                    //aggiungo lo step ai step da aggiungere
                    this.steps.Add(main);
                }
            };
            //Azione per aggiungere un'immagine
            this.immageOpen.Click += (obj, args) => {
                try
                {
                    this.Immagine.Text = UtilityImage.findFileCheckImage();
                }catch (ArgumentException exc)
                {
                    this.Immagine.Text = "";
                    AllertGestor.defaultError("Devi selezionare un'immagine valida!");
                }
            };
            //Azione per aggiungere la ricetta strumento
            this.addRS.Click += (obj, args) =>
            {
                if (this.currentStep.Count == 0)
                {
                    AllertGestor.defaultError("Devi inserire almeno uno step ");
                }
                else
                {
                    RicettaStrumento rs = new RicettaStrumento();
                    rs.idRicettaStrum = this.idRicettaStrumento;
                    try
                    {
                        rs.Kcal = (int)Double.Parse(this.kcalTot.Text);
                    } catch (Exception e)
                    {
                        rs.Kcal = 0;
                    }


                    bool begin = true;
                    foreach (int i in this.RSStrumentiSelector.CheckedIndices)
                    {
                        Utilizzo utilizzo = new Utilizzo();
                        utilizzo.idRicettaStrum = this.idRicettaStrumento;
                        utilizzo.idStrumento    = elettrodomestici[i].idStrumento;
                        rs.Utilizzo.Add(utilizzo);
                    }
                    HashSet <int> specifiche = new HashSet <int>();
                    this.ingredientiSelezionati.ToList().ForEach(x =>
                    {
                        Presenta presenta       = new Presenta();
                        presenta.idIngrediente  = x.Key.Item2;
                        presenta.idRicettaStrum = this.idRicettaStrumento;
                        presenta.NomeUDM        = x.Key.Item1;
                        presenta.Quantità       = x.Value;
                        rs.Presenta.Add(presenta);
                        if (begin)
                        {
                            specifiche = this.populateCatatteristiche(x.Key.Item2);
                            begin      = false;
                        }
                        else
                        {
                            HashSet <int> otherSpecifiche = this.populateCatatteristiche(x.Key.Item2);
                            specifiche.ToList().ForEach(z =>
                            {
                                if (!otherSpecifiche.Contains(z))
                                {
                                    specifiche.Remove(z);
                                }
                            });
                        }
                    });
                    specifiche.ToList().ForEach(x => Console.WriteLine(x));
                    specifiche.ToList().ForEach(x =>
                    {
                        Definito def         = new Definito();
                        def.idCaratteristica = x;
                        def.idRicettaStrum   = this.idRicettaStrumento;
                        rs.Definito.Add(def);
                    });
                    this.ricettaStrumentoSet.Add(rs);
                    this.Close();
                }
            };
        }
コード例 #2
0
        public RicettaInsert()
        {
            HashSet <RicettaStrumento> rsToInsert   = new HashSet <RicettaStrumento>();
            HashSet <Step>             stepToInsert = new HashSet <Step>();

            mainPanel = new TableLayoutPanel();
            this.mainPanel.AutoScroll = true;
            Size size = new Size(300, 20);
            //Label
            Label textRicetta = labelFactory.createAdvanceLabel("Inserisci il nome della ricetta", labelFactory.getGeneralFont(),
                                                                size, Color.Black, ContentAlignment.TopLeft);
            Label textTCottura = labelFactory.createAdvanceLabel("Inserisci il tempo di cottura in minuti", labelFactory.getGeneralFont(),
                                                                 size, Color.Black, ContentAlignment.TopLeft);
            Label textTPreparazione = labelFactory.createAdvanceLabel("Inserisci il tempo di preparazione in minuti", labelFactory.getGeneralFont(),
                                                                      size, Color.Black, ContentAlignment.TopLeft);
            Label textDifficolta = labelFactory.createAdvanceLabel("Inserisci la difficoltà", labelFactory.getGeneralFont(),
                                                                   size, Color.Black, ContentAlignment.TopLeft);

            Label textPersone = labelFactory.createAdvanceLabel("Inserisci il numero di persone", labelFactory.getGeneralFont(),
                                                                size, Color.Black, ContentAlignment.TopLeft);

            Label textPortata = labelFactory.createAdvanceLabel("Seleziona la portata desiderata", labelFactory.getGeneralFont(),
                                                                size, Color.Black, ContentAlignment.TopLeft);

            Label textImage = labelFactory.createAdvanceLabel("", labelFactory.getGeneralFont(),
                                                              new Size(400, 20), Color.Black, ContentAlignment.TopLeft);

            Label textDescrizione = labelFactory.createAdvanceLabel("Inserisci la descrizione", labelFactory.getGeneralFont(),
                                                                    size, Color.Black, ContentAlignment.TopLeft);

            //TextBox
            TextBox ricettaText = new TextBox();

            ricettaText.Size      = size;
            ricettaText.MaxLength = (int)textConst.NOME_RICETTA;

            TextBox cotturaText = new TextBox();

            cotturaText.Size      = size;
            cotturaText.MaxLength = 3;

            TextBox preparazioneText = new TextBox();

            preparazioneText.Size      = size;
            preparazioneText.MaxLength = 3;

            TextBox personeText = new TextBox();

            personeText.Size      = new Size(20, 20);
            personeText.MaxLength = 2;

            //RichTextBox

            RichTextBox descrizione = new RichTextBox();

            descrizione.Size = new Size(400, 100);

            //ComboBox
            ComboBox difficoltàBox = new ComboBox();

            DifficoltàUtilities.getDifficulties().ForEach(x => difficoltàBox.Items.Add(DifficoltàUtilities.getDifficulty(x)));

            ComboBox portataBox = new ComboBox();

            portataBox.Items.AddRange((from c in db.Portata
                                       select c.Nome).ToArray());

            //ListView
            ListView listRs = new ListView();

            listRs.View = View.List;
            //Button
            Button insertImage = new Button();

            insertImage.Text   = "Scegli un'immagine per la tua ricetta..";
            insertImage.Size   = size;
            insertImage.Click += (obj, arg) =>
            {
                try
                {
                    textImage.Text = UtilityImage.findFileCheckImage();
                }
                catch (ArgumentException exc)
                {
                    textImage.Text = "";
                    AllertGestor.defaultError("Devi selezionare un'immagine valida");
                }
            };

            Button addRicetta = new Button();

            addRicetta.Text    = "Aggiungi la ricetta..";
            addRicetta.Size    = new Size(200, 25);
            addRicetta.Enabled = false;
            //Cosa fare quando la ricetta deve essere inserita
            addRicetta.Click += (obj, args) =>
            {
                int tcottura, tpreparazione, persone;
                if (ricettaText.TextLength == 0)
                {
                    AllertGestor.defaultError("Devi inserire un nome!");
                }
                else if (cotturaText.TextLength == 0)
                {
                    AllertGestor.defaultError("Devi inserire il tempo di cottura");
                }
                else if (!Int32.TryParse(cotturaText.Text, out tcottura))
                {
                    AllertGestor.defaultError("il tempo di cottura deve essere numerico");
                }
                else if (preparazioneText.TextLength == 0)
                {
                    AllertGestor.defaultError("Devi inserire il tempo di preparazione");
                }
                else if (!Int32.TryParse(preparazioneText.Text, out tpreparazione))
                {
                    AllertGestor.defaultError("il tempo di preparazione deve essere numerico");
                }
                else if (textImage.Text.Equals(""))
                {
                    AllertGestor.defaultError("Devi inserire un'immagine");
                }
                else if (descrizione.TextLength == 0)
                {
                    AllertGestor.defaultError("Devi inserire una descrizione!");
                }
                else if (difficoltàBox.SelectedItem == null)
                {
                    AllertGestor.defaultError("Devi inserire una difficoltà!");
                }
                else if (portataBox.SelectedItem == null)
                {
                    AllertGestor.defaultError("Devi inserire una portata!");
                }
                else if (personeText.TextLength == 0)
                {
                    AllertGestor.defaultError("Devi inserire il numero di persone corretto!");
                }
                else if (!Int32.TryParse(personeText.Text, out persone))
                {
                    AllertGestor.defaultError("le persone devono avere un'indice numerico!");
                }
                else
                {
                    Ricetta ricetta = new Ricetta();
                    ricetta.Nome          = ricettaText.Text;
                    ricetta.TCottura      = tcottura;
                    ricetta.Persone       = persone;
                    ricetta.TPreparazione = tpreparazione;
                    ricetta.Difficoltà    = difficoltàBox.SelectedIndex;
                    ricetta.portata       = portataBox.SelectedItem.ToString();

                    string localFile = FileGestor.copyFileToLocalFolder(textImage.Text, ricetta.Nome);
                    ricetta.Immagine    = localFile;
                    ricetta.Descrizione = descrizione.Text;
                    db.Ricetta.InsertOnSubmit(ricetta);
                    bool insert = true;
                    try
                    {
                        db.SubmitChanges();
                    }
                    catch (Exception exc)
                    {
                        insert = false;
                        RicettarioDB.refresh();
                        this.db = RicettarioDB.getInstance();
                        AllertGestor.defaultError("Il nome della ricetta è già stato inserito! controlla nel menù principale\n" + exc);
                    }
                    if (insert)
                    {
                        //Aggiustamento dei vari valori trovati
                        stepToInsert.ToList().ForEach(x => {
                            x.Immagine = FileGestor.copyFileToLocalFolder(x.Immagine, "" + ricetta.idRicetta + "" + x.idRicettaStrum + "" + x.Nome);
                        });
                        rsToInsert.ToList().ForEach(x =>
                        {
                            x.idRicetta = ricetta.idRicetta;
                            x.Presenta.ToList().ForEach(z =>
                            {
                                Presenta p       = new Presenta();
                                p.idRicetta      = ricetta.idRicetta;
                                p.idRicettaStrum = z.idRicettaStrum;
                                p.idIngrediente  = z.idIngrediente;
                                p.Quantità       = z.Quantità;
                                p.NomeUDM        = z.NomeUDM;
                                x.Presenta.Remove(z);
                                x.Presenta.Add(p);
                            });
                            x.Definito.ToList().ForEach(z => {
                                Definito d         = new Definito();
                                d.idRicetta        = ricetta.idRicetta;
                                d.idRicettaStrum   = z.idRicettaStrum;
                                d.idCaratteristica = z.idCaratteristica;
                                x.Definito.Remove(z);
                                x.Definito.Add(d);
                            });
                            x.Utilizzo.ToList().ForEach(z => {
                                Utilizzo u       = new Utilizzo();
                                u.idRicetta      = ricetta.idRicetta;
                                u.idRicettaStrum = z.idRicettaStrum;
                                u.idStrumento    = z.idStrumento;
                                x.Utilizzo.Remove(z);
                                x.Utilizzo.Add(u);
                            });
                        });
                        stepToInsert.ToList().ForEach(x =>
                        {
                            x.idRicetta = ricetta.idRicetta;
                            x.Uso.ToList().ForEach(z =>
                            {
                                Uso u            = new Uso();
                                u.idRicetta      = ricetta.idRicetta;
                                u.idRicettaStrum = z.idRicettaStrum;
                                u.idStrumento    = z.idStrumento;
                                u.NomeStep       = z.NomeStep;
                                x.Uso.Remove(z);
                                x.Uso.Add(u);
                            });
                            x.Consumo.ToList().ForEach(z =>
                            {
                                Consumo p        = new Consumo();
                                p.idRicetta      = ricetta.idRicetta;
                                p.idRicettaStrum = z.idRicettaStrum;
                                p.idIngrediente  = z.idIngrediente;
                                p.Quantità       = z.Quantità;
                                p.NomeUDM        = z.NomeUDM;
                                p.NomeStep       = z.NomeStep;
                                x.Consumo.Remove(z);
                                x.Consumo.Add(p);
                            });
                        });
                        db.RicettaStrumento.InsertAllOnSubmit(rsToInsert.AsEnumerable());
                        db.Step.InsertAllOnSubmit(stepToInsert.AsEnumerable());
                        try
                        {
                            db.SubmitChanges();
                            AllertGestor.defaultShowOk("Ricetta Inserita!");
                        }
                        catch (Exception exc)
                        {
                            AllertGestor.defaultError("errore grave consultare il tecnico: \n traccia : " + exc.ToString());
                        }
                    }
                    RicettarioDB.refresh();
                    MainPaneGestor.getInstance().enableShowingSearch();
                    MainPaneGestor.getInstance().setPanel(PaneFactory.getRicettaPane());
                }
            };
            Button addSteps = new Button();

            addSteps.Text = "Inserisci gli step...";
            addSteps.Size = new Size(200, 25);
            //Cosa fare quando gli step devono essere inseriti
            addSteps.Click += (obj, args) =>
            {
                ricettaStrumentoInsert popup = new ricettaStrumentoInsert(ricettaStrumentoIndex);
                popup.StartPosition = FormStartPosition.CenterParent;
                popup.setRicettaStrumento(rsToInsert);
                popup.setSteps(stepToInsert);
                popup.ShowDialog();
                if (rsToInsert.Count != 0)
                {
                    addRicetta.Enabled = true;
                }
                listRs.Items.Clear();
                ricettaStrumentoIndex++;
                rsToInsert.ToList().ForEach(x => listRs.Items.Add("Famiglia Step id :" + x.idRicettaStrum));
            };
            //Name Ricetta
            mainPanel.Controls.Add(textRicetta);
            mainPanel.Controls.Add(ricettaText);
            //Tempo Cottura
            mainPanel.Controls.Add(textTCottura);
            mainPanel.Controls.Add(cotturaText);
            //Tempo Preparazione
            mainPanel.Controls.Add(textTPreparazione);
            mainPanel.Controls.Add(preparazioneText);
            //Difficoltà
            mainPanel.Controls.Add(textDifficolta);
            mainPanel.Controls.Add(difficoltàBox);
            //Persone
            mainPanel.Controls.Add(textPersone);
            mainPanel.Controls.Add(personeText);
            //portata
            mainPanel.Controls.Add(textPortata);
            mainPanel.Controls.Add(portataBox);
            //Immagine
            mainPanel.Controls.Add(insertImage);
            mainPanel.Controls.Add(textImage);
            //Descrizione
            mainPanel.Controls.Add(textDescrizione);
            mainPanel.Controls.Add(descrizione);

            //Inserimento
            mainPanel.Controls.Add(addSteps);

            //ListView
            mainPanel.Controls.Add(listRs);
            //Inserimento Ricetta
            mainPanel.Controls.Add(addRicetta);
        }