View Javadoc
1   /*
2    * Copyright 2014 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.application.views.common.menufactory.impl;
20  
21  import java.util.AbstractMap;
22  import java.util.AbstractMap.SimpleEntry;
23  import java.util.Arrays;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Map.Entry;
28  import java.util.stream.Collectors;
29  
30  import org.springframework.beans.factory.annotation.Autowired;
31  import org.springframework.stereotype.Service;
32  
33  import com.hack23.cia.model.internal.application.data.impl.ViewWorldbankIndicatorDataCountrySummary;
34  import com.hack23.cia.model.internal.application.data.impl.WorldbankIndicatorDataCountrySummaryEmbeddedId;
35  import com.hack23.cia.service.api.ApplicationManager;
36  import com.hack23.cia.service.api.DataContainer;
37  import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.ApplicationMenuItemFactory;
38  import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.CountryMenuItemFactory;
39  import com.hack23.cia.web.impl.ui.application.views.common.pagelinks.api.PageModeMenuCommand;
40  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
41  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.PageMode;
42  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.UserViews;
43  import com.vaadin.server.FontAwesome;
44  import com.vaadin.ui.Alignment;
45  import com.vaadin.ui.MenuBar;
46  import com.vaadin.ui.MenuBar.MenuItem;
47  import com.vaadin.ui.VerticalLayout;
48  
49  /**
50   * The Class CountryMenuItemFactoryImpl.
51   */
52  @Service
53  public final class CountryMenuItemFactoryImpl extends AbstractMenuItemFactoryImpl implements CountryMenuItemFactory {
54  
55  	/** The Constant COMMAND_PAGEVISITHISTORY. */
56  	private static final PageModeMenuCommand COMMAND_PAGEVISITHISTORY = new PageModeMenuCommand(UserViews.COUNTRY_RANKING_VIEW_NAME, PageMode.PAGEVISITHISTORY);
57  
58  
59  	/** The Constant COMMAND_OVERVIEW. */
60  	private static final PageModeMenuCommand COMMAND_OVERVIEW = new PageModeMenuCommand(UserViews.COUNTRY_RANKING_VIEW_NAME, PageMode.OVERVIEW);
61  
62  	/** The Constant BY_SOURCE. */
63  	private static final String BY_SOURCE = "By Source";
64  
65  	/** The Constant BY_TOPIC. */
66  	private static final String BY_TOPIC = "ByTopic";
67  
68  	/** The Constant COUNTRY_INDICATORS_SWEDEN. */
69  	private static final String COUNTRY_INDICATORS_SWEDEN = "Country Indicators, Sweden";
70  
71  	/** The Constant COUNTRY_RANKING_TEXT. */
72  	private static final String COUNTRY_RANKING_TEXT = "Counry Ranking";
73  
74  	/** The Constant OVERVIEW_TEXT. */
75  	private static final String OVERVIEW_TEXT = "Overview";
76  
77  	/** The application manager. */
78  	@Autowired
79  	private ApplicationManager applicationManager;
80  
81  	/** The Constant PAGE_VISIT_HISTORY_TEXT. */
82  	private static final String PAGE_VISIT_HISTORY_TEXT = "Page Visit History";
83  
84  	/** The application menu item factory. */
85  	@Autowired
86  	private ApplicationMenuItemFactory applicationMenuItemFactory;
87  
88  	/**
89  	 * Instantiates a new country menu item factory impl.
90  	 */
91  	public CountryMenuItemFactoryImpl() {
92  		super();
93  	}
94  
95  	@Override
96  	public void createCountryTopicMenu(final MenuBar menuBar) {
97  		initApplicationMenuBar(menuBar);
98  
99  		applicationMenuItemFactory.addRankingMenu(menuBar);
100 
101 
102 		createCountryTopicMenu( menuBar.addItem(COUNTRY_RANKING_TEXT, FontAwesome.SERVER, null));
103 
104 	}
105 
106 	/**
107 	 * Adds the sources and indicators to menu.
108 	 *
109 	 * @param countryIndicators
110 	 *            the country indicators
111 	 * @param sourceIndicatorMap
112 	 *            the source indicator map
113 	 */
114 	private void addSourcesAndIndicatorsToMenu(final MenuItem countryIndicators,
115 			final Map<String, List<ViewWorldbankIndicatorDataCountrySummary>> sourceIndicatorMap) {
116 
117 		final Map<String, List<ViewWorldbankIndicatorDataCountrySummary>> sortedIndicatorMap = sourceIndicatorMap
118 				.entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey()))
119 				.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
120 
121 		for (final Entry<String, List<ViewWorldbankIndicatorDataCountrySummary>> entry : sortedIndicatorMap
122 				.entrySet()) {
123 
124 			final MenuItem sourceItems = countryIndicators.addItem(entry.getKey(), null, null);
125 
126 			final List<ViewWorldbankIndicatorDataCountrySummary> sortedEntries = entry.getValue().stream()
127 					.sorted((e1, e2) -> e1.getIndicatorName().compareTo(e2.getIndicatorName()))
128 					.collect(Collectors.toList());
129 
130 			for (final ViewWorldbankIndicatorDataCountrySummary indciatorSummary : sortedEntries) {
131 				sourceItems.addItem(indciatorSummary.getIndicatorName(),
132 						new PageModeMenuCommand(UserViews.COUNTRY_RANKING_VIEW_NAME, PageMode.INDICATORS,
133 								indciatorSummary.getEmbeddedId().getIndicatorId()));
134 			}
135 		}
136 
137 	}
138 
139 	@Override
140 	public void createCountryTopicMenu(final MenuItem charts) {
141 		charts.addItem(OVERVIEW_TEXT, FontAwesome.LINE_CHART,
142 				COMMAND_OVERVIEW);
143 
144 		final MenuItem countryIndicators = charts.addItem(COUNTRY_INDICATORS_SWEDEN, FontAwesome.LINE_CHART, null);
145 
146 		addSourcesAndIndicatorsToMenu(countryIndicators.addItem(BY_TOPIC,FontAwesome.LINE_CHART, null), getTopicIndicatorMap());
147 		addSourcesAndIndicatorsToMenu(countryIndicators.addItem(BY_SOURCE,FontAwesome.LINE_CHART, null), getSourceIndicatorMap());
148 
149 		charts.addItem(PAGE_VISIT_HISTORY_TEXT, FontAwesome.LINE_CHART,	COMMAND_PAGEVISITHISTORY);
150 
151 	}
152 
153 	/**
154 	 * Gets the source indicator map.
155 	 *
156 	 * @return the source indicator map
157 	 */
158 	private Map<String, List<ViewWorldbankIndicatorDataCountrySummary>> getSourceIndicatorMap() {
159 		final DataContainer<ViewWorldbankIndicatorDataCountrySummary, WorldbankIndicatorDataCountrySummaryEmbeddedId> indicatorDataCountrSummaryDailyDataContainer = applicationManager
160 				.getDataContainer(ViewWorldbankIndicatorDataCountrySummary.class);
161 
162 		return indicatorDataCountrSummaryDailyDataContainer
163 				.getAll().parallelStream()
164 				.filter(t -> t != null && t.getSourceValue() != null && t.getEndYear() > 2010 && t.getDataPoint() > 10)
165 				.collect(Collectors.groupingBy(ViewWorldbankIndicatorDataCountrySummary::getSourceValue));
166 	}
167 
168 	/**
169 	 * Gets the topic indicator map.
170 	 *
171 	 * @return the topic indicator map
172 	 */
173 	private Map<String, List<ViewWorldbankIndicatorDataCountrySummary>> getTopicIndicatorMap() {
174 		final DataContainer<ViewWorldbankIndicatorDataCountrySummary, WorldbankIndicatorDataCountrySummaryEmbeddedId> indicatorDataCountrSummaryDailyDataContainer = applicationManager
175 				.getDataContainer(ViewWorldbankIndicatorDataCountrySummary.class);
176 
177 		return indicatorDataCountrSummaryDailyDataContainer
178 				.getAll().parallelStream()
179 				.filter(t -> t != null && t.getSourceValue() != null && t.getEndYear() > 2010 && t.getDataPoint() > 10)
180 				.flatMap(t -> Arrays.asList(t.getTopics().split(";")).stream()
181 						.map(topic -> new AbstractMap.SimpleEntry<>(topic, t)))
182 
183 				.collect(Collectors.groupingBy(SimpleEntry::getKey,
184 						Collectors.mapping(SimpleEntry::getValue, Collectors.toList())));
185 	}
186 
187 	@Override
188 	public void createOverviewPage(final VerticalLayout panelContent) {
189 		final MenuBar menuBar = new MenuBar();
190 		panelContent.addComponent(menuBar);
191 		panelContent.setComponentAlignment(menuBar, Alignment.TOP_LEFT);
192 		panelContent.setExpandRatio(menuBar, ContentRatio.LARGE);
193 
194 		addSourcesAndIndicatorsToMenu(menuBar.addItem(BY_TOPIC,FontAwesome.LINE_CHART, null), getTopicIndicatorMap());
195 		menuBar.setAutoOpen(true);
196 	}
197 }