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
202 lines
4.6 KiB
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
|
|
/**
|
|
* Credential Repository for handling user credentials
|
|
* @author Claudia Metzler
|
|
*/
|
|
|
|
public class CredentialRepository implements CredentialRepositoryInterface{
|
|
|
|
private int idCounter = 0;
|
|
private CredentialList credentials;
|
|
|
|
/**
|
|
* Konstruktor
|
|
*
|
|
* Initializes instance variable ArrayList
|
|
*/
|
|
public CredentialRepository()
|
|
{
|
|
this.credentials = new CredentialList();
|
|
}
|
|
|
|
|
|
/**
|
|
* Create a new Set of User credentials
|
|
* @param name
|
|
* @param password
|
|
* @return size of repositoy - arraylist for checking
|
|
*/
|
|
|
|
public int createNewCredential(String name, String password)
|
|
{
|
|
try {
|
|
this.credentials.add(new Credential(name, password, this.idCounter++));
|
|
}
|
|
catch(Exception fail)
|
|
{
|
|
System.err.println(fail.getMessage());
|
|
}
|
|
|
|
return this.getListSize();
|
|
}
|
|
|
|
/**
|
|
* Function to return a specific Credential via Name input
|
|
* @param needle [ aka to-search-for ]
|
|
* @return usercredential | NULL
|
|
*/
|
|
|
|
@Override
|
|
public Credential getCredentialsViaName(String needle) {
|
|
|
|
for(int c = 0; c < this.getListSize(); c++)
|
|
{
|
|
Credential credential = this.credentials.get(c);
|
|
|
|
if(credential.getName().equals(needle))
|
|
return credential;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
/**
|
|
* Function to return a specific Credential via ID
|
|
* @param id
|
|
* @return usercredential | NULL
|
|
*/
|
|
|
|
@Override
|
|
public Credential getCredentialsViaId(int id) {
|
|
|
|
try{
|
|
return this.credentials.get(id);
|
|
}
|
|
catch (IndexOutOfBoundsException outOfBoundsException)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to update userpassword, assuming we already know the userid
|
|
* @param index
|
|
* @param newPassword
|
|
*/
|
|
@Override
|
|
public void updatePassword(int index, String newPassword) {
|
|
|
|
try{
|
|
this.credentials.get(index).upDatePassword(newPassword);
|
|
}
|
|
catch (IndexOutOfBoundsException outOfBoundsException)
|
|
{
|
|
System.err.println("No Credential with Index" + index + " found");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Function to update username analoge to updatePassword
|
|
* @param index
|
|
* @param newUsername
|
|
*/
|
|
public void updateUsername(int index, String newUsername){
|
|
|
|
try{
|
|
this.credentials.get(index).updateUsername(newUsername);
|
|
}
|
|
catch (IndexOutOfBoundsException outOfBoundsException)
|
|
{
|
|
System.err.println("No Credential with Index" + index + " found");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletion function for credentials
|
|
* Will immediately delete - double check for user permission
|
|
* @param index
|
|
*/
|
|
@Override
|
|
public void delete(int index) {
|
|
|
|
this.credentials.remove(index);
|
|
|
|
}
|
|
|
|
/**
|
|
* Function for serialization of all Credentials
|
|
* @param fileName
|
|
*/
|
|
|
|
public void serializeObject(String fileName)
|
|
{
|
|
if(fileName.equals("")) return;
|
|
|
|
try {
|
|
FileOutputStream file = new FileOutputStream(this.getWorkingDirectory() + fileName + ".claud11");
|
|
ObjectOutputStream out = new ObjectOutputStream(file);
|
|
out.writeObject(this.credentials);
|
|
} catch (Exception fail) {
|
|
System.err.println("Serialization failed!");
|
|
fail.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function to load serialized Objects from hard drive by path
|
|
* @param fileName
|
|
*/
|
|
public void deserializeObjects(String fileName)
|
|
{
|
|
if(fileName.equals("")) return;
|
|
|
|
try {
|
|
FileInputStream file = new FileInputStream(this.getWorkingDirectory() + fileName + ".claud11");
|
|
ObjectInputStream in = new ObjectInputStream(file);
|
|
|
|
this.credentials = (CredentialList)in.readObject();
|
|
|
|
in.close();
|
|
file.close();
|
|
|
|
} catch(Exception fail)
|
|
{
|
|
System.err.println("Loading of CredentialRepository failed");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* helper function to check list size after insertion / deletion
|
|
* @return
|
|
*/
|
|
|
|
private int getListSize()
|
|
{
|
|
return this.credentials.size();
|
|
}
|
|
|
|
/**
|
|
* Helper function for serialization
|
|
* @return
|
|
*/
|
|
|
|
private String getWorkingDirectory()
|
|
{
|
|
return System.getProperty("user.dir");
|
|
}
|
|
|
|
public CredentialList getCredentialList() {
|
|
return credentials;
|
|
}
|
|
|
|
}
|
|
|
|
|