17 using System.Collections.Generic;
83 public sealed
class AcLock : IFormattable, IEquatable<AcLock>, IComparable<AcLock>, IComparable
85 #region class variables
91 private string _exceptFor;
92 private string _onlyFor;
93 private string _comment;
101 #region Equality comparison
112 if (ReferenceEquals(other, null))
return false;
113 if (ReferenceEquals(
this, other))
return true;
115 var left = Tuple.Create(
Name,
Kind);
116 var right = Tuple.Create(other.
Name, other.
Kind);
117 return left.Equals(right);
124 public override bool Equals(
object other)
126 if (ReferenceEquals(other, null))
return false;
127 if (ReferenceEquals(
this, other))
return true;
128 if (GetType() != other.GetType())
return false;
138 var hash = Tuple.Create(
Name,
Kind);
139 return hash.GetHashCode();
144 #region Order comparison
157 if (
AcLock.ReferenceEquals(
this, other))
161 result = (-1 *
Kind.CompareTo(other.
Kind));
163 result = String.Compare(
Name, other.
Name);
175 int IComparable.CompareTo(
object other)
178 throw new ArgumentException(
"Argument is not an AcLock",
"other");
190 get {
return _kind; }
191 internal set { _kind = value; }
199 get {
return _name ?? String.Empty; }
200 internal set { _name = value; }
208 get {
return _type; }
209 internal set { _type = value; }
217 get {
return _exceptFor ?? String.Empty; }
218 internal set { _exceptFor = value; }
226 get {
return _onlyFor ?? String.Empty; }
227 internal set { _onlyFor = value; }
235 get {
return _comment ?? String.Empty; }
236 internal set { _comment = value; }
240 public string ToString(
string format, IFormatProvider provider)
257 if (provider != null)
259 ICustomFormatter fmt = provider.GetFormat(this.GetType()) as ICustomFormatter;
261 return fmt.Format(format,
this, provider);
264 if (String.IsNullOrEmpty(format))
267 switch (format.ToUpperInvariant())
271 string kind = $
"{((Kind == LockKind.all) ? "promotions
to and
from" : "promotions
" + Kind)}";
272 string howfor = String.Format(
"{0}",
274 "for all" : !String.IsNullOrEmpty(
ExceptFor) ?
276 return $
"Lock {kind} {Name} {howfor}. {Comment}";
279 return Kind.ToString();
283 return Type.ToString();
291 throw new FormatException($
"The {format} format string is not supported.");
296 public string ToString(
string format)
316 #region class variables
317 [NonSerialized]
private readonly
object _locker =
new object();
320 #region object construction:
378 if (r != null && r.
RetVal == 0)
380 XElement xml = XElement.Parse(r.
CmdResult);
381 IEnumerable<XElement> query = null;
383 query =
from e in xml.Elements(
"Element")
386 query =
from e in xml.Elements(
"Element")
387 join
AcStream s in
depot.Streams on (
string)e.Attribute(
"Name") equals s.Name
395 AcDebug.
Log($
"AcUtilsException caught and logged in AcLocks.initAsync(AcDepot){Environment.NewLine}{ecx.Message}");
398 catch (Exception ecx)
400 AcDebug.
Log($
"Exception caught and logged in AcLocks.initAsync(AcDepot){Environment.NewLine}{ecx.Message}");
420 if (!(await dlist.
initAsync(depots).ConfigureAwait(
false)))
return false;
426 if (r != null && r.
RetVal == 0)
429 XElement xml = XElement.Parse(r.
CmdResult);
430 for (
int ii = 0; ii < dlist.Count && result; ii++)
433 IEnumerable<XElement> query =
from e in xml.Elements(
"Element")
434 join
AcStream s in depot.
Streams on (
string)e.Attribute(
"Name") equals s.Name
445 AcDebug.
Log($
"AcUtilsException caught and logged in AcLocks.initAsync(DepotsCollection){Environment.NewLine}{ecx.Message}");
448 catch (Exception ecx)
450 AcDebug.
Log($
"Exception caught and logged in AcLocks.initAsync(DepotsCollection){Environment.NewLine}{ecx.Message}");
473 if (r != null && r.
RetVal == 0)
475 XElement xml = XElement.Parse(r.
CmdResult);
476 IEnumerable<XElement> query =
from e in xml.Elements(
"Element")
477 where streams.OfType<
StreamElement>().Any(se => se.Stream == (
string)e.Attribute(
"Name"))
485 AcDebug.
Log($
"AcUtilsException caught and logged in AcLocks.initAsync(StreamsCollection){Environment.NewLine}{ecx.Message}");
488 catch (Exception ecx)
490 AcDebug.
Log($
"Exception caught and logged in AcLocks.initAsync(StreamsCollection){Environment.NewLine}{ecx.Message}");
512 foreach (XElement e
in query)
517 string kind = (string)e.Attribute(
"kind");
519 lk.
Name = (string)e.Attribute(
"Name");
520 string type = (string)e.Attribute(
"userType") ?? String.Empty;
523 lk.
ExceptFor = (string)e.Attribute(
"exceptFor") ?? String.Empty;
524 lk.
OnlyFor = (string)e.Attribute(
"onlyFor") ?? String.Empty;
525 lk.
Comment = (string)e.Attribute(
"comment") ?? String.Empty;
526 lock (_locker) { Add(lk); }
532 catch (Exception ecx)
534 AcDebug.
Log($
"Exception caught and logged in AcLocks.initList{Environment.NewLine}{ecx.Message}");
547 AcLock lk = this.FirstOrDefault(n => n.Name == stream);
572 cmd = $
@"lock -c ""{comment}"" -kf {((onlyexcept == OnlyExcept.Except) ? "-e
" : "-o
")} ""{prncpl}"" ""{stream}""";
574 cmd = $
@"lock -c ""{comment}"" -kf ""{stream}""";
577 cmd = $
@"lock -c ""{comment}"" -kt {((onlyexcept == OnlyExcept.Except) ? "-e
" : "-o
")} ""{prncpl}"" ""{stream}""";
579 cmd = $
@"lock -c ""{comment}"" -kt ""{stream}""";
581 cmd = $
@"lock -c ""{comment}"" ""{stream}""";
584 ret = (r != null && r.
RetVal == 0);
589 AcDebug.
Log($
"AcUtilsException in AcLocks.lockAsync caught and logged.{Environment.NewLine}{ecx.Message}");
611 cmd = $
@"unlock -kf ""{stream}""";
613 cmd = $
@"unlock -kt ""{stream}""";
615 cmd = $
@"unlock ""{stream}""";
618 ret = (r != null && r.
RetVal == 0);
623 AcDebug.
Log($
"AcUtilsException in AcLocks.unlockAsync caught and logged.{Environment.NewLine}{ecx.Message}");
PrinType Type
Whether the lock is for a user or group.
OnlyExcept
Lock applies to the principal only or to all except the principal.
AccuRev program return value and command result.
A container of AcLock objects that define the AccuRev locks that prevent certain users from making ch...
override bool Equals(object other)
Overridden to determine equality.
string OnlyFor
Lock applies only to this principal.
bool initList(IEnumerable< XElement > query)
Helper function that populates this container with AcLock objects as per query sent by an initAsync m...
The list of AccuRev depots from
.exe.config.
async Task< bool > initAsync(StreamsCollection streams)
Populate this container with AcLock objects on streams.
A lock object that defines the attributes of an AccuRev lock: The stream name the lock is on and mann...
AcLock()
Constructor used during AcLocks list construction. It is called internally and not by user code...
IEnumerable< AcStream > Streams
The list of streams in this depot.
AcLocks()
A container of AcLock objects that define the AccuRev locks that prevent certain users from making ch...
async Task< bool > unlockAsync(string stream, LockKind kind=LockKind.all)
Remove lock kind on stream.
async Task< bool > initAsync(DepotsCollection depotsCol=null, IProgress< int > progress=null)
Populate this container with AcDepot objects as per constructor parameters.
Contains the AccuRev principal attributes name, ID and status (active or inactive) for users and grou...
A depot object that defines the attributes of an AccuRev depot.
async Task< bool > initAsync(AcDepot depot=null)
Populate this container with AcLock objects on streams in depot or all AcLock objects in the reposito...
async Task< bool > lockAsync(string stream, string comment, LockKind kind=LockKind.all, AcPrincipal prncpl=null, OnlyExcept onlyexcept=OnlyExcept.Except)
Put a lock on stream as per lock kind.
async Task< bool > initAsync(DepotsCollection depots)
Populate this container with AcLock objects on streams in depots.
PrinType
Whether the lock is for a user or a group.
A stream object that defines the attributes of an AccuRev stream. AcStream objects are instantiated d...
override int GetHashCode()
Override appropriate for type AcLock.
string CmdResult
The command result (usually XML) emitted by AccuRev.
int RetVal
The AccuRev program return value for the command, otherwise minus one (-1) on error.
static void Log(string message, bool formatting=true)
Write the message text to STDOUT, to weekly log files located in %LOCALAPPDATA%\AcTools\Logs, and to trigger.log in the AccuRev server's ..storage\site_slice\logs folder in the case of triggers.
LockKind Kind
Kind of AccuRev lock: from, to, or all.
Exception thrown when an AccuRev command fails. The AccuRev program return value is zero (0) on succe...
AccuRev command processing.
An AccuRev stream from the Streams section in
.exe.config.
static async Task< AcResult > runAsync(string command, ICmdValidate validator=null)
Run the AccuRev command asynchronously with non-blocking I/O.
Use to log and display error and general purpose text messages, and to save the XML param data sent b...
bool hasLock(string stream)
Determine if stream has a lock on it.
A container of AcDepot objects that define AccuRev depots in the repository.
LockKind
The kind of AccuRev lock.
string ExceptFor
Lock applies to all except this principal.
bool Equals(AcLock other)
IEquatable implementation to determine the equality of instances of type AcLock. Uses stream name the...
string ToString(string format, IFormatProvider provider)
The ToString implementation.
string Name
Name of stream the lock applies to.
The list of AccuRev streams from
.exe.config.
int CompareTo(AcLock other)
Generic IComparable implementation (default) for comparing AcLock objects to sort by LockKind (descen...
string Comment
Comment given to the lock.