|
@@ -2,6 +2,8 @@ package in.ocsf.these.days.app.state;/* kpmy 20.02.2017 */
|
|
|
|
|
|
import in.ocsf.these.days.app.object.Chat;
|
|
|
import in.ocsf.these.days.app.object.User;
|
|
|
+import in.ocsf.these.days.app.state.account.AccountStateMachineFactoryConfig;
|
|
|
+import in.ocsf.these.days.app.state.account.Accounted;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
@@ -15,43 +17,67 @@ import org.springframework.stereotype.Service;
|
|
|
public class StateService {
|
|
|
|
|
|
@Autowired
|
|
|
- @Qualifier("userStateMachineFactory")
|
|
|
- private StateMachineFactory<UserState, UserEvent> userStateMachineFactory;
|
|
|
+ @Qualifier("accountStateMachineFactory")
|
|
|
+ private StateMachineFactory<Accounted.State, Accounted.Event> accountStateMachineFactory;
|
|
|
|
|
|
@Autowired
|
|
|
private ApplicationContext context;
|
|
|
|
|
|
- public StateMachine getChatStateFor(Chat chat) throws Exception {
|
|
|
+ public StateMachine getChatStateFor(Chat chat) {
|
|
|
StateMachinePersister persist = new DefaultStateMachinePersister(new InChatStateMachinePersist());
|
|
|
- StateMachineFactory factory = (StateMachineFactory) context.getBean(chat.getType().getBean());
|
|
|
+ StateMachineFactory factory = (StateMachineFactory) context.getBean(chat.getType().getFactoryBean());
|
|
|
StateMachine ret = factory.getStateMachine(chat.getId());
|
|
|
|
|
|
- if (chat.getData() != null)
|
|
|
- ret = persist.restore(ret, chat);
|
|
|
-
|
|
|
- return ret;
|
|
|
+ try {
|
|
|
+ if (chat.getData() != null)
|
|
|
+ ret = persist.restore(ret, chat);
|
|
|
+ return ret;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void setChatStateFor(Chat chat, StateMachine state) throws Exception {
|
|
|
+ public void setChatStateFor(Chat chat, StateMachine state) {
|
|
|
StateMachinePersister persist = new DefaultStateMachinePersister(new InChatStateMachinePersist());
|
|
|
-
|
|
|
- persist.persist(state, chat);
|
|
|
+ try {
|
|
|
+ state.getExtendedState().getVariables().clear();
|
|
|
+ persist.persist(state, chat);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public StateMachine<UserState, UserEvent> getStateFor(User user) throws Exception {
|
|
|
- StateMachinePersister<UserState, UserEvent, User> persist = new DefaultStateMachinePersister<>(new UserStateMachineFactoryConfig.InUserStateMachinePersist());
|
|
|
+ public StateMachine<Accounted.State, Accounted.Event> getStateFor(User user) {
|
|
|
+ StateMachinePersister<Accounted.State, Accounted.Event, User> persist = new DefaultStateMachinePersister<>(new AccountStateMachineFactoryConfig.InUserStateMachinePersist());
|
|
|
+
|
|
|
+ StateMachine<Accounted.State, Accounted.Event> ret = accountStateMachineFactory.getStateMachine(Long.toHexString(user.getId()));
|
|
|
|
|
|
- StateMachine<UserState, UserEvent> ret = userStateMachineFactory.getStateMachine(Long.toHexString(user.getId()));
|
|
|
+ try {
|
|
|
+ if (user.getState() != null)
|
|
|
+ ret = persist.restore(ret, user);
|
|
|
|
|
|
- if (user.getState() != null)
|
|
|
- ret = persist.restore(ret, user);
|
|
|
+ return ret;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setStateFor(User user, StateMachine<Accounted.State, Accounted.Event> state) {
|
|
|
+ StateMachinePersister<Accounted.State, Accounted.Event, User> persist = new DefaultStateMachinePersister<>(new AccountStateMachineFactoryConfig.InUserStateMachinePersist());
|
|
|
|
|
|
- return ret;
|
|
|
+ try {
|
|
|
+ state.getExtendedState().getVariables().clear();
|
|
|
+ persist.persist(state, user);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void setStateFor(User user, StateMachine<UserState, UserEvent> state) throws Exception {
|
|
|
- StateMachinePersister<UserState, UserEvent, User> persist = new DefaultStateMachinePersister<>(new UserStateMachineFactoryConfig.InUserStateMachinePersist());
|
|
|
+ public void put(StateMachine state, Object value) {
|
|
|
+ put(state, IChat.MESSAGE, value);
|
|
|
+ }
|
|
|
|
|
|
- persist.persist(state, user);
|
|
|
+ public void put(StateMachine state, String key, Object value) {
|
|
|
+ state.getExtendedState().getVariables().put(key, value);
|
|
|
}
|
|
|
}
|