SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IProjectCopyJob.cs
Go to the documentation of this file.
1 // SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Linq;
18 using System.Security;
19 using erminas.SmartAPI.CMS.ServerManagement;
20 using erminas.SmartAPI.Utils;
21 
22 namespace erminas.SmartAPI.CMS.Project
23 {
24  public interface IProjectCopyJob : IAsyncProjectJob
25  {
26  string DatabaseName { get; set; }
27  IDatabaseServer DatabaseServer { get; set; }
28  bool IsCopyingArchive { get; set; }
29  bool IsLoggingOffActiveUsersInProject { get; set; }
30  string NewProjectName { get; }
31  NewProjectType ProjectType { get; set; }
32  }
33 
34  internal sealed class ProjectCopyJob : AbstractAsyncProjectJob, IProjectCopyJob
35  {
36  private readonly string _newProjectName;
37 
38  internal ProjectCopyJob(IProject sourceProject, string newProjectName) : base(sourceProject)
39  {
40  _newProjectName = newProjectName;
41  DatabaseName = _newProjectName;
42  IsLoggingOffActiveUsersInProject = true;
43  ProjectType = NewProjectType.TestProject;
44  EmailSubject = String.Format("Finished copying project ({0})", sourceProject.Name);
45  EmailMessage = String.Format("Finished copying project. ({0})", sourceProject.Name);
46  IDatabaseServer dbServer;
47  if (!Session.ServerManager.DatabaseServers.TryGetByName("localhost", out dbServer))
48  {
49  dbServer = Session.ServerManager.DatabaseServers.First(server => server.IsCreateAllowed);
50  }
51  DatabaseServer = dbServer;
52  }
53 
54  public string DatabaseName { get; set; }
55  public IDatabaseServer DatabaseServer { get; set; }
56  public bool IsCopyingArchive { get; set; }
57  public bool IsLoggingOffActiveUsersInProject { get; set; }
58 
59  public string NewProjectName
60  {
61  get { return _newProjectName; }
62  }
63 
64  public NewProjectType ProjectType { get; set; }
65 
66  public override void RunAsync()
67  {
68  const string COPY_PROJECT =
69  @"<ADMINISTRATION><PROJECT userguid=""{0}"" action=""copy"" guid=""{1}"" newprojectname=""{2}"" newdatabasename=""{3}"" includearchive=""{4}"" logoutusers=""{5}"" schema="""" testproject="""" schemapassword="""" databaseserver=""{6}"" editorialserver=""{7}"" reddotserverguid="""" emailnotification=""{8}"" to=""{9}"" provider="""" subject=""{10}"" message=""{11}"" /></ADMINISTRATION>";
70 
71  string query = COPY_PROJECT.RQLFormat(Session.CurrentUser, Project, SecurityElement.Escape(NewProjectName),
72  SecurityElement.Escape(DatabaseName), IsCopyingArchive,
73  IsLoggingOffActiveUsersInProject, DatabaseServer, Server,
74  IsSendingEmailOnCompletion, EmailReceipient,
75  SecurityElement.Escape(EmailSubject),
76  SecurityElement.Escape(EmailMessage));
77  Session.ExecuteRQL(query);
78  }
79 
80  public override void RunSync(TimeSpan maxWait)
81  {
82  RunAsync();
83  if (false && Session.ServerVersion < new Version(11, 0))
84  {
85  //we wait for a copy process to appear and disappear
86  //this doesn't work reliably in newer OpentText/RedDot versions,
87  //because the process doesn't show up in the process list long enough.
88  Session.WaitForAsyncProcess(maxWait,
89  process =>
90  process.Type == AsynchronousProcessType.CopyProject &&
91  Project.Equals(process.Project));
92  }
93  else
94  {
95  var retryEverySecond = new TimeSpan(0, 0, 1);
96  Session.ServerManager.Projects.WaitFor(list =>
97  {
98  IProject project;
99  return list.Refreshed().TryGetByName(NewProjectName, out project) &&
100  !project.Refreshed().IsLockedBySystem;
101  }, maxWait, retryEverySecond);
102  }
103  }
104  }
105 }