1 package pl.matsuo.core.web.controller.message;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.beans.factory.annotation.Value;
5 import org.springframework.web.bind.annotation.RequestBody;
6 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.ResponseStatus;
8 import pl.matsuo.core.model.message.AbstractMessage;
9 import pl.matsuo.core.model.organization.AbstractParty;
10 import pl.matsuo.core.model.query.AbstractQuery;
11 import pl.matsuo.core.model.query.condition.Condition;
12 import pl.matsuo.core.model.user.Group;
13 import pl.matsuo.core.service.user.IGroupsService;
14 import pl.matsuo.core.web.controller.AbstractController;
15 import pl.matsuo.core.web.controller.organization.SimpleParty;
16
17 import javax.validation.Valid;
18
19 import static org.springframework.http.HttpStatus.*;
20 import static org.springframework.http.MediaType.*;
21 import static org.springframework.web.bind.annotation.RequestMethod.*;
22 import static pl.matsuo.core.model.query.QueryBuilder.*;
23
24
25
26
27
28 public abstract class AbstractMessageController<E extends AbstractMessage> extends AbstractController<E, IMessageRequestParams> {
29
30
31 @Autowired
32 protected IGroupsService groupsService;
33
34
35 @RequestMapping(value = "/multiMessage", method = POST, consumes = { APPLICATION_JSON_VALUE })
36 @ResponseStatus(CREATED)
37 public void create(@RequestBody @Valid MultiMessage<E> entity,
38 @Value("#{request.requestURL}") StringBuffer parentUri) {
39 for (SimpleParty simpleParty : entity.getParties()) {
40 E smsMessage = copyMessage(entity.getMessage());
41
42 if (AbstractParty.class.isAssignableFrom(simpleParty.getType())) {
43 smsMessage.setIdParty(simpleParty.getId());
44 database.create(smsMessage);
45 } else if (Group.class.isAssignableFrom(simpleParty.getType())) {
46
47 throw new RuntimeException("Not implemented yet");
48 }
49 }
50 }
51
52
53 protected abstract E copyMessage(E message);
54
55
56 @Override
57 protected AbstractQuery<E> listQuery(IMessageRequestParams params, Condition... additionalConditions) {
58 return super.listQuery(params, additionalConditions).parts(eq("idParty", params.getIdParty()));
59 }
60 }
61