Innovenergy_trunk/csharp/App/Backend/Database/Create.cs

209 lines
6.2 KiB
C#

using InnovEnergy.App.Backend.DataTypes;
using InnovEnergy.App.Backend.Relations;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
private static int _backupCounter = 0;
private static Boolean Insert(Object obj)
{
var success = Connection.Insert(obj) > 0;
if (success)
{
_backupCounter++;
if (_backupCounter > 100)
{
_backupCounter = 0;
BackupDatabase();
}
}
return success;
}
public static Boolean Create(Installation installation)
{
installation.S3BucketId = Installations.Count(f => f.Product == installation.Product)+1;
// SQLite wrapper is smart and *modifies* t's Id to the one generated (autoincrement) by the insertion
return Insert(installation);
}
public static Boolean Create(Error error)
{
return Insert(error);
}
public static Boolean Create(Warning warning)
{
return Insert(warning);
}
public static Boolean Create(CsvTimestamp csvTimestamp)
{
return Insert(csvTimestamp);
}
public static Boolean Create(Folder folder)
{
return Insert(folder);
}
public static Boolean Create(User user)
{
return Insert(user);
}
public static Boolean Create(Session session)
{
return Insert(session);
}
public static Boolean Create(InstallationAccess installationAccess)
{
return Insert(installationAccess);
}
public static Boolean Create(FolderAccess folderAccess)
{
return Insert(folderAccess);
}
public static Boolean Create(OrderNumber2Installation o2i)
{
return Insert(o2i);
}
public static Boolean Create(UserAction action)
{
return Insert(action);
}
public static void HandleAction(UserAction newAction)
{
//Find the total number of actions for this installation
var totalActions = UserActions.Count(action => action.InstallationId == newAction.InstallationId);
//If there are 100 actions, remove the one with the oldest timestamp
if (totalActions == 100)
{
var oldestAction =
UserActions.Where(action => action.InstallationId == newAction.InstallationId)
.OrderBy(action => action.Timestamp)
.FirstOrDefault();
//Remove the old action
Delete(oldestAction);
//Add the new action
Create(newAction);
}
else
{
Console.WriteLine("---------------Added the new Action to the database-----------------");
Create(newAction);
}
}
public static void UpdateAction(UserAction updatedAction)
{
var existingAction = UserActions.FirstOrDefault(action => action.Id == updatedAction.Id);
if (existingAction != null)
{
//existingAction.Description = updatedAction.Description;
//existingAction.Timestamp = updatedAction.Timestamp;
//existingAction.TestingMode = updatedAction.TestingMode;
Update(updatedAction);
Console.WriteLine("---------------Updated the Action in the database-----------------");
}
}
public static void HandleError(Error newError,int installationId)
{
//Find the total number of errors for this installation
var totalErrors = Errors.Count(error => error.InstallationId == installationId);
//If there are 100 errors, remove the one with the oldest timestamp
if (totalErrors == 100)
{
var oldestError =
Errors.Where(error => error.InstallationId == installationId)
.OrderBy(error => error.Date)
.FirstOrDefault();
//Remove the old error
Delete(oldestError);
//Add the new error
Create(newError);
}
else
{
Console.WriteLine("---------------Added the new Error to the database-----------------");
Create(newError);
}
}
public static void HandleWarning(Warning newWarning,int installationId)
{
//Find the total number of warnings for this installation
var totalWarnings = Warnings.Count(warning => warning.InstallationId == installationId);
//If there are 100 warnings, remove the one with the oldest timestamp
if (totalWarnings == 100)
{
var oldestWarning =
Warnings.Where(warning => warning.InstallationId == installationId)
.OrderBy(warning => warning.Date)
.FirstOrDefault();
//Remove the old error
Delete(oldestWarning);
//Add the new error
Create(newWarning);
}
else
{
Console.WriteLine("---------------Added the new Warning to the database-----------------");
Create(newWarning);
}
}
public static void AddCsvTimestamp(CsvTimestamp newCsvTimestamp,int installationId)
{
var maxCSvPerInstallation = 60 * 24;
//Find the total number of warnings for this installation
var totalCsvNames = CsvTimestamps.Count(csvTimestamp => csvTimestamp.InstallationId == installationId);
//If there are 100 warnings, remove the one with the oldest timestamp
if (totalCsvNames == maxCSvPerInstallation)
{
var oldestCSvTimestamp =
CsvTimestamps.Where(csvTimestamp => csvTimestamp.InstallationId == installationId)
.OrderBy(csvTimestamp => csvTimestamp.Timestamp)
.FirstOrDefault();
//Remove the old error
Delete(oldestCSvTimestamp);
//Add the new error
Create(newCsvTimestamp);
}
else
{
//Console.WriteLine("---------------Added the new Csv Timestamp to the database-----------------");
Create(newCsvTimestamp);
}
}
}