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.

202 lines
4.6 KiB

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