forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jet_vorobev
committed
Sep 26, 2016
1 parent
a7aeb59
commit acbbc2d
Showing
21 changed files
with
422 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 49 additions & 13 deletions
62
src/main/java/ru/javawebinar/topjava/repository/mock/InMemoryMealRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
80 changes: 80 additions & 0 deletions
80
src/main/java/ru/javawebinar/topjava/repository/mock/InMemoryUserRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
49 changes: 0 additions & 49 deletions
49
src/main/java/ru/javawebinar/topjava/repository/mock/MockUserRepositoryImpl.java
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
src/main/java/ru/javawebinar/topjava/service/MealService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.