Skip to content

Commit

Permalink
3_06 applied
Browse files Browse the repository at this point in the history
  • Loading branch information
jet_vorobev committed Sep 26, 2016
1 parent a7aeb59 commit acbbc2d
Show file tree
Hide file tree
Showing 21 changed files with 422 additions and 136 deletions.
3 changes: 3 additions & 0 deletions config/setenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# run tomcat with JMX ability as admin
# for remote connection add -Djava.rmi.server.hostname=TomcatServer_IP
export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
7 changes: 6 additions & 1 deletion src/main/java/ru/javawebinar/topjava/AuthorizedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
* 06.03.2015.
*/
public class AuthorizedUser {
public static int id = 1;

public static int id() {
return 1;
return id;
}

public static void setId(int id) {
AuthorizedUser.id = id;
}

public static int getCaloriesPerDay() {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/ru/javawebinar/topjava/SpringMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ru.javawebinar.topjava.model.Role;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.to.MealWithExceed;
import ru.javawebinar.topjava.web.meal.MealRestController;
import ru.javawebinar.topjava.web.user.AdminRestController;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.util.Arrays;
import java.util.List;

/**
* User: gkislin
Expand All @@ -19,6 +25,14 @@ public static void main(String[] args) {
System.out.println("Bean definition names: " + Arrays.toString(appCtx.getBeanDefinitionNames()));
AdminRestController adminUserController = appCtx.getBean(AdminRestController.class);
adminUserController.create(new User(1, "userName", "email", "password", Role.ROLE_ADMIN));
System.out.println();

MealRestController mealController = appCtx.getBean(MealRestController.class);
List<MealWithExceed> filteredMealsWithExceeded =
mealController.getBetween(
LocalDate.of(2015, Month.MAY, 30), LocalTime.of(7, 0),
LocalDate.of(2015, Month.MAY, 31), LocalTime.of(11, 0));
filteredMealsWithExceeded.forEach(System.out::println);
}
}
}
16 changes: 2 additions & 14 deletions src/main/java/ru/javawebinar/topjava/model/Meal.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* GKislin
* 11.01.2015.
*/
public class Meal {
public class Meal extends BaseEntity {
private Integer id;

private final LocalDateTime dateTime;
Expand All @@ -22,20 +22,12 @@ public Meal(LocalDateTime dateTime, String description, int calories) {
}

public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
this.id = id;
super(id);
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public LocalDateTime getDateTime() {
return dateTime;
}
Expand All @@ -56,10 +48,6 @@ public LocalTime getTime() {
return dateTime.toLocalTime();
}

public boolean isNew() {
return id == null;
}

@Override
public String toString() {
return "Meal{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

import ru.javawebinar.topjava.model.Meal;

import java.time.LocalDateTime;
import java.util.Collection;

/**
* GKislin
* 06.03.2015.
*/
public interface MealRepository {
Meal save(Meal Meal);
// null if updated meal do not belong to userId
Meal save(Meal Meal, int userId);

void delete(int id);
// false if meal do not belong to userId
boolean delete(int id, int userId);

Meal get(int id);
// null if meal do not belong to userId
Meal get(int id, int userId);

Collection<Meal> getAll();
// ORDERED dateTime
Collection<Meal> getAll(int userId);

// ORDERED dateTime
Collection<Meal> getBetween(LocalDateTime startDate, LocalDateTime endDate, int userId);
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,84 @@
package ru.javawebinar.topjava.repository.mock;

import org.springframework.stereotype.Repository;
import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.repository.MealRepository;
import ru.javawebinar.topjava.util.MealsUtil;
import ru.javawebinar.topjava.util.TimeUtil;

import java.util.Collection;
import java.util.Map;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static ru.javawebinar.topjava.repository.mock.InMemoryUserRepositoryImpl.ADMIN_ID;
import static ru.javawebinar.topjava.repository.mock.InMemoryUserRepositoryImpl.USER_ID;

/**
* GKislin
* 15.09.2015.
*/
@Repository
public class InMemoryMealRepositoryImpl implements MealRepository {
private Map<Integer, Meal> repository = new ConcurrentHashMap<>();

private static final Comparator<Meal> MEAL_COMPARATOR = Comparator.comparing(Meal::getDateTime).reversed();

// Map userId -> (mealId-> meal)
private Map<Integer, Map<Integer, Meal>> repository = new ConcurrentHashMap<>();
private AtomicInteger counter = new AtomicInteger(0);

{
MealsUtil.MEALS.forEach(this::save);
MealsUtil.MEALS.forEach(um -> save(um, USER_ID));

save(new Meal(LocalDateTime.of(2015, Month.JUNE, 1, 14, 0), "Админ ланч", 510), ADMIN_ID);
save(new Meal(LocalDateTime.of(2015, Month.JUNE, 1, 21, 0), "Админ ужин", 1500), ADMIN_ID);
}

@Override
public Meal save(Meal meal) {
public Meal save(Meal meal, int userId) {
Objects.requireNonNull(meal);

Integer mealId = meal.getId();
if (meal.isNew()) {
meal.setId(counter.incrementAndGet());
mealId = counter.incrementAndGet();
meal.setId(mealId);
} else if (get(mealId, userId) == null) {
return null;
}
repository.put(meal.getId(), meal);
Map<Integer, Meal> meals = repository.computeIfAbsent(userId, ConcurrentHashMap::new);
meals.put(mealId, meal);
return meal;
}

@Override
public void delete(int id) {
repository.remove(id);
public boolean delete(int id, int userId) {
Map<Integer, Meal> meals = repository.get(userId);
return meals != null && meals.remove(id) != null;
}

@Override
public Meal get(int id, int userId) {
Map<Integer, Meal> meals = repository.get(userId);
return meals == null ? null : meals.get(id);
}

@Override
public Meal get(int id) {
return repository.get(id);
public Collection<Meal> getAll(int userId) {
Map<Integer, Meal> meals = repository.get(userId);
return meals == null ?
Collections.emptyList() :
meals.values().stream().sorted(MEAL_COMPARATOR).collect(Collectors.toList());
}

@Override
public Collection<Meal> getAll() {
return repository.values();
public Collection<Meal> getBetween(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
Objects.requireNonNull(startDateTime);
Objects.requireNonNull(endDateTime);
return getAll(userId).stream()
.filter(um -> TimeUtil.isBetween(um.getDateTime(), startDateTime, endDateTime))
.collect(Collectors.toList());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ru.javawebinar.topjava.repository.mock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.repository.UserRepository;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
* GKislin
* 15.06.2015.
*/
@Repository
public class InMemoryUserRepositoryImpl implements UserRepository {
private static final Logger LOG = LoggerFactory.getLogger(InMemoryUserRepositoryImpl.class);

private Map<Integer, User> repository = new ConcurrentHashMap<>();
private AtomicInteger counter = new AtomicInteger(0);

private static final Comparator<User> USER_COMPARATOR = Comparator.comparing(User::getName);

public static final int USER_ID = 1;
public static final int ADMIN_ID = 2;

@Override
public User save(User user) {
Objects.requireNonNull(user);
if (user.isNew()) {
user.setId(counter.incrementAndGet());
}
repository.put(user.getId(), user);
return user;
}

@PostConstruct
public void postConstruct() {
LOG.info("+++ PostConstruct");
}

@PreDestroy
public void preDestroy() {
LOG.info("+++ PreDestroy");
}

@Override
public boolean delete(int id) {
return repository.remove(id) != null;
}

@Override
public User get(int id) {
return repository.get(id);
}

@Override
public List<User> getAll() {
return repository.values().stream()
.sorted(USER_COMPARATOR)
.collect(Collectors.toList());
}

@Override
public User getByEmail(String email) {
Objects.requireNonNull(email);
return repository.values().stream()
.filter(u -> email.equals(u.getEmail()))
.findFirst()
.orElse(null);
}
}

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/ru/javawebinar/topjava/service/MealService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
package ru.javawebinar.topjava.service;

import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.util.exception.NotFoundException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collection;

/**
* GKislin
* 15.06.2015.
*/
public interface MealService {
Meal get(int id, int userId) throws NotFoundException;

void delete(int id, int userId) throws NotFoundException;

default Collection<Meal> getBetweenDates(LocalDate startDate, LocalDate endDate, int userId) {
return getBetweenDateTimes(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX), userId);
}

Collection<Meal> getBetweenDateTimes(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId);

Collection<Meal> getAll(int userId);

Meal update(Meal meal, int userId) throws NotFoundException;

Meal save(Meal meal, int userId);
}
Loading

0 comments on commit acbbc2d

Please sign in to comment.