Skip to content

Commit

Permalink
Merge pull request #48 from jeffreybakker/feature/extension_7
Browse files Browse the repository at this point in the history
Feature/extension 7
  • Loading branch information
jeffreybakker authored Aug 6, 2017
2 parents ea3b9a2 + e5473ec commit 7db25cf
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import honours.ing.banq.card.CardUtil;
import honours.ing.banq.customer.Customer;
import honours.ing.banq.customer.CustomerRepository;
import honours.ing.banq.log.LogService;
import honours.ing.banq.util.IBANUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public NewAccountBean openAccount(String name, String surname, String initials,
String dob, String ssn,
String address, String telephoneNumber,
String email, String username, String password) throws InvalidParamValueError {
Customer existing = customerRepository.findBySsn(ssn);
if (existing != null) {
throw new InvalidParamValueError(
"Could not create customer because the given information was invalid");
}

Customer customer = new Customer(
name, surname, initials,
dob, ssn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface CustomerRepository extends JpaRepository<Customer, Integer> {
Customer findByUsername(String username);
Customer findByUsernameAndPassword(String username, String password);

Customer findBySsn(String ssn);

}
105 changes: 105 additions & 0 deletions src/main/java/honours/ing/banq/log/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package honours.ing.banq.log;

import honours.ing.banq.time.TimeServiceImpl;

import javax.persistence.*;

/**
* The model for a log message that is saved in the database.
* @author Jeffrey Bakker
* @since 6-8-17
*/
@Entity
public class Log {

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;

private long timeStamp;

@Column(columnDefinition = "TEXT")
private String eventLog;

/**
* @deprecated empty constructor for Hibernate ORM
*/
@Deprecated
public Log() { }

/**
* Creates a new {@link Log} object with the given parameters.
* @param message the message for the log
*/
public Log(String message) {
this(TimeServiceImpl.currentTimeMillis(), message);
}

/**
* Creates a new {@link Log} object with the given parameters.
* @param timeStamp the timeStamp for the log
* @param message the message for the log
*/
public Log(long timeStamp, String message) {
this.timeStamp = timeStamp;
this.eventLog = message;
}

/**
* Returns the {@link Log}'s ID.
* @return the id
*/
public Long getId() {
return id;
}

/**
* Returns the time stamp in milliseconds.
* @return the time stamp
*/
public long getTimeStamp() {
return timeStamp;
}

/**
* Sets the time for the time stamp.
* @param timeStamp the time in milliseconds
*/
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}

/**
* Returns the message for the log.
* @return the message
*/
public String getMessage() {
return eventLog;
}

/**
* Sets the message for the log.
* @param message the message
*/
public void setMessage(String message) {
this.eventLog = message;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Log log = (Log) o;

if (timeStamp != log.timeStamp) return false;
return id != null ? id.equals(log.id) : log.id == null;
}

@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (int) (timeStamp ^ (timeStamp >>> 32));
return result;
}
}
15 changes: 15 additions & 0 deletions src/main/java/honours/ing/banq/log/LogIgnore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package honours.ing.banq.log;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author jeffrey
* @since 6-8-17
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogIgnore {
}
77 changes: 77 additions & 0 deletions src/main/java/honours/ing/banq/log/LogInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package honours.ing.banq.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @author jeffrey
* @since 6-8-17
*/
@Aspect
@Component
public class LogInterceptor implements ApplicationContextAware {

private ApplicationContext context;
private LogService log;

private final ThreadLocal<AtomicBoolean> running;

@Autowired
public LogInterceptor(LogService logService) {
this.log = logService;

running = ThreadLocal.withInitial(() -> new AtomicBoolean(false));
}

@Around("within(honours.ing..*) && @annotation(org.springframework.transaction.annotation.Transactional)")
public Object intercept(ProceedingJoinPoint pjp) throws Throwable {
boolean mayLog = false;

MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
LogIgnore annotation = method.getAnnotation(LogIgnore.class);

if (annotation == null) {
synchronized (running) {
if (!running.get().get()) {
running.get().set(true);
mayLog = true;
}
}
}

try {
Object res = pjp.proceed();

if (mayLog) {
log.log(String.format("%s%s", method.getName(), Arrays.deepToString(pjp.getArgs())));
}

return res;
} catch (Throwable t) {
if (mayLog) {
log.log(String.format("[%s] %s%s", t.getClass().getSimpleName(), method.getName(), Arrays.deepToString(pjp.getArgs())));
}
throw t;
} finally {
if (mayLog) {
running.get().set(false);
}
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

}
}
15 changes: 15 additions & 0 deletions src/main/java/honours/ing/banq/log/LogRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package honours.ing.banq.log;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
* @author jeffrey
* @since 6-8-17
*/
public interface LogRepository extends JpaRepository<Log, Long> {

List<Log> findLogsByTimeStampBetween(long a, long b);

}
27 changes: 27 additions & 0 deletions src/main/java/honours/ing/banq/log/LogService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package honours.ing.banq.log;

import com.googlecode.jsonrpc4j.JsonRpcParam;
import com.googlecode.jsonrpc4j.JsonRpcService;
import honours.ing.banq.log.bean.LogBean;

import java.util.List;

/**
* This service provides access to the log messages that have been made by the application.
* @author Jeffrey Bakker
* @since 6-8-17
*/
@JsonRpcService("/api/log")
public interface LogService {

void log(String message);

/**
* Returns all event logs from a given time period.
* @param beginDate the begin of time period
* @param endDate the end of the time period
* @return a list of all event logs made in the given time period
*/
List<LogBean> getEventLogs(@JsonRpcParam("beginDate") String beginDate, @JsonRpcParam("endDate") String endDate);

}
62 changes: 62 additions & 0 deletions src/main/java/honours/ing/banq/log/LogServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package honours.ing.banq.log;

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import honours.ing.banq.Application;
import honours.ing.banq.InvalidParamValueError;
import honours.ing.banq.log.bean.LogBean;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
* An implementation for the {@link LogService}.
* @author Jeffrey Bakker
* @since 6-8-17
*/
@Service
@AutoJsonRpcServiceImpl
@Transactional(readOnly = true)
public class LogServiceImpl implements LogService {

private static final DateFormat INPUT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

private Logger logger = Application.getLogger();

// Repositories
private LogRepository repository;

@Autowired
public LogServiceImpl(LogRepository repository) {
this.repository = repository;
}

@LogIgnore
@Transactional
@Override
public void log(String message) {
logger.info(message);

Log log = new Log(message);
repository.save(log);
}

@Override
public List<LogBean> getEventLogs(String beginDate, String endDate) {
long a, b;
try {
a = INPUT_DATE_FORMAT.parse(beginDate).getTime();
b = INPUT_DATE_FORMAT.parse(endDate).getTime();
} catch (ParseException e) {
throw new InvalidParamValueError("Either the beginDate or the endDate was in an invalid format");
}

return LogBean.generate(repository.findLogsByTimeStampBetween(a, b));
}

}
50 changes: 50 additions & 0 deletions src/main/java/honours/ing/banq/log/bean/LogBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package honours.ing.banq.log.bean;

import honours.ing.banq.log.Log;
import honours.ing.banq.log.LogService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* This is a bean that will be returned in some of the {@link LogService}'s methods
* @author Jeffrey Bakker
* @since 6-8-17
*/
public class LogBean {

private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

private String timeStamp;
private String eventLog;

public LogBean(Log log) {
this.timeStamp = DATE_FORMAT.format(new Date(log.getTimeStamp()));
this.eventLog = log.getMessage();
}

public static List<LogBean> generate(List<Log> logs) {
List<LogBean> res = new ArrayList<>();
logs.forEach((l) -> res.add(new LogBean(l)));
return res;
}

public String getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}

public String getEventLog() {
return eventLog;
}

public void setEventLog(String eventLog) {
this.eventLog = eventLog;
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.datasource.url=jdbc:mysql://localhost:3306/ing_db
spring.datasource.username=ing_project
spring.datasource.password=localhost1234
Expand Down
Loading

0 comments on commit 7db25cf

Please sign in to comment.