302 lines
12 KiB
C#
302 lines
12 KiB
C#
using BeWo.Data.Access;
|
|
using BeWo.Data.Entities;
|
|
using BeWo.Service.DCEntityMapper;
|
|
using BS.Shared;
|
|
using BS.Shared.DataContracts;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using BeWo.Service.Core;
|
|
|
|
namespace BeWo.Service.Dokumentverwaltung
|
|
{
|
|
internal class DokumentManager
|
|
{
|
|
public static List<BeWoFolderDC> GetFolderTree(TableID pObjectTid, long pObjectOid)
|
|
{
|
|
|
|
IList<BeWoFolder> folder = DAOFactory.SearchDAO.FindBeWoFolders(pObjectTid, pObjectOid);
|
|
bool templateFolder = folder.Any(f => f.SystemEntryID.Equals(SystemEntryID.TemplateDirectory));
|
|
var folderDCList = new List<BeWoFolderDC>();
|
|
|
|
foreach (BeWoFolder item in SortiereOrdner(folder))
|
|
{
|
|
folderDCList.Add(MapperFactory.BeWoFolderDC_BeWoFolder.MapToNewDC(item));
|
|
}
|
|
|
|
IEnumerable<FileAttachmentInfo> files = DAOFactory.SearchDAO.FindFileAttachmentInfos(pObjectTid, pObjectOid);
|
|
var fileDCList = new List<FileAttachmentDC>();
|
|
|
|
foreach (FileAttachmentInfo item in files.OrderBy(f => f.Path))
|
|
{
|
|
fileDCList.Add(MapperFactory.FileAttachmentDC_FileAttachmentInfo.MapToNewDC(item));
|
|
}
|
|
|
|
//Thread.Sleep(500);
|
|
|
|
return CreateFolderTree(folderDCList, fileDCList, pObjectTid, pObjectOid, templateFolder);
|
|
|
|
}
|
|
|
|
private static IList<BeWoFolder> SortiereOrdner(IList<BeWoFolder> folder)
|
|
{
|
|
String maxNumberPrefix = "";
|
|
|
|
foreach (var item in folder)
|
|
{
|
|
|
|
var nummerPrefix = GetNumberPrefix(item.Name);
|
|
|
|
if (nummerPrefix.Length > maxNumberPrefix.Length || (nummerPrefix.Length == maxNumberPrefix.Length && nummerPrefix.CompareTo(maxNumberPrefix) > 0))
|
|
{
|
|
maxNumberPrefix = nummerPrefix;
|
|
}
|
|
|
|
}
|
|
|
|
return folder.OrderBy(f => GetTempName(f.Name, maxNumberPrefix)).ToList();
|
|
}
|
|
|
|
private static string GetTempName(string name, string maxNumberPrefix)
|
|
{
|
|
String tempName = name;
|
|
if (!String.IsNullOrEmpty(maxNumberPrefix))
|
|
{
|
|
var prefix = GetNumberPrefix(name);
|
|
if (!String.IsNullOrEmpty(prefix))
|
|
{
|
|
while (prefix.Length < maxNumberPrefix.Length)
|
|
{
|
|
prefix = "0" + prefix;
|
|
tempName = "0" + tempName;
|
|
}
|
|
}
|
|
}
|
|
|
|
return tempName;
|
|
}
|
|
|
|
private static string GetNumberPrefix(string name)
|
|
{
|
|
int numberOfDigitsAtStart = 0;
|
|
for (var i = 0; i < name.Length; i++)
|
|
{
|
|
if (!char.IsNumber(name[i]))
|
|
{
|
|
break;
|
|
}
|
|
|
|
numberOfDigitsAtStart++;
|
|
}
|
|
|
|
if (numberOfDigitsAtStart > 0)
|
|
{
|
|
return name.Substring(0, numberOfDigitsAtStart);
|
|
}
|
|
return "";
|
|
}
|
|
private static List<BeWoFolderDC> CreateFolderTree(List<BeWoFolderDC> folderDCList, List<FileAttachmentDC> fileDCList, TableID pObjectTid, long pObjectOid, bool templateFolder = false)
|
|
{
|
|
Dictionary<long, BeWoFolderDC> folderDict = folderDCList.ToDictionary(fo => fo.BeWoFolderOid.Value);
|
|
Dictionary<long, FileAttachmentDC> fileDict = fileDCList.ToDictionary(fi => fi.FileAttachmentOid.Value);
|
|
|
|
var resultList = new List<BeWoFolderDC>();
|
|
|
|
var root = new BeWoFolderDC
|
|
{
|
|
ObjectTid = pObjectTid,
|
|
ObjectOid = pObjectOid,
|
|
Name = "Alle Ordner",
|
|
SubFolders = new List<BeWoFolderDC>(),
|
|
Files = new List<FileAttachmentDC>()
|
|
};
|
|
|
|
if (templateFolder)
|
|
root = folderDCList.First(f => f.Name.Equals("Vorlagen"));
|
|
|
|
resultList.Add(root);
|
|
|
|
foreach (BeWoFolderDC cfo in folderDCList)
|
|
{
|
|
if (root.Equals(cfo))
|
|
continue;
|
|
|
|
if (cfo.ParentBeWoFolderOid.HasValue)
|
|
{
|
|
if (folderDict.ContainsKey(cfo.ParentBeWoFolderOid.Value))
|
|
{
|
|
BeWoFolderDC parent = folderDict[cfo.ParentBeWoFolderOid.Value];
|
|
parent.SubFolders.Add(cfo);
|
|
}
|
|
else
|
|
{
|
|
root.SubFolders.Add(cfo);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
root.SubFolders.Add(cfo);
|
|
}
|
|
}
|
|
|
|
foreach (FileAttachmentDC fi in fileDCList)
|
|
{
|
|
if (fi.BeWoFolderOid.HasValue)
|
|
{
|
|
if (folderDict.ContainsKey(fi.BeWoFolderOid.Value))
|
|
{
|
|
BeWoFolderDC parent = folderDict[fi.BeWoFolderOid.Value];
|
|
parent.Files.Add(fi);
|
|
}
|
|
else
|
|
{
|
|
root.Files.Add(fi);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
root.Files.Add(fi);
|
|
}
|
|
}
|
|
|
|
return resultList;
|
|
}
|
|
public static void DeleteFolder(long pBeWoFolderOid, long pVersion)
|
|
{
|
|
var origFolder = DAOFactory.GenericDAO.LoadByID<BeWoFolder>(pBeWoFolderOid);
|
|
MapperFactory.BeWoFolderDC_BeWoFolder.ConcurrencyCheck(pVersion, origFolder);
|
|
|
|
DAOFactory.GenericDAO.Delete(origFolder);
|
|
}
|
|
public static long InsertNewFolder(BeWoFolderDC folder)
|
|
{
|
|
BeWoFolder newFolder = MapperFactory.BeWoFolderDC_BeWoFolder.MapToNewEntity(folder);
|
|
|
|
DAOFactory.GenericDAO.Insert(newFolder);
|
|
BeWoFolderDC newFolderDC = MapperFactory.BeWoFolderDC_BeWoFolder.MapToNewDC(newFolder);
|
|
|
|
return newFolderDC.BeWoFolderOid.Value;
|
|
}
|
|
public static BeWoFolderDC UpdateFolder(BeWoFolderDC folder)
|
|
{
|
|
//Root Folder kann nicht umbenannt werden
|
|
var origFolder = DAOFactory.GenericDAO.LoadByID<BeWoFolder>(folder.BeWoFolderOid.Value);
|
|
|
|
MapperFactory.BeWoFolderDC_BeWoFolder.MergeWithEntity(folder, origFolder);
|
|
|
|
DAOFactory.GenericDAO.Update(origFolder);
|
|
BeWoFolderDC updatedFolderDC = MapperFactory.BeWoFolderDC_BeWoFolder.MapToNewDC(origFolder);
|
|
|
|
return updatedFolderDC;
|
|
}
|
|
public static FileAttachmentDC UpdateFileAttachment(FileAttachmentDC file)
|
|
{
|
|
var origFile = DAOFactory.GenericDAO.LoadByID<FileAttachment>(file.FileAttachmentOid.Value);
|
|
//var origFile = DAOFactory.GenericDAO.LoadByID<FileAttachmentInfo>(file.FileAttachmentOid.Value);
|
|
|
|
//MapperFactory.FileAttachmentDC_FileAttachmentInfo.MergeWithEntity(file, origFile); // Bei Fehlern wieder einkommentieren
|
|
MapperFactory.FileAttachmentDC_FileAttachment.MergeWithEntity(file, origFile); // Bei Fehlern auskommentieren
|
|
|
|
DAOFactory.GenericDAO.Update(origFile);
|
|
|
|
//FileAttachmentDC updatedFileDC = MapperFactory.FileAttachmentDC_FileAttachmentInfo.MapToNewDC(origFile); // Bei Fehlern wieder einkommentieren
|
|
FileAttachmentDC updatedFileDC = MapperFactory.FileAttachmentDC_FileAttachment.MapToNewDC(origFile); // Bei Fehlern auskommentieren
|
|
|
|
return updatedFileDC;
|
|
}
|
|
public static FileAttachmentDC DoTheRealFileUpdate(FileAttachmentDC file)
|
|
{
|
|
var origFile = DAOFactory.GenericDAO.LoadByID<FileAttachment>(file.FileAttachmentOid.Value);
|
|
if (!origFile.Data.Equals(file.BinaryData))
|
|
{
|
|
}
|
|
|
|
MapperFactory.FileAttachmentDC_FileAttachment.MergeWithEntity(file, origFile);
|
|
|
|
DAOFactory.GenericDAO.Update(origFile);
|
|
|
|
FileAttachmentDC updatedFileDC = MapperFactory.FileAttachmentDC_FileAttachment.MapToNewDC(origFile);
|
|
return updatedFileDC;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
public static void InsertFileAttachment(FileAttachmentDC fileAttachmentDC)
|
|
{
|
|
var entity = new FileAttachment()
|
|
{
|
|
BeWoFolderOid = fileAttachmentDC.BeWoFolderOid,
|
|
Data = fileAttachmentDC.BinaryData,
|
|
InsTs = fileAttachmentDC.InsertedOn,
|
|
InsUser = fileAttachmentDC.InsertedBy,
|
|
IsActive = ActivationTypeId.Active,
|
|
ObjectOid = fileAttachmentDC.ObjectOid,
|
|
ObjectTid = fileAttachmentDC.ObjectTid,
|
|
Type = FileAttachmentType.NotSpecified,
|
|
Version = 1,
|
|
Path = fileAttachmentDC.FileName
|
|
};
|
|
|
|
entity = MapperFactory.FileAttachmentDC_FileAttachment.MergeWithEntity(fileAttachmentDC, entity);
|
|
|
|
DAOFactory.GenericDAO.Insert(entity);
|
|
|
|
FileAttachmentUtils.SaveFileToFileDirectory(entity);
|
|
}
|
|
public static List<FileAttachmentDC> SearchForFiles(string nutzereingabe)
|
|
{
|
|
var list = DAOFactory.SearchDAO.FindFileattachmentOids(nutzereingabe);
|
|
|
|
List<FileAttachmentDC> files = new List<FileAttachmentDC>();
|
|
|
|
foreach (object[] info in list)
|
|
{
|
|
|
|
var dc = new FileAttachmentDC();
|
|
dc.FileAttachmentOid = info[0] as long?;
|
|
dc.BeWoFolderOid = info[1] as long?;
|
|
dc.ObjectOid = info[2] as long?;
|
|
dc.ObjectTid = (TableID)info[3];
|
|
dc.FileName = info[4].ToString();
|
|
files.Add(dc);
|
|
}
|
|
|
|
return files;
|
|
}
|
|
public static string GetObjectForFilePath(TableID table, long? oid)
|
|
{
|
|
string objectName = "";
|
|
|
|
switch (table)
|
|
{
|
|
case TableID.ApplicationUser: objectName = (DAOFactory.GenericDAO.GetByID<ApplicationUser>((long)oid)).LoginName; break;
|
|
case TableID.SupportConcept:
|
|
var sc = DAOFactory.GenericDAO.GetByID<SupportConcept>((long)oid);
|
|
objectName = sc.Customer.Person.LastNameFirstName + " " + CustomizeDates(sc.StartDate, sc.EndDate); break;
|
|
case TableID.Customer: objectName = (DAOFactory.GenericDAO.GetByID<Customer>((long)oid)).Person.LastNameFirstName; break;
|
|
case TableID.Organisation: objectName = (DAOFactory.GenericDAO.GetByID<Organisation>((long)oid)).Name; break;
|
|
case TableID.Person: objectName = (DAOFactory.GenericDAO.GetByID<Person>((long)oid)).LastNameFirstName; break;
|
|
case TableID.Employee: objectName = (DAOFactory.GenericDAO.GetByID<Employee>((long)oid)).Person.LastNameFirstName; break;
|
|
case TableID.Team: objectName = (DAOFactory.GenericDAO.GetByID<Team>((long)oid)).Name; break;
|
|
case TableID.UserGroup: objectName = (DAOFactory.GenericDAO.GetByID<UserGroup>((long)oid)).Name; break;
|
|
}
|
|
|
|
return objectName;
|
|
}
|
|
private static string CustomizeDates(DateTime? startdate, DateTime? enddate)
|
|
{
|
|
string span = "";
|
|
string startday, endday, startmonth, endmonth;
|
|
|
|
startday = (startdate.Value.Day < 10) ? "0" + startdate.Value.Day : startdate.Value.Day.ToString();
|
|
endday = (enddate.Value.Day < 10) ? "0" + enddate.Value.Day : enddate.Value.Day.ToString();
|
|
startmonth = (startdate.Value.Month < 10) ? "0" + startdate.Value.Month : startdate.Value.Month.ToString();
|
|
endmonth = (enddate.Value.Month < 10) ? "0" + enddate.Value.Month : enddate.Value.Month.ToString();
|
|
|
|
span = "(" + startday + "." + startmonth + "." + startdate.Value.Year + " - " + endday + "." + endmonth + "." + enddate.Value.Year + ")";
|
|
|
|
return span;
|
|
}
|
|
}
|
|
}
|