/// <summary> /// Checks parameters provided by the GUI and delegates values to database which saves them. /// </summary> public void Create(ProjektTable pj) { // check data once again IRule lnhsv = new LettersNumbersHyphenSpaceValidator(); IRule posintv = new PositiveIntValidator(); IRule slv = new StringLength150Validator(); IRule dv = new DateValidator(); // call eval methods lnhsv.Eval(pj.Projektname); slv.Eval(pj.Projektname); posintv.Eval(pj.AngebotID); dv.Eval(pj.Projektstart); // check for errors if (lnhsv.HasErrors || slv.HasErrors || posintv.HasErrors || dv.HasErrors) { throw new InvalidInputException("Invalid values provided by GUI"); } // reformat data string pj.Projektstart = GlobalActions.ParseToSQLiteDateString(pj.Projektstart); // send values to database try { DALFactory.GetDAL().CreateProjekt(pj); } catch (SQLiteException ex) { this.logger.Log(Logger.Level.Error, ex.Message + ex.StackTrace); throw new DataBaseException(ex.Message); } }
/// <summary> /// Creates a new Projekt /// </summary> /// <param name="sender">The sending object</param> /// <param name="e">The event args</param> private void CreateProjekt(object sender, EventArgs e) { // hide label this.projektNeuMsgLabel.Text = string.Empty; this.projektNeuMsgLabel.Hide(); // define Rules IRule lnhsv = new LettersNumbersHyphenSpaceValidator(); IRule slv = new StringLength150Validator(); ProjektTable projekt = new ProjektTable(); // bind values projekt.Projektname = DataBindingFramework.BindFromString(this.projektNeuProjekttitelTextbox.Text, "Projekttitel", this.projektNeuMsgLabel, false, lnhsv, slv); if (lnhsv.HasErrors || slv.HasErrors) { this.projektNeuMsgLabel.Text = "Projektname ungültig!"; } // no Angebot chosen or first element chosen (which is empty) -> show error label if (this.projektErstellenAngebotCombobox.SelectedIndex <= 0) { this.projektNeuMsgLabel.Text += "\nKein Angebot ausgewählt"; this.projektNeuMsgLabel.ForeColor = Color.Red; this.projektNeuMsgLabel.Show(); return; } // Angebot chosen - get ID out of ComboBox else { string s_angebotID = this.projektErstellenAngebotCombobox.SelectedItem.ToString(); s_angebotID = s_angebotID.Substring(0, s_angebotID.IndexOf(':')); IRule posint = new PositiveIntValidator(); IRule datev = new DateValidator(); projekt.AngebotID = DataBindingFramework.BindFromInt(s_angebotID, "AngebotID", this.projektNeuMsgLabel, false, posint); projekt.Projektstart = DataBindingFramework.BindFromString(this.projektNeuStartdatumDatepicker.Value.ToShortDateString(), "Startdatum", projektNeuMsgLabel, false, datev); // Check for errors while databinding if (this.projektNeuMsgLabel.Visible) { this.logger.Log(Logger.Level.Error, projekt.AngebotID + " is an invalid Angebot ID or invalid date chosen or invalid project name."); return; } // get Kunde table out of Database AngebotManager loader = new AngebotManager(); List<AngebotTable> results = loader.Load(projekt.AngebotID, new DateTime(1900, 1, 1), new DateTime(2100, 1, 1), this.projektNeuMsgLabel, true); // there must be exactly one result, for ID is unique! if (results.Count != 1) { this.logger.Log(Logger.Level.Error, "More than one Angebot returned - impossible, because ID is unique!"); this.projektNeuMsgLabel.Text = "Error: Datenbank inkonsistent!"; this.projektNeuMsgLabel.Visible = true; return; } // everything went fine ProjektManager saver = new ProjektManager(); try { saver.Create(projekt); //logger.Log(Logger.Level.Info, "uebergabe von projekttab: " + projekt.AngebotID+ " " + projekt.Projektstart); } catch (InvalidInputException ex) { this.logger.Log(Logger.Level.Error, "Input was invalid, although checked." + ex.Message); } catch (DataBaseException ex) { this.logger.Log(Logger.Level.Error, "Some serious problem with the database occured!" + ex.Message); } GlobalActions.ShowSuccessLabel(this.projektNeuMsgLabel); } }
/// <summary> /// Loads existing Projekte out of the SQLite database /// </summary> /// <param name="from">Start searching date in format DD.MM.YYYY</param> /// <param name="until">End searching date in format DD.MM.YYYY</param> /// <param name="kundenID">The ID of the related kundenID. -1, if any.</param> /// <returns>A resultlist of the found matching Projekte</returns> public List<ProjektTable> LoadProjekte(string from, string until, int kundenID) { string sql; if (kundenID >= 0) { sql = "SELECT t0.ID, t0.AngebotID, t0.Projektname, t0.Projektstart FROM Projekt t0 JOIN Angebot t1 on t0.AngebotID = t1.ID WHERE t1.KundenID = ? AND t0.Projektstart BETWEEN ? AND ?"; } else { sql = "SELECT * FROM Projekt WHERE Projektstart BETWEEN ? AND ?"; } List<ProjektTable> results = new List<ProjektTable>(); // open connection and get requested Projekt(e) out of database SQLiteConnection con = null; SQLiteTransaction tra = null; SQLiteCommand cmd = null; SQLiteDataReader reader = null; try { // initialise connection con = new SQLiteConnection(ConfigFileManager.ConnectionString); con.Open(); // initialise transaction tra = con.BeginTransaction(); cmd = new SQLiteCommand(sql, con); if (kundenID != -1) { SQLiteParameter p_ID = new SQLiteParameter(); p_ID.Value = kundenID; cmd.Parameters.Add(p_ID); } // bind from SQLiteParameter p_from = new SQLiteParameter(); p_from.Value = from; cmd.Parameters.Add(p_from); // bind to SQLiteParameter p_until = new SQLiteParameter(); p_until.Value = until; cmd.Parameters.Add(p_until); // execute and get results reader = cmd.ExecuteReader(); while (reader.Read()) { ProjektTable result = new ProjektTable(); result.ID = reader.GetInt32(0); result.AngebotID = reader.GetInt32(1); result.Projektname = reader.GetString(2); result.Projektstart = reader.GetString(3); results.Add(result); } return results; } catch (SQLiteException) { throw; } finally { if (reader != null) { reader.Dispose(); } if (tra != null) { tra.Dispose(); } if (cmd != null) { cmd.Dispose(); } if (con != null) { con.Dispose(); } } }
/// <summary> /// Creates a new Projekt with the provided parameters and stores it in the SQLite database /// </summary> /// <param name="projektname">The projekt which shall be saved</param> public void CreateProjekt(ProjektTable pj) { string sql = "INSERT INTO Projekt (AngebotID, Projektname, Projektstart) VALUES (?, ?, ?)"; try { this.SendStatementToDatabase(sql, -1, pj.AngebotID, pj.Projektname, pj.Projektstart); logger.Log(Logger.Level.Info, pj.AngebotID + " " + pj.Projektname + " " + pj.Projektstart); } catch (SQLiteException) { throw; } // success logging this.logger.Log(Logger.Level.Info, "New Projekt " +pj.Projektname+ " has been stored in the SQLite database."); }
/// <summary> /// Creates a new Projekt with the provided parameters and stores it in the mock database /// </summary> /// <param name="projektname">The projekt which shall be saved</param> public void CreateProjekt(ProjektTable pj) { pj.ID = MockDataBaseManager.ProjektID; MockDataBaseManager.savedProjekte.Add(pj); this.logger.Log(Logger.Level.Info, "A new Projekt has been stored within the SQLite database"); }