3232
3333import java .io .IOException ;
3434import java .io .InputStream ;
35+ import java .time .Instant ;
3536import java .util .HashMap ;
37+ import java .util .Iterator ;
3638import java .util .Map ;
3739
3840import org .springframework .beans .factory .annotation .Autowired ;
41+ import org .springframework .context .ApplicationListener ;
3942import org .springframework .stereotype .Service ;
4043import org .yaml .snakeyaml .Yaml ;
4144import org .yaml .snakeyaml .constructor .Constructor ;
4245import org .yaml .snakeyaml .error .YAMLException ;
4346
47+ import com .fasterxml .jackson .databind .ObjectMapper ;
48+
49+ import io .bssw .psip .backend .model .Category ;
50+ import io .bssw .psip .backend .model .CategoryScore ;
4451import io .bssw .psip .backend .model .Item ;
52+ import io .bssw .psip .backend .model .ItemScore ;
4553import io .bssw .psip .backend .model .Survey ;
4654import io .bssw .psip .backend .model .SurveyContent ;
55+ import io .bssw .psip .backend .model .SurveyScore ;
56+ import io .bssw .psip .backend .service .events .AuthChangeEvent ;
57+ import io .bssw .psip .backend .model .SurveyHistory ;
4758
4859// Must be session scope to ensure only one service (and resulting entities) per session
4960// @VaadinSessionScope
5061@ Service
51- public class SurveyService {
62+ public class SurveyService implements ApplicationListener < AuthChangeEvent > {
5263 private static final String SURVEY_FILE = ".psip/survey.yml" ;
64+ private static final String HISTORY_FILE = ".psip/history.json" ;
5365
5466 @ Autowired
5567 private RepositoryProviderManager repositoryManager ;
5668
5769 private Survey survey ;
70+ private SurveyHistory history ;
5871
5972 private final Map <String , Item > items = new HashMap <String , Item >();
6073 private final Map <String , Item > prevItems = new HashMap <String , Item >();
6174 private final Map <String , Item > nextItems = new HashMap <String , Item >();
6275
6376 public Survey getSurvey () {
6477 if (survey == null ) {
65- return loadSurvey ();
78+ loadSurvey ();
79+ loadSurveyHistory ();
6680 }
6781 return survey ;
6882 }
6983
84+ public void reset () {
85+ survey = null ;
86+ }
87+
7088 public Item getItem (String path ) {
7189 return items .get (path );
7290 }
@@ -98,11 +116,10 @@ public Survey load(InputStream inputStream) throws YAMLException {
98116 }
99117
100118 /*
101- * (Re)load a new survey. Make sure we get a new survey if
102- * the repository provider changes.
119+ * (Re)load a new survey from a repository. Assumes that the repository
120+ * provider is connected. If not it will load the default survey.
103121 */
104- public Survey loadSurvey () throws YAMLException {
105- survey = null ;
122+ private void loadSurvey () throws YAMLException {
106123 items .clear ();
107124 prevItems .clear ();
108125 nextItems .clear ();
@@ -119,7 +136,69 @@ public Survey loadSurvey() throws YAMLException {
119136 stream = getClass ().getResourceAsStream ("/assessment.yml" );
120137 }
121138 survey = load (stream );
122- return survey ;
123139 }
124140
141+ private void loadSurveyHistory () {
142+ history = new SurveyHistory ();
143+ InputStream stream = null ;
144+ if (repositoryManager .isLoggedIn ()) {
145+ RepositoryProvider provider = repositoryManager .getRepositoryProvider ();
146+ try {
147+ stream = provider .readFile (HISTORY_FILE );
148+ } catch (IOException e ) {
149+ // Just use default file
150+ }
151+ }
152+ if (stream != null ) {
153+ ObjectMapper mapper = new ObjectMapper ();
154+ try {
155+ history = mapper .readValue (stream , SurveyHistory .class );
156+ } catch (IOException e ) {
157+ }
158+ }
159+ }
160+
161+ public SurveyScore generateScore (Survey survey ) {
162+ SurveyScore surveyScore = new SurveyScore ();
163+ surveyScore .setVersion (survey .getVersion ());
164+ surveyScore .setTimestamp (Instant .now ().toString ());
165+ Iterator <Category > categoryIter = survey .getCategories ().iterator ();
166+ while (categoryIter .hasNext ()) {
167+ Category category = categoryIter .next ();
168+ CategoryScore catScore = new CategoryScore ();
169+ catScore .setPath (category .getPath ());
170+ Iterator <Item > itemIter = category .getItems ().iterator ();
171+ while (itemIter .hasNext ()) {
172+ Item item = itemIter .next ();
173+ ItemScore score = new ItemScore ();
174+ score .setPath (item .getPath ());
175+ score .setValue (item .getScore ().orElse (0 ).toString ());
176+ catScore .getItemScores ().add (score );
177+ }
178+ surveyScore .getCategoryScores ().add (catScore );
179+ }
180+ return surveyScore ;
181+ }
182+
183+ /*
184+ * Save the current survey values into the history and
185+ * write out to the repository
186+ */
187+ public void saveSurveyHistory (Survey survey ) throws Exception {
188+ SurveyScore score = generateScore (survey );
189+ history .addScore (score );
190+ ObjectMapper mapper = new ObjectMapper ();
191+ String value = mapper .writeValueAsString (history );
192+ if (repositoryManager .isLoggedIn ()) {
193+ RepositoryProvider provider = repositoryManager .getRepositoryProvider ();
194+ provider .writeFile (HISTORY_FILE , value );
195+ } else {
196+ throw new IOException ("Not logged in" );
197+ }
198+ }
199+
200+ @ Override
201+ public void onApplicationEvent (AuthChangeEvent event ) {
202+ reset ();
203+ }
125204}
0 commit comments