@@ -5,8 +5,13 @@
import org.springframework.stereotype.Service;
import pl.frej.waw.prediction.core.entity.Answer;
import pl.frej.waw.prediction.core.entity.Question;
import pl.frej.waw.prediction.core.entity.User;
import pl.waw.frej.prediction.persistence.database.entity.AnswerEntity;
import pl.waw.frej.prediction.persistence.database.entity.QuestionEntity;
import pl.waw.frej.prediction.persistence.database.entity.UserEntity;

import java.util.HashMap;
import java.util.Map;

@Service
public class Transformer {
@@ -26,6 +31,22 @@ public QuestionEntity getQuestionEntity(Question question) {
qE.setAnswers(Lists.newArrayList(
Iterables.transform(question.getAnswers(), this::getAnswerEntity)));
qE.setCompletionTime(question.getCompletionTime());
qE.setCompletionValue(question.getCompletionValue());
return qE;
}

public UserEntity getUserEntity(User user) {
UserEntity uE = new UserEntity();
uE.setFunds(user.getFunds());
uE.setAnswerQuantities(getAnswerEntityLongMap(user.getAnswerQuantities()));
return uE;
}

private Map<AnswerEntity, Long> getAnswerEntityLongMap(Map<Answer, Long> answerLongMap){
Map<AnswerEntity, Long> map = new HashMap<>();
for (Map.Entry<Answer, Long> entry : answerLongMap.entrySet()) {
map.put(getAnswerEntity(entry.getKey()),entry.getValue());
}
return map;
}
}
@@ -11,7 +11,8 @@
public class AnswerEntity implements Answer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue
@Column(name = "ANSWER_ID")
private Long id;


@@ -16,6 +16,7 @@ public class QuestionEntity implements Question {
private String name;
private String description;
private Date completionTime;
private Long completionValue;

@OneToMany(mappedBy = "question")
private List<AnswerEntity> answers;
@@ -64,4 +65,14 @@ public Date getCompletionTime() {
public void setCompletionTime(Date completionTime) {
this.completionTime = completionTime;
}

@Override
public Long getCompletionValue() {
return completionValue;
}

@Override
public void setCompletionValue(Long completionValue) {
this.completionValue = completionValue;
}
}
@@ -3,15 +3,13 @@
import pl.frej.waw.prediction.core.entity.Transaction;
import pl.frej.waw.prediction.core.entity.User;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.*;

@Entity
public class TransactionEntity implements Transaction {

@Id
@GeneratedValue
private Long id;

@ManyToOne @JoinColumn(name = "USER_ID") private UserEntity user;
@@ -1,9 +1,12 @@
package pl.waw.frej.prediction.persistence.database.entity;

import com.google.common.collect.Lists;
import pl.frej.waw.prediction.core.entity.Answer;
import pl.frej.waw.prediction.core.entity.Transaction;
import pl.frej.waw.prediction.core.entity.User;

import javax.persistence.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@@ -18,8 +21,11 @@ public class UserEntity implements User {
@OneToMany(mappedBy = "user")
private List<TransactionEntity> transactions;


// private Map<Long, Long> answerQuantities;
@ElementCollection
@CollectionTable(name="ANSWER_QUANTITIES")
@MapKeyJoinColumn(name="ANSWER_ID")
@Column(name="QUANTITY")
private Map<AnswerEntity, Long> answerQuantities = new HashMap<>();
private Long funds;

@Override
@@ -29,21 +35,45 @@ public Long getId() {

@Override
public List<Transaction> getTransactions() {
return null;
return Lists.newArrayList(transactions);
}

@Override
public void setTransactions(List<Transaction> transactions) {
// this.transactions = transactions;
public Map<Answer, Long> getAnswerQuantities() {
Map<Answer, Long> map = new HashMap<>();
for (Map.Entry<AnswerEntity, Long> entry : answerQuantities.entrySet()) {
map.put(entry.getKey(),entry.getValue());
}
return map;
}

public void setAnswerQuantities(Map<AnswerEntity, Long> answerQuantities){
this.answerQuantities=answerQuantities;
}

@Override
public Map<Long, Long> getAnswerQuantities() {
return null;
public void addAnswer(Answer answer, Long quantity) {
modifyAnswerQuantity(answer, quantity);
}

private void modifyAnswerQuantity(Answer answer, Long quantity) {
Long previousQuantity = answerQuantities.get(answer);
answerQuantities.put((AnswerEntity)answer, previousQuantity == null ? quantity : previousQuantity + quantity);
}

public void setAnswerQuantities(Map<Long, Long> answerQuantities) {
// this.answerQuantities = answerQuantities;
@Override
public void removeAnswer(Answer answer, Long quantity) {
Long currentQuantity = answerQuantities.get(answer);

if (currentQuantity == null)
throw new IllegalArgumentException("Answer doesn't exist");
else if (currentQuantity < quantity)
throw new IllegalArgumentException(String.format("Answer quantity to high %d <:%d", currentQuantity, quantity));

modifyAnswerQuantity(answer, -quantity);

if(answerQuantities.get(answer) == 0)
answerQuantities.remove(answer);
}

@Override
@@ -10,6 +10,8 @@ public interface UserRepository extends Repository<UserEntity,Long> {
Optional<UserEntity> findOne(Long id);

UserEntity save(UserEntity persisted);
void delete(UserEntity userEntity);

List<UserEntity> findAll();

}
@@ -37,19 +37,11 @@ public class MaklerController {

@RequestMapping(value = "/makler", method = RequestMethod.GET)
public ModelAndView maklerMain(HttpSession session) {
UserEntity user = getUserFromSession(session);
if (user == null) {
user = new UserEntity();
user.setFunds(15L);
userRepository.save(user);
session.setAttribute("user", user);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("pages/makler");
modelAndView.addObject("websiteTitle", "Makler");
modelAndView.addObject("questions", questionController.read());
modelAndView.addObject("offers", offerController.find(user.getId()));
modelAndView.addObject("user", user);
modelAndView.addObject("offers", offerController.find(getUserFromSession(session).getId()));
return modelAndView;
}

@@ -87,6 +79,13 @@ public String addOffer(OfferForm f, HttpSession session) {
}

private UserEntity getUserFromSession(HttpSession session) {
return (UserEntity) session.getAttribute("user");
UserEntity user = (UserEntity) session.getAttribute("user");
if (user == null) {
user = new UserEntity();
user.setFunds(15L);
userRepository.save(user);
session.setAttribute("user", user);
}
return user;
}
}
@@ -3,16 +3,11 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pl.frej.waw.prediction.core.boundary.QuestionController;
import pl.frej.waw.prediction.core.entity.Answer;
import pl.waw.frej.prediction.persistence.database.entity.QuestionEntity;
import pl.waw.frej.prediction.persistence.database.repository.QuestionRepository;
import pl.waw.frej.prediction.web.model.AnswerForm;
import pl.waw.frej.prediction.web.model.QuestionForm;

import java.util.ArrayList;
@@ -24,7 +19,7 @@ public class OperatorController {
private QuestionController questionController;

@RequestMapping(value = "/operator", method = RequestMethod.GET)
public ModelAndView maklerMain(){
public ModelAndView operatorMain(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("pages/operator");
modelAndView.addObject("websiteTitle", "Operator");
@@ -16,6 +16,7 @@ public class QuestionForm implements Serializable, Question {
private AnswerForm answerOne;
private AnswerForm answerTwo;
private Date completionTime;
private Long completionValue;

@Override
public Long getId() {
@@ -77,4 +78,14 @@ public Date getCompletionTime() {
public void setCompletionTime(Date completionTime) {
this.completionTime = completionTime;
}

@Override
public Long getCompletionValue() {
return completionValue;
}

@Override
public void setCompletionValue(Long completionValue) {
this.completionValue = completionValue;
}
}
@@ -35,7 +35,7 @@ EntityFactory entityFactory(){

@Bean
TransactionController transactionController(){
return new SimpleTransactionController(transactions,offers, answers);
return new SimpleTransactionController(transactions, answers, answerController(), users, entityFactory());
}

@Bean
@@ -12,11 +12,11 @@
<tbody>
{%for answer in answers %}
<tr>
<td><a href="offer/{{offer.id}}">{{answer.id}}</a></td>
<td>{{answer.id}}</td>
<td>{{answer.name}}</td>
<td>answer.buyPrice</td>
<td>answer.sellPrice</td>
<td>answer.AveragePrice</td>
<td>{{answer.buyPrice}}</td>
<td>{{answer.sellPrice}}</td>
<td>{{answer.AveragePrice}}</td>
</tr>
{% endfor %}
</tbody>