18 using System.Security;
20 using erminas.SmartAPI.CMS.ServerManagement;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 using erminas.SmartAPI.Utils.CachedCollections;
25 namespace erminas.SmartAPI.CMS.Project
30 ImportWithoutReplacingExistingUsers = 1,
31 ImportAndReplaceExistingUsers = 2
36 string DatabaseName {
get;
set; }
38 string ImportFolder {
get;
set; }
40 bool IsImportingArchive {
get;
set; }
41 bool IsImportingReleases {
get;
set; }
42 bool IsIncludingAdministratorSettings {
get;
set; }
43 string ProjectName {
get; }
48 internal class ProjectImportJob : AbstractAsyncJob, IProjectImportJob
50 private readonly
string _newProjectName;
52 internal ProjectImportJob(
ISession session,
string newProjectName,
string importFolder) : base(session)
54 _newProjectName = newProjectName;
56 DatabaseServer = Session.ServerManager.DatabaseServers.First();
57 DatabaseName = _newProjectName;
58 ImportFolder = importFolder;
59 ImportServer = Session.ServerManager.ApplicationServers.First();
60 EmailSubject =
"Project import completed";
61 EmailMessage =
"Project import completed";
64 public string DatabaseName {
get;
set; }
66 public string ImportFolder {
get;
set; }
68 public bool IsImportingArchive {
get;
set; }
69 public bool IsImportingReleases {
get;
set; }
70 public bool IsIncludingAdministratorSettings {
get;
set; }
72 public string ProjectName
74 get {
return _newProjectName; }
79 public override void RunAsync()
85 public override void RunSync(TimeSpan maxWait)
88 var retryEverySecond =
new TimeSpan(0, 0, 1);
89 Predicate<IRDList<IAsynchronousProcess>> hasImportProcess =
94 var start = DateTime.Now;
96 Func<bool> hasImportProcessOrProjectIsAlreadyImported =
98 hasImportProcess(Session.ServerManager.AsynchronousProcesses.Refreshed()) ||
99 Session.ServerManager.Projects.Refreshed().ContainsName(ProjectName);
100 Wait.For(hasImportProcessOrProjectIsAlreadyImported, maxWait, retryEverySecond);
102 if (Session.ServerManager.Projects.ContainsName(ProjectName))
107 TimeSpan timeLeft = maxWait - (DateTime.Now - start);
108 timeLeft = timeLeft.TotalMilliseconds > 0 ? timeLeft :
new TimeSpan(0, 0, 0);
110 Session.ServerManager.AsynchronousProcesses.WaitFor(list => !hasImportProcess(list), timeLeft,
116 private void CheckImport()
121 if (Session.ServerVersion <
new Version(11, 0))
126 const string CHECK_IMPORT =
127 @"<ADMINISTRATION><PROJECT action=""checkbeforeimport"" xmlpath=""{0}"" /></ADMINISTRATION>";
128 var xmlDoc = Session.ExecuteRQL(CHECK_IMPORT.RQLFormat(SecurityElement.Escape(ImportFolder)),
129 RQL.IODataFormat.LogonGuidOnly);
131 XmlElement projectElement = xmlDoc.GetSingleElement(
"PROJECT");
132 if (projectElement == null || !projectElement.TryGetGuid(out projectGuid))
136 "Could not import project from {0}, please check folder/share permissions.",
141 private void ExecuteImport()
143 const string IMPORT_PROJECT =
@"<ADMINISTRATION>
144 <PROJECT action=""import"" userguid=""{0}"" databaseserver=""{1}"" editorialserver=""{2}"" databasename=""{3}""
145 emailnotification=""{4}"" includearchive=""{5}"" projectname=""{6}"" xmlpath=""{7}"" schema="""" schemapassword=""""
146 reddotserverguid=""{8}"" testproject=""{9}"" useoldguid=""0"" importusers=""{10}"" importobjectrelease=""{11}""
147 projectguid="""" to=""{12}"" provider="""" subject=""{13}"" message=""{14}""/>
149 var query = IMPORT_PROJECT.RQLFormat(Session.CurrentUser, DatabaseServer, Server,
150 SecurityElement.Escape(DatabaseName), IsSendingEmailOnCompletion,
151 IsImportingArchive, SecurityElement.Escape(ProjectName), ImportFolder,
152 ImportServer, (int) ProjectType, (
int) UserGroupAndAssignmentsSettings,
153 IsImportingReleases, EmailReceipient,
154 SecurityElement.Escape(EmailSubject),
155 SecurityElement.Escape(EmailMessage));
157 Session.ExecuteRQL(query, RQL.IODataFormat.LogonGuidOnly);