|
@@ -2,15 +2,26 @@ package in.ocsf.these.days.app.state.chat;/* kpmy 22.02.2017 */
|
|
|
|
|
|
import in.ocsf.these.days.app.messaging.ChatHelper;
|
|
|
import in.ocsf.these.days.app.messaging.UpdateHelper;
|
|
|
+import in.ocsf.these.days.app.object.Chat;
|
|
|
+import in.ocsf.these.days.app.object.ChatState;
|
|
|
+import in.ocsf.these.days.app.object.User;
|
|
|
+import in.ocsf.these.days.app.repo.ChatRepository;
|
|
|
+import in.ocsf.these.days.app.state.ChatType;
|
|
|
import in.ocsf.these.days.app.state.IChat;
|
|
|
import in.ocsf.these.days.app.state.IState;
|
|
|
+import in.ocsf.these.days.app.state.StateService;
|
|
|
+import in.ocsf.these.days.app.state.account.Accounted;
|
|
|
import in.ocsf.these.days.app.state.utils.MessageTemplate;
|
|
|
import org.apache.log4j.Logger;
|
|
|
+import org.bson.types.ObjectId;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.statemachine.StateContext;
|
|
|
import org.springframework.statemachine.StateMachine;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
@Service
|
|
|
public class WelcomeChat implements IChat<WelcomeChat.State, WelcomeChat.Event> {
|
|
|
|
|
@@ -21,23 +32,66 @@ public class WelcomeChat implements IChat<WelcomeChat.State, WelcomeChat.Event>
|
|
|
@Autowired
|
|
|
private ChatHelper chatHelper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private StateService stateService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ChatRepository chatRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private Accounted accounted;
|
|
|
+
|
|
|
public void hello(StateContext<State, Event> ctx) {
|
|
|
- UpdateHelper upd = ctx.getExtendedState().get(IChat.MESSAGE, UpdateHelper.class);
|
|
|
+ UpdateHelper upd = get(ctx, IChat.MESSAGE);
|
|
|
chatHelper.sendSimpleTextMessage(upd.getChatId(), helloMsg.render());
|
|
|
}
|
|
|
|
|
|
public void ask(StateContext<State, Event> ctx) {
|
|
|
- UpdateHelper upd = ctx.getExtendedState().get(IChat.MESSAGE, UpdateHelper.class);
|
|
|
+ UpdateHelper upd = get(ctx, IChat.MESSAGE);
|
|
|
chatHelper.sendSimpleTextMessage(upd.getChatId(), "Как дела?");
|
|
|
}
|
|
|
|
|
|
public void goodbye(StateContext<State, Event> ctx) {
|
|
|
- log.info("hello");
|
|
|
+ UpdateHelper upd = get(ctx, IChat.MESSAGE);
|
|
|
+ chatHelper.sendSimpleTextMessage(upd.getChatId(), "Ну пока...");
|
|
|
+ Chat chat = get(ctx, IChat.ENTITY);
|
|
|
+ chat.setState(ChatState.closed);
|
|
|
+
|
|
|
+ StateMachine userState = stateService.getStateFor(chat.getUser());
|
|
|
+ stateService.inherit(ctx, userState);
|
|
|
+ accounted.handleCommand(Arrays.asList("/_register"), userState);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void handle(StateMachine<State, Event> state) {
|
|
|
+ Chat chat = chatRepo.findOne(new ObjectId((String) get(state, CHAT)));
|
|
|
+ StateMachine<WelcomeChat.State, WelcomeChat.Event> chatState = stateService.getChatStateFor(chat);
|
|
|
+ stateService.inherit(state, chatState);
|
|
|
+ stateService.put(chatState, IChat.ENTITY, chat);
|
|
|
+ switch (chatState.getState().getId()) {
|
|
|
+ case hello:
|
|
|
+ chatState.sendEvent(Event.reply);
|
|
|
+ break;
|
|
|
+ case ask:
|
|
|
+ chatState.sendEvent(Event.answer);
|
|
|
+ default:
|
|
|
+
|
|
|
+ }
|
|
|
+ stateService.setChatStateFor(chat, chatState);
|
|
|
+ chatRepo.save(chat);
|
|
|
+ }
|
|
|
|
|
|
+ public void startWith(User user, UpdateHelper upd) {
|
|
|
+ Chat chat = new Chat();
|
|
|
+ chat.setUser(user);
|
|
|
+ chat.setType(ChatType.welcome);
|
|
|
+ StateMachine<WelcomeChat.State, WelcomeChat.Event> chatState = stateService.getChatStateFor(chat);
|
|
|
+ stateService.put(chatState, upd);
|
|
|
+ stateService.put(chatState, IChat.ENTITY, chat);
|
|
|
+ chatState.start();
|
|
|
+ stateService.setChatStateFor(chat, chatState);
|
|
|
+ chatRepo.save(chat);
|
|
|
}
|
|
|
|
|
|
public enum Event {
|