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.user.document.pagemode;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.jsoup.Jsoup;
27  import org.jsoup.nodes.Document.OutputSettings;
28  import org.jsoup.safety.Whitelist;
29  import org.springframework.security.access.annotation.Secured;
30  import org.springframework.stereotype.Component;
31  import org.springframework.web.context.request.RequestContextHolder;
32  
33  import com.hack23.cia.model.external.riksdagen.documentcontent.impl.DocumentContentData;
34  import com.hack23.cia.model.external.riksdagen.documentcontent.impl.DocumentContentData_;
35  import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement;
36  import com.hack23.cia.model.external.riksdagen.utskottsforslag.impl.CommitteeProposalComponentData;
37  import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
38  import com.hack23.cia.service.api.DataContainer;
39  import com.hack23.cia.service.api.action.user.DocumentWordCountRequest;
40  import com.hack23.cia.service.api.action.user.DocumentWordCountResponse;
41  import com.hack23.cia.web.impl.ui.application.action.ViewAction;
42  import com.hack23.cia.web.impl.ui.application.views.common.labelfactory.LabelFactory;
43  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
44  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.DocumentPageMode;
45  import com.vaadin.shared.ui.label.ContentMode;
46  import com.vaadin.ui.FormLayout;
47  import com.vaadin.ui.Label;
48  import com.vaadin.ui.Layout;
49  import com.vaadin.ui.MenuBar;
50  import com.vaadin.ui.Panel;
51  import com.vaadin.ui.VerticalLayout;
52  
53  /**
54   * The Class DocumentDataPageModContentFactoryImpl.
55   */
56  @Component
57  public final class DocumentDataPageModContentFactoryImpl extends AbstractDocumentPageModContentFactoryImpl {
58  
59  	/** The Constant DOCUMENT_DATA. */
60  	private static final String DOCUMENT_DATA = "Document Data";
61  
62  
63  	/**
64  	 * Instantiates a new document data page mod content factory impl.
65  	 */
66  	public DocumentDataPageModContentFactoryImpl() {
67  		super();
68  	}
69  
70  	@Override
71  	public boolean matches(final String page, final String parameters) {
72  		return NAME.equals(page) && (!StringUtils.isEmpty(parameters) && parameters.contains(DocumentPageMode.DOCUMENTDATA.toString()));
73  	}
74  
75  	@Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
76  	@Override
77  	public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
78  		final VerticalLayout panelContent = createPanelContent();
79  
80  		final String pageId = getPageId(parameters);
81  
82  		final DataContainer<DocumentElement, String> documentElementDataContainer = getApplicationManager()
83  				.getDataContainer(DocumentElement.class);
84  
85  		final DataContainer<DocumentContentData, String> documentContentDataDataContainer = getApplicationManager()
86  				.getDataContainer(DocumentContentData.class);
87  
88  		getApplicationManager()
89  				.getDataContainer(CommitteeProposalComponentData.class);
90  
91  		final DocumentElement documentElement = documentElementDataContainer.load(pageId);
92  
93  		if (documentElement != null) {
94  
95  			getDocumentMenuItemFactory().createDocumentMenuBar(menuBar, pageId);
96  
97  			LabelFactory.createHeader2Label(panelContent,DOCUMENT_DATA);
98  
99  
100 			final List<DocumentContentData> documentContentlist = documentContentDataDataContainer
101 					.getAllBy(DocumentContentData_.id, pageId);
102 
103 			if (!documentContentlist.isEmpty()) {
104 
105 				final Panel formPanel = new Panel();
106 				formPanel.setSizeFull();
107 
108 				panelContent.addComponent(formPanel);
109 
110 				final FormLayout formContent = new FormLayout();
111 				formPanel.setContent(formContent);
112 
113 				final String cleanContent = Jsoup.clean(documentContentlist.get(0).getContent(),"", Whitelist.simpleText(), new OutputSettings().indentAmount(4));
114 
115 				final Label htmlContent = new Label(cleanContent, ContentMode.HTML);
116 
117 				formContent.addComponent(htmlContent);
118 
119 				final DocumentWordCountRequest documentWordCountRequest = new DocumentWordCountRequest();
120 				documentWordCountRequest.setDocumentId(pageId);
121 				documentWordCountRequest.setMaxResults(30);
122 				documentWordCountRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
123 				final DocumentWordCountResponse resp = (DocumentWordCountResponse) getApplicationManager().service(documentWordCountRequest);
124 
125 				if (resp.getWordCountMap() != null) {
126 					final Label wordCloud = new Label(createWordCloud(resp.getWordCountMap()), ContentMode.HTML);
127 					formContent.addComponent(wordCloud);
128 				}
129 
130 				panelContent.setExpandRatio(formPanel, ContentRatio.GRID);
131 
132 			}
133 
134 
135 
136 			panel.setContent(panelContent);
137 			getPageActionEventHelper().createPageEvent(ViewAction.VISIT_DOCUMENT_VIEW, ApplicationEventGroup.USER, NAME, parameters, pageId);
138 		}
139 
140 		return panelContent;
141 
142 	}
143 
144 	private static String createWordCloud(final Map<String, Integer> wordMap) {
145 		final StringBuilder builder = new StringBuilder();
146 		builder.append("<p>");
147 		for (final Entry<String, Integer> entry : wordMap.entrySet()) {
148 			final int fontSize = entry.getValue().intValue();
149 			builder.append("<font size=\"").append(entry.getValue()).append(")\">").append(entry.getKey()).append("</font> ");
150 		}
151 		builder.append("/<p>");
152 		return builder.toString();
153 	}
154 
155 
156 }