DocumentAttachementsPageModContentFactoryImpl.java

  1. /*
  2.  * Copyright 2010-2025 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. import java.util.List;

  21. import org.springframework.security.access.annotation.Secured;
  22. import org.springframework.stereotype.Component;

  23. import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentAttachment;
  24. import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentData;
  25. import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentData_;
  26. import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentStatusContainer;
  27. import com.hack23.cia.model.external.riksdagen.dokumentstatus.impl.DocumentStatusContainer_;
  28. import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
  29. import com.hack23.cia.service.api.DataContainer;
  30. import com.hack23.cia.web.impl.ui.application.action.ViewAction;
  31. import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.pagecommands.PageCommandDocumentConstants;
  32. import com.hack23.cia.web.impl.ui.application.views.common.pagelinks.impl.ExternalAttachmentDownloadLink;
  33. import com.hack23.cia.web.impl.ui.application.views.common.pagelinks.impl.StreamSourceImplementation;
  34. import com.hack23.cia.web.impl.ui.application.views.common.pagemode.CardInfoRowUtil;
  35. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  36. import com.vaadin.server.StreamResource;
  37. import com.vaadin.ui.Layout;
  38. import com.vaadin.ui.MenuBar;
  39. import com.vaadin.ui.Panel;
  40. import com.vaadin.ui.VerticalLayout;
  41. import com.whitestein.vaadin.widgets.wtpdfviewer.WTPdfViewer;

  42. /**
  43.  * The Class DocumentAttachementsPageModContentFactoryImpl.
  44.  */
  45. @Component
  46. public final class DocumentAttachementsPageModContentFactoryImpl extends AbstractDocumentPageModContentFactoryImpl {

  47.     /** The Constant COLUMN_ORDER. */
  48.     private static final String[] COLUMN_ORDER = { "fileName", "fileSize", "fileType", "fileUrl" };

  49.     /** The Constant DOCUMENT_ATTACHMENTS. */
  50.     private static final String DOCUMENT_ATTACHMENTS = "Document Attachments";

  51.     /** The Constant HIDE_COLUMNS. */
  52.     private static final String[] HIDE_COLUMNS = { "hjid" };

  53.     /** The Constant PDF. */
  54.     private static final String PDF = "pdf";

  55.     /**
  56.      * Instantiates a new document attachements page mod content factory impl.
  57.      */
  58.     public DocumentAttachementsPageModContentFactoryImpl() {
  59.         super();
  60.     }

  61.     /**
  62.      * Display document attachements.
  63.      *
  64.      * @param panelContent the panel content
  65.      * @param documentAttachmentList the document attachment list
  66.      */
  67.     private static void displayDocumentAttachements(final VerticalLayout panelContent,
  68.             final List<DocumentAttachment> documentAttachmentList) {
  69.         for (final DocumentAttachment documentAttachment : documentAttachmentList) {

  70.             if (PDF.equalsIgnoreCase(documentAttachment.getFileType())) {
  71.                 final WTPdfViewer wtPdfViewer = new WTPdfViewer();
  72.                 wtPdfViewer.setSizeFull();

  73.                 wtPdfViewer.setResource(new StreamResource(new StreamSourceImplementation(documentAttachment.getFileUrl()), documentAttachment.getFileName()));

  74.                 panelContent.addComponent(wtPdfViewer);
  75.                 panelContent.setExpandRatio(wtPdfViewer, ContentRatio.LARGE);
  76.             } else {
  77.                 final VerticalLayout verticalLayout = new VerticalLayout();
  78.                 panelContent.addComponent(verticalLayout);
  79.                 panelContent.setExpandRatio(verticalLayout, ContentRatio.SMALL);
  80.                 final ExternalAttachmentDownloadLink link = new ExternalAttachmentDownloadLink(
  81.                         documentAttachment.getFileName(), documentAttachment.getFileType(),
  82.                         documentAttachment.getFileUrl());
  83.                 verticalLayout.addComponent(link);
  84.             }
  85.         }
  86.     }

  87.     @Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
  88.     @Override
  89.     public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
  90.         final VerticalLayout panelContent = createPanelContent();

  91.         final String pageId = getPageId(parameters);

  92.         getDocumentMenuItemFactory().createDocumentMenuBar(menuBar, pageId);

  93.         final DataContainer<DocumentStatusContainer, String> documentStatusContainerDataContainer = getApplicationManager()
  94.                 .getDataContainer(DocumentStatusContainer.class);

  95.         final DocumentStatusContainer documentStatusContainer = documentStatusContainerDataContainer
  96.                 .findByQueryProperty(DocumentStatusContainer.class, DocumentStatusContainer_.document,
  97.                         DocumentData.class, DocumentData_.id, pageId);

  98.         CardInfoRowUtil.createPageHeader(panel, panelContent,
  99.             DocumentViewConstants.ATTACHMENTS_TITLE,
  100.             DocumentViewConstants.ATTACHMENTS_SUBTITLE,
  101.             DocumentViewConstants.ATTACHMENTS_DESC);

  102.         if (documentStatusContainer != null && documentStatusContainer.getDocumentAttachmentContainer() != null
  103.                 && documentStatusContainer.getDocumentAttachmentContainer().getDocumentAttachmentList() != null) {

  104.             getGridFactory().createBasicBeanItemGrid(panelContent, DocumentAttachment.class,
  105.                     documentStatusContainer.getDocumentAttachmentContainer().getDocumentAttachmentList(),
  106.                     DOCUMENT_ATTACHMENTS, COLUMN_ORDER, HIDE_COLUMNS, null, null, null);

  107.             displayDocumentAttachements(panelContent,
  108.                     documentStatusContainer.getDocumentAttachmentContainer().getDocumentAttachmentList());
  109.         }

  110.         panel.setContent(panelContent);
  111.         getPageActionEventHelper().createPageEvent(ViewAction.VISIT_DOCUMENT_VIEW, ApplicationEventGroup.USER, NAME,
  112.                 parameters, pageId);
  113.         return panelContent;

  114.     }

  115.     @Override
  116.     public boolean matches(final String page, final String parameters) {
  117.         return PageCommandDocumentConstants.COMMAND_DOCUMENT_ATTACHMENTS.matches(page, parameters);
  118.     }

  119. }