You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
3.9 KiB

  1. import java.io.FileOutputStream;
  2. import java.io.ObjectOutputStream;
  3. /**
  4. * Credential Repository for handling user credentials
  5. * @author Claudia Metzler
  6. */
  7. public class CredentialRepository implements CredentialRepositoryInterface{
  8. private int idCounter = 0;
  9. private CredentialList credentials;
  10. /**
  11. * Konstruktor
  12. *
  13. * Initializes instance variable ArrayList
  14. */
  15. public CredentialRepository()
  16. {
  17. this.credentials = new CredentialList();
  18. }
  19. /**
  20. * Create a new Set of User credentials
  21. * @param name
  22. * @param password
  23. * @return size of repositoy - arraylist for checking
  24. */
  25. public int createNewCredential(String name, String password)
  26. {
  27. try {
  28. this.credentials.add(new Credential(name, password, this.idCounter++));
  29. }
  30. catch(Exception fail)
  31. {
  32. System.err.println(fail.getMessage());
  33. }
  34. return this.getListSize();
  35. }
  36. /**
  37. * Function to return a specific Credential via Name input
  38. * @param needle [ aka to-search-for ]
  39. * @return usercredential | NULL
  40. */
  41. @Override
  42. public Credential getCredentialsViaName(String needle) {
  43. for(int c = 0; c < this.getListSize(); c++)
  44. {
  45. Credential credential = this.credentials.get(c);
  46. if(credential.getName().equals(needle))
  47. return credential;
  48. }
  49. return null;
  50. }
  51. /**
  52. * Function to return a specific Credential via ID
  53. * @param id
  54. * @return usercredential | NULL
  55. */
  56. @Override
  57. public Credential getCredentialsViaId(int id) {
  58. try{
  59. return this.credentials.get(id);
  60. }
  61. catch (IndexOutOfBoundsException outOfBoundsException)
  62. {
  63. return null;
  64. }
  65. }
  66. /**
  67. * Function to update userpassword, assuming we already know the userid
  68. * @param index
  69. * @param newPassword
  70. */
  71. @Override
  72. public void updatePassword(int index, String newPassword) {
  73. try{
  74. this.credentials.get(index).upDatePassword(newPassword);
  75. }
  76. catch (IndexOutOfBoundsException outOfBoundsException)
  77. {
  78. System.err.println("No Credential with Index" + index + " found");
  79. }
  80. }
  81. /**
  82. * Function to update username analoge to updatePassword
  83. * @param index
  84. * @param newUsername
  85. */
  86. public void updateUsername(int index, String newUsername){
  87. try{
  88. this.credentials.get(index).updateUsername(newUsername);
  89. }
  90. catch (IndexOutOfBoundsException outOfBoundsException)
  91. {
  92. System.err.println("No Credential with Index" + index + " found");
  93. }
  94. }
  95. /**
  96. * Deletion function for credentials
  97. * Will immediately delete - double check for user permission
  98. * @param index
  99. */
  100. @Override
  101. public void delete(int index) {
  102. this.credentials.remove(index);
  103. }
  104. /**
  105. * Function for serialization of all Credentials
  106. * @param fileName
  107. */
  108. public void serializeObject(String fileName)
  109. {
  110. if(fileName.equals("")) return;
  111. try {
  112. FileOutputStream file = new FileOutputStream(this.getWorkingDirectory() + fileName + ".claud11");
  113. ObjectOutputStream out = new ObjectOutputStream(file);
  114. out.writeObject(this.credentials);
  115. } catch (Exception fail) {
  116. System.err.println("Serialization failed!");
  117. fail.printStackTrace();
  118. }
  119. }
  120. /**
  121. * helper function to check list size after insertion / deletion
  122. * @return
  123. */
  124. private int getListSize()
  125. {
  126. return this.credentials.size();
  127. }
  128. /**
  129. * Helper function for serialization
  130. * @return
  131. */
  132. private String getWorkingDirectory()
  133. {
  134. return System.getProperty("user.dir");
  135. }
  136. }