PageRenderTime 89ms CodeModel.GetById 31ms RepoModel.GetById 2ms app.codeStats 0ms

/NuGenCase/UseCaseMakerLibrary/Package.cs

https://github.com/AnthonyNystrom/GenXSource
C# | 551 lines | 503 code | 47 blank | 1 comment | 30 complexity | a243991fff8995fc6f0de244b32fdef2 MD5 | raw file
  1. using System;
  2. using System.Collections.Specialized;
  3. namespace UseCaseMakerLibrary
  4. {
  5. public class Package : IdentificableObject
  6. {
  7. #region Class Members
  8. private readonly Actors actors = null;
  9. private readonly Packages packages = null;
  10. private readonly UseCases useCases = null;
  11. private CommonAttributes commonAttributes = new CommonAttributes();
  12. private Requirements requirements = new Requirements();
  13. #endregion
  14. #region Constructors
  15. internal Package()
  16. : base()
  17. {
  18. this.actors = new Actors(this);
  19. this.packages = new Packages(this);
  20. this.useCases = new UseCases(this);
  21. }
  22. internal Package(String name, String prefix, Int32 id)
  23. : base(name,prefix,id)
  24. {
  25. this.actors = new Actors(this);
  26. this.packages = new Packages(this);
  27. this.useCases = new UseCases(this);
  28. }
  29. internal Package(String name, String prefix, Int32 id, Package owner)
  30. : base(name,prefix,id,owner)
  31. {
  32. this.actors = new Actors(this);
  33. this.packages = new Packages(this);
  34. this.useCases = new UseCases(this);
  35. }
  36. #endregion
  37. #region Public Methods
  38. #region Actors Handling
  39. public Actor NewActor(String name, String prefix, Int32 id)
  40. {
  41. Actor actor = new Actor(name,prefix,id,this);
  42. return actor;
  43. }
  44. public void AddActor(Actor actor)
  45. {
  46. ValidateActor(actor);
  47. actor.Owner = this;
  48. Actors.Add(actor);
  49. }
  50. public void RemoveActor(
  51. Actor actor,
  52. String oldNameStartTag,
  53. String oldNameEndTag,
  54. String newNameStartTag,
  55. String newNameEndTag,
  56. Boolean dontMarkOccurrences)
  57. {
  58. ValidateActor(actor);
  59. this.PurgeReferences(
  60. actor,
  61. null,
  62. oldNameStartTag,
  63. oldNameEndTag,
  64. newNameStartTag,
  65. newNameEndTag,
  66. dontMarkOccurrences);
  67. Actors.Remove(actor);
  68. }
  69. public Actor GetActor(String uniqueID)
  70. {
  71. return (Actor)Actors.FindByUniqueID(uniqueID);
  72. }
  73. public String [] GetActorNames()
  74. {
  75. StringCollection sc = new StringCollection();
  76. this.RecursiveGetActorNameList(sc);
  77. String [] actorNames = new String[sc.Count];
  78. sc.CopyTo(actorNames,0);
  79. return actorNames;
  80. }
  81. #endregion // Actors Handling
  82. #region Packages Handling
  83. public Package NewPackage(String name, String prefix, Int32 id)
  84. {
  85. Package package = new Package(name,prefix,id,this);
  86. return package;
  87. }
  88. public void AddPackage(Package package)
  89. {
  90. package.Owner = this;
  91. Packages.Add(package);
  92. }
  93. public void RemovePackage(
  94. Package package,
  95. String oldNameStartTag,
  96. String oldNameEndTag,
  97. String newNameStartTag,
  98. String newNameEndTag,
  99. Boolean dontMarkOccurrences)
  100. {
  101. this.PurgeReferences(
  102. package,
  103. null,
  104. oldNameStartTag,
  105. oldNameEndTag,
  106. newNameStartTag,
  107. newNameEndTag,
  108. dontMarkOccurrences);
  109. Packages.Remove(package);
  110. }
  111. public Package GetPackage(String uniqueID)
  112. {
  113. return (Package)Packages.FindByUniqueID(uniqueID);
  114. }
  115. #endregion // Packages Handling
  116. #region UseCases Handling
  117. public UseCase NewUseCase(String name, String prefix, Int32 id)
  118. {
  119. UseCase useCase = new UseCase(name,prefix,id,this);
  120. return useCase;
  121. }
  122. public void AddUseCase(UseCase useCase)
  123. {
  124. useCase.Owner = this;
  125. UseCases.Add(useCase);
  126. }
  127. public void RemoveUseCase(
  128. UseCase useCase,
  129. String oldNameStartTag,
  130. String oldNameEndTag,
  131. String newNameStartTag,
  132. String newNameEndTag,
  133. Boolean dontMarkOccurrences)
  134. {
  135. this.PurgeReferences(
  136. useCase,
  137. null,
  138. oldNameStartTag,
  139. oldNameEndTag,
  140. newNameStartTag,
  141. newNameEndTag,
  142. dontMarkOccurrences);
  143. UseCases.Remove(useCase);
  144. }
  145. public UseCase GetUseCase(String uniqueID)
  146. {
  147. return (UseCase)UseCases.FindByUniqueID(uniqueID);
  148. }
  149. #endregion // UseCase Handling
  150. #region Requirements Handling
  151. public Int32 AddRequrement()
  152. {
  153. Requirement requirement = new Requirement();
  154. Int32 index = this.requirements.Count;
  155. Int32 ret;
  156. if(index == 0)
  157. {
  158. requirement.ID = 1;
  159. }
  160. else
  161. {
  162. requirement.ID = ((Requirement)this.requirements[index - 1]).ID + 1;
  163. }
  164. ret = this.requirements.Add(requirement);
  165. return ret;
  166. }
  167. public void RemoveRequirement(Requirement requirement)
  168. {
  169. foreach(Requirement tmpRequirement in this.requirements)
  170. {
  171. if(tmpRequirement.ID > requirement.ID)
  172. {
  173. tmpRequirement.ID -= 1;
  174. }
  175. }
  176. this.requirements.Remove(requirement);
  177. }
  178. public Requirement FindRequirementByUniqueID(String uniqueID)
  179. {
  180. Requirement requirement = null;
  181. foreach(Requirement tmpRequirement in this.requirements)
  182. {
  183. if(tmpRequirement.UniqueID == uniqueID)
  184. {
  185. requirement = tmpRequirement;
  186. }
  187. }
  188. return requirement;
  189. }
  190. #endregion
  191. #region Common Handling
  192. public void ReplaceElementName(
  193. String oldName,
  194. String oldNameStartTag,
  195. String oldNameEndTag,
  196. String newName,
  197. String newNameStartTag,
  198. String newNameEndTag)
  199. {
  200. this.ChangeReferences(
  201. oldName,
  202. oldNameStartTag,
  203. oldNameEndTag,
  204. newName,
  205. newNameStartTag,
  206. newNameEndTag,
  207. true);
  208. }
  209. public void ReplaceElementPath(
  210. String oldPath,
  211. String oldPathStartTag,
  212. String oldPathEndTag,
  213. String newPath,
  214. String newPathStartTag,
  215. String newPathEndTag)
  216. {
  217. this.ChangeReferences(
  218. oldPath,
  219. oldPathStartTag,
  220. oldPathEndTag,
  221. newPath,
  222. newPathStartTag,
  223. newPathEndTag,
  224. true);
  225. }
  226. #endregion
  227. #endregion
  228. #region Public Properties
  229. public Actors Actors
  230. {
  231. get
  232. {
  233. return this.actors;
  234. }
  235. }
  236. public Packages Packages
  237. {
  238. get
  239. {
  240. return this.packages;
  241. }
  242. }
  243. public UseCases UseCases
  244. {
  245. get
  246. {
  247. return this.useCases;
  248. }
  249. }
  250. public Requirements Requirements
  251. {
  252. get
  253. {
  254. return this.requirements;
  255. }
  256. }
  257. public CommonAttributes Attributes
  258. {
  259. get
  260. {
  261. return this.commonAttributes;
  262. }
  263. }
  264. #endregion
  265. #region Private Methods
  266. void ValidateActor(Actor actor)
  267. {
  268. if(actor == null)
  269. {
  270. throw new NullReferenceException("Actor cannot be null");
  271. }
  272. if(actor.ID == -1)
  273. {
  274. throw new InvalidOperationException("Actor must have a valid ID");
  275. }
  276. }
  277. void RecursiveGetActorNameList(StringCollection sc)
  278. {
  279. foreach(Actor actor in this.actors)
  280. {
  281. sc.Add(actor.Name);
  282. }
  283. foreach(Package subPackage in this.packages)
  284. {
  285. subPackage.RecursiveGetActorNameList(sc);
  286. }
  287. }
  288. #endregion
  289. #region Internal Methods
  290. internal void ChangeReferences(
  291. String oldName,
  292. String oldNameStartTag,
  293. String oldNameEndTag,
  294. String newName,
  295. String newNameStartTag,
  296. String newNameEndTag,
  297. Boolean deep)
  298. {
  299. String oldFullName = oldNameStartTag + oldName + oldNameEndTag;
  300. String newFullName = newNameStartTag + newName + newNameEndTag;
  301. foreach(Requirement requirement in this.requirements)
  302. {
  303. requirement.Description = requirement.Description.Replace(oldFullName,newFullName);
  304. }
  305. foreach(UseCase useCase in this.UseCases)
  306. {
  307. foreach(OpenIssue openIssue in useCase.OpenIssues)
  308. {
  309. openIssue.Description = openIssue.Description.Replace(oldFullName,newFullName);
  310. }
  311. foreach(Step step in useCase.Steps)
  312. {
  313. step.Description = step.Description.Replace(oldFullName,newFullName);
  314. }
  315. if(useCase.Prose != null)
  316. {
  317. useCase.Prose = useCase.Prose.Replace(oldFullName,newFullName);
  318. }
  319. }
  320. if(deep)
  321. {
  322. foreach(Package package in this.Packages)
  323. {
  324. package.ChangeReferences(
  325. oldName,
  326. oldNameStartTag,
  327. oldNameEndTag,
  328. newName,
  329. newNameStartTag,
  330. oldNameEndTag,
  331. deep);
  332. }
  333. }
  334. }
  335. internal void PurgeReferences(
  336. object element,
  337. Package targetPackage,
  338. String oldNameStartTag,
  339. String oldNameEndTag,
  340. String newNameStartTag,
  341. String newNameEndTag,
  342. Boolean dontMark)
  343. {
  344. Package currentPackage = this;
  345. if(targetPackage == null)
  346. {
  347. while(currentPackage.Owner != null)
  348. {
  349. currentPackage = currentPackage.Owner;
  350. }
  351. }
  352. if(element.GetType() == typeof(Package))
  353. {
  354. foreach(Actor actor in ((Package)element).Actors)
  355. {
  356. currentPackage.PurgeReferences(
  357. actor,
  358. null,
  359. oldNameStartTag,
  360. oldNameEndTag,
  361. newNameStartTag,
  362. newNameEndTag,
  363. dontMark);
  364. }
  365. foreach(UseCase useCase in ((Package)element).UseCases)
  366. {
  367. currentPackage.PurgeReferences(
  368. useCase,
  369. null,
  370. oldNameStartTag,
  371. oldNameEndTag,
  372. newNameStartTag,
  373. newNameEndTag,
  374. dontMark);
  375. }
  376. foreach(Package package in ((Package)element).Packages)
  377. {
  378. currentPackage.PurgeReferences(
  379. package,
  380. null,
  381. oldNameStartTag,
  382. oldNameEndTag,
  383. newNameStartTag,
  384. newNameEndTag,
  385. dontMark);
  386. }
  387. if(!dontMark)
  388. {
  389. this.ChangeReferences(
  390. ((Package)element).Name,
  391. oldNameStartTag,
  392. oldNameEndTag,
  393. ((Package)element).Name,
  394. newNameStartTag,
  395. newNameEndTag,
  396. false);
  397. this.ChangeReferences(
  398. ((Package)element).Path,
  399. oldNameStartTag,
  400. oldNameEndTag,
  401. ((Package)element).Path,
  402. newNameStartTag,
  403. newNameEndTag,
  404. false);
  405. }
  406. }
  407. if(element.GetType() == typeof(UseCase))
  408. {
  409. foreach(Package package in this.Packages)
  410. {
  411. package.PurgeReferences(
  412. element,
  413. null,
  414. oldNameStartTag,
  415. oldNameEndTag,
  416. newNameStartTag,
  417. newNameEndTag,
  418. dontMark);
  419. }
  420. if(!dontMark)
  421. {
  422. this.ChangeReferences(
  423. ((UseCase)element).Name,
  424. oldNameStartTag,
  425. oldNameEndTag,
  426. ((UseCase)element).Name,
  427. newNameStartTag,
  428. newNameEndTag,
  429. false);
  430. this.ChangeReferences(
  431. ((UseCase)element).Path,
  432. oldNameStartTag,
  433. oldNameEndTag,
  434. ((UseCase)element).Path,
  435. newNameStartTag,
  436. newNameEndTag,
  437. false);
  438. }
  439. // remove use case references in use case's steps
  440. foreach(UseCase useCase in this.UseCases)
  441. {
  442. foreach(Step step in useCase.Steps)
  443. {
  444. if(step.Dependency.PartnerUniqueID == ((UseCase)element).UniqueID)
  445. {
  446. step.Dependency.Type = DependencyItem.ReferenceType.None;
  447. step.Dependency.PartnerUniqueID = "";
  448. step.Dependency.Stereotype = "";
  449. }
  450. }
  451. }
  452. }
  453. if(element.GetType() == typeof(Actor))
  454. {
  455. foreach(UseCase useCase in this.UseCases)
  456. {
  457. ActiveActor tmpAActor = null;
  458. foreach(ActiveActor aactor in useCase.ActiveActors)
  459. {
  460. if(aactor.ActorUniqueID == ((Actor)element).UniqueID)
  461. {
  462. tmpAActor = aactor;
  463. }
  464. }
  465. if(tmpAActor != null)
  466. {
  467. useCase.ActiveActors.Remove(tmpAActor);
  468. }
  469. }
  470. foreach(Package package in this.Packages)
  471. {
  472. package.PurgeReferences(
  473. element,
  474. null,
  475. oldNameStartTag,
  476. oldNameEndTag,
  477. newNameStartTag,
  478. newNameEndTag,
  479. dontMark);
  480. }
  481. if(!dontMark)
  482. {
  483. this.ChangeReferences(
  484. ((Actor)element).Name,
  485. oldNameStartTag,
  486. oldNameEndTag,
  487. ((Actor)element).Name,
  488. newNameStartTag,
  489. newNameEndTag,
  490. false);
  491. this.ChangeReferences(
  492. ((Actor)element).Path,
  493. oldNameStartTag,
  494. oldNameEndTag,
  495. ((Actor)element).Path,
  496. newNameStartTag,
  497. newNameEndTag,
  498. false);
  499. }
  500. }
  501. }
  502. #endregion
  503. }
  504. }