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  
23  import org.apache.commons.lang3.StringUtils;
24  import org.springframework.security.access.annotation.Secured;
25  import org.springframework.stereotype.Component;
26  
27  import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement;
28  import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentAttachment;
29  import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentData;
30  import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentData_;
31  import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentStatusContainer;
32  import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentStatusContainer_;
33  import com.hack23.cia.model.external.riksdagen.utskottsforslag.impl.CommitteeProposalComponentData;
34  import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
35  import com.hack23.cia.service.api.DataContainer;
36  import com.hack23.cia.web.impl.ui.application.action.ViewAction;
37  import com.hack23.cia.web.impl.ui.application.views.common.labelfactory.LabelFactory;
38  import com.hack23.cia.web.impl.ui.application.views.common.pagelinks.impl.ExternalAttachmentDownloadLink;
39  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
40  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.DocumentPageMode;
41  import com.vaadin.data.util.BeanItemContainer;
42  import com.vaadin.ui.Layout;
43  import com.vaadin.ui.MenuBar;
44  import com.vaadin.ui.Panel;
45  import com.vaadin.ui.VerticalLayout;
46  
47  /**
48   * The Class DocumentAttachementsPageModContentFactoryImpl.
49   */
50  @Component
51  public final class DocumentAttachementsPageModContentFactoryImpl extends AbstractDocumentPageModContentFactoryImpl {
52  
53  	/** The Constant DOCUMENT_ATTACHMENTS. */
54  	private static final String DOCUMENT_ATTACHMENTS = "Document Attachments";
55  
56  	/**
57  	 * Instantiates a new document attachements page mod content factory impl.
58  	 */
59  	public DocumentAttachementsPageModContentFactoryImpl() {
60  		super();
61  	}
62  
63  	@Override
64  	public boolean matches(final String page, final String parameters) {
65  		return NAME.equals(page) && (!StringUtils.isEmpty(parameters)
66  				&& parameters.contains(DocumentPageMode.DOCUMENTATTACHMENTS.toString()));
67  	}
68  
69  	@Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
70  	@Override
71  	public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
72  		final VerticalLayout panelContent = createPanelContent();
73  
74  		final String pageId = getPageId(parameters);
75  
76  		final DataContainer<DocumentElement, String> documentElementDataContainer = getApplicationManager()
77  				.getDataContainer(DocumentElement.class);
78  
79  		final DataContainer<DocumentStatusContainer, String> documentStatusContainerDataContainer = getApplicationManager()
80  				.getDataContainer(DocumentStatusContainer.class);
81  
82  		getApplicationManager().getDataContainer(CommitteeProposalComponentData.class);
83  
84  		final DocumentElement documentElement = documentElementDataContainer.load(pageId);
85  
86  		if (documentElement != null) {
87  
88  			getDocumentMenuItemFactory().createDocumentMenuBar(menuBar, pageId);
89  
90  			final DocumentStatusContainer documentStatusContainer = documentStatusContainerDataContainer
91  					.findByQueryProperty(DocumentStatusContainer.class, DocumentStatusContainer_.document,
92  							DocumentData.class, DocumentData_.id, pageId);
93  
94  			LabelFactory.createHeader2Label(panelContent,DOCUMENT_ATTACHMENTS);
95  
96  
97  			if (documentStatusContainer != null && documentStatusContainer.getDocumentAttachmentContainer() != null
98  					&& documentStatusContainer.getDocumentAttachmentContainer().getDocumentAttachmentList() != null) {
99  				final BeanItemContainer<DocumentAttachment> documentAttachmentDataSource = new BeanItemContainer<>(
100 						DocumentAttachment.class,
101 						documentStatusContainer.getDocumentAttachmentContainer().getDocumentAttachmentList());
102 
103 				getGridFactory().createBasicBeanItemGrid(
104 						panelContent, documentAttachmentDataSource,
105 						"Document attachements", new String[] { "fileName", "fileSize", "fileType", "fileUrl" }, new String[] { "hjid" },
106 						null, null, null);
107 
108 				final List<DocumentAttachment> documentAttachmentList = documentStatusContainer.getDocumentAttachmentContainer().getDocumentAttachmentList();
109 
110 				final VerticalLayout verticalLayout = new VerticalLayout();
111 				panelContent.addComponent(verticalLayout);
112 				panelContent.setExpandRatio(verticalLayout,ContentRatio.SMALL);
113 
114 				for (final DocumentAttachment documentAttachment : documentAttachmentList) {
115 					final ExternalAttachmentDownloadLink link = new ExternalAttachmentDownloadLink(documentAttachment.getFileName(), documentAttachment.getFileType(), documentAttachment.getFileUrl());
116 					verticalLayout.addComponent(link);
117 				}
118 			}
119 
120 			panel.setContent(panelContent);
121 			getPageActionEventHelper().createPageEvent(ViewAction.VISIT_DOCUMENT_VIEW, ApplicationEventGroup.USER, NAME,
122 					parameters, pageId);
123 		}
124 
125 		return panelContent;
126 
127 	}
128 
129 }