root / client / src / com / gonnatrak / client / subject / SubjectsController.java @ 1

View | Annotate | Download

1
/**
2
 * Created by Andrey Khalzov
3
 * 08.01.2009 22:19:05
4
 */
5
package com.gonnatrak.client.subject;
6
7
import com.gonnatrak.client.*;
8
import com.google.gwt.user.client.History;
9
10
import java.util.Set;
11
import java.util.HashSet;
12
13
public class SubjectsController extends HistoryTokenParamHandler {
14
    private static final String SUBJECTS_PARAM_KEY = "Subjects=";
15
    private static final String SUBJECTS_PARAM_REGEX = "Subjects=((\\d)+,)*&";
16
17
    private final Mediator mediator;
18
19
    private final ServerRequest<SubjectData[]> getSubjectsRequest;
20
    private final Set<Long> checkedSubjects;
21
22
    private SubjectsWidget subjectsWidget;
23
    private boolean demo;
24
25
    public SubjectsController(Mediator mediator) {
26
        this.mediator = mediator;
27
        mediator.setSubjectsController(this);
28
29
        checkedSubjects = new HashSet<Long>();
30
31
        getSubjectsRequest = new ServerRequest<SubjectData[]>(mediator) {
32
            @Override
33
            protected void request() {
34
                SubjectService.App.getInstance().getSubjects(getAsyncCallback());
35
            }
36
37
            @Override
38
            protected void handleSuccess(SubjectData[] result) {
39
                boolean checkedExists = false;
40
                for (SubjectData subjectData : result) {
41
                    boolean checked = checkedSubjects.contains(subjectData.getSubjectId());
42
                    checkedExists |= checked;
43
44
                    subjectsWidget.addSubject(subjectData, checked);
45
                }
46
                if (!checkedExists && demo && result.length > 0) {
47
                    checkSubject(result[0].getSubjectId());
48
                }
49
            }
50
51
            @Override
52
            protected void handleTimeout() {
53
                //To change body of implemented methods use File | Settings | File Templates.
54
            }
55
56
            @Override
57
            protected void handleStatusCodeException(Throwable caught) {
58
                //To change body of implemented methods use File | Settings | File Templates.
59
            }
60
61
            @Override
62
            protected void handleUncheckedException(Throwable caught) {
63
                //To change body of implemented methods use File | Settings | File Templates.
64
            }
65
66
            @Override
67
            protected void handleUnknownException(Throwable caught) {
68
                //To change body of implemented methods use File | Settings | File Templates.
69
            }
70
        };
71
    }
72
73
    public void applyParams(String params) {
74
        subjectsWidget.resetSubjects();
75
76
        if (params == null || params.isEmpty()) {
77
            return;
78
        }
79
        final String[] subjectIds = params.split(",");
80
        for (String subjectIdStr : subjectIds) {
81
            try {
82
                final Long subjectId = Long.parseLong(subjectIdStr);
83
                checkedSubjects.add(subjectId);
84
                subjectsWidget.checkSubject(subjectId);
85
            } catch (NumberFormatException e) {
86
            }
87
        }
88
89
        mediator.showSubjects();
90
    }
91
92
    public HistoryTokenParam getHistoryTokenParam() {
93
        return HistoryTokenParam.Subjects;
94
    }
95
96
    public void setSubjectsWidget(SubjectsWidget subjectsWidget) {
97
        this.subjectsWidget = subjectsWidget;
98
    }
99
100
    public Set<Long> getCheckedSubjects() {
101
        return checkedSubjects;
102
    }
103
104
    public void setDemo(boolean demo) {
105
        this.demo = demo;
106
    }
107
108
    public boolean isInitialized() {
109
        return subjectsWidget != null;
110
    }
111
112
    public void requestSubjects() {
113
        checkIsInitialized();
114
115
        subjectsWidget.clearSubjects();
116
        getSubjectsRequest.execute();
117
    }
118
119
    public void checkSubject(long id) {
120
        checkedSubjects.add(id);
121
122
        final String subjectParams = createSubjectParamsString();
123
124
        updateHistoryToken(subjectParams);
125
    }
126
127
    public void uncheckSubject(long id) {
128
        checkedSubjects.remove(id);
129
130
        final String subjectParams = createSubjectParamsString();
131
132
        updateHistoryToken(subjectParams);
133
    }
134
135
    private void checkIsInitialized() {
136
        if (!isInitialized()) {
137
            throw new IllegalStateException("SubjectsController is not initailzed!");
138
        }
139
    }
140
141
    private void updateHistoryToken(String subjectParams) {
142
        String historyToken = History.getToken();
143
        int subjectsParamIndex = historyToken.indexOf(SUBJECTS_PARAM_KEY);
144
        if (subjectsParamIndex >= 0) {
145
            historyToken = historyToken.replaceFirst(SUBJECTS_PARAM_REGEX, subjectParams);
146
        } else {
147
            historyToken = historyToken + HistoryListener.PARAMS_SEPARATOR + subjectParams;
148
        }
149
        historyToken = historyToken.replaceAll(
150
                HistoryListener.PARAMS_SEPARATOR + "{2,}",
151
                HistoryListener.PARAMS_SEPARATOR
152
        );
153
        History.newItem(historyToken);
154
    }
155
156
    private String createSubjectParamsString() {
157
        if (checkedSubjects.isEmpty()) {
158
            return HistoryListener.PARAMS_SEPARATOR;
159
        }
160
        final StringBuilder sb = new StringBuilder(SUBJECTS_PARAM_KEY);
161
        for (Long checkedSubjectId : checkedSubjects) {
162
            sb.append(checkedSubjectId).append(',');
163
        }
164
        sb.append(HistoryListener.PARAMS_SEPARATOR);
165
        return sb.toString();
166
    }
167
}