Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY1819S1#50 from weiqing-nic/new
Browse files Browse the repository at this point in the history
Edit Photo class
  • Loading branch information
Aadit Kamat authored Oct 16, 2018
2 parents 80fadd6 + 817a222 commit 9c7661d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 64 deletions.
25 changes: 13 additions & 12 deletions src/main/java/seedu/address/logic/commands/UploadPhotoCommand.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.transaction.Transaction;

/**
* Uploads the photo of the transaction in a transaction for record keeping purposes.
*/
public class UploadPhotoCommand extends Command {
import java.util.List;

import static java.util.Objects.requireNonNull;


public class UploadPhotoCommand extends Command{

public static final String COMMAND_WORD = "uploadphoto";
public static final String COMMAND_ALIAS = "uploadp";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": upload image to that transaction contact";

private String filePath;
private Index imageindex;
private Index photoIndex;


public UploadPhotoCommand(Index index, String path) {
Expand All @@ -31,7 +29,7 @@ public UploadPhotoCommand(Index index, String path) {
requireNonNull(index);
requireNonNull(path);

imageindex = index;
photoIndex = index;
filePath = path;
}

Expand All @@ -43,13 +41,16 @@ public CommandResult execute(Model model, CommandHistory history) throws Command

int lastPersonListIndex = lastTransactionList.size();

int thatPerson = imageindex.getZeroBased();
int thatPersonIndex = photoIndex.getZeroBased();

if (thatPerson >= lastPersonListIndex) {
if (thatPersonIndex >= lastPersonListIndex ) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

// = lastPersonList.get(thatPersonIndex);


// model.updateFilteredPersonList();

return null;
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package seedu.address.model.person;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import seedu.address.model.tag.Tag;

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import seedu.address.model.tag.Tag;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

/**
* Represents a Person in the address book.
Expand All @@ -16,13 +16,14 @@
public class Person {

// Identity fields
private final Email email;
private final Name name;
private final Phone phone;
private final Email email;

// Data fields
private final Address address;
private final Set<Tag> tags = new HashSet<>();
private Photo photo;

/**
* Parameterized constructor that takes in a UniqueId argument
Expand Down
116 changes: 67 additions & 49 deletions src/main/java/seedu/address/model/person/Photo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.Objects.requireNonNull;

/**
* Each Person should have a photo
*/

public class Photo {

public static final String DEFAULT_MESSAGE_PHOTO = "Filepath be less than 10MB and FilePath must be valid ";
public static final String DEFAULT_PHOTO = "images/default_person.png";

private static final int tenMB = 1048576;
private static final String FOLDER = getOperatingPath();
//cannot be blank space
//double space equals to one in java
private static final String PHOTO_INTITAL_REGEX_ = "[^\\s].*";
private static final int tenMB = 1048576;

private String photoPath;


public Photo(){
public Photo() {
this.photoPath = DEFAULT_PHOTO;
}

public Photo(String path){
public Photo(String path) {

requireNonNull(path);

if(checkPath(path)){
if (checkPath(path)) {
this.photoPath = path;
} else{
} else {
this.photoPath = DEFAULT_PHOTO;
}

Expand All @@ -48,10 +49,13 @@ public Photo(String filePath, String newPhoto) throws IllegalValueException {
}
//link to the path
this.photoPath = FOLDER + "//" + newPhoto;

makePhoto( filePath, newPhoto);
makePhoto(filePath, newPhoto);
}

/**
* Make a photo
*/

private void makePhoto(String filePath, String newPhoto) {

makePhotoFolder();
Expand All @@ -62,13 +66,10 @@ private void makePhoto(String filePath, String newPhoto) {

//create file object
File pictureFinal = new File(FOLDER + "//" + newPhoto);

//if cannot get file object create an empty object


//if cannot get file object create an empty object


if (!pictureFinal.exists() ) {

if (!pictureFinal.exists()) {
try {
pictureFinal.createNewFile();
} catch (IOException e) {
Expand All @@ -80,89 +81,106 @@ private void makePhoto(String filePath, String newPhoto) {
try {

Files.copy(getImage.toPath(), pictureFinal.toPath(), REPLACE_EXISTING);
this.photoPath =pictureFinal.toPath().toString();
} catch (IOException e){
this.photoPath = pictureFinal.toPath().toString();
} catch (IOException e) {
e.printStackTrace();

}

}

/**
* Make a photoFolder
*/

public void makePhotoFolder(){
public void makePhotoFolder() {

File locationFolder = new File(FOLDER);

if ( !locationFolder.exists()) {
if (!locationFolder.exists()) {
locationFolder.mkdir();
}

}

/**
* Check Operating System of User
*/



private static String getOperatingPath(){
String oSystem = System.getProperty("os.name");
private static String getOperatingPath() {
String oSystem = System.getProperty("os.name");

//mac
if (oSystem.contains("mac")){
return System.getProperty("user.home") +"/Documents/cs2103/debt-tracker/PhotoFolder";
}
//windows
else{
return System.getenv("APPDATA")+"/PhotoFolder";
if (oSystem.contains("mac")) {
return System.getProperty("user.home") + "/Documents/cs2103/debt-tracker/PhotoFolder";
} else {
return System.getenv("APPDATA") + "/PhotoFolder";
}
}

private static String getOsName(){
/**
* Get Operating System of User
*/

private static String getOsName() {
return System.getProperty("os.name");
}

/**
* Get Photo Path
*/

public String getPicturePath(){
public String getPhoto() {
return this.photoPath;
}

public static boolean checkPath(String path){
/**
* Check Photo Path of User
*/

if(path.equals(DEFAULT_PHOTO)){
public static boolean checkPath(String path) {

if (path.equals(DEFAULT_PHOTO)) {
return true;
}

if( path.matches(PHOTO_INTITAL_REGEX_) ){
return checkPicture(path);
if (path.matches(PHOTO_INTITAL_REGEX_)) {
return checkPicture(path);

}

return false;

}

public static boolean checkPicture(String path){
/**
* Photo Validation
*/

public static boolean checkPicture(String path) {

File pictureNew = new File(path);

try{
if (ImageIO.read(pictureNew) == null)
try {
if (ImageIO.read(pictureNew) == null) {
return false;
}

} catch (IOException error){
} catch (IOException error) {
return false;
}

if (pictureNew.length() > tenMB){
if (pictureNew.length() > tenMB) {
return false;
}

return true;



}





@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Photo // instanceof handles nulls
&& this.photoPath.equals(((Photo) other).photoPath));
}
}

0 comments on commit 9c7661d

Please sign in to comment.