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.

176 lines
3.9 KiB

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